Refactor error classes
This commit is contained in:
parent
03ac8336c0
commit
b501c5e4a6
0
src/analysis.cpp
Normal file
0
src/analysis.cpp
Normal file
6
src/include/analysis.h
Normal file
6
src/include/analysis.h
Normal file
@ -0,0 +1,6 @@
|
||||
#ifndef ANALYSIS_H
|
||||
#define ANALYSIS_H
|
||||
|
||||
|
||||
|
||||
#endif
|
@ -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
|
||||
*/
|
||||
|
@ -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
|
||||
*/
|
||||
|
@ -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
|
||||
*/
|
||||
|
@ -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
|
@ -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;
|
||||
|
@ -231,7 +231,7 @@ vector<Token> tokenize(vector<string> input, int initial_line) {
|
||||
continue;
|
||||
}
|
||||
|
||||
throw TokenError("Unknown token", pos);
|
||||
throw SyntaxError("Unknown token", pos);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user