add l and ll

This commit is contained in:
blueloveTH 2024-04-15 19:38:11 +08:00
parent 21e5122c44
commit 49045f2c48
5 changed files with 55 additions and 21 deletions

View File

@ -38,6 +38,7 @@ struct SourceData {
SourceData(std::string_view source, const Str& filename, CompileMode mode); SourceData(std::string_view source, const Str& filename, CompileMode mode);
SourceData(const Str& filename, CompileMode mode); SourceData(const Str& filename, CompileMode mode);
std::pair<const char*,const char*> _get_line(int lineno) const; std::pair<const char*,const char*> _get_line(int lineno) const;
std::string_view get_line(int lineno) const;
Str snapshot(int lineno, const char* cursor, std::string_view name) const; Str snapshot(int lineno, const char* cursor, std::string_view name) const;
}; };

View File

@ -36,6 +36,12 @@ namespace pkpy{
return {_start, i}; return {_start, i};
} }
std::string_view SourceData::get_line(int lineno) const{
auto [_0, _1] = _get_line(lineno);
if(_0 && _1) return std::string_view(_0, _1-_0);
return "<?>";
}
Str SourceData::snapshot(int lineno, const char* cursor, std::string_view name) const{ Str SourceData::snapshot(int lineno, const char* cursor, std::string_view name) const{
SStream ss; SStream ss;
ss << " " << "File \"" << filename << "\", line " << lineno; ss << " " << "File \"" << filename << "\", line " << lineno;

View File

@ -115,8 +115,7 @@ Str LineProfiler::stats(){
} }
} }
// line_content // line_content
auto [_0, _1] = decl->code->src->_get_line(line); ss << " " << decl->code->src->get_line(line) << "\n";
ss << " " << std::string_view(_0, _1-_0) << "\n";
} }
ss << "\n"; ss << "\n";
} }

View File

@ -1388,18 +1388,13 @@ void VM::_breakpoint(){
SStream ss; SStream ss;
Frame* frame = &frames[i]->frame; Frame* frame = &frames[i]->frame;
int lineno = frame->curr_lineno(); int lineno = frame->curr_lineno();
auto [_0, _1] = frame->co->src->_get_line(lineno);
ss << "File \"" << frame->co->src->filename << "\", line " << lineno; ss << "File \"" << frame->co->src->filename << "\", line " << lineno;
if(frame->_callable){ if(frame->_callable){
ss << ", in "; ss << ", in ";
ss << PK_OBJ_GET(Function, frame->_callable).decl->code->name; ss << PK_OBJ_GET(Function, frame->_callable).decl->code->name;
} }
ss << '\n'; ss << '\n';
if(_0 && _1){ ss << frame->co->src->get_line(lineno) << '\n';
ss << "-> " << std::string_view(_0, _1-_0) << '\n';
}else{
ss << "-> <no source code available>\n";
}
stdout_write(ss.str()); stdout_write(ss.str());
} }
show_headers = false; show_headers = false;
@ -1423,6 +1418,8 @@ void VM::_breakpoint(){
stdout_write("c, continue: continue execution\n"); stdout_write("c, continue: continue execution\n");
stdout_write("a, args: show local variables\n"); stdout_write("a, args: show local variables\n");
stdout_write("p, print <expr>: evaluate expression\n"); stdout_write("p, print <expr>: evaluate expression\n");
stdout_write("l, list: show lines around current line\n");
stderr_write("ll, longlist: show all lines\n");
stdout_write("!: execute statement\n"); stdout_write("!: execute statement\n");
continue; continue;
} }
@ -1453,6 +1450,37 @@ void VM::_breakpoint(){
} }
continue; continue;
} }
bool is_list = line == "l" || line == "list";
bool is_longlist = line == "ll" || line == "longlist";
if(is_list || is_longlist){
if(frame_0->co->src->is_precompiled) continue;
int lineno = frame_0->curr_lineno();
int start, end;
if(is_list){
int max_line = frame_0->co->src->line_starts.size() + 1;
start = std::max(1, lineno-5);
end = std::min(max_line, lineno+5);
}else{
start = frame_0->co->start_line;
end = frame_0->co->end_line;
if(start == -1 || end == -1) continue;
}
SStream ss;
int max_width = std::to_string(end).size();
for(int i=start; i<=end; i++){
int spaces = max_width - std::to_string(i).size();
ss << std::string(spaces, ' ') << std::to_string(i);
if(i == lineno) ss << " -> ";
else ss << " ";
ss << frame_0->co->src->get_line(i) << '\n';
}
stdout_write(ss.str());
continue;
}
int space = line.find_first_of(' '); int space = line.find_first_of(' ');
if(space != -1){ if(space != -1){

View File

@ -1,19 +1,19 @@
# a = 1 a = 1
# b = 2 b = 2
# print(a, b) print(a, b)
# def f(a, b): def f(a, b):
# b = a + b b = a + b
# print(a, b) print(a, b)
# return b return b
# def g(a, b): def g(a, b):
# breakpoint() breakpoint()
# c = f(a, b) c = f(a, b)
# return c return c
# g(1, 2) g(1, 2)
# f(3, 4) f(3, 4)