mirror of
https://github.com/pocketpy/pocketpy
synced 2025-12-07 02:30:17 +00:00
fix error report
This commit is contained in:
parent
406465219c
commit
4842a108c3
@ -625,7 +625,7 @@ __LISTCOMP:
|
|||||||
lexToken();
|
lexToken();
|
||||||
_TokenType op = parser->previous.type;
|
_TokenType op = parser->previous.type;
|
||||||
GrammarFn infix = rules[op].infix;
|
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)();
|
(this->*infix)();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -881,14 +881,24 @@ __LITERAL_EXIT:
|
|||||||
void indentationError(_Str msg){
|
void indentationError(_Str msg){
|
||||||
throw CompileError("IndentationError", msg, getLineSnapshot());
|
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) {
|
_Code compile(VM* vm, const char* source, _Str filename, CompileMode mode=EXEC_MODE) {
|
||||||
try{
|
|
||||||
Compiler compiler(vm, source, filename, mode);
|
Compiler compiler(vm, source, filename, mode);
|
||||||
|
try{
|
||||||
return compiler.__fillCode();
|
return compiler.__fillCode();
|
||||||
}catch(std::exception& e){
|
}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;
|
return nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
15
src/error.h
15
src/error.h
@ -87,18 +87,3 @@ public:
|
|||||||
RuntimeError(_Str type, _Str msg, std::stack<_Str> snapshots)
|
RuntimeError(_Str type, _Str msg, std::stack<_Str> snapshots)
|
||||||
: _Error(type, msg, __concat(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");
|
|
||||||
|
|
||||||
|
|||||||
@ -154,7 +154,7 @@ struct Parser {
|
|||||||
|
|
||||||
char eatChar() {
|
char eatChar() {
|
||||||
char c = peekChar();
|
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++;
|
current_char++;
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -167,6 +167,7 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|||||||
|
|
||||||
_vm->bindMethod("float", "__repr__", [](VM* vm, PyVarList args) {
|
_vm->bindMethod("float", "__repr__", [](VM* vm, PyVarList args) {
|
||||||
_Float val = vm->PyFloat_AS_C(args[0]);
|
_Float val = vm->PyFloat_AS_C(args[0]);
|
||||||
|
if(std::isinf(val) || std::isnan(val)) return vm->PyStr(std::to_string(val));
|
||||||
_StrStream ss;
|
_StrStream ss;
|
||||||
ss << std::setprecision(std::numeric_limits<_Float>::max_digits10-1) << val;
|
ss << std::setprecision(std::numeric_limits<_Float>::max_digits10-1) << val;
|
||||||
std::string s = ss.str();
|
std::string s = ss.str();
|
||||||
|
|||||||
16
src/vm.h
16
src/vm.h
@ -414,9 +414,13 @@ public:
|
|||||||
try {
|
try {
|
||||||
return runFrame(frame);
|
return runFrame(frame);
|
||||||
} catch (const std::exception& e) {
|
} catch (const std::exception& e) {
|
||||||
while(!callstack.empty()) callstack.pop();
|
if(const _Error* _ = dynamic_cast<const _Error*>(&e)){
|
||||||
VM* vm = this;
|
_stderr(e.what());
|
||||||
REDIRECT_ERROR()
|
}else{
|
||||||
|
auto re = RuntimeError("UnexpectedError", e.what(), _cleanErrorAndGetSnapshots());
|
||||||
|
_stderr(re.what());
|
||||||
|
}
|
||||||
|
_stderr("\n");
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -636,13 +640,17 @@ public:
|
|||||||
/***** Error Reporter *****/
|
/***** Error Reporter *****/
|
||||||
private:
|
private:
|
||||||
void _error(const _Str& name, const _Str& msg){
|
void _error(const _Str& name, const _Str& msg){
|
||||||
|
throw RuntimeError(name, msg, _cleanErrorAndGetSnapshots());
|
||||||
|
}
|
||||||
|
|
||||||
|
std::stack<_Str> _cleanErrorAndGetSnapshots(){
|
||||||
std::stack<_Str> snapshots;
|
std::stack<_Str> snapshots;
|
||||||
while (!callstack.empty()){
|
while (!callstack.empty()){
|
||||||
auto frame = callstack.top();
|
auto frame = callstack.top();
|
||||||
snapshots.push(frame->errorSnapshot());
|
snapshots.push(frame->errorSnapshot());
|
||||||
callstack.pop();
|
callstack.pop();
|
||||||
}
|
}
|
||||||
throw RuntimeError(name, msg, snapshots);
|
return snapshots;
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user