diff --git a/src/include/parser.h b/src/include/parser.h index 2b41879..28049d3 100644 --- a/src/include/parser.h +++ b/src/include/parser.h @@ -131,4 +131,9 @@ ParseReturn parse_u(vector tokens); */ ParseReturn parse_f(vector tokens); +/** + * Prints a tree for debugging it +*/ +void _debug_print_tree(const Node& node, int depth, const string& prefix); + #endif \ No newline at end of file diff --git a/src/include/tokenize.h b/src/include/tokenize.h index 300f040..d0ceacc 100644 --- a/src/include/tokenize.h +++ b/src/include/tokenize.h @@ -21,6 +21,11 @@ struct Token { */ vector tokenize(string str); +/* + Format and print a Token +*/ +void _debug_print_token(Token token); + /* Formats a list of tokens and prints it */ diff --git a/src/main.cpp b/src/main.cpp index e4382d4..1b197b2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,6 +12,7 @@ int main(int argc, char* argv[]) { string input = get_input(); vector tokens = tokenize(input); Node ast = parse(tokens); + // _debug_print_tree(ast, 0, ""); EvalResult res = eval(ast); cout << get(res) << endl; } diff --git a/src/parser.cpp b/src/parser.cpp index fcb9da1..925bfdd 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -7,8 +7,34 @@ using namespace std; #include "include/parser.h" +const char* _node_names[] = {"Prog", "Epsilon", "AssignedDeclaration", "Declaration", "Plus", "Minus", "Mult", "Div", "Mod", "UnaryMinus", "UnaryPlus", "Assignment"}; +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"; + + for (size_t i = 0; i < innerNode.children.size(); ++i) { + string childPrefix = (i == innerNode.children.size() - 1) ? "└── " : "├── "; + _debug_print_tree(innerNode.children[i], depth + 1, prefix + childPrefix); + } + } else { + const Token& token = get(node); + cout << prefix; + _debug_print_token(token); + if (holds_alternative(token.data)) { + cout << ", Value: " << get(token.data); + } else if (holds_alternative(token.data)) { + cout << ", Value: " << get(token.data); + } + cout << "\n"; + } +} + + Node parse(vector tokens) { reverse(tokens.begin(), tokens.end()); + tokens.pop_back(); // Remove the last ';' if (tokens.size() == 0) throw ParseException(); @@ -125,7 +151,7 @@ ParseReturn parse_expr_statement(vector tokens) { ParseReturn ret = parse_expr(tokens); InnerNode node = { - .type=NodeType::Declaration, + .type=NodeType::AssignedDeclaration, .children={type, identifier, ret.node} }; return { diff --git a/src/tokenize.cpp b/src/tokenize.cpp index cc55cde..a562361 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -9,46 +9,50 @@ regex NUMBER_REGEX ("\\d+(\\.\\d+)?"); regex TYPE_INT_REGEX ("int\\s"); regex IDENTIFIER_REGEX ("[A-Za-z_]\\w*"); +void _debug_print_token(Token token) { + switch (token.type) { + case TokenType::Type: + cout << "Type(INT)"; + break; + case TokenType::Int: + cout << "Number(" << get(token.data) << ")"; + break; + case TokenType::Identifier: + cout << "Identifier(" << get(token.data) << ")"; + break; + case TokenType::Plus: + cout << "+"; + break; + case TokenType::Minus: + cout << "-"; + break; + case TokenType::Star: + cout << "*"; + break; + case TokenType::Slash: + cout << "/"; + break; + case TokenType::Percent: + cout << "%"; + break; + case TokenType::Equal: + cout << "="; + break; + case TokenType::Semicolon: + cout << ";"; + break; + case TokenType::LParenthese: + cout << "("; + break; + case TokenType::RParenthese: + cout << ")"; + break; + } +} + void _debug_print_tokens(vector tokens) { for (Token token : tokens) { - switch (token.type) { - case TokenType::Type: - cout << "Type(INT)"; - break; - case TokenType::Int: - cout << "Number(" << get(token.data) << ")"; - break; - case TokenType::Identifier: - cout << "Identifier(" << get(token.data) << ")"; - break; - case TokenType::Plus: - cout << "+"; - break; - case TokenType::Minus: - cout << "-"; - break; - case TokenType::Star: - cout << "*"; - break; - case TokenType::Slash: - cout << "/"; - break; - case TokenType::Percent: - cout << "%"; - break; - case TokenType::Equal: - cout << "="; - break; - case TokenType::Semicolon: - cout << ";"; - break; - case TokenType::LParenthese: - cout << "("; - break; - case TokenType::RParenthese: - cout << ")"; - break; - } + _debug_print_token(token); cout << " "; } cout << endl;