From 5fed47367db62a6a421e1f6ca4c9645b29d24c05 Mon Sep 17 00:00:00 2001 From: augustin64 Date: Fri, 15 Dec 2023 14:57:07 +0100 Subject: [PATCH] Move errors: types.h -> errors.h --- src/analysis.cpp | 1 + src/include/errors.h | 89 +++++++++++++++++++++++++++++++++++++++ src/include/interpreter.h | 1 + src/include/parser.h | 1 + src/include/types.h | 83 ------------------------------------ src/tokenize.cpp | 1 + 6 files changed, 93 insertions(+), 83 deletions(-) diff --git a/src/analysis.cpp b/src/analysis.cpp index 6cbb94c..fdb568c 100644 --- a/src/analysis.cpp +++ b/src/analysis.cpp @@ -1,5 +1,6 @@ #include #include "include/utils.h" +#include "include/errors.h" #include "include/analysis.h" bool bool_castable(AnalysisResult type) { diff --git a/src/include/errors.h b/src/include/errors.h index 4374f10..2bc9b38 100644 --- a/src/include/errors.h +++ b/src/include/errors.h @@ -1,3 +1,6 @@ +#ifndef DEF_ERRORS_H +#define DEF_ERRORS_H + #include using namespace std; @@ -7,3 +10,89 @@ using namespace std; * Display the line of code associated with an error, and highlight the associated part */ void print_error_position(vector history, CodePosition pos); + + +/** + * Errors +*/ +enum class ErrorType { + // Generic + NotImplemented, + + // Lexing + UnknownToken, + IntegerTooLarge, + + // Parsing + EmptyInput, + InvalidSyntax, + ExceptedLParen, + ExpectedRParen, + ExpectedRCurlyBracket, + ExpectedSemicolon, + DependentDeclaration, + + // Analysis + UnknownType, + TypeNotCastable, + TypesNotComparable, + ExpectedIntegralType, + ExpectedArithmeticType, + + // Runtime + DivisionByZero, + ModuloByZero, + UnknownIdentifier, + AlreadyDefinedIdentifier, + UninitializedIdentifier, + BreakNotWithinLoop, + ContinueNotWithinLoop, +}; + +using ErrorData = variant; + +class UserError : public exception { +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 CodePosition pos; + const ErrorType type; + const ErrorData data; +}; + +class SyntaxError : public UserError { +public: + explicit SyntaxError(ErrorType type, CodePosition pos, ErrorData data = {}) + : UserError(type, pos, data) {} +}; + +class TypeError : public UserError { +public: + explicit TypeError(ErrorType type, CodePosition pos, ErrorData data = {}) + : UserError(type, pos, data) {} +}; + +class RuntimeError : public UserError { +public: + explicit RuntimeError(ErrorType type, CodePosition pos, ErrorData data = {}) + : UserError(type, pos, data) {} +}; + +class InternalError : public exception { +public: + explicit InternalError(CodePosition pos = {}) + : pos(pos) {} + + const char* what() const noexcept override { + return "Internal error occurred."; + } + + const CodePosition pos; +}; + +#endif \ No newline at end of file diff --git a/src/include/interpreter.h b/src/include/interpreter.h index 975699a..49eff90 100644 --- a/src/include/interpreter.h +++ b/src/include/interpreter.h @@ -5,6 +5,7 @@ #include #include "types.h" #include "memory.h" +#include "errors.h" using namespace std; /* diff --git a/src/include/parser.h b/src/include/parser.h index b3e98c6..fb10586 100644 --- a/src/include/parser.h +++ b/src/include/parser.h @@ -5,6 +5,7 @@ #include #include #include "tokenize.h" +#include "errors.h" using namespace std; /** diff --git a/src/include/types.h b/src/include/types.h index 6a12e8c..adadbb0 100644 --- a/src/include/types.h +++ b/src/include/types.h @@ -188,87 +188,4 @@ struct Scope { ScopeType type; }; -/** - * Errors -*/ -enum class ErrorType { - // Generic - NotImplemented, - - // Lexing - UnknownToken, - IntegerTooLarge, - - // Parsing - EmptyInput, - InvalidSyntax, - ExceptedLParen, - ExpectedRParen, - ExpectedRCurlyBracket, - ExpectedSemicolon, - DependentDeclaration, - - // Analysis - UnknownType, - TypeNotCastable, - TypesNotComparable, - ExpectedIntegralType, - ExpectedArithmeticType, - - // Runtime - DivisionByZero, - ModuloByZero, - UnknownIdentifier, - AlreadyDefinedIdentifier, - UninitializedIdentifier, - BreakNotWithinLoop, - ContinueNotWithinLoop, -}; - -using ErrorData = variant; - -class UserError : public exception { -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 CodePosition pos; - const ErrorType type; - const ErrorData data; -}; - -class SyntaxError : public UserError { -public: - explicit SyntaxError(ErrorType type, CodePosition pos, ErrorData data = {}) - : UserError(type, pos, data) {} -}; - -class TypeError : public UserError { -public: - explicit TypeError(ErrorType type, CodePosition pos, ErrorData data = {}) - : UserError(type, pos, data) {} -}; - -class RuntimeError : public UserError { -public: - explicit RuntimeError(ErrorType type, CodePosition pos, ErrorData data = {}) - : UserError(type, pos, data) {} -}; - -class InternalError : public exception { -public: - explicit InternalError(CodePosition pos = {}) - : pos(pos) {} - - const char* what() const noexcept override { - return "Internal error occurred."; - } - - const CodePosition pos; -}; - #endif \ No newline at end of file diff --git a/src/tokenize.cpp b/src/tokenize.cpp index e3588fa..daac99f 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -2,6 +2,7 @@ #include #include #include +#include "include/errors.h" #include "include/tokenize.h" using namespace std;