mirror of
https://github.com/catseye/SixtyPical.git
synced 2024-12-01 16:50:09 +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
|
import traceback
|
||||||
|
|
||||||
from sixtypical.parser import Parser
|
from sixtypical.parser import Parser
|
||||||
from sixtypical.evaluator import eval_program
|
from sixtypical.evaluator import Evaluator
|
||||||
from sixtypical.analyzer import analyze_program
|
from sixtypical.analyzer import analyze_program
|
||||||
from sixtypical.emitter import Emitter, Byte, Word
|
from sixtypical.emitter import Emitter, Byte, Word
|
||||||
from sixtypical.compiler import Compiler
|
from sixtypical.compiler import Compiler
|
||||||
@ -88,5 +88,5 @@ if __name__ == '__main__':
|
|||||||
emitter.serialize(fh)
|
emitter.serialize(fh)
|
||||||
|
|
||||||
if options.execute:
|
if options.execute:
|
||||||
context = eval_program(program)
|
context = Evaluator().eval_program(program)
|
||||||
print str(context)
|
print str(context)
|
||||||
|
@ -29,7 +29,9 @@ class Context(object):
|
|||||||
self._store[ref.name] = value
|
self._store[ref.name] = value
|
||||||
|
|
||||||
|
|
||||||
def eval_program(program):
|
class Evaluator(object):
|
||||||
|
|
||||||
|
def eval_program(self, program):
|
||||||
assert isinstance(program, Program)
|
assert isinstance(program, Program)
|
||||||
context = Context()
|
context = Context()
|
||||||
for ref in (REG_A, REG_X, REG_Y, FLAG_Z, FLAG_N, FLAG_V, FLAG_C):
|
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)
|
context.set(routine.location, routine)
|
||||||
if routine.name == 'main':
|
if routine.name == 'main':
|
||||||
main = routine
|
main = routine
|
||||||
eval_routine(main, context)
|
self.eval_routine(main, context)
|
||||||
return context
|
return context
|
||||||
|
|
||||||
|
def eval_routine(self, routine, context):
|
||||||
def eval_routine(routine, context):
|
|
||||||
assert isinstance(routine, Routine)
|
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(self, block, context):
|
||||||
def eval_block(block, context):
|
|
||||||
assert isinstance(block, Block)
|
assert isinstance(block, Block)
|
||||||
for i in block.instrs:
|
for i in block.instrs:
|
||||||
eval_instr(i, context)
|
self.eval_instr(i, context)
|
||||||
|
if self.next_routine:
|
||||||
|
break
|
||||||
|
|
||||||
|
def eval_instr(self, instr, context):
|
||||||
def eval_instr(instr, context):
|
|
||||||
assert isinstance(instr, Instr)
|
assert isinstance(instr, Instr)
|
||||||
opcode = instr.opcode
|
opcode = instr.opcode
|
||||||
dest = instr.dest
|
dest = instr.dest
|
||||||
@ -148,18 +153,20 @@ def eval_instr(instr, context):
|
|||||||
context.set(FLAG_N, 1 if result & 128 else 0)
|
context.set(FLAG_N, 1 if result & 128 else 0)
|
||||||
context.set(dest, result)
|
context.set(dest, result)
|
||||||
elif opcode == 'call':
|
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':
|
elif opcode == 'if':
|
||||||
val = context.get(src)
|
val = context.get(src)
|
||||||
test = (val != 0) if not instr.inverted else (val == 0)
|
test = (val != 0) if not instr.inverted else (val == 0)
|
||||||
if test:
|
if test:
|
||||||
eval_block(instr.block1, context)
|
self.eval_block(instr.block1, context)
|
||||||
elif instr.block2:
|
elif instr.block2:
|
||||||
eval_block(instr.block2, context)
|
self.eval_block(instr.block2, context)
|
||||||
elif opcode == 'repeat':
|
elif opcode == 'repeat':
|
||||||
eval_block(instr.block, context)
|
self.eval_block(instr.block, context)
|
||||||
while context.get(src) == 0:
|
while context.get(src) == 0:
|
||||||
eval_block(instr.block, context)
|
self.eval_block(instr.block, context)
|
||||||
elif opcode == 'copy':
|
elif opcode == 'copy':
|
||||||
context.set(dest, context.get(src))
|
context.set(dest, context.get(src))
|
||||||
# these are trashed; so could be anything really
|
# 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_Z, 0)
|
||||||
context.set(FLAG_N, 0)
|
context.set(FLAG_N, 0)
|
||||||
elif opcode == 'with-sei':
|
elif opcode == 'with-sei':
|
||||||
eval_block(instr.block)
|
self.eval_block(instr.block)
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
@ -420,3 +420,21 @@ Indirect call.
|
|||||||
= x: 200
|
= x: 200
|
||||||
= y: 0
|
= y: 0
|
||||||
= z: 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