imode.c

00001 /*
00002  * Copyright (c) 2011 Jiri Svoboda
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00040 #include <stdio.h>
00041 #include <stdlib.h>
00042 #include "os/os.h"
00043 #include "ancr.h"
00044 #include "assert.h"
00045 #include "builtin.h"
00046 #include "intmap.h"
00047 #include "list.h"
00048 #include "mytypes.h"
00049 #include "program.h"
00050 #include "rdata.h"
00051 #include "stree.h"
00052 #include "strtab.h"
00053 #include "stype.h"
00054 #include "input.h"
00055 #include "lex.h"
00056 #include "parse.h"
00057 #include "run.h"
00058 
00059 #include "imode.h"
00060 
00065 void imode_run(void)
00066 {
00067         input_t *input;
00068         lex_t lex;
00069         parse_t parse;
00070         stree_program_t *program;
00071         stree_stat_t *stat;
00072         stree_proc_t *proc;
00073         stree_fun_t *fun;
00074         stree_symbol_t *fun_sym;
00075         stype_t stype;
00076         stype_block_vr_t *block_vr;
00077         list_node_t *bvr_n;
00078         rdata_item_t *rexpr;
00079         rdata_item_t *rexpr_vi;
00080 
00081         run_t run;
00082         run_proc_ar_t *proc_ar;
00083 
00084         bool_t quit_im;
00085 
00086         /* Create an empty program. */
00087         program = stree_program_new();
00088         program->module = stree_module_new();
00089 
00090         /* Declare builtin symbols. */
00091         builtin_declare(program);
00092 
00093         /* Process the library. */
00094         if (program_lib_process(program) != EOK)
00095                 exit(1);
00096 
00097         /* Resolve ancestry. */
00098         ancr_module_process(program, program->module);
00099 
00100         /* Bind internal interpreter references to symbols. */
00101         builtin_bind(program->builtin);
00102 
00103         /* Resolve ancestry. */
00104         ancr_module_process(program, program->module);
00105 
00106         /* Construct typing context. */
00107         stype.program = program;
00108         stype.proc_vr = stype_proc_vr_new();
00109         list_init(&stype.proc_vr->block_vr);
00110         stype.current_csi = NULL;
00111         proc = stree_proc_new();
00112 
00113         fun = stree_fun_new();
00114         fun_sym = stree_symbol_new(sc_fun);
00115         fun_sym->u.fun = fun;
00116         fun->name = stree_ident_new();
00117         fun->name->sid = strtab_get_sid("$imode");
00118         fun->sig = stree_fun_sig_new();
00119 
00120         stype.proc_vr->proc = proc;
00121         fun->symbol = fun_sym;
00122         proc->outer_symbol = fun_sym;
00123 
00124         /* Create block visit record. */
00125         block_vr = stype_block_vr_new();
00126         intmap_init(&block_vr->vdecls);
00127 
00128         /* Add block visit record to the stack. */
00129         list_append(&stype.proc_vr->block_vr, block_vr);
00130 
00131         /* Construct run context. */
00132         run_gdata_init(&run);
00133 
00134         run.thread_ar = run_thread_ar_new();
00135         list_init(&run.thread_ar->proc_ar);
00136         run_proc_ar_create(&run, run.gdata, proc, &proc_ar);
00137         list_append(&run.thread_ar->proc_ar, proc_ar);
00138 
00139         printf("SBI interactive mode. ");
00140         os_input_disp_help();
00141 
00142         quit_im = b_false;
00143         while (quit_im != b_true) {
00144                 parse.error = b_false;
00145                 stype.error = b_false;
00146                 run.thread_ar->exc_payload = NULL;
00147                 run.thread_ar->bo_mode = bm_none;
00148 
00149                 input_new_interactive(&input);
00150 
00151                 /* Parse input. */
00152                 lex_init(&lex, input);
00153                 parse_init(&parse, program, &lex);
00154 
00155                 if (lcur_lc(&parse) == lc_eof)
00156                         break;
00157 
00158                 stat = parse_stat(&parse);
00159 
00160                 if (parse.error != b_false)
00161                         continue;
00162 
00163                 /* Type statement. */
00164                 stype_stat(&stype, stat, b_true);
00165 
00166                 if (stype.error != b_false)
00167                         continue;
00168 
00169                 /* Run statement. */
00170                 run_init(&run);
00171                 run.program = program;
00172                 run_stat(&run, stat, &rexpr);
00173 
00174                 /* Check for unhandled exceptions. */
00175                 run_exc_check_unhandled(&run);
00176 
00177                 if (rexpr != NULL) {
00178                         /* Convert expression result to value item. */
00179                         run_cvt_value_item(&run, rexpr, &rexpr_vi);
00180                         rdata_item_destroy(rexpr);
00181 
00182                         /* Check for unhandled exceptions. */
00183                         run_exc_check_unhandled(&run);
00184                 } else {
00185                         rexpr_vi = NULL;
00186                 }
00187 
00188                 /*
00189                  * rexpr_vi can be NULL if either repxr was null or
00190                  * if the conversion to value item raised an exception.
00191                  */
00192                 if (rexpr_vi != NULL) {
00193                         assert(rexpr_vi->ic == ic_value);
00194 
00195                         /* Print result. */
00196                         printf("Result: ");
00197                         rdata_value_print(rexpr_vi->u.value);
00198                         printf("\n");
00199 
00200                         rdata_item_destroy(rexpr_vi);
00201                 }
00202         }
00203 
00204         run_proc_ar_destroy(&run, proc_ar);
00205 
00206         /* Remove block visit record from the stack, */
00207         bvr_n = list_last(&stype.proc_vr->block_vr);
00208         assert(list_node_data(bvr_n, stype_block_vr_t *) == block_vr);
00209         list_remove(&stype.proc_vr->block_vr, bvr_n);
00210 
00211         printf("\nBye!\n");
00212 }

Generated on Thu Jun 2 07:45:42 2011 for HelenOS/USB by  doxygen 1.4.7