Add _debug_print_tree
This commit is contained in:
parent
966bd36dad
commit
fea4292700
@ -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
|
@ -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
|
||||
*/
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -9,8 +9,7 @@ regex NUMBER_REGEX ("\\d+(\\.\\d+)?");
|
||||
regex TYPE_INT_REGEX ("int\\s");
|
||||
regex IDENTIFIER_REGEX ("[A-Za-z_]\\w*");
|
||||
|
||||
void _debug_print_tokens(vector<Token> tokens) {
|
||||
for (Token token : tokens) {
|
||||
void _debug_print_token(Token token) {
|
||||
switch (token.type) {
|
||||
case TokenType::Type:
|
||||
cout << "Type(INT)";
|
||||
@ -49,6 +48,11 @@ void _debug_print_tokens(vector<Token> tokens) {
|
||||
cout << ")";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void _debug_print_tokens(vector<Token> tokens) {
|
||||
for (Token token : tokens) {
|
||||
_debug_print_token(token);
|
||||
cout << " ";
|
||||
}
|
||||
cout << endl;
|
||||
|
Loading…
Reference in New Issue
Block a user