Add memory._debug_print()
This commit is contained in:
parent
accf3bad4b
commit
cee884342a
@ -2,7 +2,10 @@
|
||||
#define MEMORY_H
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#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);
|
||||
|
||||
|
25
src/main.cpp
25
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<int>(res) || holds_alternative<double>(res))) {
|
||||
cout << BOLD BLUE " === RESULT ===" RESET << endl;
|
||||
}
|
||||
|
||||
if (holds_alternative<int>(res)) {
|
||||
cout << get<int>(res) << endl;
|
||||
|
@ -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<monostate>(var.value)) {
|
||||
cout << " ";
|
||||
if (holds_alternative<int>(var.value)) {
|
||||
cout << get<int>(var.value);
|
||||
} else if (holds_alternative<double>(var.value)) {
|
||||
cout << get<double>(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);
|
||||
}
|
||||
}
|
@ -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<InnerNode>(node)) {
|
||||
const InnerNode& innerNode = get<InnerNode>(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<Token> tokens) {
|
||||
.node=ret.node,
|
||||
.tokens=tokens
|
||||
};
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user