瀏覽代碼

fix error report

blueloveTH 3 年之前
父節點
當前提交
4842a108c3
共有 5 個文件被更改,包括 28 次插入24 次删除
  1. 13 3
      src/compiler.h
  2. 1 16
      src/error.h
  3. 1 1
      src/parser.h
  4. 1 0
      src/pocketpy.h
  5. 12 4
      src/vm.h

+ 13 - 3
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<const _Error*>(&e)){
+            vm->_stderr(e.what());
+        }else{
+            auto ce = CompileError("UnexpectedError", e.what(), compiler.getLineSnapshot());
+            vm->_stderr(ce.what());
+        }
+        vm->_stderr("\n");
         return nullptr;
     }
 }

+ 1 - 16
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<const _Error*>(&e)){          \
-        vm->_stderr(e.what());                                      \
-    }else{                                                          \
-        vm->_stderr(UnexpectedError(e.what()).what());              \
-    }                                                               \
-    vm->_stderr("\n");
-
+};

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

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

+ 12 - 4
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<const _Error*>(&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: