diff --git a/include/Evalvisitor.h b/include/Evalvisitor.h index 6a1d095..c92be3f 100644 --- a/include/Evalvisitor.h +++ b/include/Evalvisitor.h @@ -4,6 +4,8 @@ #include "Python3ParserBaseVisitor.h" #include "int2048/int2048.h" +#include +#include class EvalVisitor : public Python3ParserBaseVisitor { @@ -44,5 +46,34 @@ class EvalVisitor : public Python3ParserBaseVisitor std::any visitArglist(Python3Parser::ArglistContext *ctx) override; std::any visitArgument(Python3Parser::ArgumentContext *ctx) override; }; +class InterpretException : public std::exception +{ +public: + InterpretException() {} + InterpretException(const char *message) : m_message(message) {} + const char *what() const noexcept override + { + return m_message; + } + + ~InterpretException() noexcept override {} + +private: + const char *m_message; +}; + +class FatalError : public std::exception +{ +public: + FatalError(const char *message) : m_message(message) {} + + const char *what() const noexcept override + { + return m_message; + } + +private: + const char *m_message; +}; #endif // PYTHON_INTERPRETER_EVALVISITOR_H diff --git a/src/main.cpp b/src/main.cpp index 69b1995..80e8978 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -39,7 +39,26 @@ int main(int argc, char *argv[]) Python3Parser parser(&tokens); tree::ParseTree *tree = parser.file_input(); EvalVisitor visitor; - visitor.visit(tree); + try + { + visitor.visit(tree); + } + catch (const InterpretException &e) + { + std::cerr << "[Interpret Error] " << e.what() << std::endl; + } + catch (const FatalError &e) + { + std::cerr << "\e[7m\e[31m[Fatal Error] " << e.what() << "\e[0m" << std::endl; + } + catch (const std::exception &e) + { + std::cerr << "\e[7m\e[31m[other std::exception] " << e.what() << "\e[0m" << std::endl; + } + catch (...) + { + std::cerr << "\e[7m\e[31m[Unknown Exception]\e[0m" << std::endl; + } delete input_p; return 0; } \ No newline at end of file