diff --git a/src/debug.cpp b/src/debug.cpp new file mode 100644 index 0000000..1b21e20 --- /dev/null +++ b/src/debug.cpp @@ -0,0 +1,176 @@ +#include +#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(token.data)) + value = to_string(get(token.data)); + else if (holds_alternative(token.data)) + value = to_string(get(token.data)); + + return "Litteral(" + value + ")"; + } + case TokenType::Identifier: return "Identifier(" + get(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 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(node)) { + const InnerNode& inner_node = get(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(node); + cout << prefix; + _debug_print_token(token); + cout << endl; + } +} \ No newline at end of file diff --git a/src/execute.cpp b/src/execute.cpp index 301577c..11b307e 100644 --- a/src/execute.cpp +++ b/src/execute.cpp @@ -2,6 +2,7 @@ using namespace std; #include "include/execute.hpp" +#include "include/debug.hpp" EvalResult execute(vector input, Memory& memory, int initial_line, ExecArgs args) { diff --git a/src/include/debug.hpp b/src/include/debug.hpp new file mode 100644 index 0000000..40042a8 --- /dev/null +++ b/src/include/debug.hpp @@ -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 tokens); + +#endif \ No newline at end of file diff --git a/src/include/lexer.hpp b/src/include/lexer.hpp index 4f4b8b7..2ae887c 100644 --- a/src/include/lexer.hpp +++ b/src/include/lexer.hpp @@ -12,19 +12,4 @@ using namespace std; */ vector tokenize(vector 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 tokens); - #endif \ No newline at end of file diff --git a/src/include/parser.hpp b/src/include/parser.hpp index ceb1267..773011c 100644 --- a/src/include/parser.hpp +++ b/src/include/parser.hpp @@ -91,9 +91,5 @@ ParseReturn parse_par_identifier(vector tokens); */ tuple> parse_args(vector tokens); -/** - * Prints a tree for debugging it -*/ -void _debug_print_tree(const Node& node, int depth = 0, const string& prefix = ""); #endif \ No newline at end of file diff --git a/src/lexer.cpp b/src/lexer.cpp index 1469d10..e716c87 100644 --- a/src/lexer.cpp +++ b/src/lexer.cpp @@ -43,158 +43,6 @@ vector> simple_tokens = { { ",", 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(token.data)) { - cout << "Litteral(" << get(token.data) << ")"; - } - else if (holds_alternative(token.data)) { - cout << "Litteral(" << get(token.data) << ")"; - } - break; - case TokenType::Identifier: - cout << "Identifier(" << get(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 tokens) { - for (Token token : tokens) { - _debug_print_token(token); - cout << " "; - } - cout << endl; -} - vector tokenize(vector input, int initial_line) { vector tokens; diff --git a/src/parser.cpp b/src/parser.cpp index d6bf41b..66d0b01 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -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(node)) { - const InnerNode& inner_node = get(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(node); - cout << prefix; - _debug_print_token(token); - cout << endl; - } -} - Node parse(vector tokens) { reverse(tokens.begin(), tokens.end()); diff --git a/test/lexer.cpp b/test/lexer.cpp index 59b5531..cb45f86 100644 --- a/test/lexer.cpp +++ b/test/lexer.cpp @@ -4,6 +4,7 @@ using namespace std; #include "include/test.hpp" +#include "../src/include/debug.hpp" #include "../src/include/lexer.hpp" int main() {