Explorar o código

add `l` and `ll`

blueloveTH hai 1 ano
pai
achega
49045f2c48
Modificáronse 5 ficheiros con 55 adicións e 21 borrados
  1. 1 0
      include/pocketpy/error.h
  2. 6 0
      src/error.cpp
  3. 1 2
      src/profiler.cpp
  4. 34 6
      src/vm.cpp
  5. 13 13
      tests/95_pdb.py

+ 1 - 0
include/pocketpy/error.h

@@ -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;
 };
 };
 
 

+ 6 - 0
src/error.cpp

@@ -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;

+ 1 - 2
src/profiler.cpp

@@ -115,8 +115,7 @@ Str LineProfiler::stats(){
                 }
                 }
             }
             }
             // line_content
             // 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";
         ss << "\n";
     }
     }

+ 34 - 6
src/vm.cpp

@@ -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 << "-> " << 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());
                 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){

+ 13 - 13
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)