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); 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 #endif

View File

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

View File

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

View File

@ -7,8 +7,34 @@ using namespace std;
#include "include/parser.h" #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) { Node parse(vector<Token> tokens) {
reverse(tokens.begin(), tokens.end()); reverse(tokens.begin(), tokens.end());
tokens.pop_back(); // Remove the last ';'
if (tokens.size() == 0) if (tokens.size() == 0)
throw ParseException(); throw ParseException();
@ -125,7 +151,7 @@ ParseReturn parse_expr_statement(vector<Token> tokens) {
ParseReturn ret = parse_expr(tokens); ParseReturn ret = parse_expr(tokens);
InnerNode node = { InnerNode node = {
.type=NodeType::Declaration, .type=NodeType::AssignedDeclaration,
.children={type, identifier, ret.node} .children={type, identifier, ret.node}
}; };
return { return {

View File

@ -9,8 +9,7 @@ regex NUMBER_REGEX ("\\d+(\\.\\d+)?");
regex TYPE_INT_REGEX ("int\\s"); regex TYPE_INT_REGEX ("int\\s");
regex IDENTIFIER_REGEX ("[A-Za-z_]\\w*"); regex IDENTIFIER_REGEX ("[A-Za-z_]\\w*");
void _debug_print_tokens(vector<Token> tokens) { void _debug_print_token(Token token) {
for (Token token : tokens) {
switch (token.type) { switch (token.type) {
case TokenType::Type: case TokenType::Type:
cout << "Type(INT)"; cout << "Type(INT)";
@ -49,6 +48,11 @@ void _debug_print_tokens(vector<Token> tokens) {
cout << ")"; cout << ")";
break; break;
} }
}
void _debug_print_tokens(vector<Token> tokens) {
for (Token token : tokens) {
_debug_print_token(token);
cout << " "; cout << " ";
} }
cout << endl; cout << endl;