Commits

Pierre Surply  committed 01b9bc4

robot: Added mem_stack

  • Participants
  • Parent commits 065a31b

Comments (0)

Files changed (6)

 ##    along with BASIC-RoBots.  If not, see <http://www.gnu.org/licenses/>.
 ## 
 ## Started on  Wed May  9 17:39:28 2012 Pierre Surply
-## Last update Wed Aug  1 17:46:30 2012 Pierre Surply
+## Last update Sat Aug 11 20:41:18 2012 Pierre Surply
 ##
 
 import os
             elif self.state == 2:
                 if self.game.update(self.display, self.input):
                     self.state = 1
-        self.dashboard.worldmap.save()
+        if self.dashboard != None:
+            self.dashboard.worldmap.save()
 
     def menu_command(self, cmd):
         if cmd[0] == 0:
 ##    along with BASIC-RoBots.  If not, see <http://www.gnu.org/licenses/>.
 ##
 ## Started on  Sat Jun 30 12:05:55 2012 Pierre Surply
-## Last update Wed Aug  1 17:48:57 2012 Pierre Surply
+## Last update Sat Aug 11 20:42:25 2012 Pierre Surply
 ##
 
 import os
                 self.show_menu()
 
     def title(self):
-        title = """ ____           _____ _____ _____      _____       ____        _       
+        title = """  ____           _____ _____ _____      _____       ____        _       
  |  _ \   /\    / ____|_   _/ ____|    |  __ \     |  _ \      | |      
  | |_) | /  \  | (___   | || |   ______| |__) |___ | |_) | ___ | |_ ___ 
  |  _ < / /\ \  \___ \  | || |  |______|  _  // _ \|  _ < / _ \| __/ __|

File 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 Wed Aug  1 17:59:19 2012 Pierre Surply
+## Last update Sat Aug 11 19:15:50 2012 Pierre Surply
 ##
 
 import random
         self.src = src
         self.terminal = terminal
         self.mem = mem
+        self.mem_stack = robot.mem_stack
         self.mem_ro = mem_ro
         self.robot = robot
         self.ext_cmd = self.robot.ext_cmd
                             self.line = self.call(opr[1], opr[2], self.line)
                     except IndexError:
                         self.line = self.raise_error_arg(cmd, 1, self.line)
+                elif cmd == "PUSH":
+                    try:
+                        self.line = self.push(opr[1], self.line)
+                    except IndexError:
+                        self.line = self.raise_error_arg(cmd, 1, self.line)
+                elif cmd == "POP":
+                    try:
+                        self.line = self.pop(opr[1], self.line)
+                    except IndexError:
+                        self.line = self.raise_error_arg(cmd, 1, self.line)
                 elif not(cmd in ["LABEL", "REM"]):
                     self.line = self.raise_error("Unknown command \"" + cmd + "\"" , self.line)
             self.line += 1
             else:
                 return line-1
         else:
-            return self.raise_error("The robot hasn't the \"" + var + "\" register", line)
+            return self.raise_error_reg(var, line)
 
     def let(self, var, expr, line):
+        return self.set_mem(var, self.eval_exp(expr, line), line)
+
+    def set_mem(self, var, val, line):
         if var in self.mem:
-            self.set_mem(var, self.eval_exp(expr, line))
+            self.mem[var] = val
+            if self.mem[var] > 255:
+                self.mem[var] = 255
+            elif self.mem[var] < 0:
+                self.mem[var] = 0
             return line
         else:
-            return self.raise_error("The robot hasn't the \"" + var + "\" register", line)
-
-    def set_mem(self, var, val):
-        self.mem[var] = val
-        if self.mem[var] > 255:
-            self.mem[var] = 255
-        elif self.mem[var] < 0:
-            self.mem[var] = 0
+            return self.raise_error_reg(var, line)
 
     def goto_return(self, label, line):
         return self.goto(label, "GOTOSUB", line)
                 self.ext_cmd[function][0]()
                 return line
             else:
-                if reg in self.mem:
-                    self.set_mem(reg, self.robot.run_ext_cmd(self.ext_cmd[function][0], \
-                                                                 self.ext_cmd[function][3]))
-                    return line
-                else:
-                    return self.raise_error("The robot hasn't the \"" + var + "\" register", line)
+                return self.set_mem(reg, self.robot.run_ext_cmd(self.ext_cmd[function][0], \
+                                                                 self.ext_cmd[function][3]), line)
         else:
             return self.raise_error("The external command \""+ function + "\"is not defined for this robot", line)
 
+    def push(self, reg, line):
+        if reg in self.mem:
+            self.mem_stack.append(self.mem[reg])
+            return line
+        else:
+            return self.raise_error_reg(reg, line)
+
+    def pop(self, reg, line):
+        if reg in self.mem:
+            if self.mem_stack != []:
+                self.mem[reg] = self.mem_stack.pop()
+            return line
+        else:
+            return self.raise_error_reg(reg, line)
+            
+            
+    def raise_error_reg(self, reg, line):
+        return self.raise_error("The robot has not the \"" + var + "\" register", line)
+
     def raise_error_arg(self, cmd, nbr_arg, line):
         self.raise_error("The command \"" + cmd + "\" expect " + str(nbr_arg) + " argument(s)", line)
         

File src/robot/robot.py

 ##    along with BASIC-RoBots.  If not, see <http://www.gnu.org/licenses/>.
 ## 
 ## Started on  Sun Jul  1 15:34:10 2012 Pierre Surply
-## Last update Sat Aug 11 17:50:15 2012 Pierre Surply
+## Last update Sat Aug 11 20:24:15 2012 Pierre Surply
 ##
 
 import os
         self.mem = {"A" : 0,\
                         "B" : 0,\
                         "C" : 0,\
-                        "D" : 0,\
-                        "E" : 0}
+                        "D" : 0}
+        self.mem_stack = []
         self.ext_cmd = {"forward" : (self.forward, \
                                          "Move the robot forward", \
                                          "1 if the robot can move, 0 otherwise",\

File 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 Sat Aug 11 14:24:40 2012 Pierre Surply
+## Last update Sat Aug 11 20:51:33 2012 Pierre Surply
 ##
 
 import os
                            ("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)),\
+                           ("stack", "", "print stack", lambda o, a: o.stack(a)),\
                            ("clear", "", "clear screen", lambda o, a: o.clear(a))])
         if self.robot.energy > 0:
             self.welcome(None)
             if self.task == 0 and selected:
                 cmd = self.terminal.prompt()
                 if cmd != None:
-                    self.command(cmd)
+                    for i in cmd.split("&&"):
+                        i = i.strip()
+                        if i != "":
+                            self.command(i)
                     if self.task == 0 and self.robot.energy > 0:
                         self.terminal.start_prompt(">")
-                info = (self.mem, 8)
+                info = (self.mem, 10)
             elif self.task == 1:
                 if not self.basic.eval_line() or \
                         (selected and events.key[K_LCTRL] and \
                     self.task = 0
                     if self.robot.energy > 0:
                         self.terminal.start_prompt(">")
-                info = (self.mem, 8)
+                info = (self.mem, 10)
             elif self.task == 2 and selected:
                 if not self.terminal.edit(self, h):
                     self.task = 0
                                      self.robot,\
                                      self.terminal)
         self.task = 1
+
+    def stack(self, arg):
+        if self.robot.mem_stack == []:
+            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(" <-")

File src/terminal.py

 ##    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 Sat Aug 11 14:24:51 2012 Pierre Surply
+## Last update Sat Aug 11 20:40:09 2012 Pierre Surply
 ##
 
 import time
         self.events = events
 
         self.set_title("Terminal")
-        self.lines = [[ord(" ")]]
+        self.lines = [[]]
         
         self.hist = []
         self.id_hist = 0
         elif info_bottom != None:
             x = 0
             for i in sorted(info_bottom[0].keys()):
+                if isinstance(info_bottom[0][i], int):
+                    val = hex(info_bottom[0][i])
+                else:
+                    val = str(info_bottom[0][i])
                 self.display_line(display, \
-                                      self.str2charlist(i+":"+hex(info_bottom[0][i]), 1), (org_x+x, h))
+                                      self.str2charlist(i+": "+val, 1), (org_x+x, h))
                 x += info_bottom[1]
         if info_top != None:
             x = w-16