git @ Cat's Eye Technologies Castile / 3342729
Tell stackmac the # of globals; "only" 17 fails now. catseye 12 years ago
2 changed file(s) with 28 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
6060 for child in ast.children:
6161 self.compile(child)
6262 self.out.write("""\
63 ; ...
64 global_pos=%d
6365 ; call main
6466 get_global main_index
6567 call
66 """)
68 """ % self.global_pos)
6769 elif ast.type == 'Defn':
6870 self.out.write('%s_index=%d\n' % (ast.value, self.global_pos))
6971 self.global_pos += 1
44 import sys
55
66 from castile.builtins import BUILTINS
7
8
9 labels = {}
10 debug = False
711
812
913 def boo(b):
1418
1519
1620 def run(program):
21 global labels
1722 ip = 0
1823 iter = 0
1924 stack = []
2227 returnsize = 0
2328 while ip < len(program):
2429 (op, arg) = program[ip]
25 #print ip, op, arg, stack, callstack
30 if debug:
31 print ip, op, arg, stack, callstack
2632 if op == 'push':
2733 stack.append(arg)
2834 elif op == 'jmp':
6874 b = stack.pop()
6975 a = stack.pop()
7076 stack.append(a | b)
77 elif op == 'not':
78 a = stack.pop()
79 stack.append(boo(a == 0))
7180 elif op == 'set_baseptr':
7281 stack.append(baseptr)
7382 baseptr = len(stack) - 1
7988 while x < returnsize:
8089 rs.append(stack.pop())
8190 x += 1
82 target = baseptr + arg + 1
91 target = baseptr + arg
8392 baseptr = stack[baseptr]
8493 while len(stack) > target:
8594 stack.pop()
100109 if iter > 10000:
101110 raise ValueError("infinite loop?")
102111
103 result = stack.pop()
104 if result != 0:
105 print repr(result)
112 if len(stack) > labels['global_pos']:
113 result = stack.pop()
114 if result == 0:
115 result = 'False'
116 if result == -1:
117 result = 'True'
118 print result
106119
107120
108121 def main(args):
109122 address = 0
110123 program = []
111 labels = {}
112
124 global labels
125 global debug
126
127 if args[1] == '-d':
128 args[1] = args[2]
129 debug = True
130
113131 # load program
114132 with open(args[1], 'r') as f:
115133 for line in f: