diff --git a/src/compiler.h b/src/compiler.h index 20501b43..37e61559 100644 --- a/src/compiler.h +++ b/src/compiler.h @@ -625,7 +625,7 @@ __LISTCOMP: lexToken(); _TokenType op = parser->previous.type; GrammarFn infix = rules[op].infix; - if(infix == nullptr) throw UnexpectedError("(infix == nullptr) is true"); + if(infix == nullptr) throw std::runtime_error("(infix == nullptr) is true"); (this->*infix)(); } } @@ -881,14 +881,24 @@ __LITERAL_EXIT: void indentationError(_Str msg){ throw CompileError("IndentationError", msg, getLineSnapshot()); } + + void unexpectedError(_Str msg){ + throw CompileError("UnexpectedError", msg, getLineSnapshot()); + } }; _Code compile(VM* vm, const char* source, _Str filename, CompileMode mode=EXEC_MODE) { + Compiler compiler(vm, source, filename, mode); try{ - Compiler compiler(vm, source, filename, mode); return compiler.__fillCode(); }catch(std::exception& e){ - REDIRECT_ERROR() + if(const _Error* _ = dynamic_cast(&e)){ + vm->_stderr(e.what()); + }else{ + auto ce = CompileError("UnexpectedError", e.what(), compiler.getLineSnapshot()); + vm->_stderr(ce.what()); + } + vm->_stderr("\n"); return nullptr; } } \ No newline at end of file diff --git a/src/error.h b/src/error.h index c83b3521..0141b32d 100644 --- a/src/error.h +++ b/src/error.h @@ -86,19 +86,4 @@ private: public: RuntimeError(_Str type, _Str msg, std::stack<_Str> snapshots) : _Error(type, msg, __concat(snapshots)) {} -}; - -class UnexpectedError : public _Error { -public: - UnexpectedError(_Str msg) - : _Error("UnexpectedError", msg, "") {} -}; - -#define REDIRECT_ERROR() \ - if(const _Error* _ = dynamic_cast(&e)){ \ - vm->_stderr(e.what()); \ - }else{ \ - vm->_stderr(UnexpectedError(e.what()).what()); \ - } \ - vm->_stderr("\n"); - +}; \ No newline at end of file diff --git a/src/parser.h b/src/parser.h index 5ddd4ece..c9790241 100644 --- a/src/parser.h +++ b/src/parser.h @@ -154,7 +154,7 @@ struct Parser { char eatChar() { char c = peekChar(); - if(c == '\n') throw UnexpectedError("eatChar() cannot consume a newline"); + if(c == '\n') throw std::runtime_error("eatChar() cannot consume a newline"); current_char++; return c; } diff --git a/src/pocketpy.h b/src/pocketpy.h index 9b3ee900..054cca33 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -167,6 +167,7 @@ void __initializeBuiltinFunctions(VM* _vm) { _vm->bindMethod("float", "__repr__", [](VM* vm, PyVarList args) { _Float val = vm->PyFloat_AS_C(args[0]); + if(std::isinf(val) || std::isnan(val)) return vm->PyStr(std::to_string(val)); _StrStream ss; ss << std::setprecision(std::numeric_limits<_Float>::max_digits10-1) << val; std::string s = ss.str(); diff --git a/src/vm.h b/src/vm.h index f371959b..137e6f77 100644 --- a/src/vm.h +++ b/src/vm.h @@ -414,9 +414,13 @@ public: try { return runFrame(frame); } catch (const std::exception& e) { - while(!callstack.empty()) callstack.pop(); - VM* vm = this; - REDIRECT_ERROR() + if(const _Error* _ = dynamic_cast(&e)){ + _stderr(e.what()); + }else{ + auto re = RuntimeError("UnexpectedError", e.what(), _cleanErrorAndGetSnapshots()); + _stderr(re.what()); + } + _stderr("\n"); return None; } } @@ -636,13 +640,17 @@ public: /***** Error Reporter *****/ private: void _error(const _Str& name, const _Str& msg){ + throw RuntimeError(name, msg, _cleanErrorAndGetSnapshots()); + } + + std::stack<_Str> _cleanErrorAndGetSnapshots(){ std::stack<_Str> snapshots; while (!callstack.empty()){ auto frame = callstack.top(); snapshots.push(frame->errorSnapshot()); callstack.pop(); } - throw RuntimeError(name, msg, snapshots); + return snapshots; } public: