source: mainline/uspace/app/sbi/src/imode.c@ d9fae235

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

Update SBI to rev. 184.

  • Property mode set to 100644
File size: 5.0 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 Interactive mode.
30 *
31 * In interactive mode the user types in statements. As soon as the outermost
32 * statement is complete (terminated with ';' or 'end'), the interpreter
33 * executes it. Otherwise it prompts the user until the entire statement
34 * is read in.
35 *
36 * The user interface depends on the OS. In HelenOS we use the CLUI library
37 * which gives us rich line editing capabilities.
38 */
39
40#include <stdio.h>
41#include <stdlib.h>
42#include "os/os.h"
43#include "ancr.h"
44#include "assert.h"
45#include "builtin.h"
46#include "intmap.h"
47#include "list.h"
48#include "mytypes.h"
49#include "program.h"
50#include "rdata.h"
51#include "stree.h"
52#include "strtab.h"
53#include "stype.h"
54#include "input.h"
55#include "lex.h"
56#include "parse.h"
57#include "run.h"
58
59#include "imode.h"
60
61/** Run in interactive mode.
62 *
63 * Repeatedly read in statements from the user and execute them.
64 */
65void imode_run(void)
66{
67 input_t *input;
68 lex_t lex;
69 parse_t parse;
70 stree_program_t *program;
71 stree_stat_t *stat;
72 stree_proc_t *proc;
73 stree_fun_t *fun;
74 stree_symbol_t *fun_sym;
75 stype_t stype;
76 stype_block_vr_t *block_vr;
77 list_node_t *bvr_n;
78 rdata_item_t *rexpr;
79 rdata_item_t *rexpr_vi;
80
81 run_t run;
82 run_proc_ar_t *proc_ar;
83
84 bool_t quit_im;
85
86 /* Create an empty program. */
87 program = stree_program_new();
88 program->module = stree_module_new();
89
90 /* Declare builtin symbols. */
91 builtin_declare(program);
92
93 /* Process the library. */
94 if (program_lib_process(program) != EOK)
95 exit(1);
96
97 /* Resolve ancestry. */
98 ancr_module_process(program, program->module);
99
100 /* Construct typing context. */
101 stype.program = program;
102 stype.proc_vr = stype_proc_vr_new();
103 list_init(&stype.proc_vr->block_vr);
104 stype.current_csi = NULL;
105 proc = stree_proc_new();
106
107 fun = stree_fun_new();
108 fun_sym = stree_symbol_new(sc_fun);
109 fun_sym->u.fun = fun;
110 fun->name = stree_ident_new();
111 fun->name->sid = strtab_get_sid("$imode");
112
113 stype.proc_vr->proc = proc;
114 fun->symbol = fun_sym;
115 proc->outer_symbol = fun_sym;
116
117 /* Create block visit record. */
118 block_vr = stype_block_vr_new();
119 intmap_init(&block_vr->vdecls);
120
121 /* Add block visit record to the stack. */
122 list_append(&stype.proc_vr->block_vr, block_vr);
123
124 /* Construct run context. */
125 run.thread_ar = run_thread_ar_new();
126 list_init(&run.thread_ar->proc_ar);
127 run_proc_ar_create(&run, NULL, proc, &proc_ar);
128 list_append(&run.thread_ar->proc_ar, proc_ar);
129
130 printf("SBI interactive mode. ");
131 os_input_disp_help();
132
133 quit_im = b_false;
134 while (quit_im != b_true) {
135 parse.error = b_false;
136 stype.error = b_false;
137 run.thread_ar->exc_payload = NULL;
138 run.thread_ar->bo_mode = bm_none;
139
140 input_new_interactive(&input);
141
142 /* Parse input. */
143 lex_init(&lex, input);
144 parse_init(&parse, program, &lex);
145
146 if (lcur_lc(&parse) == lc_eof)
147 break;
148
149 stat = parse_stat(&parse);
150
151 if (parse.error != b_false)
152 continue;
153
154 /* Type statement. */
155 stype_stat(&stype, stat, b_true);
156
157 if (stype.error != b_false)
158 continue;
159
160 /* Run statement. */
161 run_init(&run);
162 run.program = program;
163 run_stat(&run, stat, &rexpr);
164
165 /* Check for unhandled exceptions. */
166 run_exc_check_unhandled(&run);
167
168 if (rexpr != NULL) {
169 /* Convert expression result to value item. */
170 run_cvt_value_item(&run, rexpr, &rexpr_vi);
171 assert(rexpr_vi->ic == ic_value);
172
173 /* Print result. */
174 printf("Result: ");
175 rdata_value_print(rexpr_vi->u.value);
176 printf("\n");
177 }
178 }
179
180 /* Remove block visit record from the stack, */
181 bvr_n = list_last(&stype.proc_vr->block_vr);
182 assert(list_node_data(bvr_n, stype_block_vr_t *) == block_vr);
183 list_remove(&stype.proc_vr->block_vr, bvr_n);
184
185 printf("\nBye!\n");
186}
Note: See TracBrowser for help on using the repository browser.