Commits

Anonymous committed 71a4e99

Tell stackmac the # of globals; "only" 17 fails now.

  • Participants
  • Parent commits d5be88d

Comments (0)

Files changed (2)

src/castile/backends/stackmac.py

             for child in ast.children:
                 self.compile(child)
             self.out.write("""\
+; ...
+global_pos=%d
 ; call main
 get_global main_index
 call
-""")
+""" % self.global_pos)
         elif ast.type == 'Defn':
             self.out.write('%s_index=%d\n' % (ast.value, self.global_pos))
             self.global_pos += 1

src/castile/stackmac.py

 from castile.builtins import BUILTINS
 
 
+labels = {}
+debug = False
+
+
 def boo(b):
     if b:
         return -1
 
 
 def run(program):
+    global labels
     ip = 0
     iter = 0
     stack = []
     returnsize = 0
     while ip < len(program):
         (op, arg) = program[ip]
-        #print ip, op, arg, stack, callstack
+        if debug:
+            print ip, op, arg, stack, callstack
         if op == 'push':
             stack.append(arg)
         elif op == 'jmp':
             b = stack.pop()
             a = stack.pop()
             stack.append(a | b)
+        elif op == 'not':
+            a = stack.pop()
+            stack.append(boo(a == 0))
         elif op == 'set_baseptr':
             stack.append(baseptr)
             baseptr = len(stack) - 1
             while x < returnsize:
                 rs.append(stack.pop())
                 x += 1
-            target = baseptr + arg + 1
+            target = baseptr + arg
             baseptr = stack[baseptr]
             while len(stack) > target:
                 stack.pop()
         if iter > 10000:
             raise ValueError("infinite loop?")
 
-    result = stack.pop()
-    if result != 0:
-        print repr(result)
+    if len(stack) > labels['global_pos']:
+        result = stack.pop()
+        if result == 0:
+            result = 'False'
+        if result == -1:
+            result = 'True'
+        print result
 
 
 def main(args):
     address = 0
     program = []
-    labels = {}
-    
+    global labels
+    global debug
+
+    if args[1] == '-d':
+        args[1] = args[2]
+        debug = True
+
     # load program
     with open(args[1], 'r') as f:
         for line in f: