diff --git a/src/include/interpreter.h b/src/include/interpreter.h index e4b5a64..02b895d 100644 --- a/src/include/interpreter.h +++ b/src/include/interpreter.h @@ -2,10 +2,18 @@ #define INTERPRETER_H #include +#include +#include using namespace std; using EvalResult = variant; +class RuntimeError : public runtime_error { +public: + explicit RuntimeError(const string& message) + : runtime_error(message) {} +}; + /* Evaluates the AST, returning the latest calulated value */ diff --git a/src/include/tokenize.h b/src/include/tokenize.h index 167b490..6310ab3 100644 --- a/src/include/tokenize.h +++ b/src/include/tokenize.h @@ -17,10 +17,10 @@ struct Token { TokenData data { }; }; -class TokenError : public std::runtime_error { +class TokenError : public runtime_error { public: - explicit TokenError(const std::string& message) - : std::runtime_error(message) {} + explicit TokenError(const string& message) + : runtime_error(message) {} }; /* diff --git a/src/interpreter.cpp b/src/interpreter.cpp index 7b246a5..55ce148 100644 --- a/src/interpreter.cpp +++ b/src/interpreter.cpp @@ -37,13 +37,13 @@ EvalResult eval(Node &ast) { case NodeType::Div: { int e1 = get(eval(node.children[0])); int e2 = get(eval(node.children[1])); - // if (e2 == 0) + if (e2 == 0) throw RuntimeError("Division by 0"); return e1 / e2; } break; case NodeType::Mod: { int e1 = get(eval(node.children[0])); int e2 = get(eval(node.children[1])); - // if (e2 == 0) + if (e2 == 0) throw RuntimeError("Modulo by 0"); return e1 % e2; } break; case NodeType::UnaryPlus: { @@ -106,7 +106,7 @@ EvalResult eval(Node &ast) { } break; case TokenType::Identifier: { string identifier = get(token.data); - // if (!memory.contains(identifier)) return; + if (!memory.contains(identifier)) throw RuntimeError("Unknown identifier"); return memory[identifier]; } break; default: