From 69276515b1af5f2f06d2e28611c68cf0c41d5d88 Mon Sep 17 00:00:00 2001 From: ala89 Date: Tue, 26 Dec 2023 12:34:05 +0100 Subject: [PATCH] Fix errors --- src/errors.cpp | 55 ++++++++++++++++++++++---------------------- src/include/errors.h | 6 ++++- src/main.cpp | 6 ++--- test/variables.cpp | 2 +- 4 files changed, 36 insertions(+), 33 deletions(-) diff --git a/src/errors.cpp b/src/errors.cpp index 5586fd2..160dd7c 100644 --- a/src/errors.cpp +++ b/src/errors.cpp @@ -7,6 +7,33 @@ using namespace std; #include "include/colors.h" #include "include/tokenize.h" +string UserError::get_message(void) const { + switch (this->type) { + case ErrorType::NotImplemented: return "Not implemented"; + case ErrorType::UnknownToken: return "Unknown token"; + case ErrorType::IntegerTooLarge: return "Integer constant is too large"; + case ErrorType::EmptyInput: return "Input must not be empty"; + case ErrorType::InvalidSyntax: return "Invalid syntax"; + case ErrorType::ExceptedLParen: return "Expected '("; + case ErrorType::ExpectedRParen: return "Expected ')'"; + case ErrorType::ExpectedRCurlyBracket: return "Expected '}'"; + case ErrorType::ExpectedSemicolon: return "Expected ';'"; + case ErrorType::DependentDeclaration: return "A dependant statement may not be a declaration"; + case ErrorType::UnknownType: return "Unknown type '"+get(this->data)+"'"; + case ErrorType::TypeNotCastable: return "Can't find an explicit cast to "+get(this->data)+"'"; + case ErrorType::TypesNotComparable: return "Types not comparable"; + case ErrorType::ExpectedIntegralType: return "Expression must have integral type"; + case ErrorType::ExpectedArithmeticType: return "Expression must have arithmetic type"; + case ErrorType::UnknownIdentifier: return "Unknown identifier '"+get(this->data)+"'"; + case ErrorType::AlreadyDeclaredIdentifier: return "Already declared identifier '"+get(this->data)+"'"; + case ErrorType::UninitializedIdentifier: return "Accessing uninitialized identifier '"+get(this->data)+"'"; + case ErrorType::DivisionByZero: return "Division by 0"; + case ErrorType::ModuloByZero: return "Modulo by 0"; + case ErrorType::BreakNotWithinLoop: return "Break statement not within loop"; + case ErrorType::ContinueNotWithinLoop: return "Continue statement not within loop"; + default: return "Unknown error type"; + } +} void print_error_position(vector history, CodePosition pos) { if (pos.column == -1 || pos.line == -1) @@ -22,32 +49,4 @@ void print_error_position(vector history, CodePosition pos) { cout << " "; cout << BOLD RED "^--" RESET << endl; -} - -string get_error_message(const UserError& e) { - switch (e.type) { - case ErrorType::NotImplemented: return "Not implemented"; - case ErrorType::UnknownToken: return "Unknown token"; - case ErrorType::IntegerTooLarge: return "Integer constant is too large"; - case ErrorType::EmptyInput: return "Input must not be empty"; - case ErrorType::InvalidSyntax: return "Invalid syntax"; - case ErrorType::ExceptedLParen: return "Expected '("; - case ErrorType::ExpectedRParen: return "Expected ')'"; - case ErrorType::ExpectedRCurlyBracket: return "Expected '}'"; - case ErrorType::ExpectedSemicolon: return "Expected ';'"; - case ErrorType::DependentDeclaration: return "A dependant statement may not be a declaration"; - case ErrorType::UnknownType: return "Unknown type '"+get(e.data)+"'"; - case ErrorType::TypeNotCastable: return "Can't find an explicit cast to "+get(e.data)+"'"; - case ErrorType::TypesNotComparable: return "Types not comparable"; - case ErrorType::ExpectedIntegralType: return "Expression must have integral type"; - case ErrorType::ExpectedArithmeticType: return "Expression must have arithmetic type"; - case ErrorType::UnknownIdentifier: return "Unknown identifier '"+get(e.data)+"'"; - case ErrorType::AlreadyDeclaredIdentifier: return "Already declared identifier '"+get(e.data)+"'"; - case ErrorType::UninitializedIdentifier: return "Accessing uninitialized identifier '"+get(e.data)+"'"; - case ErrorType::DivisionByZero: return "Division by 0"; - case ErrorType::ModuloByZero: return "Modulo by 0"; - case ErrorType::BreakNotWithinLoop: return "Break statement not within loop"; - case ErrorType::ContinueNotWithinLoop: return "Continue statement not within loop"; - default: return "Unknown error type"; - } } \ No newline at end of file diff --git a/src/include/errors.h b/src/include/errors.h index dd4b067..7d05ffc 100644 --- a/src/include/errors.h +++ b/src/include/errors.h @@ -50,7 +50,11 @@ public: explicit UserError(ErrorType type, CodePosition pos, ErrorData data = {}) : pos(pos), type(type), data(data) {} - const char* what() const noexcept override; + const char* what() const noexcept override { + return "User error occured."; + } + + string get_message(void) const; const CodePosition pos; const ErrorType type; diff --git a/src/main.cpp b/src/main.cpp index 923fb76..2a165c3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -108,15 +108,15 @@ int main(int argc, char* argv[]) { } catch (const SyntaxError& e) { print_error_position(input, e.pos); - cout << BOLD RED "Syntax Error: " RESET << e.what() << endl; + cout << BOLD RED "Syntax Error: " RESET << e.get_message() << endl; } catch (const TypeError& e) { print_error_position(input, e.pos); - cout << BOLD RED "Type Error: " RESET << e.what() << endl; + cout << BOLD RED "Type Error: " RESET << e.get_message() << endl; } catch (const RuntimeError& e) { print_error_position(input, e.pos); - cout << BOLD RED "Runtime Error: " RESET << e.what() << endl; + cout << BOLD RED "Runtime Error: " RESET << e.get_message() << endl; } cout << endl; } diff --git a/test/variables.cpp b/test/variables.cpp index 07d3c6b..05aabc0 100644 --- a/test/variables.cpp +++ b/test/variables.cpp @@ -38,7 +38,7 @@ int main() { ); _TEST_ASSERT( - _TEST_IS_EXCEPTION(execute("int x; int x;"), ErrorType::AlreadyDefinedIdentifier), + _TEST_IS_EXCEPTION(execute("int x; int x;"), ErrorType::AlreadyDeclaredIdentifier), "Identifieur déjà défini", true );