mirror of
https://github.com/catseye/SixtyPical.git
synced 2024-11-29 03:51:35 +00:00
Recast Evaluator as an object. Handle goto
inside it.
This commit is contained in:
parent
be76b9a00d
commit
6e0ca3838e
@ -18,7 +18,7 @@ import sys
|
||||
import traceback
|
||||
|
||||
from sixtypical.parser import Parser
|
||||
from sixtypical.evaluator import eval_program
|
||||
from sixtypical.evaluator import Evaluator
|
||||
from sixtypical.analyzer import analyze_program
|
||||
from sixtypical.emitter import Emitter, Byte, Word
|
||||
from sixtypical.compiler import Compiler
|
||||
@ -88,5 +88,5 @@ if __name__ == '__main__':
|
||||
emitter.serialize(fh)
|
||||
|
||||
if options.execute:
|
||||
context = eval_program(program)
|
||||
context = Evaluator().eval_program(program)
|
||||
print str(context)
|
||||
|
@ -29,7 +29,9 @@ class Context(object):
|
||||
self._store[ref.name] = value
|
||||
|
||||
|
||||
def eval_program(program):
|
||||
class Evaluator(object):
|
||||
|
||||
def eval_program(self, program):
|
||||
assert isinstance(program, Program)
|
||||
context = Context()
|
||||
for ref in (REG_A, REG_X, REG_Y, FLAG_Z, FLAG_N, FLAG_V, FLAG_C):
|
||||
@ -39,22 +41,25 @@ def eval_program(program):
|
||||
context.set(routine.location, routine)
|
||||
if routine.name == 'main':
|
||||
main = routine
|
||||
eval_routine(main, context)
|
||||
self.eval_routine(main, context)
|
||||
return context
|
||||
|
||||
|
||||
def eval_routine(routine, context):
|
||||
def eval_routine(self, routine, context):
|
||||
assert isinstance(routine, Routine)
|
||||
eval_block(routine.block, context)
|
||||
self.next_routine = routine
|
||||
while self.next_routine:
|
||||
routine = self.next_routine
|
||||
self.next_routine = None
|
||||
self.eval_block(routine.block, context)
|
||||
|
||||
|
||||
def eval_block(block, context):
|
||||
def eval_block(self, block, context):
|
||||
assert isinstance(block, Block)
|
||||
for i in block.instrs:
|
||||
eval_instr(i, context)
|
||||
self.eval_instr(i, context)
|
||||
if self.next_routine:
|
||||
break
|
||||
|
||||
|
||||
def eval_instr(instr, context):
|
||||
def eval_instr(self, instr, context):
|
||||
assert isinstance(instr, Instr)
|
||||
opcode = instr.opcode
|
||||
dest = instr.dest
|
||||
@ -148,18 +153,20 @@ def eval_instr(instr, context):
|
||||
context.set(FLAG_N, 1 if result & 128 else 0)
|
||||
context.set(dest, result)
|
||||
elif opcode == 'call':
|
||||
eval_routine(context.get(instr.location), context)
|
||||
self.eval_routine(context.get(instr.location), context)
|
||||
elif opcode == 'goto':
|
||||
self.next_routine = context.get(instr.location)
|
||||
elif opcode == 'if':
|
||||
val = context.get(src)
|
||||
test = (val != 0) if not instr.inverted else (val == 0)
|
||||
if test:
|
||||
eval_block(instr.block1, context)
|
||||
self.eval_block(instr.block1, context)
|
||||
elif instr.block2:
|
||||
eval_block(instr.block2, context)
|
||||
self.eval_block(instr.block2, context)
|
||||
elif opcode == 'repeat':
|
||||
eval_block(instr.block, context)
|
||||
self.eval_block(instr.block, context)
|
||||
while context.get(src) == 0:
|
||||
eval_block(instr.block, context)
|
||||
self.eval_block(instr.block, context)
|
||||
elif opcode == 'copy':
|
||||
context.set(dest, context.get(src))
|
||||
# these are trashed; so could be anything really
|
||||
@ -167,6 +174,6 @@ def eval_instr(instr, context):
|
||||
context.set(FLAG_Z, 0)
|
||||
context.set(FLAG_N, 0)
|
||||
elif opcode == 'with-sei':
|
||||
eval_block(instr.block)
|
||||
self.eval_block(instr.block)
|
||||
else:
|
||||
raise NotImplementedError
|
||||
|
@ -420,3 +420,21 @@ Indirect call.
|
||||
= x: 200
|
||||
= y: 0
|
||||
= z: 0
|
||||
|
||||
goto.
|
||||
|
||||
| routine bar outputs x trashes z, n {
|
||||
| ld x, 200
|
||||
| }
|
||||
|
|
||||
| routine main outputs x trashes a, z, n {
|
||||
| ld y, 200
|
||||
| goto bar
|
||||
| }
|
||||
= a: 0
|
||||
= c: 0
|
||||
= n: 1
|
||||
= v: 0
|
||||
= x: 200
|
||||
= y: 200
|
||||
= z: 0
|
||||
|
Loading…
Reference in New Issue
Block a user