blueloveTH 3 лет назад
Родитель
Сommit
2ad01d1b79
4 измененных файлов с 17 добавлено и 5 удалено
  1. 1 1
      src/compiler.h
  2. 2 2
      src/error.h
  3. 6 1
      src/pocketpy.h
  4. 8 1
      src/vm.h

+ 1 - 1
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;
     }

+ 2 - 2
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;

+ 6 - 1
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)                                                                    \

+ 8 - 1
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();
     }