Create SyntaxError Exception

This commit is contained in:
augustin64 2023-11-11 09:05:49 +01:00
parent f9159fa92e
commit b07c06ec9f
3 changed files with 36 additions and 8 deletions

View File

@ -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

View File

@ -1,23 +1,41 @@
#include <string.h>
#include <iostream>
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<Token> 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<int>(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;
}
}

View File

@ -36,7 +36,7 @@ Node parse(vector<Token> 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<Token> tokens) {
tokens=ret.tokens;
if (tokens.size() < 1 || tokens.back().type != TokenType::RParenthese)
throw ParseException();
throw SyntaxError("Missing ')'");
tokens.pop_back();