Add _debug_print_tree

This commit is contained in:
augustin64 2023-11-10 19:04:24 +01:00
parent 966bd36dad
commit fea4292700
5 changed files with 80 additions and 39 deletions

View File

@ -131,4 +131,9 @@ ParseReturn parse_u(vector<Token> tokens);
*/
ParseReturn parse_f(vector<Token> tokens);
/**
* Prints a tree for debugging it
*/
void _debug_print_tree(const Node& node, int depth, const string& prefix);
#endif

View File

@ -21,6 +21,11 @@ struct Token {
*/
vector<Token> tokenize(string str);
/*
Format and print a Token
*/
void _debug_print_token(Token token);
/*
Formats a list of tokens and prints it
*/

View File

@ -12,6 +12,7 @@ int main(int argc, char* argv[]) {
string input = get_input();
vector<Token> tokens = tokenize(input);
Node ast = parse(tokens);
// _debug_print_tree(ast, 0, "");
EvalResult res = eval(ast);
cout << get<int>(res) << endl;
}

View File

@ -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<InnerNode>(node)) {
const InnerNode& innerNode = get<InnerNode>(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<Token>(node);
cout << prefix;
_debug_print_token(token);
if (holds_alternative<int>(token.data)) {
cout << ", Value: " << get<int>(token.data);
} else if (holds_alternative<string>(token.data)) {
cout << ", Value: " << get<string>(token.data);
}
cout << "\n";
}
}
Node parse(vector<Token> 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<Token> tokens) {
ParseReturn ret = parse_expr(tokens);
InnerNode node = {
.type=NodeType::Declaration,
.type=NodeType::AssignedDeclaration,
.children={type, identifier, ret.node}
};
return {

View File

@ -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<int>(token.data) << ")";
break;
case TokenType::Identifier:
cout << "Identifier(" << get<string>(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<Token> tokens) {
for (Token token : tokens) {
switch (token.type) {
case TokenType::Type:
cout << "Type(INT)";
break;
case TokenType::Int:
cout << "Number(" << get<int>(token.data) << ")";
break;
case TokenType::Identifier:
cout << "Identifier(" << get<string>(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;