diff --git a/src/include/memory.h b/src/include/memory.h index 1bfe920..fec96da 100644 --- a/src/include/memory.h +++ b/src/include/memory.h @@ -2,7 +2,10 @@ #define MEMORY_H #include +#include +#include #include "types.h" +#include "colors.h" using namespace std; class Memory { @@ -17,6 +20,7 @@ class Memory { void update(string identifier, EvalResult value); Scope& _debug_top(void); + void _debug_print(void); Memory(void); diff --git a/src/main.cpp b/src/main.cpp index e5a9a44..ef16277 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -91,31 +91,36 @@ int main(int argc, char* argv[]) { tokens = tokenize(input, initial_line); if (print_tokens) { - cout << BOLD YELLOW "=== TOKENS ===" RESET << endl; + cout << BOLD YELLOW " === TOKENS ===" RESET << endl; _debug_print_tokens(tokens); } Node ast = parse(tokens); if (print_ast) { - cout << BOLD YELLOW "=== AST ===" RESET << endl; + cout << BOLD YELLOW " === AST ===" RESET << endl; _debug_print_tree(ast, 0, ""); } Memory type_mem = memory; analyze(ast, type_mem); - //if (dump_type_mem) { - // cout << BOLD YELLOW "=== TYPE MEMORY ===" RESET << endl; - // type_mem._debug_print(); - //} + 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 (dump_mem) { + cout << BOLD YELLOW " === MEMORY ===" RESET << endl; + memory._debug_print(); + } + + if ((print_tokens || print_ast || dump_type_mem || dump_mem) && + (holds_alternative(res) || holds_alternative(res))) { + cout << BOLD BLUE " === RESULT ===" RESET << endl; + } if (holds_alternative(res)) { cout << get(res) << endl; diff --git a/src/memory.cpp b/src/memory.cpp index f37d9c5..a0dec4e 100644 --- a/src/memory.cpp +++ b/src/memory.cpp @@ -61,4 +61,50 @@ void Memory::update(string identifier, EvalResult value) { Scope& Memory::_debug_top(void) { Scope& top = scopes.back(); return top; +} + +void _debug_print_memory_var(MemoryVar var) { + if (var.initialized && !holds_alternative(var.value)) { + cout << " "; + if (holds_alternative(var.value)) { + cout << get(var.value); + } else if (holds_alternative(var.value)) { + cout << get(var.value); + } else { + cout << "{Unsupported}"; + } + } else { + cout << setw (5) << "{}"; + } +} + +const char* _debug_type_names[] = { + "Int", "Double" +}; + +void _debug_print_scope(Scope scope) { + for (auto it : scope.vars) { + if (it.first.length() > 6) { + cout << it.first.substr(0, 6) << ".."; + } else { + cout << setw (6) << it.first << " "; + } + cout << "|"; + cout << setw (7) << _debug_type_names[int(it.second.type)] << " |"; + + _debug_print_memory_var(it.second); + cout << endl; + } +} + +void Memory::_debug_print(void) { + cout << BOLD " Name | Type | Value" RESET << endl; + for (auto rit = scopes.rbegin(); rit != scopes.rend(); ++rit) { + Scope& scope = *rit; + if (rit != scopes.rbegin()) { + cout << " --- New Scope ---" << endl; + } + + _debug_print_scope(scope); + } } \ No newline at end of file diff --git a/src/parser.cpp b/src/parser.cpp index d1a3c9b..4974f2a 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -14,7 +14,7 @@ CodePosition null_pos = { }; -const char* _node_names[] = { +const char* _debug_ast_node_names[] = { "Prog", "Epsilon", "AssignedDeclaration", "Declaration", "Plus", "Minus", "Mult", "Div", "Mod", "UnaryMinus", "UnaryPlus", "Neg", "Assignment", "LIncr", "RIncr", "LDecr", "RDecr", "If", "IfElse", "For", "While", "Bloc", "Lt", "Gt", "Leq", "Geq", "Eq", "Neq", "Land", "Lor" @@ -23,7 +23,7 @@ void _debug_print_tree(const Node& node, int depth, const string& prefix) { if (holds_alternative(node)) { const InnerNode& innerNode = get(node); - cout << prefix << _node_names[int(innerNode.type)] << "\n"; + cout << prefix << _debug_ast_node_names[int(innerNode.type)] << "\n"; string new_prefix = prefix; size_t pos = new_prefix.find("└──"); @@ -775,4 +775,4 @@ ParseReturn parse_par_identifier(vector tokens) { .node=ret.node, .tokens=tokens }; -} \ No newline at end of file +}