c-repl/src/main.cpp

42 lines
1.1 KiB
C++
Raw Normal View History

2023-11-11 09:05:49 +01:00
#include <string.h>
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-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-10 17:35:33 +01:00
while (true) {
try {
vector<string> input = get_input();
2023-11-10 17:35:33 +01:00
vector<Token> 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-11 09:05:49 +01:00
} catch (const SyntaxError& e) {
cout << RED "SyntaxError: " 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
}