This commit is contained in:
blueloveTH 2023-02-03 18:34:59 +08:00
parent 9d341d2ffa
commit 8468747ae2
5 changed files with 49 additions and 46 deletions

View File

@ -8,12 +8,16 @@ def get_loc(path):
def get_loc_for_dir(path): def get_loc_for_dir(path):
loc = 0 loc = 0
loc_ex = 0
for root, dirs, files in os.walk(path): for root, dirs, files in os.walk(path):
for file in files: for file in files:
if file.endswith('.h'): if file.endswith('.h'):
_i = get_loc(os.path.join(root, file)) _i = get_loc(os.path.join(root, file))
print(f"{file}: {_i}") print(f"{file}: {_i}")
loc += _i if file.startswith('_'):
return loc loc_ex += _i
else:
loc += _i
return f'{loc} (+{loc_ex})'
print(get_loc_for_dir('src')) print(get_loc_for_dir('src'))

View File

@ -884,8 +884,7 @@ __LISTCOMP:
consume(TK("@id")); consume(TK("@id"));
int dummy_t = co()->add_name(parser->prev.str(), NAME_SPECIAL); int dummy_t = co()->add_name(parser->prev.str(), NAME_SPECIAL);
if(match(TK("("))){ if(match(TK("("))){
EXPR(); EXPR(); consume(TK(")"));
consume(TK(")"));
}else{ }else{
emit(OP_LOAD_NONE); emit(OP_LOAD_NONE);
} }
@ -1077,7 +1076,11 @@ __LISTCOMP:
return parser->src->snapshot(lineno, cursor); return parser->src->snapshot(lineno, cursor);
} }
void syntaxError(_Str msg){ throw CompileError("SyntaxError", msg, getLineSnapshot()); } void __throw_e(_Str type, _Str msg){
void indentationError(_Str msg){ throw CompileError("IndentationError", msg, getLineSnapshot()); } auto e = _Error0("SyntaxError", msg, false);
void unexpectedError(_Str msg){ throw CompileError("UnexpectedError", msg, getLineSnapshot()); } e.st_push(getLineSnapshot());
throw e;
}
void syntaxError(_Str msg){ __throw_e("SyntaxError", msg); }
void indentationError(_Str msg){ __throw_e("IndentationError", msg); }
}; };

View File

@ -52,12 +52,10 @@ struct SourceMetadata {
removedSpaces = pair.second - pair.first - line.size(); removedSpaces = pair.second - pair.first - line.size();
if(line.empty()) line = "<?>"; if(line.empty()) line = "<?>";
} }
ss << " " << line << '\n'; ss << " " << line;
if(cursor && line != "<?>" && cursor >= pair.first && cursor <= pair.second){ if(cursor && line != "<?>" && cursor >= pair.first && cursor <= pair.second){
auto column = cursor - pair.first - removedSpaces; auto column = cursor - pair.first - removedSpaces;
if(column >= 0){ if(column >= 0) ss << "\n " << std::string(column, ' ') << "^";
ss << " " << std::string(column, ' ') << "^\n";
}
} }
return ss.str(); return ss.str();
} }
@ -69,37 +67,24 @@ struct SourceMetadata {
typedef pkpy::shared_ptr<SourceMetadata> _Source; typedef pkpy::shared_ptr<SourceMetadata> _Source;
class _Error : public std::exception { class _Error0 : public std::exception {
private: _Str type;
_Str _what; _Str msg;
bool is_runtime_error;
std::stack<_Str> stacktrace;
mutable _Str _what_cached;
public: public:
_Error(_Str type, _Str msg, _Str desc){ _Error0(_Str type, _Str msg, bool is_runtime_error): type(type), msg(msg), is_runtime_error(is_runtime_error) {}
_what = desc + type + ": " + msg; void st_push(_Str snapshot){ stacktrace.push(snapshot); }
}
const char* what() const noexcept override { const char* what() const noexcept override {
return _what.c_str(); std::stack<_Str> st(stacktrace);
}
};
class CompileError : public _Error {
public:
CompileError(_Str type, _Str msg, _Str snapshot)
: _Error(type, msg, snapshot) {}
};
class RuntimeError : public _Error {
private:
static _Str __concat(std::stack<_Str> snapshots){
_StrStream ss; _StrStream ss;
ss << "Traceback (most recent call last):" << '\n'; if(is_runtime_error) ss << "Traceback (most recent call last):\n";
while(!snapshots.empty()){ while(!st.empty()) { ss << st.top() << '\n'; st.pop(); }
ss << snapshots.top(); ss << type << ": " << msg;
snapshots.pop(); _what_cached = ss.str();
} return _what_cached.c_str();
return ss.str();
} }
public:
RuntimeError(_Str type, _Str msg, const std::stack<_Str>& snapshots)
: _Error(type, msg, __concat(snapshots)) {}
}; };

View File

@ -8,10 +8,11 @@ _Code VM::compile(_Str source, _Str filename, CompileMode mode) {
Compiler compiler(this, source.c_str(), filename, mode); Compiler compiler(this, source.c_str(), filename, mode);
try{ try{
return compiler.__fillCode(); return compiler.__fillCode();
}catch(_Error& e){ }catch(_Error0& e){
throw e; throw e;
}catch(std::exception& e){ }catch(std::exception& e){
throw CompileError("UnexpectedError", e.what(), compiler.getLineSnapshot()); compiler.__throw_e("UnexpectedError", e.what());
return nullptr;
} }
} }

View File

@ -520,13 +520,17 @@ public:
if(_module == nullptr) _module = _main; if(_module == nullptr) _module = _main;
try { try {
_Code code = compile(source, filename, mode); _Code code = compile(source, filename, mode);
//if(filename != "<builtins>") std::cout << disassemble(code) << std::endl;
return _exec(code, _module, pkpy::make_shared<PyVarDict>()); return _exec(code, _module, pkpy::make_shared<PyVarDict>());
}catch (const _Error& e){ }catch (const _Error0& e){
*_stderr << e.what() << '\n'; *_stderr << e.what() << '\n';
} }
catch (const std::exception& e) { catch (const std::exception& e) {
auto re = RuntimeError("UnexpectedError", e.what(), _cleanErrorAndGetSnapshots()); auto re = _Error0("UnexpectedError", e.what(), false);
auto snapshots = _cleanErrorAndGetSnapshots();
while(!snapshots.empty()){
re.st_push(snapshots.top());
snapshots.pop();
}
*_stderr << re.what() << '\n'; *_stderr << re.what() << '\n';
} }
return nullptr; return nullptr;
@ -535,7 +539,7 @@ public:
template<typename ...Args> template<typename ...Args>
Frame* __push_new_frame(Args&&... args){ Frame* __push_new_frame(Args&&... args){
if(callstack.size() > maxRecursionDepth){ if(callstack.size() > maxRecursionDepth){
throw RuntimeError("RecursionError", "maximum recursion depth exceeded", _cleanErrorAndGetSnapshots()); _error("RecursionError", "maximum recursion depth exceeded");
} }
callstack.emplace_back(std::make_unique<Frame>(std::forward<Args>(args)...)); callstack.emplace_back(std::make_unique<Frame>(std::forward<Args>(args)...));
return callstack.back().get(); return callstack.back().get();
@ -888,7 +892,13 @@ 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()); auto e = _Error0(name, msg, true);
std::stack<_Str> snapshots = _cleanErrorAndGetSnapshots();
while (!snapshots.empty()){
e.st_push(snapshots.top());
snapshots.pop();
}
throw e;
} }
std::stack<_Str> _cleanErrorAndGetSnapshots(){ std::stack<_Str> _cleanErrorAndGetSnapshots(){