Re-add error messages

This commit is contained in:
ala89 2023-12-15 15:36:09 +01:00
parent a1873af50d
commit 9589a2b51e
3 changed files with 45 additions and 14 deletions

View File

@ -211,7 +211,7 @@ AnalysisResult analyze(Node &ast, Memory &memory) {
string identifier = get<string>(token.data); string identifier = get<string>(token.data);
if (memory.contains_top(identifier)) 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); Type type = try_string_to_type(type_string, type_token.pos);
memory.declare(identifier, type); memory.declare(identifier, type);
@ -226,7 +226,7 @@ AnalysisResult analyze(Node &ast, Memory &memory) {
string identifier = get<string>(token.data); string identifier = get<string>(token.data);
if (memory.contains_top(identifier)) 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); Type type = try_string_to_type(type_string, type_token.pos);
memory.declare(identifier, type); memory.declare(identifier, type);

View File

@ -3,6 +3,7 @@
#include <iostream> #include <iostream>
using namespace std; using namespace std;
#include "include/errors.h"
#include "include/colors.h" #include "include/colors.h"
#include "include/tokenize.h" #include "include/tokenize.h"
@ -21,4 +22,32 @@ void print_error_position(vector<string> history, CodePosition pos) {
cout << " "; cout << " ";
cout << BOLD RED "^--" RESET << endl; 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<string>(e.data)+"'";
case ErrorType::TypeNotCastable: return "Can't find an explicit cast to "+get<string>(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<string>(e.data)+"'";
case ErrorType::AlreadyDeclaredIdentifier: return "Already declared identifier '"+get<string>(e.data)+"'";
case ErrorType::UninitializedIdentifier: return "Accessing uninitialized identifier '"+get<string>(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";
}
} }

View File

@ -6,12 +6,6 @@ using namespace std;
#include "tokenize.h" #include "tokenize.h"
/**
* Display the line of code associated with an error, and highlight the associated part
*/
void print_error_position(vector<string> history, CodePosition pos);
/** /**
* Errors * Errors
*/ */
@ -40,11 +34,11 @@ enum class ErrorType {
ExpectedArithmeticType, ExpectedArithmeticType,
// Runtime // Runtime
UnknownIdentifier,
AlreadyDeclaredIdentifier,
UninitializedIdentifier,
DivisionByZero, DivisionByZero,
ModuloByZero, ModuloByZero,
UnknownIdentifier,
AlreadyDefinedIdentifier,
UninitializedIdentifier,
BreakNotWithinLoop, BreakNotWithinLoop,
ContinueNotWithinLoop, ContinueNotWithinLoop,
}; };
@ -56,9 +50,7 @@ public:
explicit UserError(ErrorType type, CodePosition pos, ErrorData data = {}) explicit UserError(ErrorType type, CodePosition pos, ErrorData data = {})
: pos(pos), type(type), data(data) {} : pos(pos), type(type), data(data) {}
const char* what() const noexcept override { const char* what() const noexcept override;
return "User error occurred.";
}
const CodePosition pos; const CodePosition pos;
const ErrorType type; const ErrorType type;
@ -95,4 +87,14 @@ public:
const CodePosition pos; const CodePosition pos;
}; };
/**
* Display the line of code associated with an error, and highlight the associated part
*/
void print_error_position(vector<string> history, CodePosition pos);
/**
* Returns the error message associated with a user error
*/
string get_error_message(const UserError& e);
#endif #endif