1
0
mirror of https://github.com/catseye/SixtyPical.git synced 2025-02-09 01:30:50 +00:00

backpatch_constraint_labels can resolve ForwardReferences.

This commit is contained in:
Chris Pressey 2018-09-07 14:51:16 +01:00
parent 29a5bba85c
commit 07ef1e243a
2 changed files with 20 additions and 13 deletions

View File

@ -27,11 +27,11 @@ TYPE_WORD = Type('word', max_range=(0, 65535))
class RoutineType(Type):
"""This memory location contains the code for a routine."""
def __init__(self, inputs=None, outputs=None, trashes=None):
def __init__(self, inputs, outputs, trashes):
self.name = 'routine'
self.inputs = inputs or set()
self.outputs = outputs or set()
self.trashes = trashes or set()
self.inputs = inputs
self.outputs = outputs
self.trashes = trashes
def __repr__(self):
return '%s(%r, inputs=%r, outputs=%r, trashes=%r)' % (

View File

@ -74,26 +74,29 @@ class Parser(object):
def clear_statics(self):
self.context.statics = {}
def resolve_symbols(self, program):
# ---- symbol resolution
def backpatch_constraint_labels(type_, resolver):
def resolve_symbols(self, program):
# This could stand to be better unified.
def backpatch_constraint_labels(type_):
def resolve(w):
if not isinstance(w, str):
if not isinstance(w, ForwardReference):
return w
return resolver(w)
return self.lookup(w.name)
if isinstance(type_, TableType):
backpatch_constraint_labels(type_.of_type, resolver)
backpatch_constraint_labels(type_.of_type)
elif isinstance(type_, VectorType):
backpatch_constraint_labels(type_.of_type, resolver)
backpatch_constraint_labels(type_.of_type)
elif isinstance(type_, RoutineType):
type_.inputs = set([resolve(w) for w in type_.inputs])
type_.outputs = set([resolve(w) for w in type_.outputs])
type_.trashes = set([resolve(w) for w in type_.trashes])
for defn in program.defns:
backpatch_constraint_labels(defn.location.type, lambda w: self.lookup(w))
backpatch_constraint_labels(defn.location.type)
for routine in program.routines:
backpatch_constraint_labels(routine.location.type, lambda w: self.lookup(w))
backpatch_constraint_labels(routine.location.type)
def resolve_fwd_reference(obj, field):
field_value = getattr(obj, field, None)
@ -279,7 +282,11 @@ class Parser(object):
outputs = set(self.labels())
if self.scanner.consume('trashes'):
trashes = set(self.labels())
return (inputs, outputs, trashes)
return (
set([ForwardReference(n) for n in inputs]),
set([ForwardReference(n) for n in outputs]),
set([ForwardReference(n) for n in trashes])
)
def legacy_routine(self):
self.scanner.expect('routine')