1
0
mirror of https://github.com/catseye/SixtyPical.git synced 2025-02-26 22:29:03 +00:00

Beginnings of implementing word tables. One test still fails.

This commit is contained in:
Chris Pressey 2017-12-08 13:41:48 +00:00
parent ad8e0647a4
commit 689ed37f2e
5 changed files with 103 additions and 13 deletions

View File

@ -2,8 +2,8 @@
from sixtypical.ast import Program, Routine, Block, Instr from sixtypical.ast import Program, Routine, Block, Instr
from sixtypical.model import ( from sixtypical.model import (
TYPE_BYTE, TYPE_WORD, TYPE_BYTE_TABLE, BufferType, PointerType, VectorType, ExecutableType, TYPE_BYTE, TYPE_WORD, TYPE_BYTE_TABLE, TYPE_WORD_TABLE, BufferType, PointerType, VectorType, ExecutableType,
ConstantRef, LocationRef, IndirectRef, AddressRef, ConstantRef, LocationRef, IndirectRef, IndexedRef, AddressRef,
REG_A, REG_Y, FLAG_Z, FLAG_N, FLAG_V, FLAG_C REG_A, REG_Y, FLAG_Z, FLAG_N, FLAG_V, FLAG_C
) )
@ -348,6 +348,13 @@ class Analyzer(object):
pass pass
else: else:
raise TypeMismatchError((src, dest)) raise TypeMismatchError((src, dest))
elif isinstance(src, LocationRef) and isinstance(dest, IndexedRef):
if src.type == TYPE_WORD and dest.ref.type == TYPE_WORD_TABLE:
pass
else:
raise TypeMismatchError((src, dest))
elif isinstance(src, (LocationRef, ConstantRef)) and isinstance(dest, LocationRef): elif isinstance(src, (LocationRef, ConstantRef)) and isinstance(dest, LocationRef):
if src.type == dest.type: if src.type == dest.type:
pass pass

View File

@ -106,7 +106,7 @@ class IndirectRef(Ref):
self.ref = ref self.ref = ref
def __eq__(self, other): def __eq__(self, other):
return self.ref == other.ref return isinstance(other, self.__class__) and self.ref == other.ref
def __hash__(self): def __hash__(self):
return hash(self.__class__.name) ^ hash(self.ref) return hash(self.__class__.name) ^ hash(self.ref)
@ -122,6 +122,28 @@ class IndirectRef(Ref):
return False return False
class IndexedRef(Ref):
def __init__(self, ref, index):
self.ref = ref
self.index = index
def __eq__(self, other):
return isinstance(other, self.__class__) and self.ref == other.ref and self.index == other.index
def __hash__(self):
return hash(self.__class__.name) ^ hash(self.ref) ^ hash(self.index)
def __repr__(self):
return '%s(%r, %r)' % (self.__class__.__name__, self.ref, self.index)
@property
def name(self):
return '{}+{}'.format(self.ref.name, self.index.name)
def is_constant(self):
return False
class AddressRef(Ref): class AddressRef(Ref):
def __init__(self, ref): def __init__(self, ref):
self.ref = ref self.ref = ref

View File

@ -4,7 +4,7 @@ from sixtypical.ast import Program, Defn, Routine, Block, Instr
from sixtypical.model import ( from sixtypical.model import (
TYPE_BIT, TYPE_BYTE, TYPE_BYTE_TABLE, TYPE_WORD, TYPE_WORD_TABLE, TYPE_BIT, TYPE_BYTE, TYPE_BYTE_TABLE, TYPE_WORD, TYPE_WORD_TABLE,
RoutineType, VectorType, ExecutableType, BufferType, PointerType, RoutineType, VectorType, ExecutableType, BufferType, PointerType,
LocationRef, ConstantRef, IndirectRef, AddressRef, LocationRef, ConstantRef, IndirectRef, IndexedRef, AddressRef,
) )
from sixtypical.scanner import Scanner from sixtypical.scanner import Scanner
@ -175,7 +175,12 @@ class Parser(object):
loc = self.locexpr() loc = self.locexpr()
return AddressRef(loc) return AddressRef(loc)
else: else:
return self.locexpr() loc = self.locexpr()
index = None
if self.scanner.consume('+'):
index = self.locexpr()
loc = IndexedRef(loc, index)
return loc
def block(self): def block(self):
instrs = [] instrs = []

View File

@ -207,6 +207,21 @@ Can't `st` to a memory location that doesn't appear in (outputs trashes).
| } | }
? ForbiddenWriteError: lives in main ? ForbiddenWriteError: lives in main
Can't `st` a `word` type.
| word foo
|
| routine main
| outputs foo
| trashes a, n, z
| {
| ld a, 0
| st a, foo
| }
? TypeMismatchError: a and foo in main
### tables ###
Storing to a table, you must use an index, and vice-versa. Storing to a table, you must use an index, and vice-versa.
| byte one | byte one
@ -313,18 +328,47 @@ Reading from a table, you must use an index, and vice-versa.
| } | }
= ok = ok
Can't `st` a `word` type. Copying to and from a word table.
| word foo | word one
| word table many
| |
| routine main | routine main
| outputs foo | inputs one, many
| trashes a, n, z | outputs one, many
| trashes a, x, n, z
| { | {
| ld a, 0 | ld x, 0
| st a, foo | copy one, many + x
| copy many + x, one
| } | }
? TypeMismatchError: a and foo in main = ok
| word one
| word table many
|
| routine main
| inputs one, many
| outputs one, many
| trashes a, x, n, z
| {
| ld x, 0
| copy one, many
| }
? TypeMismatchError
| word one
| word table many
|
| routine main
| inputs one, many
| outputs one, many
| trashes a, x, n, z
| {
| ld x, 0
| copy one + x, many
| }
? TypeMismatchError
### add ### ### add ###

View File

@ -129,6 +129,7 @@ User-defined memory addresses of different types.
| word wor | word wor
| vector vec | vector vec
| byte table tab | byte table tab
| word table wtab
| buffer[2048] buf | buffer[2048] buf
| pointer ptr | pointer ptr
| |
@ -250,7 +251,7 @@ Can't define two routines with the same name.
| } | }
? SyntaxError ? SyntaxError
Declaring a byte table memory location. Declaring byte and word table memory location.
| byte table tab | byte table tab
| |
@ -262,6 +263,17 @@ Declaring a byte table memory location.
| } | }
= ok = ok
| word one
| word table many
|
| routine main {
| ld x, 0
| copy one, many + x
| copy word 0, many + x
| copy many + x, one
| }
= ok
Declaring and calling a vector. Declaring and calling a vector.
| vector cinv | vector cinv