fix error report

This commit is contained in:
blueloveTH 2022-11-10 13:46:05 +08:00
parent 406465219c
commit 4842a108c3
5 changed files with 28 additions and 24 deletions

View File

@ -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;
}
}

View File

@ -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");
};

View File

@ -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;
}

View File

@ -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();

View File

@ -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: