62 lines
1.6 KiB
C++
62 lines
1.6 KiB
C++
#include <cstring>
|
|
#include <iostream>
|
|
using namespace std;
|
|
|
|
#include "include/input.h"
|
|
#include "include/errors.h"
|
|
#include "include/colors.h"
|
|
#include "include/parser.h"
|
|
#include "include/tokenize.h"
|
|
#include "include/interpreter.h"
|
|
#include "include/memory.h"
|
|
|
|
int main(int argc, char* argv[]) {
|
|
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++;
|
|
}
|
|
|
|
vector<string> input;
|
|
vector<Token> tokens;
|
|
Memory<EvalResult> memory;
|
|
|
|
while (true) {
|
|
try {
|
|
int initial_line = input.size();
|
|
input = get_input(input);
|
|
tokens = tokenize(input, initial_line);
|
|
|
|
Node ast = parse(tokens);
|
|
|
|
if (print_ast)
|
|
_debug_print_tree(ast, 0, "");
|
|
|
|
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;
|
|
}
|
|
|
|
} catch (const SyntaxError& e) {
|
|
pretty_print_error(input, e.pos);
|
|
cout << BOLD RED "Syntax Error: " RESET << e.what() << endl;
|
|
|
|
} 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;
|
|
}
|
|
cout << endl;
|
|
}
|
|
} |