From 49045f2c4895540d4e480fd3e952ae9c6622a5cf Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Mon, 15 Apr 2024 19:38:11 +0800 Subject: [PATCH] add `l` and `ll` --- include/pocketpy/error.h | 1 + src/error.cpp | 6 ++++++ src/profiler.cpp | 3 +-- src/vm.cpp | 40 ++++++++++++++++++++++++++++++++++------ tests/95_pdb.py | 26 +++++++++++++------------- 5 files changed, 55 insertions(+), 21 deletions(-) diff --git a/include/pocketpy/error.h b/include/pocketpy/error.h index 9a03121c..26878fcf 100644 --- a/include/pocketpy/error.h +++ b/include/pocketpy/error.h @@ -38,6 +38,7 @@ struct SourceData { SourceData(std::string_view source, const Str& filename, CompileMode mode); SourceData(const Str& filename, CompileMode mode); std::pair _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; }; diff --git a/src/error.cpp b/src/error.cpp index 269a25e7..65000196 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -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; diff --git a/src/profiler.cpp b/src/profiler.cpp index db7e63a0..728d6f0f 100644 --- a/src/profiler.cpp +++ b/src/profiler.cpp @@ -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"; } diff --git a/src/vm.cpp b/src/vm.cpp index e2700f7d..6bb2875d 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -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 << "-> \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 : 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){ diff --git a/tests/95_pdb.py b/tests/95_pdb.py index f60ef89d..b44f7536 100644 --- a/tests/95_pdb.py +++ b/tests/95_pdb.py @@ -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)