diff --git a/src/errors.cpp b/src/errors.cpp index 9db6893..b82688c 100644 --- a/src/errors.cpp +++ b/src/errors.cpp @@ -7,7 +7,7 @@ using namespace std; #include "include/tokenize.h" -void pretty_print_error(vector history, CodePosition pos) { +void print_error_position(vector history, CodePosition pos) { if (pos.column == -1 || pos.line == -1) return; diff --git a/src/execute.cpp b/src/execute.cpp new file mode 100644 index 0000000..174dadf --- /dev/null +++ b/src/execute.cpp @@ -0,0 +1,44 @@ +#include +using namespace std; + +#include "include/execute.h" + + +EvalResult execute(vector input, Memory memory, int initial_line, ExecArgs args) { + try { + vector tokens = tokenize(input, initial_line); + + if (args.print_tokens) { + cout << BOLD YELLOW " === TOKENS ===" RESET << endl; + _debug_print_tokens(tokens); + } + + Node ast = parse(tokens); + + if (args.print_ast) { + cout << BOLD YELLOW " === AST ===" RESET << endl; + _debug_print_tree(ast, 0, ""); + } + + Memory type_mem = memory; + analyze(ast, type_mem); + + if (args.dump_type_mem) { + cout << BOLD YELLOW " === TYPE MEMORY ===" RESET << endl; + type_mem._debug_print(); + } + + EvalResult res = eval(ast, memory); + + if (args.dump_mem) { + cout << BOLD YELLOW " === MEMORY ===" RESET << endl; + memory._debug_print(); + } + + return res; + } catch (const BreakException& except) { + throw RuntimeError(ErrorType::BreakNotWithinLoop, except.pos); + } catch (const ContinueException& except) { + throw RuntimeError(ErrorType::ContinueNotWithinLoop, except.pos); + } +} \ No newline at end of file diff --git a/src/include/errors.h b/src/include/errors.h index 66ed1fd..4374f10 100644 --- a/src/include/errors.h +++ b/src/include/errors.h @@ -3,4 +3,7 @@ using namespace std; #include "tokenize.h" -void pretty_print_error(vector history, CodePosition pos); \ No newline at end of file +/** + * Display the line of code associated with an error, and highlight the associated part +*/ +void print_error_position(vector history, CodePosition pos); diff --git a/src/include/execute.h b/src/include/execute.h new file mode 100644 index 0000000..d785fd7 --- /dev/null +++ b/src/include/execute.h @@ -0,0 +1,21 @@ +#ifndef DEF_EXECUTE_H +#define DEF_EXECUTE_H + +#include "errors.h" +#include "parser.h" +#include "colors.h" +#include "memory.h" +#include "tokenize.h" +#include "analysis.h" +#include "interpreter.h" + +typedef struct ExecArgs { + bool print_tokens=false; + bool print_ast=false; + bool dump_type_mem=false; + bool dump_mem=false; +} ExecArgs; + +EvalResult execute(vector input, Memory memory, int initial_line=0, ExecArgs args={}); + +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index ef16277..923fb76 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,11 +5,8 @@ using namespace std; #include "include/input.h" #include "include/errors.h" #include "include/colors.h" -#include "include/parser.h" +#include "include/execute.h" #include "include/memory.h" -#include "include/tokenize.h" -#include "include/analysis.h" -#include "include/interpreter.h" void show_help(char* prog_name) { printf("Usage: %s [OPTIONS]\n\n", prog_name); @@ -81,41 +78,21 @@ int main(int argc, char* argv[]) { } vector input; - vector tokens; Memory memory; while (true) { try { int initial_line = input.size(); input = get_input(input); - tokens = tokenize(input, initial_line); + + ExecArgs args = { + print_tokens=print_tokens, + print_ast=print_ast, + dump_type_mem=dump_type_mem, + dump_mem=dump_mem, + }; - if (print_tokens) { - cout << BOLD YELLOW " === TOKENS ===" RESET << endl; - _debug_print_tokens(tokens); - } - - Node ast = parse(tokens); - - if (print_ast) { - cout << BOLD YELLOW " === AST ===" RESET << endl; - _debug_print_tree(ast, 0, ""); - } - - Memory type_mem = memory; - analyze(ast, type_mem); - - if (dump_type_mem) { - cout << BOLD YELLOW " === TYPE MEMORY ===" RESET << endl; - type_mem._debug_print(); - } - - EvalResult res = eval(ast, memory); - - if (dump_mem) { - cout << BOLD YELLOW " === MEMORY ===" RESET << endl; - memory._debug_print(); - } + EvalResult res = execute(input, memory, initial_line, args); if ((print_tokens || print_ast || dump_type_mem || dump_mem) && (holds_alternative(res) || holds_alternative(res))) { @@ -130,19 +107,16 @@ int main(int argc, char* argv[]) { } } catch (const SyntaxError& e) { - pretty_print_error(input, e.pos); + print_error_position(input, e.pos); cout << BOLD RED "Syntax Error: " RESET << e.what() << endl; } catch (const TypeError& e) { - pretty_print_error(input, e.pos); + print_error_position(input, e.pos); cout << BOLD RED "Type Error: " RESET << e.what() << endl; } catch (const RuntimeError& e) { - pretty_print_error(input, e.pos); + print_error_position(input, e.pos); cout << BOLD RED "Runtime Error: " RESET << e.what() << endl; - - } catch (const ParseException& e) { - cout << RED "ParsingError" RESET << endl; } cout << endl; }