source: mainline/uspace/app/sbi/src/builtin.c@ 09ababb7

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 09ababb7 was 09ababb7, checked in by Jiri Svoboda <jiri@…>, 15 years ago

Add Sysel Bootstrap Interpreter (SBI) from Sysel repository rev. 53.

  • Property mode set to 100644
File size: 3.7 KB
Line 
1/*
2 * Copyright (c) 2010 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @file Builtin functions. */
30
31#include <stdio.h>
32#include <stdlib.h>
33#include <assert.h>
34#include "list.h"
35#include "mytypes.h"
36#include "run.h"
37#include "stree.h"
38#include "strtab.h"
39
40#include "builtin.h"
41
42static void builtin_write_line(run_t *run);
43
44static stree_symbol_t *bi_write_line;
45
46/** Declare builtin symbols in the program.
47 *
48 * Declares symbols that will be hooked to builtin interpreter functions.
49 */
50void builtin_declare(stree_program_t *program)
51{
52 stree_modm_t *modm;
53 stree_csi_t *csi;
54 stree_ident_t *ident;
55 stree_csimbr_t *csimbr;
56 stree_fun_t *fun;
57 stree_fun_arg_t *fun_arg;
58 stree_symbol_t *symbol;
59
60 ident = stree_ident_new();
61 ident->sid = strtab_get_sid("Builtin");
62
63 csi = stree_csi_new(csi_class);
64 csi->name = ident;
65 list_init(&csi->members);
66
67 modm = stree_modm_new(mc_csi);
68 modm->u.csi = csi;
69
70 symbol = stree_symbol_new(sc_csi);
71 symbol->u.csi = csi;
72 symbol->outer_csi = NULL;
73 csi->symbol = symbol;
74
75 list_append(&program->module->members, modm);
76
77 /* Declare builtin functions. */
78 ident = stree_ident_new();
79 ident->sid = strtab_get_sid("WriteLine");
80
81 fun = stree_fun_new();
82 fun->name = ident;
83 fun->body = NULL;
84 list_init(&fun->args);
85
86 csimbr = stree_csimbr_new(csimbr_fun);
87 csimbr->u.fun = fun;
88
89 symbol = stree_symbol_new(sc_fun);
90 symbol->u.fun = fun;
91 symbol->outer_csi = csi;
92 fun->symbol = symbol;
93
94 list_append(&csi->members, csimbr);
95
96 fun_arg = stree_fun_arg_new();
97 fun_arg->name = stree_ident_new();
98 fun_arg->name->sid = strtab_get_sid("arg");
99 fun_arg->type = NULL; /* XXX */
100
101 list_append(&fun->args, fun_arg);
102
103 bi_write_line = symbol;
104}
105
106void builtin_run_fun(run_t *run, stree_symbol_t *fun_sym)
107{
108#ifdef DEBUG_RUN_TRACE
109 printf("Run builtin function.\n");
110#endif
111 if (fun_sym == bi_write_line) {
112 builtin_write_line(run);
113 } else {
114 assert(b_false);
115 }
116}
117
118static void builtin_write_line(run_t *run)
119{
120 rdata_var_t *var;
121
122#ifdef DEBUG_RUN_TRACE
123 printf("Called Builtin.writeLine()\n");
124#endif
125 var = run_local_vars_lookup(run, strtab_get_sid("arg"));
126 assert(var);
127
128 switch (var->vc) {
129 case vc_int:
130 printf("%d\n", var->u.int_v->value);
131 break;
132 case vc_string:
133 printf("%s\n", var->u.string_v->value);
134 break;
135 default:
136 printf("Unimplemented: writeLine() with unsupported type.\n");
137 exit(1);
138 }
139}
Note: See TracBrowser for help on using the repository browser.