Commits

Pierre Surply committed 416d337

Added debugger

Comments (0)

Files changed (3)

src/robot/basic.py

 ##    along with BASIC-RoBots.  If not, see <http://www.gnu.org/licenses/>.
 ## 
 ## Started on  Sat May  5 13:04:41 2012 Pierre Surply
-## Last update Sat Aug 18 11:55:01 2012 Pierre Surply
+## Last update Sun Aug 19 18:14:41 2012 Pierre Surply
 ##
 
 import random
 class Basic:
     func = {"rand" : (lambda: random.randint(0, 255)),\
                 "time" : (lambda: int(time.time()) % 256)}
-    def __init__(self, current_dir, src, robot, terminal):
+    def __init__(self, current_dir, src, robot, terminal, debug=False):
         self.current_dir = current_dir
         self.src = src
         self.terminal = terminal
         self.whilestack = []
         self.opr = self.set_opr(self.src)
         self.line = 0
+        self.debug = debug
 
     def set_opr(self, src):
         opr = []
         for i in src.split("\n"):
             opr.append(filter(lambda x: x != "", i.split(" ")))
         return opr
+    
+    def print_debug(self):
+        self.terminal.clear()
+        self.terminal.write_line("\x04\x04\x04 Basic Debug \x04\x04\x04", 1)
+        self.terminal.write_line("Press N to execute next instruction")
+        self.terminal.write_line("Press R to quit debugger\n")
+        self.terminal.write_line("Instructions (line " + str(self.line) + ")", 1)
+        for i in self.opr[self.line-2:self.line]:
+            self.terminal.write_line("   " + " ".join(i))
+        if self.line < len(self.opr):
+            self.terminal.write_line("\x1A  " + " ".join(self.opr[self.line]))
+        for i in self.opr[self.line+1:self.line+5]:
+            self.terminal.write_line("   " + " ".join(i))
+        self.terminal.write_line("\n", 1)
+        self.terminal.write_line("Registers", 1)
+        for i in sorted(self.robot.mem.keys()):
+            self.terminal.write(" ")
+            self.terminal.write(i, 1)
+            self.terminal.write(":" + hex(self.robot.mem[i]))
+        self.terminal.write_line("\n")
+        self.terminal.write_line("Registers (RO)", 1)
+        mem_ro = self.robot.get_mem_ro()
+        for i in sorted(mem_ro):
+            self.terminal.write(" ")
+            self.terminal.write(i, 1)
+            self.terminal.write(":" + hex(mem_ro[i]))
+        self.terminal.write_line("\n")
+        self.terminal.write_line("Stack", 1)
+        if self.robot.mem_stack == []:
+            self.terminal.write_line("The stack is empty")
+        else:
+            for i in self.robot.mem_stack[-5::]:
+                self.terminal.write("\xB3" + hex(i))
+            self.terminal.write_line(" \x1B")
+        self.terminal.write_line("\nOut", 1)
             
     def eval_line(self):
         if self.line < len(self.opr):

src/robot/robotsos.py

 ##    along with BASIC-RoBots.  If not, see <http://www.gnu.org/licenses/>.
 ##  
 ## Started on  Wed May  9 22:56:00 2012 Pierre Surply
-## Last update Sun Aug 19 16:15:18 2012 Pierre Surply
+## Last update Sun Aug 19 18:15:44 2012 Pierre Surply
 ##
 
 import os
                            ("touch", "FILE", "make file", lambda o, a: o.touch(a)),\
                            ("cd", "[DIRECTORY]", "change current directory", lambda o, a: o.cd(a)),\
                            ("cat", "FILE", "print file", lambda o, a: o.cat(a)),\
-                           ("run", "FILE", "execute a BASIC script", lambda o, a: o.execute(a)),\
+                           ("run", "FILE", "execute a BASIC script", lambda o, a: o.execute(a, False)),\
+                           ("debug", "FILE", "debug a BASIC script", lambda o, a: o.execute(a, True)),\
                            ("edit", "FILE", "edit a file", lambda o, a: o.edit(a)),\
                            ("inv", "", "list inventory", lambda o, a: o.ls_inv(a)),\
                            ("let", "REG VALUE", "set register value", lambda o, a: o.let(a)),\
                         self.start_prompt()
                 info = (self.mem, 8)
             elif self.task == 1:
-                if not self.basic.eval_line():
+                if self.basic.debug:
+                    self.basic.print_debug()
+                    if events.get_key_once(K_r):
+                        self.basic.debug = False
+                        self.terminal.clear()
+                    if events.get_key_once(K_n) or self.terminal.act_prompt:
+                        b = self.basic.eval_line()
+                    else:
+                        b = True
+                else:
+                    b = self.basic.eval_line()
+                if not b:
                     self.task = 0
                     if self.robot.energy > 0:
                         self.start_prompt()
             else:
                 self.terminal.write_line(arg[0] + ": No such file or directory")
 
-    def execute(self, arg):
+    def execute(self, arg, debug):
         if len(arg) > 0:
             if os.path.isfile(self.get_path(self.current_dir + arg[0])):
                 f = open(self.get_path(self.current_dir) + arg[0], 'r')
                 self.basic = basic.Basic(self.get_path(self.current_dir),\
                                              f.read(), \
                                              self.robot,\
-                                             self.terminal)
+                                             self.terminal, \
+                                             debug)
                 f.close()
                 self.terminal.write_line("Running " + arg[0] + "...\nPress CTRL+C to interrupt")
                 self.task = 1
             self.terminal.write_line("The stack is empty")
         else:
             for i in self.robot.mem_stack:
-                self.terminal.write("|" + hex(i))
-            self.terminal.write_line(" <-")
+                self.terminal.write("\xB3" + hex(i))
+            self.terminal.write_line(" \x1B")
 
     def tree(self, arg):
         self.rec_tree(True, 0, self.get_path(self.current_dir))
 ##    along with BASIC-RoBots.  If not, see <http://www.gnu.org/licenses/>.
 ## 
 ## Started on  Wed May  9 18:57:29 2012 Pierre Surply
-## Last update Fri Aug 17 14:44:38 2012 Pierre Surply
+## Last update Sun Aug 19 18:25:54 2012 Pierre Surply
 ##
 
 import time
             x += 1
 
     def cut_line(self, display, line, (org_x, org_y, w, h), y, first):
-        if y < h:
+        if y <= h:
             if not first:
                 display.print_char((org_x+w, org_y+h-y), ord('\\'))
             if len(line) > w: