git @ Cat's Eye Technologies SixtyPical / 29a5bba
Distinct symbol resolution phase (as a method on parser.) Chris Pressey 4 years ago
2 changed file(s) with 36 addition(s) and 37 deletion(s). Raw diff Collapse all Expand all
1616
1717 def __hash__(self):
1818 return hash(self.name)
19
20 def backpatch_constraint_labels(self, resolver):
21 def resolve(w):
22 if not isinstance(w, str):
23 return w
24 return resolver(w)
25 if isinstance(self, TableType):
26 self.of_type.backpatch_constraint_labels(resolver)
27 elif isinstance(self, VectorType):
28 self.of_type.backpatch_constraint_labels(resolver)
29 elif isinstance(self, RoutineType):
30 self.inputs = set([resolve(w) for w in self.inputs])
31 self.outputs = set([resolve(w) for w in self.outputs])
32 self.trashes = set([resolve(w) for w in self.trashes])
3319
3420
3521 TYPE_BIT = Type('bit', max_range=(0, 1))
7272
7373 def clear_statics(self):
7474 self.context.statics = {}
75
76 def resolve_symbols(self, program):
77
78 def backpatch_constraint_labels(type_, resolver):
79 def resolve(w):
80 if not isinstance(w, str):
81 return w
82 return resolver(w)
83 if isinstance(type_, TableType):
84 backpatch_constraint_labels(type_.of_type, resolver)
85 elif isinstance(type_, VectorType):
86 backpatch_constraint_labels(type_.of_type, resolver)
87 elif isinstance(type_, RoutineType):
88 type_.inputs = set([resolve(w) for w in type_.inputs])
89 type_.outputs = set([resolve(w) for w in type_.outputs])
90 type_.trashes = set([resolve(w) for w in type_.trashes])
91
92 for defn in program.defns:
93 backpatch_constraint_labels(defn.location.type, lambda w: self.lookup(w))
94 for routine in program.routines:
95 backpatch_constraint_labels(routine.location.type, lambda w: self.lookup(w))
96
97 def resolve_fwd_reference(obj, field):
98 field_value = getattr(obj, field, None)
99 if isinstance(field_value, ForwardReference):
100 setattr(obj, field, self.lookup(field_value.name))
101 elif isinstance(field_value, IndexedRef):
102 if isinstance(field_value.ref, ForwardReference):
103 field_value.ref = self.lookup(field_value.ref.name)
104
105 for node in program.all_children():
106 if isinstance(node, SingleOp):
107 resolve_fwd_reference(node, 'location')
108 resolve_fwd_reference(node, 'src')
109 resolve_fwd_reference(node, 'dest')
75110
76111 # --- grammar productions
77112
101136 routines.append(routine)
102137 self.scanner.check_type('EOF')
103138
104 # now backpatch the executable types.
105 #for type_name, type_ in self.context.typedefs.items():
106 # type_.backpatch_constraint_labels(lambda w: self.lookup(w))
107 for defn in defns:
108 defn.location.type.backpatch_constraint_labels(lambda w: self.lookup(w))
109 for routine in routines:
110 routine.location.type.backpatch_constraint_labels(lambda w: self.lookup(w))
111
112139 program = Program(self.scanner.line_number, defns=defns, routines=routines)
113
114 def resolve_fwd_reference(obj, field):
115 field_value = getattr(obj, field, None)
116 if isinstance(field_value, ForwardReference):
117 setattr(obj, field, self.lookup(field_value.name))
118 elif isinstance(field_value, IndexedRef):
119 if isinstance(field_value.ref, ForwardReference):
120 field_value.ref = self.lookup(field_value.ref.name)
121
122 for node in program.all_children():
123 if isinstance(node, SingleOp):
124 resolve_fwd_reference(node, 'location')
125 resolve_fwd_reference(node, 'src')
126 resolve_fwd_reference(node, 'dest')
127
140 self.resolve_symbols(program)
128141 return program
129142
130143 def typedef(self):