diff --git a/src/analysis.cpp b/src/analysis.cpp index fdb568c..3231c8e 100644 --- a/src/analysis.cpp +++ b/src/analysis.cpp @@ -211,7 +211,7 @@ AnalysisResult analyze(Node &ast, Memory &memory) { string identifier = get(token.data); if (memory.contains_top(identifier)) - throw RuntimeError(ErrorType::AlreadyDefinedIdentifier, token.pos, identifier); + throw RuntimeError(ErrorType::AlreadyDeclaredIdentifier, token.pos, identifier); Type type = try_string_to_type(type_string, type_token.pos); memory.declare(identifier, type); @@ -226,7 +226,7 @@ AnalysisResult analyze(Node &ast, Memory &memory) { string identifier = get(token.data); if (memory.contains_top(identifier)) - throw RuntimeError(ErrorType::AlreadyDefinedIdentifier, token.pos, identifier); + throw RuntimeError(ErrorType::AlreadyDeclaredIdentifier, token.pos, identifier); Type type = try_string_to_type(type_string, type_token.pos); memory.declare(identifier, type); diff --git a/src/errors.cpp b/src/errors.cpp index b82688c..5586fd2 100644 --- a/src/errors.cpp +++ b/src/errors.cpp @@ -3,6 +3,7 @@ #include using namespace std; +#include "include/errors.h" #include "include/colors.h" #include "include/tokenize.h" @@ -21,4 +22,32 @@ 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 2bc9b38..dd4b067 100644 --- a/src/include/errors.h +++ b/src/include/errors.h @@ -6,12 +6,6 @@ using namespace std; #include "tokenize.h" -/** - * Display the line of code associated with an error, and highlight the associated part -*/ -void print_error_position(vector history, CodePosition pos); - - /** * Errors */ @@ -40,11 +34,11 @@ enum class ErrorType { ExpectedArithmeticType, // Runtime + UnknownIdentifier, + AlreadyDeclaredIdentifier, + UninitializedIdentifier, DivisionByZero, ModuloByZero, - UnknownIdentifier, - AlreadyDefinedIdentifier, - UninitializedIdentifier, BreakNotWithinLoop, ContinueNotWithinLoop, }; @@ -56,9 +50,7 @@ public: explicit UserError(ErrorType type, CodePosition pos, ErrorData data = {}) : pos(pos), type(type), data(data) {} - const char* what() const noexcept override { - return "User error occurred."; - } + const char* what() const noexcept override; const CodePosition pos; const ErrorType type; @@ -95,4 +87,14 @@ public: const CodePosition pos; }; +/** + * Display the line of code associated with an error, and highlight the associated part +*/ +void print_error_position(vector history, CodePosition pos); + +/** + * Returns the error message associated with a user error +*/ +string get_error_message(const UserError& e); + #endif \ No newline at end of file