Refactor error classes

This commit is contained in:
ala89 2023-12-08 09:40:14 +01:00
parent 03ac8336c0
commit b501c5e4a6
8 changed files with 34 additions and 29 deletions

0
src/analysis.cpp Normal file
View File

6
src/include/analysis.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef ANALYSIS_H
#define ANALYSIS_H
#endif

View File

@ -7,14 +7,6 @@
#include "memory.h"
using namespace std;
class RuntimeError : public runtime_error {
public:
explicit RuntimeError(const string& message, CodePosition pos)
: runtime_error(message), pos(pos) {}
const CodePosition pos;
};
/*
Evaluates the AST, returning the latest calulated value
*/

View File

@ -16,14 +16,6 @@ class ParseException : public std::exception {
}
};
class SyntaxError : public runtime_error {
public:
explicit SyntaxError(const string& message, CodePosition pos)
: runtime_error(message), pos(pos) {}
const CodePosition pos;
};
/**
* Parse a list of tokens and return the associated AST
*/

View File

@ -7,14 +7,6 @@
#include "types.h"
using namespace std;
class TokenError : public runtime_error {
public:
explicit TokenError(const string& message, CodePosition pos)
: runtime_error(message), pos(pos) {}
const CodePosition pos;
};
/*
Parses a string into a vector of tokens
*/

View File

@ -170,4 +170,31 @@ struct Scope {
ScopeType type;
};
/**
* Errors
*/
class SyntaxError : public runtime_error {
public:
explicit SyntaxError(const string& message, CodePosition pos)
: runtime_error(message), pos(pos) {}
const CodePosition pos;
};
class TypeError : public runtime_error {
public:
explicit TypeError(const string& message, CodePosition pos)
: runtime_error(message), pos(pos) {}
const CodePosition pos;
};
class RuntimeError : public runtime_error {
public:
explicit RuntimeError(const string& message, CodePosition pos)
: runtime_error(message), pos(pos) {}
const CodePosition pos;
};
#endif

View File

@ -46,10 +46,6 @@ int main(int argc, char* argv[]) {
cout << get<double>(res) << endl;
}
} catch (const TokenError& e) {
pretty_print_error(input, e.pos);
cout << BOLD RED "Token Error: " RESET << e.what() << endl;
} catch (const SyntaxError& e) {
pretty_print_error(input, e.pos);
cout << BOLD RED "Syntax Error: " RESET << e.what() << endl;

View File

@ -231,7 +231,7 @@ vector<Token> tokenize(vector<string> input, int initial_line) {
continue;
}
throw TokenError("Unknown token", pos);
throw SyntaxError("Unknown token", pos);
}
}