c-repl/src/main.cpp

56 lines
1.5 KiB
C++
Raw Normal View History

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-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-11-10 17:35:33 +01:00
while (true) {
try {
2023-11-15 15:23:33 +01:00
input = get_input();
tokens = tokenize(input);
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-10 17:35:33 +01:00
EvalResult res = eval(ast);
cout << get<int>(res) << endl;
2023-11-15 15:23:33 +01:00
2023-11-15 15:44:42 +01:00
} catch (const TokenError& e) {
pretty_print_error(input, e.pos);
cout << BOLD RED "Token Error: " RESET << e.what() << endl;
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;
} 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
}