From e46af8eff6fcaa1cf06a08dde28ad6ea201657e7 Mon Sep 17 00:00:00 2001 From: Teddy Date: Mon, 12 Aug 2013 11:10:28 +0800 Subject: ready for alpha release --- main.cpp | 99 +++++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 63 insertions(+), 36 deletions(-) (limited to 'main.cpp') diff --git a/main.cpp b/main.cpp index 28bd27d..e751dd3 100644 --- a/main.cpp +++ b/main.cpp @@ -3,64 +3,91 @@ #include "parser.h" #include "eval.h" #include "exc.h" -#include -int main(int argc, char **argv) { - //freopen("in.scm", "r", stdin); - Tokenizor *tk = new Tokenizor(); - ASTGenerator *ast = new ASTGenerator(); - Evaluator *eval = new Evaluator(); +#include +#include - bool interactive = false; - bool preload = false; - char *fname; +Tokenizor *tk = new Tokenizor(); +ASTGenerator *ast = new ASTGenerator(); +Evaluator *eval = new Evaluator(); - int rcnt = 0; - for (int i = 1; i < argc; i++) - if (strcmp(argv[i], "-i") == 0) - interactive = true; - else if (strcmp(argv[i], "-l") == 0 && i < argc - 1) +void load_file(const char *fname) { + FILE *f = fopen(fname, "r"); + if (!f) + { + printf("Can not open file: %s\n", fname); + exit(0); + } + tk->set_stream(f); + while (1) + { + try { - preload = true; - fname = argv[i + 1]; + Pair *tree = ast->absorb(tk); + if (!tree) break; + eval->run_expr(tree); } - - if (preload) - { - FILE *f = fopen(fname, "r"); - if (!f) + catch (GeneralError &e) { - printf("Can not open file: %s\n", fname); - return 0; + fprintf(stderr, "An error occured: %s\n", e.get_msg().c_str()); } - tk->set_stream(f); - while (1) + } +} + +void print_help(const char *cmd) { + fprintf(stderr, + "Sonsi: Stupid and Obvious Scheme Interpreter\n" + "Usage: %s OPTION ...\n" + "Evaluate Scheme code, interactively or from a script.\n\n" + " FILE \t\tload Scheme source code from FILE, and exit\n" + "The above switches stop argument processing\n\n" + " -l FILE \tload Scheme source code from FILE\n" + " -h display \tthis help and exit\n", cmd); + exit(0); +} + +int main(int argc, char **argv) { + + for (int i = 1; i < argc; i++) + { + if (*argv[i] == '-') // parsing options { - try + if (strcmp(argv[i], "-l") == 0) { - Pair *tree = ast->absorb(tk); - if (!tree) break; - eval->run_expr(tree)->ext_repr().c_str(); + if (i + 1 < argc) + load_file(argv[++i]); + else + { + puts("missing argument to `-l` switch"); + print_help(*argv); + } } - catch (GeneralError &e) + else if (strcmp(argv[i], "-h") == 0) + print_help(*argv); + else { - fprintf(stderr, "An error occured: %s\n", e.get_msg().c_str()); + printf("unrecognized switch `%s`\n", argv[i]); + print_help(*argv); } } - interactive = true; + else + { + load_file(argv[i]); + exit(0); + } } - tk->set_stream(stdin); + + int rcnt = 0; + tk->set_stream(stdin); // interactive mode while (1) { - if (interactive) fprintf(stderr, "Sonsi> "); try { Pair *tree = ast->absorb(tk); if (!tree) break; string output = eval->run_expr(tree)->ext_repr(); - if (interactive) - fprintf(stderr, "Ret> $%d = %s\n", rcnt++, output.c_str()); + fprintf(stderr, "Ret> $%d = %s\n", rcnt++, output.c_str()); } catch (GeneralError &e) { -- cgit v1.2.3