diff options
Diffstat (limited to 'src/driver.c')
-rw-r--r-- | src/driver.c | 49 |
1 files changed, 26 insertions, 23 deletions
diff --git a/src/driver.c b/src/driver.c index 597d7a8..60d3ce2 100644 --- a/src/driver.c +++ b/src/driver.c @@ -1,32 +1,35 @@ #include <stdio.h> #include <stdlib.h> +#include <string.h> +#include <unistd.h> #include "interpreter.h" +#include "repl.h" -extern FILE *yyin; -extern int yyparse(void); -extern ASTNode *ast_root; +static void interpret_file(const char *path) { + hashmap_t context = hashmap_create(); + exec_file(context, path); + context_print(context); + hashmap_free(context); +} int main(int argc, char **argv) { - if (argc > 1) { - yyin = fopen(argv[1], "r"); - if (!yyin) { - perror(argv[1]); - return EXIT_FAILURE; + int opt; + const char *script = NULL; + while ((opt = getopt(argc, argv, "i:h")) != -1) { + switch (opt){ + case 'i': + interpret_file(optarg); + return EXIT_SUCCESS; + case 'h': + default: + fprintf(stderr, + "Usage: %s [-i program.imp]\n" + " -i <program.imp> interpret program and exit\n" + " (no args) start REPL\n", + argv[0]); + return (opt == 'h') ? EXIT_SUCCESS : EXIT_FAILURE; } - } else { - yyin = stdin; - } - - if (yyparse() != 0) { - fprintf(stderr, "Parsing failed.\n"); - return EXIT_FAILURE; } - - hashmap_t context = hashmap_create(); - exec_stmt(context, ast_root); - context_print(context); - hashmap_free(context); - ast_free(ast_root); - + repl(); return EXIT_SUCCESS; -} +}
\ No newline at end of file |