123 lines
3.8 KiB
C++
123 lines
3.8 KiB
C++
#include <cstring>
|
|
#include <iostream>
|
|
using namespace std;
|
|
|
|
#include "include/input.h"
|
|
#include "include/errors.h"
|
|
#include "include/colors.h"
|
|
#include "include/execute.h"
|
|
#include "include/memory.h"
|
|
|
|
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) {
|
|
int i=1;
|
|
while (i < argc) {
|
|
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")) {
|
|
print_ast = true;
|
|
} else if (!strcmp(cur, "--type-memory")) {
|
|
dump_type_mem = true;
|
|
} else if (!strcmp(cur, "--memory")) {
|
|
dump_mem = true;
|
|
} else {
|
|
cerr << "Unknow argument: " << cur << endl;
|
|
}
|
|
i++;
|
|
}
|
|
}
|
|
|
|
|
|
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;
|
|
}
|
|
|
|
vector<string> input;
|
|
Memory memory;
|
|
|
|
while (true) {
|
|
try {
|
|
int initial_line = input.size();
|
|
input = get_input(input);
|
|
|
|
ExecArgs args = {
|
|
print_tokens=print_tokens,
|
|
print_ast=print_ast,
|
|
dump_type_mem=dump_type_mem,
|
|
dump_mem=dump_mem,
|
|
};
|
|
|
|
EvalResult res = execute(input, memory, initial_line, args);
|
|
|
|
if ((print_tokens || print_ast || dump_type_mem || dump_mem) &&
|
|
(holds_alternative<int>(res) || holds_alternative<double>(res))) {
|
|
cout << BOLD BLUE " === RESULT ===" RESET << endl;
|
|
}
|
|
|
|
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) {
|
|
print_error_position(input, e.pos);
|
|
cout << BOLD RED "Syntax Error: " RESET << e.what() << endl;
|
|
|
|
} catch (const TypeError& e) {
|
|
print_error_position(input, e.pos);
|
|
cout << BOLD RED "Type Error: " RESET << e.what() << endl;
|
|
|
|
} catch (const RuntimeError& e) {
|
|
print_error_position(input, e.pos);
|
|
cout << BOLD RED "Runtime Error: " RESET << e.what() << endl;
|
|
}
|
|
cout << endl;
|
|
}
|
|
} |