| 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 "builtin.h"
|
|---|
| 40 | #include "imode.h"
|
|---|
| 41 | #include "mytypes.h"
|
|---|
| 42 | #include "strtab.h"
|
|---|
| 43 | #include "stree.h"
|
|---|
| 44 | #include "stype.h"
|
|---|
| 45 | #include "input.h"
|
|---|
| 46 | #include "lex.h"
|
|---|
| 47 | #include "parse.h"
|
|---|
| 48 | #include "run.h"
|
|---|
| 49 |
|
|---|
| 50 | void syntax_print(void);
|
|---|
| 51 |
|
|---|
| 52 | /** Main entry point.
|
|---|
| 53 | *
|
|---|
| 54 | * @return Zero on success, non-zero on error.
|
|---|
| 55 | */
|
|---|
| 56 | int main(int argc, char *argv[])
|
|---|
| 57 | {
|
|---|
| 58 | input_t *input;
|
|---|
| 59 | lex_t lex;
|
|---|
| 60 | parse_t parse;
|
|---|
| 61 | stree_program_t *program;
|
|---|
| 62 | stype_t stype;
|
|---|
| 63 | run_t run;
|
|---|
| 64 | int rc;
|
|---|
| 65 |
|
|---|
| 66 | if (argc == 1) {
|
|---|
| 67 | /* Enter interactive mode */
|
|---|
| 68 | strtab_init();
|
|---|
| 69 | imode_run();
|
|---|
| 70 | return 0;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | if (argc != 2) {
|
|---|
| 74 | syntax_print();
|
|---|
| 75 | exit(1);
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | rc = input_new_file(&input, argv[1]);
|
|---|
| 79 | if (rc != EOK) {
|
|---|
| 80 | printf("Failed opening source file '%s'.\n", argv[1]);
|
|---|
| 81 | exit(1);
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | strtab_init();
|
|---|
| 85 | program = stree_program_new();
|
|---|
| 86 | program->module = stree_module_new();
|
|---|
| 87 |
|
|---|
| 88 | /* Declare builtin symbols. */
|
|---|
| 89 | builtin_declare(program);
|
|---|
| 90 |
|
|---|
| 91 | /* Parse input file. */
|
|---|
| 92 | lex_init(&lex, input);
|
|---|
| 93 | parse_init(&parse, program, &lex);
|
|---|
| 94 | parse_module(&parse);
|
|---|
| 95 |
|
|---|
| 96 | if (parse.error)
|
|---|
| 97 | return 1;
|
|---|
| 98 |
|
|---|
| 99 | /* Resolve ancestry. */
|
|---|
| 100 | ancr_module_process(program, parse.cur_mod);
|
|---|
| 101 |
|
|---|
| 102 | /* Type program. */
|
|---|
| 103 | stype.program = program;
|
|---|
| 104 | stype.error = b_false;
|
|---|
| 105 | stype_module(&stype, program->module);
|
|---|
| 106 |
|
|---|
| 107 | if (stype.error)
|
|---|
| 108 | return 1;
|
|---|
| 109 |
|
|---|
| 110 | /* Run program. */
|
|---|
| 111 | run_init(&run);
|
|---|
| 112 | run_program(&run, program);
|
|---|
| 113 |
|
|---|
| 114 | return 0;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | /** Print command-line syntax help. */
|
|---|
| 118 | void syntax_print(void)
|
|---|
| 119 | {
|
|---|
| 120 | printf("Missing or invalid arguments.\n");
|
|---|
| 121 | printf("Syntax: sbi <source_file.sy>\n");
|
|---|
| 122 | }
|
|---|