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(const Str& filename, CompileMode mode);
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;
};

View File

@ -36,6 +36,12 @@ namespace pkpy{
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{
SStream ss;
ss << " " << "File \"" << filename << "\", line " << lineno;

View File

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

View File

@ -1388,18 +1388,13 @@ void VM::_breakpoint(){
SStream ss;
Frame* frame = &frames[i]->frame;
int lineno = frame->curr_lineno();
auto [_0, _1] = frame->co->src->_get_line(lineno);
ss << "File \"" << frame->co->src->filename << "\", line " << lineno;
if(frame->_callable){
ss << ", in ";
ss << PK_OBJ_GET(Function, frame->_callable).decl->code->name;
}
ss << '\n';
if(_0 && _1){
ss << "-> " << std::string_view(_0, _1-_0) << '\n';
}else{
ss << "-> <no source code available>\n";
}
ss << frame->co->src->get_line(lineno) << '\n';
stdout_write(ss.str());
}
show_headers = false;
@ -1423,6 +1418,8 @@ void VM::_breakpoint(){
stdout_write("c, continue: continue execution\n");
stdout_write("a, args: show local variables\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");
continue;
}
@ -1453,6 +1450,37 @@ void VM::_breakpoint(){
}
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(' ');
if(space != -1){

View File

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