Add more debug arguments

This commit is contained in:
augustin64 2023-12-09 12:05:36 +01:00
parent bef747d558
commit accf3bad4b

View File

@ -11,17 +11,74 @@ using namespace std;
#include "include/analysis.h" #include "include/analysis.h"
#include "include/interpreter.h" #include "include/interpreter.h"
int main(int argc, char* argv[]) { void show_help(char* prog_name) {
bool print_ast = false; 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; int i=1;
while (i < argc) { 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; print_ast = true;
} else if (!strcmp(cur, "--type-memory")) {
dump_type_mem = true;
} else if (!strcmp(cur, "--memory")) {
dump_mem = true;
} else { } else {
cerr << "Unknow argument: " << argv[i] << endl; cerr << "Unknow argument: " << cur << endl;
} }
i++; 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<string> input; vector<string> input;
vector<Token> tokens; vector<Token> tokens;
@ -33,14 +90,32 @@ int main(int argc, char* argv[]) {
input = get_input(input); input = get_input(input);
tokens = tokenize(input, initial_line); tokens = tokenize(input, initial_line);
if (print_tokens) {
cout << BOLD YELLOW "=== TOKENS ===" RESET << endl;
_debug_print_tokens(tokens);
}
Node ast = parse(tokens); Node ast = parse(tokens);
if (print_ast) {
cout << BOLD YELLOW "=== AST ===" RESET << endl;
_debug_print_tree(ast, 0, "");
}
Memory type_mem = memory; Memory type_mem = memory;
analyze(ast, type_mem); analyze(ast, type_mem);
if (print_ast) //if (dump_type_mem) {
_debug_print_tree(ast, 0, ""); // cout << BOLD YELLOW "=== TYPE MEMORY ===" RESET << endl;
// type_mem._debug_print();
//}
EvalResult res = eval(ast, memory); EvalResult res = eval(ast, memory);
//if (dump_mem) {
// cout << BOLD YELLOW "=== MEMORY ===" RESET << endl;
// memory._debug_print();
//}
if (holds_alternative<int>(res)) { if (holds_alternative<int>(res)) {
cout << get<int>(res) << endl; cout << get<int>(res) << endl;
@ -53,6 +128,10 @@ int main(int argc, char* argv[]) {
pretty_print_error(input, e.pos); pretty_print_error(input, e.pos);
cout << BOLD RED "Syntax Error: " RESET << e.what() << endl; 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) { } catch (const RuntimeError& e) {
pretty_print_error(input, e.pos); pretty_print_error(input, e.pos);
cout << BOLD RED "Runtime Error: " RESET << e.what() << endl; cout << BOLD RED "Runtime Error: " RESET << e.what() << endl;