c-repl/src/debug.cpp
2024-01-12 16:22:04 +01:00

176 lines
7.8 KiB
C++

#include <iostream>
#include "include/debug.hpp"
using namespace std;
string _debug_get_token_type_name(TokenType type) {
switch (type) {
case TokenType::Identifier: return "Identifier";
case TokenType::Litteral: return "Litteral";
case TokenType::Plus: return "Plus";
case TokenType::Minus: return "Minus";
case TokenType::DoublePlus: return "DoublePlus";
case TokenType::DoubleMinus: return "DoubleMinus";
case TokenType::DoubleEqual: return "DoubleEqual";
case TokenType::Land: return "Land";
case TokenType::Lor: return "Lor";
case TokenType::Lt: return "Lt";
case TokenType::Gt: return "Gt";
case TokenType::Leq: return "Leq";
case TokenType::Geq: return "Geq";
case TokenType::NotEqual: return "NotEqual";
case TokenType::Not: return "Not";
case TokenType::Star: return "Star";
case TokenType::Slash: return "Slash";
case TokenType::Percent: return "Percent";
case TokenType::Equal: return "Equal";
case TokenType::Semicolon: return "Semicolon";
case TokenType::LParenthese: return "LParenthese";
case TokenType::RParenthese: return "RParenthese";
case TokenType::LCurlyBracket: return "LCurlyBracket";
case TokenType::RCurlyBracket: return "RCurlyBracket";
case TokenType::If: return "If";
case TokenType::Else: return "Else";
case TokenType::While: return "While";
case TokenType::For: return "For";
case TokenType::Break: return "Break";
case TokenType::Continue: return "Continue";
case TokenType::Return: return "Return";
case TokenType::Comma: return "Comma";
default: return "Unknown";
}
}
string _debug_token_to_string(Token token) {
switch (token.type) {
case TokenType::Litteral: {
string value;
if (holds_alternative<int>(token.data))
value = to_string(get<int>(token.data));
else if (holds_alternative<double>(token.data))
value = to_string(get<double>(token.data));
return "Litteral(" + value + ")";
}
case TokenType::Identifier: return "Identifier(" + get<string>(token.data) + ")";
case TokenType::Plus: return "+";
case TokenType::Minus: return "-";
case TokenType::DoublePlus: return "++";
case TokenType::DoubleEqual: return "==";
case TokenType::DoubleMinus: return "--";
case TokenType::Land: return "&&";
case TokenType::Lor: return "||";
case TokenType::Lt: return "<";
case TokenType::Gt: return ">";
case TokenType::Leq: return "<=";
case TokenType::Geq: return ">=";
case TokenType::NotEqual: return "!=";
case TokenType::Not: return "!";
case TokenType::Star: return "*";
case TokenType::Slash: return "/";
case TokenType::Percent: return "%";
case TokenType::Equal: return "=";
case TokenType::Semicolon: return ";";
case TokenType::LParenthese: return "(";
case TokenType::RParenthese: return ")";
case TokenType::LCurlyBracket: return "{";
case TokenType::RCurlyBracket: return "}";
case TokenType::If: return "If";
case TokenType::Else: return "Else";
case TokenType::While: return "While";
case TokenType::For: return "For";
case TokenType::Break: return "Break";
case TokenType::Continue: return "Continue";
case TokenType::Return: return "Return";
case TokenType::Comma: return "Comma";
default: return "Unknown";
}
}
void _debug_print_token(Token token) {
cout << _debug_token_to_string(token);
}
void _debug_print_tokens(vector<Token> tokens) {
for (Token token : tokens) {
_debug_print_token(token);
cout << " ";
}
cout << endl;
}
string _debug_get_ast_node_name(NodeType type) {
switch (type) {
case NodeType::Prog: return "Prog";
case NodeType::Epsilon: return "Epsilon";
case NodeType::AssignedDeclaration: return "AssignedDeclaration";
case NodeType::Declaration: return "Declaration";
case NodeType::Plus: return "Plus";
case NodeType::Minus: return "Minus";
case NodeType::Mult: return "Mult";
case NodeType::Div: return "Div";
case NodeType::Mod: return "Mod";
case NodeType::UnaryMinus: return "UnaryMinus";
case NodeType::UnaryPlus: return "UnaryPlus";
case NodeType::Neg: return "Neg";
case NodeType::Assignment: return "Assignment";
case NodeType::LIncr: return "LIncr";
case NodeType::RIncr: return "RIncr";
case NodeType::LDecr: return "LDecr";
case NodeType::RDecr: return "RDecr";
case NodeType::If: return "If";
case NodeType::IfElse: return "IfElse";
case NodeType::For: return "For";
case NodeType::While: return "While";
case NodeType::Bloc: return "Bloc";
case NodeType::Lt: return "Lt";
case NodeType::Gt: return "Gt";
case NodeType::Leq: return "Leq";
case NodeType::Geq: return "Geq";
case NodeType::Eq: return "Eq";
case NodeType::Neq: return "Neq";
case NodeType::Land: return "Land";
case NodeType::Lor: return "Lor";
case NodeType::Comma: return "Comma";
case NodeType::FunctionPrototype: return "FunctionPrototype";
case NodeType::FunctionDeclaration: return "FunctionDeclaration";
case NodeType::FunctionCall: return "FunctionCall";
case NodeType::FunctionArgs: return "FunctionArgs";
case NodeType::FunctionArgsValues: return "FunctionArgsValues";
case NodeType::Return: return "Return";
default: return "Unknown";
}
}
void _debug_print_tree(const Node& node, int depth, const string& prefix) {
if (holds_alternative<InnerNode>(node)) {
const InnerNode& inner_node = get<InnerNode>(node);
cout << prefix << _debug_get_ast_node_name(inner_node.type) << "\n";
string new_prefix = prefix;
size_t pos = new_prefix.find("└──");
while (pos != string::npos) {
new_prefix.replace(pos, 9, " ");
pos = new_prefix.find("└──", pos + 4);
}
pos = new_prefix.find("├──");
while (pos != string::npos) {
new_prefix.replace(pos, 9, "");
pos = new_prefix.find("├──", pos + 6);
}
for (size_t i = 0; i < inner_node.children.size(); ++i) {
string child_prefix = (i == inner_node.children.size() - 1) ? "└── " : "├── ";
_debug_print_tree(inner_node.children[i], depth + 1, new_prefix + child_prefix);
}
} else {
const Token& token = get<Token>(node);
cout << prefix;
_debug_print_token(token);
cout << endl;
}
}