2023-11-15 15:23:33 +01:00
|
|
|
#include <cstring>
|
2023-10-20 17:06:27 +02:00
|
|
|
#include <iostream>
|
|
|
|
using namespace std;
|
|
|
|
|
2023-10-27 14:09:25 +02:00
|
|
|
#include "include/input.h"
|
2023-11-15 15:23:33 +01:00
|
|
|
#include "include/errors.h"
|
2023-11-11 09:05:49 +01:00
|
|
|
#include "include/colors.h"
|
2023-10-27 17:53:58 +02:00
|
|
|
#include "include/parser.h"
|
2023-11-11 09:05:49 +01:00
|
|
|
#include "include/tokenize.h"
|
2023-11-10 17:35:33 +01:00
|
|
|
#include "include/interpreter.h"
|
2023-11-22 13:52:16 +01:00
|
|
|
#include "include/memory.h"
|
2023-10-20 17:06:27 +02:00
|
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
2023-11-11 09:05:49 +01:00
|
|
|
bool print_ast = false;
|
|
|
|
int i=1;
|
|
|
|
while (i < argc) {
|
|
|
|
if (!strcmp(argv[i], "--show-ast") || !strcmp(argv[i], "-s")) {
|
|
|
|
print_ast = true;
|
|
|
|
} else {
|
|
|
|
cerr << "Unknow argument: " << argv[i] << endl;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2023-11-15 15:23:33 +01:00
|
|
|
vector<string> input;
|
|
|
|
vector<Token> tokens;
|
2023-12-08 15:29:30 +01:00
|
|
|
Memory memory;
|
2023-11-22 13:52:16 +01:00
|
|
|
|
2023-11-10 17:35:33 +01:00
|
|
|
while (true) {
|
|
|
|
try {
|
2023-11-15 16:07:50 +01:00
|
|
|
int initial_line = input.size();
|
|
|
|
input = get_input(input);
|
|
|
|
tokens = tokenize(input, initial_line);
|
2023-11-15 13:48:40 +01:00
|
|
|
|
2023-11-10 17:35:33 +01:00
|
|
|
Node ast = parse(tokens);
|
2023-11-11 09:05:49 +01:00
|
|
|
|
|
|
|
if (print_ast)
|
|
|
|
_debug_print_tree(ast, 0, "");
|
|
|
|
|
2023-11-22 13:52:16 +01:00
|
|
|
EvalResult res = eval(ast, memory);
|
|
|
|
|
|
|
|
if (holds_alternative<int>(res)) {
|
|
|
|
cout << get<int>(res) << endl;
|
|
|
|
}
|
|
|
|
else if (holds_alternative<double>(res)) {
|
|
|
|
cout << get<double>(res) << endl;
|
|
|
|
}
|
2023-11-15 15:23:33 +01:00
|
|
|
|
2023-11-11 09:05:49 +01:00
|
|
|
} catch (const SyntaxError& e) {
|
2023-11-15 15:23:33 +01:00
|
|
|
pretty_print_error(input, e.pos);
|
|
|
|
cout << BOLD RED "Syntax Error: " RESET << e.what() << endl;
|
|
|
|
|
2023-11-15 15:44:42 +01:00
|
|
|
} catch (const RuntimeError& e) {
|
|
|
|
pretty_print_error(input, e.pos);
|
|
|
|
cout << BOLD RED "Runtime Error: " RESET << e.what() << endl;
|
|
|
|
|
2023-11-15 13:48:40 +01:00
|
|
|
} catch (const ParseException& e) {
|
|
|
|
cout << RED "ParsingError" RESET << endl;
|
2023-11-10 17:35:33 +01:00
|
|
|
}
|
2023-11-11 09:05:49 +01:00
|
|
|
cout << endl;
|
2023-11-10 17:35:33 +01:00
|
|
|
}
|
2023-10-20 17:06:27 +02:00
|
|
|
}
|