Tell stackmac the # of globals; "only" 17 fails now.
catseye
12 years ago
60 | 60 |
for child in ast.children:
|
61 | 61 |
self.compile(child)
|
62 | 62 |
self.out.write("""\
|
|
63 |
; ...
|
|
64 |
global_pos=%d
|
63 | 65 |
; call main
|
64 | 66 |
get_global main_index
|
65 | 67 |
call
|
66 | |
""")
|
|
68 |
""" % self.global_pos)
|
67 | 69 |
elif ast.type == 'Defn':
|
68 | 70 |
self.out.write('%s_index=%d\n' % (ast.value, self.global_pos))
|
69 | 71 |
self.global_pos += 1
|
4 | 4 |
import sys
|
5 | 5 |
|
6 | 6 |
from castile.builtins import BUILTINS
|
|
7 |
|
|
8 |
|
|
9 |
labels = {}
|
|
10 |
debug = False
|
7 | 11 |
|
8 | 12 |
|
9 | 13 |
def boo(b):
|
|
14 | 18 |
|
15 | 19 |
|
16 | 20 |
def run(program):
|
|
21 |
global labels
|
17 | 22 |
ip = 0
|
18 | 23 |
iter = 0
|
19 | 24 |
stack = []
|
|
22 | 27 |
returnsize = 0
|
23 | 28 |
while ip < len(program):
|
24 | 29 |
(op, arg) = program[ip]
|
25 | |
#print ip, op, arg, stack, callstack
|
|
30 |
if debug:
|
|
31 |
print ip, op, arg, stack, callstack
|
26 | 32 |
if op == 'push':
|
27 | 33 |
stack.append(arg)
|
28 | 34 |
elif op == 'jmp':
|
|
68 | 74 |
b = stack.pop()
|
69 | 75 |
a = stack.pop()
|
70 | 76 |
stack.append(a | b)
|
|
77 |
elif op == 'not':
|
|
78 |
a = stack.pop()
|
|
79 |
stack.append(boo(a == 0))
|
71 | 80 |
elif op == 'set_baseptr':
|
72 | 81 |
stack.append(baseptr)
|
73 | 82 |
baseptr = len(stack) - 1
|
|
79 | 88 |
while x < returnsize:
|
80 | 89 |
rs.append(stack.pop())
|
81 | 90 |
x += 1
|
82 | |
target = baseptr + arg + 1
|
|
91 |
target = baseptr + arg
|
83 | 92 |
baseptr = stack[baseptr]
|
84 | 93 |
while len(stack) > target:
|
85 | 94 |
stack.pop()
|
|
100 | 109 |
if iter > 10000:
|
101 | 110 |
raise ValueError("infinite loop?")
|
102 | 111 |
|
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
|
106 | 119 |
|
107 | 120 |
|
108 | 121 |
def main(args):
|
109 | 122 |
address = 0
|
110 | 123 |
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 |
|
113 | 131 |
# load program
|
114 | 132 |
with open(args[1], 'r') as f:
|
115 | 133 |
for line in f:
|