c-repl/src/main.cpp

144 lines
4.4 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-12-08 15:59:45 +01:00
#include "include/memory.h"
2023-11-11 09:05:49 +01:00
#include "include/tokenize.h"
2023-12-08 15:59:45 +01:00
#include "include/analysis.h"
2023-11-10 17:35:33 +01:00
#include "include/interpreter.h"
2023-10-20 17:06:27 +02:00
2023-12-09 12:05:36 +01:00
void show_help(char* prog_name) {
printf("Usage: %s [OPTIONS]\n\n", prog_name);
printf("\t--help | -h\tAfficher cette aide\n");
printf("\t--tokens | -t\tAfficher les tokens lus\n");
printf("\t--ast | -a\tAfficher l'AST créé\n");
printf("\t--type-memory | -T\tAfficher le contenu de la mémoire de types avant exécution\n");
printf("\t--memory | -m\tAfficher le contenu de la mémoire après exécution\n");
}
void parse_args(int argc, char* argv[], bool &print_tokens, bool &print_ast, bool &dump_type_mem, bool &dump_mem, bool &help) {
2023-11-11 09:05:49 +01:00
int i=1;
while (i < argc) {
2023-12-09 12:05:36 +01:00
char* cur = argv[i];
int n = strlen(cur);
if (n >= 2 && cur[0] == '-' && cur[1] != '-') {
for (int j=1; j < n; j++) {
switch (cur[j]) {
case 'h':
help = true;
break;
case 't':
print_tokens = true;
break;
case 'a':
print_ast = true;
break;
case 'T':
dump_type_mem = true;
break;
case 'm':
dump_mem = true;
break;
default:
cerr << "Unknow argument: -" << cur[j] << endl;
}
}
} else if (!strcmp(cur, "--help")) {
help = true;
} else if (!strcmp(cur, "--tokens")) {
print_tokens = true;
} else if (!strcmp(cur, "--ast")) {
2023-11-11 09:05:49 +01:00
print_ast = true;
2023-12-09 12:05:36 +01:00
} else if (!strcmp(cur, "--type-memory")) {
dump_type_mem = true;
} else if (!strcmp(cur, "--memory")) {
dump_mem = true;
2023-11-11 09:05:49 +01:00
} else {
2023-12-09 12:05:36 +01:00
cerr << "Unknow argument: " << cur << endl;
2023-11-11 09:05:49 +01:00
}
i++;
}
2023-12-09 12:05:36 +01:00
}
int main(int argc, char* argv[]) {
bool print_tokens = false;
bool print_ast = false;
bool dump_type_mem = false;
bool dump_mem = false;
bool help = false;
parse_args(argc, argv, print_tokens, print_ast, dump_type_mem, dump_mem, help);
if (help) {
show_help(argv[0]);
return 0;
}
2023-11-11 09:05:49 +01:00
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-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-12-09 12:05:36 +01:00
if (print_tokens) {
cout << BOLD YELLOW "=== TOKENS ===" RESET << endl;
_debug_print_tokens(tokens);
}
2023-11-10 17:35:33 +01:00
Node ast = parse(tokens);
2023-12-09 12:05:36 +01:00
if (print_ast) {
cout << BOLD YELLOW "=== AST ===" RESET << endl;
_debug_print_tree(ast, 0, "");
}
2023-12-08 15:59:45 +01:00
Memory type_mem = memory;
analyze(ast, type_mem);
2023-11-11 09:05:49 +01:00
2023-12-09 12:05:36 +01:00
//if (dump_type_mem) {
// cout << BOLD YELLOW "=== TYPE MEMORY ===" RESET << endl;
// type_mem._debug_print();
//}
2023-11-11 09:05:49 +01:00
EvalResult res = eval(ast, memory);
2023-12-09 12:05:36 +01:00
//if (dump_mem) {
// cout << BOLD YELLOW "=== MEMORY ===" RESET << endl;
// memory._debug_print();
//}
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-12-09 12:05:36 +01:00
} catch (const TypeError& e) {
pretty_print_error(input, e.pos);
cout << BOLD RED "Type 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
}