diff --git a/src/sixtypical/parser.py b/src/sixtypical/parser.py index 896a4c9..f80616d 100644 --- a/src/sixtypical/parser.py +++ b/src/sixtypical/parser.py @@ -75,11 +75,7 @@ class Parser(object): return Program(defns=defns, routines=routines) def defn(self): - type_ = self.defn_type() - - self.scanner.check_type('identifier') - name = self.scanner.token - self.scanner.scan() + type_, name = self.defn_type_and_name() initial = None if self.scanner.consume(':'): @@ -111,30 +107,45 @@ class Parser(object): self.scanner.expect(']') return size - def defn_type(self): + def defn_type_and_name(self): if self.scanner.consume('byte'): - if self.scanner.consume('table'): - size = self.defn_size() - return TableType(TYPE_BYTE, size) - return TYPE_BYTE - elif self.scanner.consume('word'): - if self.scanner.consume('table'): - size = self.defn_size() - return TableType(TYPE_WORD, size) - return TYPE_WORD - elif self.scanner.consume('vector'): - (inputs, outputs, trashes) = self.constraints() - type_ = VectorType(inputs=inputs, outputs=outputs, trashes=trashes) + type_ = TYPE_BYTE if self.scanner.consume('table'): size = self.defn_size() type_ = TableType(type_, size) - return type_ + name = self.defn_name() + return type_, name + elif self.scanner.consume('word'): + type_ = TYPE_WORD + if self.scanner.consume('table'): + size = self.defn_size() + type_ = TableType(type_, size) + name = self.defn_name() + return type_, name + elif self.scanner.consume('vector'): + size = None + if self.scanner.consume('table'): + size = self.defn_size() + name = self.defn_name() + (inputs, outputs, trashes) = self.constraints() + type_ = VectorType(inputs=inputs, outputs=outputs, trashes=trashes) + if size is not None: + type_ = TableType(type_, size) + return type_, name elif self.scanner.consume('buffer'): size = self.defn_size() - return BufferType(size) + name = self.defn_name() + return BufferType(size), name else: self.scanner.expect('pointer') - return PointerType() + name = self.defn_name() + return PointerType(), name + + def defn_name(self): + self.scanner.check_type('identifier') + name = self.scanner.token + self.scanner.scan() + return name def constraints(self): inputs = set() diff --git a/tests/SixtyPical Analysis.md b/tests/SixtyPical Analysis.md index 5971ea5..20289aa 100644 --- a/tests/SixtyPical Analysis.md +++ b/tests/SixtyPical Analysis.md @@ -1554,11 +1554,10 @@ Read through a pointer. Routines are constants. You need not, and in fact cannot, specify a constant as an input to, an output of, or as a trashed value of a routine. - | vector + | vector vec | inputs x | outputs x | trashes z, n - | vec | | routine foo | inputs x @@ -1577,11 +1576,10 @@ as an input to, an output of, or as a trashed value of a routine. | } ? ConstantConstraintError: foo in main - | vector + | vector vec | inputs x | outputs x | trashes z, n - | vec | | routine foo | inputs x @@ -1599,11 +1597,10 @@ as an input to, an output of, or as a trashed value of a routine. | } ? ConstantConstraintError: foo in main - | vector + | vector vec | inputs x | outputs x | trashes z, n - | vec | | routine foo | inputs x @@ -1624,11 +1621,10 @@ as an input to, an output of, or as a trashed value of a routine. You can copy the address of a routine into a vector, if that vector is declared appropriately. - | vector + | vector vec | inputs x | outputs x | trashes z, n - | vec | | routine foo | inputs x @@ -1648,11 +1644,10 @@ declared appropriately. But not if the vector is declared inappropriately. - | vector + | vector vec | inputs y | outputs y | trashes z, n - | vec | | routine foo | inputs x @@ -1673,11 +1668,10 @@ But not if the vector is declared inappropriately. "Appropriately" means, if the routine affects no more than what is named in the input/output sets of the vector. - | vector + | vector vec | inputs a, x | outputs x | trashes a, z, n - | vec | | routine foo | inputs x @@ -1697,11 +1691,10 @@ in the input/output sets of the vector. Routines are read-only. - | vector + | vector vec | inputs x | outputs x | trashes z, n - | vec | | routine foo | inputs x @@ -1721,7 +1714,7 @@ Routines are read-only. Indirect call. - | vector outputs x trashes z, n foo + | vector foo outputs x trashes z, n | | routine bar outputs x trashes z, n { | ld x, 200 @@ -1735,7 +1728,7 @@ Indirect call. Calling the vector does indeed trash the things the vector says it does. - | vector trashes x, z, n foo + | vector foo trashes x, z, n | | routine bar trashes x, z, n { | ld x, 200 @@ -1835,7 +1828,7 @@ Can `goto` a routine that outputs or trashes less than the current routine. Indirect goto. - | vector outputs x trashes a, z, n foo + | vector foo outputs x trashes a, z, n | | routine bar outputs x trashes a, z, n { | ld x, 200 @@ -1850,9 +1843,8 @@ Indirect goto. Jumping through the vector does indeed trash, or output, the things the vector says it does. - | vector + | vector foo | trashes a, x, z, n - | foo | | routine bar | trashes a, x, z, n { @@ -1874,10 +1866,9 @@ vector says it does. | } ? UnmeaningfulReadError: x in main - | vector + | vector foo | outputs x | trashes a, z, n - | foo | | routine bar | outputs x @@ -1905,14 +1896,12 @@ vector says it does. Copying to and from a vector table. - | vector + | vector one | outputs x | trashes a, z, n - | one - | vector + | vector table[256] many | outputs x | 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 6d5262f..af10e26 100644 --- a/tests/SixtyPical Compilation.md +++ b/tests/SixtyPical Compilation.md @@ -568,9 +568,7 @@ Copy word to word table and back, with both `x` and `y` as indexes. Indirect call. - | vector outputs x - | trashes z, n - | foo + | vector foo outputs x trashes z, n | | routine bar outputs x trashes z, n { | ld x, 200 @@ -610,14 +608,12 @@ goto. Copying to and from a vector table. - | vector + | vector one | outputs x | trashes a, z, n - | one - | vector + | vector table[256] many | outputs x | trashes a, z, n - | table[256] many | | routine bar outputs x trashes a, z, n { | ld x, 200 diff --git a/tests/SixtyPical Execution.md b/tests/SixtyPical Execution.md index 8c1c942..50e3d7e 100644 --- a/tests/SixtyPical Execution.md +++ b/tests/SixtyPical Execution.md @@ -453,7 +453,7 @@ Copy literal word to word. Indirect call. - | vector outputs x trashes z, n foo + | vector foo outputs x trashes z, n | | routine bar outputs x trashes z, n { | ld x, 200 diff --git a/tests/SixtyPical Syntax.md b/tests/SixtyPical Syntax.md index 3bae409..8f046b6 100644 --- a/tests/SixtyPical Syntax.md +++ b/tests/SixtyPical Syntax.md @@ -135,10 +135,10 @@ User-defined memory addresses of different types. | byte byt | word wor - | vector trashes a vec + | vector vec trashes a | byte table[256] tab | word table[256] wtab - | vector trashes a table[256] vtab + | vector table[256] vtab trashes a | buffer[2048] buf | pointer ptr | @@ -314,11 +314,11 @@ Declaring byte and word table memory location. Declaring and calling a vector. - | vector + | vector cinv | inputs a | outputs x | trashes a, x, z, n - | cinv @ 788 + | @ 788 | | routine foo { | ld a, 0 @@ -333,11 +333,11 @@ Declaring and calling a vector. Only vectors can be decorated with constraints like that. - | byte + | byte cinv | inputs a | outputs x | trashes a, x, z, n - | cinv @ 788 + | @ 788 | | routine main { | } @@ -345,11 +345,11 @@ Only vectors can be decorated with constraints like that. Constraints set may only contain labels. - | vector + | vector cinv | inputs a | outputs 200 | trashes a, x, z, n - | cinv @ 788 + | @ 788 | | routine foo { | ld a, 0 @@ -364,11 +364,11 @@ Constraints set may only contain labels. A vector can name itself in its inputs, outputs, and trashes. - | vector + | vector cinv | inputs cinv, a | outputs cinv, x | trashes a, x, z, n - | cinv @ 788 + | @ 788 | | routine foo { | ld a, 0 @@ -384,11 +384,11 @@ A vector can name itself in its inputs, outputs, and trashes. A routine can be copied into a vector before the routine appears in the program, *however*, it must be marked as such with the keyword `forward`. - | vector + | vector cinv | inputs cinv, a | outputs cinv, x | trashes a, x, z, n - | cinv @ 788 + | @ 788 | routine main { | with interrupts off { | copy foo, cinv @@ -400,11 +400,11 @@ A routine can be copied into a vector before the routine appears in the program, | } ? SyntaxError: Undefined symbol - | vector + | vector cinv | inputs cinv, a | outputs cinv, x | trashes a, x, z, n - | cinv @ 788 + | @ 788 | routine main { | with interrupts off { | copy forward foo, cinv