Add execute.cpp

This commit is contained in:
augustin64 2023-12-15 14:52:46 +01:00
parent 965d834d22
commit ea11ec2944
5 changed files with 82 additions and 40 deletions

View File

@ -7,7 +7,7 @@ using namespace std;
#include "include/tokenize.h"
void pretty_print_error(vector<string> history, CodePosition pos) {
void print_error_position(vector<string> history, CodePosition pos) {
if (pos.column == -1 || pos.line == -1)
return;

44
src/execute.cpp Normal file
View File

@ -0,0 +1,44 @@
#include <vector>
using namespace std;
#include "include/execute.h"
EvalResult execute(vector<string> input, Memory memory, int initial_line, ExecArgs args) {
try {
vector<Token> 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);
}
}

View File

@ -3,4 +3,7 @@ using namespace std;
#include "tokenize.h"
void pretty_print_error(vector<string> history, CodePosition pos);
/**
* Display the line of code associated with an error, and highlight the associated part
*/
void print_error_position(vector<string> history, CodePosition pos);

21
src/include/execute.h Normal file
View File

@ -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<string> input, Memory memory, int initial_line=0, ExecArgs args={});
#endif

View File

@ -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<string> input;
vector<Token> 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<int>(res) || holds_alternative<double>(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;
}