diff --git a/src/include/parser.h b/src/include/parser.h index 28049d3..d94c31a 100644 --- a/src/include/parser.h +++ b/src/include/parser.h @@ -89,7 +89,17 @@ struct ParseReturn { /** * Utilisé pour revenir en arrière quand quelque chose n'est pas reconnu */ -class ParseException : public std::exception {}; +class ParseException : public std::exception { + const char* what() const noexcept override { + return "Parse Exception"; + } +}; + +class SyntaxError : public std::runtime_error { +public: + explicit SyntaxError(const std::string& message) + : std::runtime_error(message) {} +}; /** * Parse a list of tokens and return the associated AST diff --git a/src/main.cpp b/src/main.cpp index 1b197b2..b6bd8c2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,23 +1,41 @@ +#include #include using namespace std; #include "include/input.h" -#include "include/tokenize.h" +#include "include/colors.h" #include "include/parser.h" +#include "include/tokenize.h" #include "include/interpreter.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++; + } + while (true) { try { string input = get_input(); vector tokens = tokenize(input); Node ast = parse(tokens); - // _debug_print_tree(ast, 0, ""); + + if (print_ast) + _debug_print_tree(ast, 0, ""); + EvalResult res = eval(ast); cout << get(res) << endl; + } catch (const SyntaxError& e) { + cout << RED "SyntaxError: " RESET << e.what() << endl; + } catch (const std::exception& e) { // temp + cout << RED "err: " RESET << e.what() << endl; } - catch (...) { // temp - cout << "err" << endl; - } + cout << endl; } } \ No newline at end of file diff --git a/src/parser.cpp b/src/parser.cpp index ad35d0a..af03a9b 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -36,7 +36,7 @@ Node parse(vector tokens) { reverse(tokens.begin(), tokens.end()); if (tokens.size() == 0) - throw ParseException(); + throw SyntaxError("Input must not be empty"); // At least 1 instruction @@ -331,7 +331,7 @@ ParseReturn parse_f(vector tokens) { tokens=ret.tokens; if (tokens.size() < 1 || tokens.back().type != TokenType::RParenthese) - throw ParseException(); + throw SyntaxError("Missing ')'"); tokens.pop_back();