git @ Cat's Eye Technologies SixtyPical / bbd3a84
Retain silly 'name inside the type' syntax (a la C) for now. Chris Pressey 3 years ago
5 changed file(s) with 85 addition(s) and 89 deletion(s). Raw diff Collapse all Expand all
7474 return Program(defns=defns, routines=routines)
7575
7676 def defn(self):
77 type_ = self.defn_type()
78
79 self.scanner.check_type('identifier')
80 name = self.scanner.token
81 self.scanner.scan()
77 type_, name = self.defn_type_and_name()
8278
8379 initial = None
8480 if self.scanner.consume(':'):
110106 self.scanner.expect(']')
111107 return size
112108
113 def defn_type(self):
109 def defn_type_and_name(self):
114110 if self.scanner.consume('byte'):
115 if self.scanner.consume('table'):
116 size = self.defn_size()
117 return TableType(TYPE_BYTE, size)
118 return TYPE_BYTE
119 elif self.scanner.consume('word'):
120 if self.scanner.consume('table'):
121 size = self.defn_size()
122 return TableType(TYPE_WORD, size)
123 return TYPE_WORD
124 elif self.scanner.consume('vector'):
125 (inputs, outputs, trashes) = self.constraints()
126 type_ = VectorType(inputs=inputs, outputs=outputs, trashes=trashes)
111 type_ = TYPE_BYTE
127112 if self.scanner.consume('table'):
128113 size = self.defn_size()
129114 type_ = TableType(type_, size)
130 return type_
115 name = self.defn_name()
116 return type_, name
117 elif self.scanner.consume('word'):
118 type_ = TYPE_WORD
119 if self.scanner.consume('table'):
120 size = self.defn_size()
121 type_ = TableType(type_, size)
122 name = self.defn_name()
123 return type_, name
124 elif self.scanner.consume('vector'):
125 size = None
126 if self.scanner.consume('table'):
127 size = self.defn_size()
128 name = self.defn_name()
129 (inputs, outputs, trashes) = self.constraints()
130 type_ = VectorType(inputs=inputs, outputs=outputs, trashes=trashes)
131 if size is not None:
132 type_ = TableType(type_, size)
133 return type_, name
131134 elif self.scanner.consume('buffer'):
132135 size = self.defn_size()
133 return BufferType(size)
136 name = self.defn_name()
137 return BufferType(size), name
134138 else:
135139 self.scanner.expect('pointer')
136 return PointerType()
140 name = self.defn_name()
141 return PointerType(), name
142
143 def defn_name(self):
144 self.scanner.check_type('identifier')
145 name = self.scanner.token
146 self.scanner.scan()
147 return name
137148
138149 def constraints(self):
139150 inputs = set()
15531553 Routines are constants. You need not, and in fact cannot, specify a constant
15541554 as an input to, an output of, or as a trashed value of a routine.
15551555
1556 | vector
1557 | inputs x
1558 | outputs x
1559 | trashes z, n
1560 | vec
1556 | vector vec
1557 | inputs x
1558 | outputs x
1559 | trashes z, n
15611560 |
15621561 | routine foo
15631562 | inputs x
15761575 | }
15771576 ? ConstantConstraintError: foo in main
15781577
1579 | vector
1580 | inputs x
1581 | outputs x
1582 | trashes z, n
1583 | vec
1578 | vector vec
1579 | inputs x
1580 | outputs x
1581 | trashes z, n
15841582 |
15851583 | routine foo
15861584 | inputs x
15981596 | }
15991597 ? ConstantConstraintError: foo in main
16001598
1601 | vector
1602 | inputs x
1603 | outputs x
1604 | trashes z, n
1605 | vec
1599 | vector vec
1600 | inputs x
1601 | outputs x
1602 | trashes z, n
16061603 |
16071604 | routine foo
16081605 | inputs x
16231620 You can copy the address of a routine into a vector, if that vector is
16241621 declared appropriately.
16251622
1626 | vector
1627 | inputs x
1628 | outputs x
1629 | trashes z, n
1630 | vec
1623 | vector vec
1624 | inputs x
1625 | outputs x
1626 | trashes z, n
16311627 |
16321628 | routine foo
16331629 | inputs x
16471643
16481644 But not if the vector is declared inappropriately.
16491645
1650 | vector
1646 | vector vec
16511647 | inputs y
16521648 | outputs y
16531649 | trashes z, n
1654 | vec
16551650 |
16561651 | routine foo
16571652 | inputs x
16721667 "Appropriately" means, if the routine affects no more than what is named
16731668 in the input/output sets of the vector.
16741669
1675 | vector
1670 | vector vec
16761671 | inputs a, x
16771672 | outputs x
16781673 | trashes a, z, n
1679 | vec
16801674 |
16811675 | routine foo
16821676 | inputs x
16961690
16971691 Routines are read-only.
16981692
1699 | vector
1700 | inputs x
1701 | outputs x
1702 | trashes z, n
1703 | vec
1693 | vector vec
1694 | inputs x
1695 | outputs x
1696 | trashes z, n
17041697 |
17051698 | routine foo
17061699 | inputs x
17201713
17211714 Indirect call.
17221715
1723 | vector outputs x trashes z, n foo
1716 | vector foo outputs x trashes z, n
17241717 |
17251718 | routine bar outputs x trashes z, n {
17261719 | ld x, 200
17341727
17351728 Calling the vector does indeed trash the things the vector says it does.
17361729
1737 | vector trashes x, z, n foo
1730 | vector foo trashes x, z, n
17381731 |
17391732 | routine bar trashes x, z, n {
17401733 | ld x, 200
18341827
18351828 Indirect goto.
18361829
1837 | vector outputs x trashes a, z, n foo
1830 | vector foo outputs x trashes a, z, n
18381831 |
18391832 | routine bar outputs x trashes a, z, n {
18401833 | ld x, 200
18491842 Jumping through the vector does indeed trash, or output, the things the
18501843 vector says it does.
18511844
1852 | vector
1845 | vector foo
18531846 | trashes a, x, z, n
1854 | foo
18551847 |
18561848 | routine bar
18571849 | trashes a, x, z, n {
18731865 | }
18741866 ? UnmeaningfulReadError: x in main
18751867
1876 | vector
1877 | outputs x
1878 | trashes a, z, n
1879 | foo
1868 | vector foo
1869 | outputs x
1870 | trashes a, z, n
18801871 |
18811872 | routine bar
18821873 | outputs x
19041895
19051896 Copying to and from a vector table.
19061897
1907 | vector
1908 | outputs x
1909 | trashes a, z, n
1910 | one
1911 | vector
1912 | outputs x
1913 | trashes a, z, n
1914 | table[256] many
1898 | vector one
1899 | outputs x
1900 | trashes a, z, n
1901 | vector table[256] many
1902 | outputs x
1903 | trashes a, z, n
19151904 |
19161905 | routine bar outputs x trashes a, z, n {
19171906 | ld x, 200
567567
568568 Indirect call.
569569
570 | vector outputs x
571 | trashes z, n
572 | foo
570 | vector foo outputs x trashes z, n
573571 |
574572 | routine bar outputs x trashes z, n {
575573 | ld x, 200
609607
610608 Copying to and from a vector table.
611609
612 | vector
610 | vector one
613611 | outputs x
614612 | trashes a, z, n
615 | one
616 | vector
613 | vector table[256] many
617614 | outputs x
618615 | trashes a, z, n
619 | table[256] many
620616 |
621617 | routine bar outputs x trashes a, z, n {
622618 | ld x, 200
452452
453453 Indirect call.
454454
455 | vector outputs x trashes z, n foo
455 | vector foo outputs x trashes z, n
456456 |
457457 | routine bar outputs x trashes z, n {
458458 | ld x, 200
134134
135135 | byte byt
136136 | word wor
137 | vector trashes a vec
137 | vector vec trashes a
138138 | byte table[256] tab
139139 | word table[256] wtab
140 | vector trashes a table[256] vtab
140 | vector table[256] vtab trashes a
141141 | buffer[2048] buf
142142 | pointer ptr
143143 |
313313
314314 Declaring and calling a vector.
315315
316 | vector
316 | vector cinv
317317 | inputs a
318318 | outputs x
319319 | trashes a, x, z, n
320 | cinv @ 788
320 | @ 788
321321 |
322322 | routine foo {
323323 | ld a, 0
332332
333333 Only vectors can be decorated with constraints like that.
334334
335 | byte
335 | byte cinv
336336 | inputs a
337337 | outputs x
338338 | trashes a, x, z, n
339 | cinv @ 788
339 | @ 788
340340 |
341341 | routine main {
342342 | }
344344
345345 Constraints set may only contain labels.
346346
347 | vector
347 | vector cinv
348348 | inputs a
349349 | outputs 200
350350 | trashes a, x, z, n
351 | cinv @ 788
351 | @ 788
352352 |
353353 | routine foo {
354354 | ld a, 0
363363
364364 A vector can name itself in its inputs, outputs, and trashes.
365365
366 | vector
366 | vector cinv
367367 | inputs cinv, a
368368 | outputs cinv, x
369369 | trashes a, x, z, n
370 | cinv @ 788
370 | @ 788
371371 |
372372 | routine foo {
373373 | ld a, 0
383383 A routine can be copied into a vector before the routine appears in the program,
384384 *however*, it must be marked as such with the keyword `forward`.
385385
386 | vector
386 | vector cinv
387387 | inputs cinv, a
388388 | outputs cinv, x
389389 | trashes a, x, z, n
390 | cinv @ 788
390 | @ 788
391391 | routine main {
392392 | with interrupts off {
393393 | copy foo, cinv
399399 | }
400400 ? SyntaxError: Undefined symbol
401401
402 | vector
402 | vector cinv
403403 | inputs cinv, a
404404 | outputs cinv, x
405405 | trashes a, x, z, n
406 | cinv @ 788
406 | @ 788
407407 | routine main {
408408 | with interrupts off {
409409 | copy forward foo, cinv