main.c

00001 /*
00002  * Copyright (c) 2010 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 
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 #include "ancr.h"
00039 #include "os/os.h"
00040 #include "builtin.h"
00041 #include "imode.h"
00042 #include "mytypes.h"
00043 #include "program.h"
00044 #include "strtab.h"
00045 #include "stree.h"
00046 #include "stype.h"
00047 #include "input.h"
00048 #include "lex.h"
00049 #include "parse.h"
00050 #include "run.h"
00051 
00052 static void syntax_print(void);
00053 
00058 int main(int argc, char *argv[])
00059 {
00060         stree_program_t *program;
00061         stype_t stype;
00062         run_t run;
00063         int rc;
00064 
00065         /* Store executable file path under which we have been invoked. */
00066         os_store_ef_path(*argv);
00067 
00068         argv += 1;
00069         argc -= 1;
00070 
00071         if (argc == 0) {
00072                 /* Enter interactive mode */
00073                 strtab_init();
00074                 imode_run();
00075                 return 0;
00076         }
00077 
00078         if (os_str_cmp(*argv, "-h") == 0) {
00079                 syntax_print();
00080                 return 0;
00081         }
00082 
00083         strtab_init();
00084         program = stree_program_new();
00085         program->module = stree_module_new();
00086 
00087         /* Declare builtin symbols. */
00088         builtin_declare(program);
00089 
00090         /* Process source files in the library. */
00091         if (program_lib_process(program) != EOK)
00092                 return 1;
00093 
00094         /* Resolve ancestry. */
00095         ancr_module_process(program, program->module);
00096 
00097         /* Bind internal interpreter references to symbols. */
00098         builtin_bind(program->builtin);
00099 
00100         /* Process all source files specified in command-line arguments. */
00101         while (argc > 0) {
00102                 rc = program_file_process(program, *argv);
00103                 if (rc != EOK)
00104                         return 1;
00105 
00106                 argv += 1;
00107                 argc -= 1;
00108         }
00109 
00110         /* Resolve ancestry. */
00111         ancr_module_process(program, program->module);
00112 
00113         /* Type program. */
00114         stype.program = program;
00115         stype.error = b_false;
00116         stype_module(&stype, program->module);
00117 
00118         /* Check for typing errors. */
00119         if (stype.error)
00120                 return 1;
00121 
00122         /* Run program. */
00123         run_init(&run);
00124         run_program(&run, program);
00125 
00126         /* Check for run-time errors. */
00127         if (run.thread_ar->error)
00128                 return 1;
00129 
00130         return 0;
00131 }
00132 
00134 static void syntax_print(void)
00135 {
00136         printf("Syntax: sbi <source_file.sy>\n");
00137 }

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