Add debug.cpp

This commit is contained in:
augustin64 2024-01-12 16:22:04 +01:00
parent cdf52b85a5
commit ac872a64c9
8 changed files with 214 additions and 245 deletions

176
src/debug.cpp Normal file
View File

@ -0,0 +1,176 @@
#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;
}
}

View File

@ -2,6 +2,7 @@
using namespace std; using namespace std;
#include "include/execute.hpp" #include "include/execute.hpp"
#include "include/debug.hpp"
EvalResult execute(vector<string> input, Memory& memory, int initial_line, ExecArgs args) { EvalResult execute(vector<string> input, Memory& memory, int initial_line, ExecArgs args) {

36
src/include/debug.hpp Normal file
View File

@ -0,0 +1,36 @@
#ifndef DEF_DEBUG_H
#define DEF_DEBUG_H
#include "types.hpp"
/**
* Returns the name of a TokenType
*/
string _debug_token_to_string(Token token);
/**
* Returns the name of a NodeType
*/
string _debug_get_ast_node_name(NodeType type);
/**
* Prints a tree for debugging it
*/
void _debug_print_tree(const Node& node, int depth = 0, const string& prefix = "");
/*
Returns the name of a TokenType
*/
string _debug_get_token_type_name(TokenType type);
/*
Format and print a Token
*/
void _debug_print_token(Token token);
/*
Formats a list of tokens and prints it
*/
void _debug_print_tokens(vector<Token> tokens);
#endif

View File

@ -12,19 +12,4 @@ using namespace std;
*/ */
vector<Token> tokenize(vector<string> str, int initial_line=0); vector<Token> tokenize(vector<string> str, int initial_line=0);
/*
Format and print a Token
*/
void _debug_print_token(Token token);
/*
Returns the name of a TokenType
*/
string _debug_get_token_type_name(TokenType type);
/*
Formats a list of tokens and prints it
*/
void _debug_print_tokens(vector<Token> tokens);
#endif #endif

View File

@ -91,9 +91,5 @@ ParseReturn parse_par_identifier(vector<Token> tokens);
*/ */
tuple<ParseReturn, optional<CodePosition>> parse_args(vector<Token> tokens); tuple<ParseReturn, optional<CodePosition>> parse_args(vector<Token> tokens);
/**
* Prints a tree for debugging it
*/
void _debug_print_tree(const Node& node, int depth = 0, const string& prefix = "");
#endif #endif

View File

@ -43,158 +43,6 @@ vector<tuple<string, TokenType>> simple_tokens = {
{ ",", TokenType::Comma } { ",", TokenType::Comma }
}; };
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";
}
}
void _debug_print_token(Token token) {
switch (token.type) {
case TokenType::Litteral:
if (holds_alternative<int>(token.data)) {
cout << "Litteral(" << get<int>(token.data) << ")";
}
else if (holds_alternative<double>(token.data)) {
cout << "Litteral(" << get<double>(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::DoublePlus:
cout << "++";
break;
case TokenType::DoubleMinus:
cout << "--";
break;
case TokenType::DoubleEqual:
cout << "==";
break;
case TokenType::Land:
cout << "&&";
break;
case TokenType::Lor:
cout << "||";
break;
case TokenType::Lt:
cout << "<";
break;
case TokenType::Gt:
cout << ">";
break;
case TokenType::Leq:
cout << "<=";
break;
case TokenType::Geq:
cout << ">=";
break;
case TokenType::NotEqual:
cout << "!=";
break;
case TokenType::Not:
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;
case TokenType::LCurlyBracket:
cout << "{";
break;
case TokenType::RCurlyBracket:
cout << "}";
break;
case TokenType::If:
cout << "If";
break;
case TokenType::Else:
cout << "Else";
break;
case TokenType::While:
cout << "While";
break;
case TokenType::For:
cout << "For";
break;
case TokenType::Break:
cout << "Break";
break;
case TokenType::Continue:
cout << "Continue";
break;
case TokenType::Return:
cout << "Return";
break;
case TokenType::Comma:
cout << "Comma";
break;
}
}
void _debug_print_tokens(vector<Token> tokens) {
for (Token token : tokens) {
_debug_print_token(token);
cout << " ";
}
cout << endl;
}
vector<Token> tokenize(vector<string> input, int initial_line) { vector<Token> tokenize(vector<string> input, int initial_line) {
vector<Token> tokens; vector<Token> tokens;

View File

@ -15,80 +15,6 @@ CodePosition null_pos = {
}; };
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;
}
}
Node parse(vector<Token> tokens) { Node parse(vector<Token> tokens) {
reverse(tokens.begin(), tokens.end()); reverse(tokens.begin(), tokens.end());

View File

@ -4,6 +4,7 @@ using namespace std;
#include "include/test.hpp" #include "include/test.hpp"
#include "../src/include/debug.hpp"
#include "../src/include/lexer.hpp" #include "../src/include/lexer.hpp"
int main() { int main() {