diff --git a/src/compiler.h b/src/compiler.h index 2a35ce23..cd7c4d16 100644 --- a/src/compiler.h +++ b/src/compiler.h @@ -1083,7 +1083,7 @@ __LISTCOMP: } void __throw_e(_Str type, _Str msg){ - auto e = _Exception("SyntaxError", msg, false); + auto e = _Exception("SyntaxError", msg); e.st_push(getLineSnapshot()); throw e; } diff --git a/src/error.h b/src/error.h index 16fe7bbb..1094a542 100644 --- a/src/error.h +++ b/src/error.h @@ -71,11 +71,11 @@ struct SourceData { class _Exception { _Str type; _Str msg; - bool is_re; std::stack<_Str> stacktrace; public: - _Exception(_Str type, _Str msg, bool is_re): type(type), msg(msg), is_re(is_re) {} + _Exception(_Str type, _Str msg): type(type), msg(msg) {} bool match_type(const _Str& type) const { return this->type == type;} + bool is_re = true; void st_push(_Str snapshot){ if(stacktrace.size() >= 8) return; diff --git a/src/pocketpy.h b/src/pocketpy.h index a52cafbf..19f1e4be 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -6,7 +6,12 @@ _Code VM::compile(_Str source, _Str filename, CompileMode mode) { Compiler compiler(this, source.c_str(), filename, mode); - return compiler.__fillCode(); + try{ + return compiler.__fillCode(); + }catch(_Exception& e){ + _error(e); + return nullptr; + } } #define BIND_NUM_ARITH_OPT(name, op) \ diff --git a/src/vm.h b/src/vm.h index e43c0ea8..2049230c 100644 --- a/src/vm.h +++ b/src/vm.h @@ -908,7 +908,14 @@ public: /***** Error Reporter *****/ private: void _error(const _Str& name, const _Str& msg){ - auto e = _Exception(name, msg, true); + _error(_Exception(name, msg)); + } + + void _error(_Exception e){ + if(callstack.empty()){ + e.is_re = false; + throw e; + } top_frame()->push(PyException(e)); _raise(); }