git @ Cat's Eye Technologies SixtyPical / 689ed37
Beginnings of implementing word tables. One test still fails. Chris Pressey 3 years ago
5 changed file(s) with 108 addition(s) and 18 deletion(s). Raw diff Collapse all Expand all
11
22 from sixtypical.ast import Program, Routine, Block, Instr
33 from sixtypical.model import (
4 TYPE_BYTE, TYPE_WORD, TYPE_BYTE_TABLE, BufferType, PointerType, VectorType, ExecutableType,
5 ConstantRef, LocationRef, IndirectRef, AddressRef,
4 TYPE_BYTE, TYPE_WORD, TYPE_BYTE_TABLE, TYPE_WORD_TABLE, BufferType, PointerType, VectorType, ExecutableType,
5 ConstantRef, LocationRef, IndirectRef, IndexedRef, AddressRef,
66 REG_A, REG_Y, FLAG_Z, FLAG_N, FLAG_V, FLAG_C
77 )
88
347347 pass
348348 else:
349349 raise TypeMismatchError((src, dest))
350
351 elif isinstance(src, LocationRef) and isinstance(dest, IndexedRef):
352 if src.type == TYPE_WORD and dest.ref.type == TYPE_WORD_TABLE:
353 pass
354 else:
355 raise TypeMismatchError((src, dest))
356
350357 elif isinstance(src, (LocationRef, ConstantRef)) and isinstance(dest, LocationRef):
351358 if src.type == dest.type:
352359 pass
105105 self.ref = ref
106106
107107 def __eq__(self, other):
108 return self.ref == other.ref
108 return isinstance(other, self.__class__) and self.ref == other.ref
109109
110110 def __hash__(self):
111111 return hash(self.__class__.name) ^ hash(self.ref)
116116 @property
117117 def name(self):
118118 return '[{}]+y'.format(self.ref.name)
119
120 def is_constant(self):
121 return False
122
123
124 class IndexedRef(Ref):
125 def __init__(self, ref, index):
126 self.ref = ref
127 self.index = index
128
129 def __eq__(self, other):
130 return isinstance(other, self.__class__) and self.ref == other.ref and self.index == other.index
131
132 def __hash__(self):
133 return hash(self.__class__.name) ^ hash(self.ref) ^ hash(self.index)
134
135 def __repr__(self):
136 return '%s(%r, %r)' % (self.__class__.__name__, self.ref, self.index)
137
138 @property
139 def name(self):
140 return '{}+{}'.format(self.ref.name, self.index.name)
119141
120142 def is_constant(self):
121143 return False
33 from sixtypical.model import (
44 TYPE_BIT, TYPE_BYTE, TYPE_BYTE_TABLE, TYPE_WORD, TYPE_WORD_TABLE,
55 RoutineType, VectorType, ExecutableType, BufferType, PointerType,
6 LocationRef, ConstantRef, IndirectRef, AddressRef,
6 LocationRef, ConstantRef, IndirectRef, IndexedRef, AddressRef,
77 )
88 from sixtypical.scanner import Scanner
99
174174 loc = self.locexpr()
175175 return AddressRef(loc)
176176 else:
177 return self.locexpr()
177 loc = self.locexpr()
178 index = None
179 if self.scanner.consume('+'):
180 index = self.locexpr()
181 loc = IndexedRef(loc, index)
182 return loc
178183
179184 def block(self):
180185 instrs = []
205205 | st 0, lives
206206 | }
207207 ? ForbiddenWriteError: lives in main
208
209 Can't `st` a `word` type.
210
211 | word foo
212 |
213 | routine main
214 | outputs foo
215 | trashes a, n, z
216 | {
217 | ld a, 0
218 | st a, foo
219 | }
220 ? TypeMismatchError: a and foo in main
221
222 ### tables ###
208223
209224 Storing to a table, you must use an index, and vice-versa.
210225
312327 | }
313328 = ok
314329
315 Can't `st` a `word` type.
316
317 | word foo
318 |
319 | routine main
320 | outputs foo
321 | trashes a, n, z
322 | {
323 | ld a, 0
324 | st a, foo
325 | }
326 ? TypeMismatchError: a and foo in main
330 Copying to and from a word table.
331
332 | word one
333 | word table many
334 |
335 | routine main
336 | inputs one, many
337 | outputs one, many
338 | trashes a, x, n, z
339 | {
340 | ld x, 0
341 | copy one, many + x
342 | copy many + x, one
343 | }
344 = ok
345
346 | word one
347 | word table many
348 |
349 | routine main
350 | inputs one, many
351 | outputs one, many
352 | trashes a, x, n, z
353 | {
354 | ld x, 0
355 | copy one, many
356 | }
357 ? TypeMismatchError
358
359 | word one
360 | word table many
361 |
362 | routine main
363 | inputs one, many
364 | outputs one, many
365 | trashes a, x, n, z
366 | {
367 | ld x, 0
368 | copy one + x, many
369 | }
370 ? TypeMismatchError
327371
328372 ### add ###
329373
128128 | word wor
129129 | vector vec
130130 | byte table tab
131 | word table wtab
131132 | buffer[2048] buf
132133 | pointer ptr
133134 |
249250 | }
250251 ? SyntaxError
251252
252 Declaring a byte table memory location.
253 Declaring byte and word table memory location.
253254
254255 | byte table tab
255256 |
258259 | ld y, 0
259260 | ld a, tab + x
260261 | st a, tab + y
262 | }
263 = ok
264
265 | word one
266 | word table many
267 |
268 | routine main {
269 | ld x, 0
270 | copy one, many + x
271 | copy word 0, many + x
272 | copy many + x, one
261273 | }
262274 = ok
263275