git @ Cat's Eye Technologies ZOWIE / 85007cc
Compiles under RPython, which apparently doesn't do getitem/setitem. At least, not for me. Chris Pressey 7 years ago
2 changed file(s) with 59 addition(s) and 27 deletion(s). Raw diff Collapse all Expand all
88 import sys
99
1010
11 def input():
12 try:
13 c = sys.stdin.read(1)
14 if len(c) == 0:
15 x = 0
16 else:
17 x = ord(c)
18 except EOFError:
19 x = 0
20 return x
21
22
23 def output(code):
24 try:
25 sys.stdout.write(unichr(code))
26 except UnicodeEncodeError:
27 sys.stdout.write("&#%d;" % code)
28
29
1130 # check if running under Skulpt, and if so, apply appropriate modifications
1231 if getattr(sys, 'resetTimeout', None) is not None:
1332 __name__ = '__skulpt__'
2140 class UnicodeEncodeError:
2241 pass
2342
24 def output(code):
43 def skulpt_output(code):
2544 print "&#%d;" % code
26 else:
27 def output(code):
28 try:
29 sys.stdout.write(unichr(code))
30 except UnicodeEncodeError:
31 sys.stdout.write("&#%d;" % code)
45
46 output = skulpt_output
3247
3348
3449 def copy_dict(d):
5166 pass
5267
5368 def read(self):
54 try:
55 c = sys.stdin.read(1)
56 if len(c) == 0:
57 x = 0
58 else:
59 x = ord(c)
60 except EOFError:
61 x = 0
62 return x
69 return input()
6370
6471 def write(self, payload):
6572 output(payload)
112119 return 4
113120
114121 def write(self, payload):
115 self.state[8] = self.state[8] + payload
122 self.state.__setitem__(8, self.state.__getitem__(8) + payload)
116123
117124
118125 class SubtractionRegister(MappedRegister):
123130 return 5
124131
125132 def write(self, payload):
126 result = self.state[8] - payload
133 result = self.state.__getitem__(8) - payload
127134 if result < 0:
128135 result = 0
129 self.state[8] = result
136 self.state.__setitem__(8, result)
130137
131138
132139 class MultiplicationRegister(MappedRegister):
137144 return 6
138145
139146 def write(self, payload):
140 self.state[8] = self.state[8] * payload
147 self.state.__setitem__(8, self.state.__getitem__(8) * payload)
141148
142149
143150 class NegationRegister(MappedRegister):
149156
150157 def write(self, payload):
151158 if payload == 0:
152 self.state[8] = 1
159 self.state.__setitem__(8, 1)
153160 else:
154 self.state[8] = 0
161 self.state.__setitem__(8, 0)
155162
156163
157164 class MachineState(object):
176183 return other
177184
178185 def __getitem__(self, key):
186 assert isinstance(key, int)
179187 if key in self.mmap_register:
180188 return self.mmap_register[key].read()
181189 if key not in self.storage_register:
183191 return self.storage_register[key]
184192
185193 def __setitem__(self, key, value):
194 assert isinstance(key, int)
195 assert isinstance(value, int)
186196 if key in self.mmap_register:
187197 self.mmap_register[key].write(value)
188198 self.storage_register[key] = value
212222 self.index = index
213223
214224 def get_value(self, state):
215 return state[self.index]
225 return state.__getitem__(self.index)
216226
217227 def set_value(self, state, value):
218 state[self.index] = value
228 state.__setitem__(self.index, value)
219229
220230 def __str__(self):
221231 return "R%d" % self.number
227237 self.ref = ref
228238
229239 def get_value(self, state):
230 return state[self.ref.get_value(state)]
240 return state.__getitem__(self.ref.get_value(state))
231241
232242 def set_value(self, state, value):
233 state[self.ref.get_value(state)] = value
243 state.__setitem__(self.ref.get_value(state), value)
234244
235245 def __str__(self):
236246 return "R[%s]" % self.ref
367377 for filename in argv[1:]:
368378 p.load(filename)
369379 p.run()
380
381
382 def rpython_input():
383 return 0
384
385 def rpython_output(code):
386 print "&#%d;" % code
370387
371388
372389 def rpython_main(argv):
389406
390407
391408 def target(*args):
409 global input, output
410 input = rpython_input
411 output = rpython_output
392412 return rpython_main, None
393413
394414
6464 | MOV 5, R11
6565 ?
6666
67 TODO need tests for indirect references here.
67 Retrieve indirect reference.
68
69 | MOV R11, 65
70 | MOV R12, 11
71 | MOV R0, R[R12]
72 = A
73
74 Store indirect reference.
75
76 | MOV R12, 11
77 | MOV R[R12], 65
78 | MOV R0, R11
79 = A