From 70ecd52e8ad2be5cafcdd203307b4bb4b4139687 Mon Sep 17 00:00:00 2001 From: Chris Pressey Date: Tue, 6 Feb 2018 11:34:21 +0000 Subject: [PATCH] Retain some syntax compatibility: make `table` a postfix operator. --- src/sixtypical/parser.py | 36 +++++++++++++++--------- tests/SixtyPical Analysis.md | 50 ++++++++++++++++----------------- tests/SixtyPical Compilation.md | 18 ++++++------ tests/SixtyPical Syntax.md | 16 +++++------ 4 files changed, 65 insertions(+), 55 deletions(-) diff --git a/src/sixtypical/parser.py b/src/sixtypical/parser.py index 5cff4c3..2b42017 100644 --- a/src/sixtypical/parser.py +++ b/src/sixtypical/parser.py @@ -130,32 +130,42 @@ class Parser(object): return size def defn_type(self): - if self.scanner.consume('byte'): - return TYPE_BYTE - elif self.scanner.consume('word'): - return TYPE_WORD - elif self.scanner.consume('table'): - size = self.defn_size() + type_ = None + + if self.scanner.consume('('): type_ = self.defn_type() - return TableType(type_, size) + self.scanner.expect(')') + return type_ + + if self.scanner.consume('byte'): + type_ = TYPE_BYTE + elif self.scanner.consume('word'): + type_ = TYPE_WORD elif self.scanner.consume('vector'): type_ = self.defn_type() - # TODO: assert that it's a routine type - return VectorType(type_) + if not isinstance(type_, RoutineType): + raise SyntaxError("Vectors can only be of a routine, not %r" % type_) + type_ = VectorType(type_) elif self.scanner.consume('routine'): (inputs, outputs, trashes) = self.constraints() - return RoutineType(inputs=inputs, outputs=outputs, trashes=trashes) + type_ = RoutineType(inputs=inputs, outputs=outputs, trashes=trashes) elif self.scanner.consume('buffer'): size = self.defn_size() - return BufferType(size) + type_ = BufferType(size) elif self.scanner.consume('pointer'): - return PointerType() + type_ = PointerType() else: type_name = self.scanner.token self.scanner.scan() if type_name not in self.typedefs: raise SyntaxError("Undefined type '%s'" % type_name) - return self.typedefs[type_name] + type_ = self.typedefs[type_name] + + if self.scanner.consume('table'): + size = self.defn_size() + type_ = TableType(type_, size) + + return type_ def defn_name(self): self.scanner.check_type('identifier') diff --git a/tests/SixtyPical Analysis.md b/tests/SixtyPical Analysis.md index ecde18e..0efa53d 100644 --- a/tests/SixtyPical Analysis.md +++ b/tests/SixtyPical Analysis.md @@ -243,7 +243,7 @@ Can't `st` a `word` type. Storing to a table, you must use an index. | byte one - | table[256] byte many + | byte table[256] many | | routine main | outputs one @@ -256,7 +256,7 @@ Storing to a table, you must use an index. = ok | byte one - | table[256] byte many + | byte table[256] many | | routine main | outputs many @@ -269,7 +269,7 @@ Storing to a table, you must use an index. ? TypeMismatchError | byte one - | table[256] byte many + | byte table[256] many | | routine main | outputs one @@ -282,7 +282,7 @@ Storing to a table, you must use an index. ? TypeMismatchError | byte one - | table[256] byte many + | byte table[256] many | | routine main | outputs many @@ -297,7 +297,7 @@ Storing to a table, you must use an index. The index must be initialized. | byte one - | table[256] byte many + | byte table[256] many | | routine main | outputs many @@ -334,7 +334,7 @@ Reading from a table, you must use an index. | } ? TypeMismatchError - | table[256] byte many + | byte table[256] many | | routine main | outputs many @@ -347,7 +347,7 @@ Reading from a table, you must use an index. | } ? TypeMismatchError - | table[256] byte many + | byte table[256] many | | routine main | outputs many @@ -360,7 +360,7 @@ Reading from a table, you must use an index. | } = ok - | table[256] byte many + | byte table[256] many | | routine main | inputs many @@ -374,7 +374,7 @@ Reading from a table, you must use an index. The index must be initialized. - | table[256] byte many + | byte table[256] many | | routine main | inputs many @@ -388,7 +388,7 @@ The index must be initialized. Copying to and from a word table. | word one - | table[256] word many + | word table[256] many | | routine main | inputs one, many @@ -402,7 +402,7 @@ Copying to and from a word table. = ok | word one - | table[256] word many + | byte table[256] many | | routine main | inputs one, many @@ -415,7 +415,7 @@ Copying to and from a word table. ? TypeMismatchError | word one - | table[256] word many + | byte table[256] many | | routine main | inputs one, many @@ -429,7 +429,7 @@ Copying to and from a word table. You can also copy a literal word to a word table. - | table[256] word many + | word table[256] many | | routine main | inputs many @@ -1949,10 +1949,10 @@ A vector can be copied into a vector table. | outputs x | trashes a, z, n | one - | table[256] vector routine + | vector (routine | outputs x - | trashes a, z, n - | many + | trashes a, z, n) + | table[256] many | | routine bar outputs x trashes a, z, n { | ld x, 200 @@ -1975,10 +1975,10 @@ A vector can be copied out of a vector table. | outputs x | trashes a, z, n | one - | table[256] vector routine + | vector (routine | outputs x - | trashes a, z, n - | many + | trashes a, z, n) + | table[256] many | | routine bar outputs x trashes a, z, n { | ld x, 200 @@ -1997,10 +1997,10 @@ A vector can be copied out of a vector table. A routine can be copied into a vector table. - | table[256] vector routine + | vector (routine | outputs x - | trashes a, z, n - | many + | trashes a, z, n) + | table[256] many | | routine bar outputs x trashes a, z, n { | ld x, 200 @@ -2018,10 +2018,10 @@ A routine can be copied into a vector table. A vector in a vector table cannot be directly called. - | table[256] vector routine + | vector (routine | outputs x - | trashes a, z, n - | many + | trashes a, z, n) + | table[256] many | | routine bar outputs x trashes a, z, n { | ld x, 200 diff --git a/tests/SixtyPical Compilation.md b/tests/SixtyPical Compilation.md index 3effdf2..4ef66f1 100644 --- a/tests/SixtyPical Compilation.md +++ b/tests/SixtyPical Compilation.md @@ -139,7 +139,7 @@ Word memory locations with explicit address, initial value. Initialized byte table. Bytes allocated, but beyond the string, are 0's. - | table[8] byte message : "WHAT?" + | byte table[8] message : "WHAT?" | | routine main | inputs message @@ -352,7 +352,7 @@ The body of `repeat forever` can be empty. Indexed access. | byte one - | table[256] byte many + | byte table[256] many | | routine main | outputs many @@ -371,8 +371,8 @@ Indexed access. Byte tables take up 256 bytes in memory. - | table[256] byte tab1 - | table[256] byte tab2 + | byte table[256] tab1 + | byte table[256] tab2 | | routine main | inputs tab1 @@ -458,7 +458,7 @@ Copy literal word to word. You can also copy a literal word to a word table. - | table[256] word many + | word table[256] many | | routine main | inputs many @@ -527,7 +527,7 @@ Copy routine to vector, inside an `interrupts off` block. Copy word to word table and back, with both `x` and `y` as indexes. | word one - | table[256] word many + | word table[256] many | | routine main | inputs one, many @@ -612,10 +612,10 @@ Copying to and from a vector table. | outputs x | trashes a, z, n | one - | table[256] vector routine + | vector (routine | outputs x - | trashes a, z, n - | many + | trashes a, z, n) + | table[256] many | | routine bar outputs x trashes a, z, n { | ld x, 200 diff --git a/tests/SixtyPical Syntax.md b/tests/SixtyPical Syntax.md index 1410942..a178386 100644 --- a/tests/SixtyPical Syntax.md +++ b/tests/SixtyPical Syntax.md @@ -145,9 +145,9 @@ User-defined memory addresses of different types. Tables of different types. - | table[256] byte tab - | table[256] word wtab - | table[256] vector routine trashes a vtab + | byte table[256] tab + | word table[256] wtab + | vector (routine trashes a) table[256] vtab | | routine main { | } @@ -156,7 +156,7 @@ Tables of different types. Typedefs of different types. | typedef byte octet - | typedef table[256] octet twokay + | typedef octet table[256] twokay | typedef routine trashes a game_routine | vector game_routine start_game | @@ -205,7 +205,7 @@ Cannot have both initial value and explicit address. User-defined locations of other types. - | table[256] byte screen @ 1024 + | byte table[256] screen @ 1024 | word r1 | word r2 @ 60000 | word r3 : 2000 @@ -216,7 +216,7 @@ User-defined locations of other types. Initialized byte table. - | table[28] byte message : "WHAT DO YOU WANT TO DO NEXT?" + | byte table[32] message : "WHAT DO YOU WANT TO DO NEXT?" | | routine main { | } @@ -318,7 +318,7 @@ Can't define two routines with the same name. Declaring byte and word table memory location. - | table[256] byte tab + | byte table[256] tab | | routine main { | ld x, 0 @@ -329,7 +329,7 @@ Declaring byte and word table memory location. = ok | word one - | table[256] word many + | word table[256] many | | routine main { | ld x, 0