From accf3bad4b0e05045f690b5fd6bc779635714bd8 Mon Sep 17 00:00:00 2001 From: augustin64 Date: Sat, 9 Dec 2023 12:05:36 +0100 Subject: [PATCH] Add more debug arguments --- src/main.cpp | 91 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 85 insertions(+), 6 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 8371508..e5a9a44 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,17 +11,74 @@ using namespace std; #include "include/analysis.h" #include "include/interpreter.h" -int main(int argc, char* argv[]) { - bool print_ast = false; +void show_help(char* prog_name) { + printf("Usage: %s [OPTIONS]\n\n", prog_name); + printf("\t--help | -h\tAfficher cette aide\n"); + printf("\t--tokens | -t\tAfficher les tokens lus\n"); + printf("\t--ast | -a\tAfficher l'AST créé\n"); + printf("\t--type-memory | -T\tAfficher le contenu de la mémoire de types avant exécution\n"); + printf("\t--memory | -m\tAfficher le contenu de la mémoire après exécution\n"); +} + + +void parse_args(int argc, char* argv[], bool &print_tokens, bool &print_ast, bool &dump_type_mem, bool &dump_mem, bool &help) { int i=1; while (i < argc) { - if (!strcmp(argv[i], "--show-ast") || !strcmp(argv[i], "-s")) { + char* cur = argv[i]; + int n = strlen(cur); + if (n >= 2 && cur[0] == '-' && cur[1] != '-') { + for (int j=1; j < n; j++) { + switch (cur[j]) { + case 'h': + help = true; + break; + case 't': + print_tokens = true; + break; + case 'a': + print_ast = true; + break; + case 'T': + dump_type_mem = true; + break; + case 'm': + dump_mem = true; + break; + default: + cerr << "Unknow argument: -" << cur[j] << endl; + } + } + } else if (!strcmp(cur, "--help")) { + help = true; + } else if (!strcmp(cur, "--tokens")) { + print_tokens = true; + } else if (!strcmp(cur, "--ast")) { print_ast = true; + } else if (!strcmp(cur, "--type-memory")) { + dump_type_mem = true; + } else if (!strcmp(cur, "--memory")) { + dump_mem = true; } else { - cerr << "Unknow argument: " << argv[i] << endl; + cerr << "Unknow argument: " << cur << endl; } i++; } +} + + +int main(int argc, char* argv[]) { + bool print_tokens = false; + bool print_ast = false; + bool dump_type_mem = false; + bool dump_mem = false; + bool help = false; + + parse_args(argc, argv, print_tokens, print_ast, dump_type_mem, dump_mem, help); + + if (help) { + show_help(argv[0]); + return 0; + } vector input; vector tokens; @@ -33,14 +90,32 @@ int main(int argc, char* argv[]) { input = get_input(input); tokens = tokenize(input, initial_line); + if (print_tokens) { + cout << BOLD YELLOW "=== TOKENS ===" RESET << endl; + _debug_print_tokens(tokens); + } + Node ast = parse(tokens); + + if (print_ast) { + cout << BOLD YELLOW "=== AST ===" RESET << endl; + _debug_print_tree(ast, 0, ""); + } + Memory type_mem = memory; analyze(ast, type_mem); - if (print_ast) - _debug_print_tree(ast, 0, ""); + //if (dump_type_mem) { + // cout << BOLD YELLOW "=== TYPE MEMORY ===" RESET << endl; + // type_mem._debug_print(); + //} EvalResult res = eval(ast, memory); + + //if (dump_mem) { + // cout << BOLD YELLOW "=== MEMORY ===" RESET << endl; + // memory._debug_print(); + //} if (holds_alternative(res)) { cout << get(res) << endl; @@ -53,6 +128,10 @@ int main(int argc, char* argv[]) { pretty_print_error(input, e.pos); cout << BOLD RED "Syntax Error: " RESET << e.what() << endl; + } catch (const TypeError& e) { + pretty_print_error(input, e.pos); + cout << BOLD RED "Type Error: " RESET << e.what() << endl; + } catch (const RuntimeError& e) { pretty_print_error(input, e.pos); cout << BOLD RED "Runtime Error: " RESET << e.what() << endl;