Move errors: types.h -> errors.h

This commit is contained in:
augustin64 2023-12-15 14:57:07 +01:00
parent ea11ec2944
commit 5fed47367d
6 changed files with 93 additions and 83 deletions

View File

@ -1,5 +1,6 @@
#include <iostream> #include <iostream>
#include "include/utils.h" #include "include/utils.h"
#include "include/errors.h"
#include "include/analysis.h" #include "include/analysis.h"
bool bool_castable(AnalysisResult type) { bool bool_castable(AnalysisResult type) {

View File

@ -1,3 +1,6 @@
#ifndef DEF_ERRORS_H
#define DEF_ERRORS_H
#include <string> #include <string>
using namespace std; using namespace std;
@ -7,3 +10,89 @@ using namespace std;
* Display the line of code associated with an error, and highlight the associated part * Display the line of code associated with an error, and highlight the associated part
*/ */
void print_error_position(vector<string> history, CodePosition pos); void print_error_position(vector<string> 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<monostate, string>;
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

View File

@ -5,6 +5,7 @@
#include <stdexcept> #include <stdexcept>
#include "types.h" #include "types.h"
#include "memory.h" #include "memory.h"
#include "errors.h"
using namespace std; using namespace std;
/* /*

View File

@ -5,6 +5,7 @@
#include <variant> #include <variant>
#include <stdexcept> #include <stdexcept>
#include "tokenize.h" #include "tokenize.h"
#include "errors.h"
using namespace std; using namespace std;
/** /**

View File

@ -188,87 +188,4 @@ struct Scope {
ScopeType type; 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<monostate, string>;
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 #endif

View File

@ -2,6 +2,7 @@
#include <vector> #include <vector>
#include <iostream> #include <iostream>
#include <string> #include <string>
#include "include/errors.h"
#include "include/tokenize.h" #include "include/tokenize.h"
using namespace std; using namespace std;