| 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 Main module.
|
|---|
| 30 | *
|
|---|
| 31 | * Main entry point for SBI, the Sysel Bootstrap Interpreter.
|
|---|
| 32 | * When run without parameters, the interpreter will enter interactive
|
|---|
| 33 | * mode.
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <stdio.h>
|
|---|
| 37 | #include <stdlib.h>
|
|---|
| 38 | #include "ancr.h"
|
|---|
| 39 | #include "os/os.h"
|
|---|
| 40 | #include "builtin.h"
|
|---|
| 41 | #include "imode.h"
|
|---|
| 42 | #include "mytypes.h"
|
|---|
| 43 | #include "program.h"
|
|---|
| 44 | #include "strtab.h"
|
|---|
| 45 | #include "stree.h"
|
|---|
| 46 | #include "stype.h"
|
|---|
| 47 | #include "input.h"
|
|---|
| 48 | #include "lex.h"
|
|---|
| 49 | #include "parse.h"
|
|---|
| 50 | #include "run.h"
|
|---|
| 51 |
|
|---|
| 52 | static void syntax_print(void);
|
|---|
| 53 |
|
|---|
| 54 | /** Main entry point.
|
|---|
| 55 | *
|
|---|
| 56 | * @return Zero on success, non-zero on error.
|
|---|
| 57 | */
|
|---|
| 58 | int main(int argc, char *argv[])
|
|---|
| 59 | {
|
|---|
| 60 | stree_program_t *program;
|
|---|
| 61 | stype_t stype;
|
|---|
| 62 | run_t run;
|
|---|
| 63 | errno_t rc;
|
|---|
| 64 |
|
|---|
| 65 | /* Store executable file path under which we have been invoked. */
|
|---|
| 66 | os_store_ef_path(*argv);
|
|---|
| 67 |
|
|---|
| 68 | argv += 1;
|
|---|
| 69 | argc -= 1;
|
|---|
| 70 |
|
|---|
| 71 | if (argc == 0) {
|
|---|
| 72 | /* Enter interactive mode */
|
|---|
| 73 | strtab_init();
|
|---|
| 74 | imode_run();
|
|---|
| 75 | return 0;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | if (os_str_cmp(*argv, "-h") == 0) {
|
|---|
| 79 | syntax_print();
|
|---|
| 80 | return 0;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | strtab_init();
|
|---|
| 84 | program = stree_program_new();
|
|---|
| 85 | program->module = stree_module_new();
|
|---|
| 86 |
|
|---|
| 87 | /* Declare builtin symbols. */
|
|---|
| 88 | builtin_declare(program);
|
|---|
| 89 |
|
|---|
| 90 | /* Process source files in the library. */
|
|---|
| 91 | if (program_lib_process(program) != EOK)
|
|---|
| 92 | return 1;
|
|---|
| 93 |
|
|---|
| 94 | /* Resolve ancestry. */
|
|---|
| 95 | ancr_module_process(program, program->module);
|
|---|
| 96 |
|
|---|
| 97 | /* Bind internal interpreter references to symbols. */
|
|---|
| 98 | builtin_bind(program->builtin);
|
|---|
| 99 |
|
|---|
| 100 | /* Process all source files specified in command-line arguments. */
|
|---|
| 101 | while (argc > 0) {
|
|---|
| 102 | rc = program_file_process(program, *argv);
|
|---|
| 103 | if (rc != EOK)
|
|---|
| 104 | return 1;
|
|---|
| 105 |
|
|---|
| 106 | argv += 1;
|
|---|
| 107 | argc -= 1;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | /* Resolve ancestry. */
|
|---|
| 111 | ancr_module_process(program, program->module);
|
|---|
| 112 |
|
|---|
| 113 | /* Type program. */
|
|---|
| 114 | stype.program = program;
|
|---|
| 115 | stype.error = b_false;
|
|---|
| 116 | stype_module(&stype, program->module);
|
|---|
| 117 |
|
|---|
| 118 | /* Check for typing errors. */
|
|---|
| 119 | if (stype.error)
|
|---|
| 120 | return 1;
|
|---|
| 121 |
|
|---|
| 122 | /* Run program. */
|
|---|
| 123 | run_init(&run);
|
|---|
| 124 | run_program(&run, program);
|
|---|
| 125 |
|
|---|
| 126 | /* Check for run-time errors. */
|
|---|
| 127 | if (run.thread_ar->error)
|
|---|
| 128 | return 1;
|
|---|
| 129 |
|
|---|
| 130 | return 0;
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | /** Print command-line syntax help. */
|
|---|
| 134 | static void syntax_print(void)
|
|---|
| 135 | {
|
|---|
| 136 | printf("Syntax: sbi <source_file.sy>\n");
|
|---|
| 137 | }
|
|---|