git @ Cat's Eye Technologies SixtyPical / 42438dd
Beginning of: buffers and pointers. Chris Pressey 4 years ago
5 changed file(s) with 66 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
2525
2626 * X table (256 entries, each holding a value of type X)
2727
28 This constructor can only be applied to bytes or words.
28 This constructor can only be applied to one type, `byte`.
2929
3030 Memory locations
3131 ----------------
5555 """This memory location contains the address of a routine."""
5656 def __init__(self, **kwargs):
5757 super(VectorType, self).__init__('vector', **kwargs)
58
59
60 class BufferType(Type):
61 def __init__(self, size):
62 self.size = size
63 self.name = 'buffer[%s]' % self.size
64
65
66 class PointerType(Type):
67 def __init__(self):
68 self.name = 'pointer'
5869
5970
6071 class Ref(object):
22 from sixtypical.ast import Program, Defn, Routine, Block, Instr
33 from sixtypical.model import (
44 TYPE_BIT, TYPE_BYTE, TYPE_BYTE_TABLE, TYPE_WORD, TYPE_WORD_TABLE,
5 RoutineType, VectorType, ExecutableType,
5 RoutineType, VectorType, ExecutableType, BufferType, PointerType,
66 LocationRef, ConstantRef
77 )
88 from sixtypical.scanner import Scanner
3131 def program(self):
3232 defns = []
3333 routines = []
34 while self.scanner.on('byte', 'word', 'vector'):
34 while self.scanner.on('byte', 'word', 'vector', 'buffer', 'pointer'):
3535 defn = self.defn()
3636 name = defn.name
3737 if name in self.symbols:
4949 return Program(defns=defns, routines=routines)
5050
5151 def defn(self):
52 type = None
53 if self.scanner.consume('byte'):
54 type = TYPE_BYTE
55 if self.scanner.consume('table'):
56 type = TYPE_BYTE_TABLE
57 elif self.scanner.consume('word'):
58 type = TYPE_WORD
59 if self.scanner.consume('table'):
60 type = TYPE_WORD_TABLE
61 else:
62 self.scanner.expect('vector')
63 type = 'vector' # will be resolved to a Type below
52 type_ = self.defn_type()
53
6454 self.scanner.check_type('identifier')
6555 name = self.scanner.token
6656 self.scanner.scan()
6757
6858 (inputs, outputs, trashes) = self.constraints()
69 if type == 'vector':
70 type = VectorType(inputs=inputs, outputs=outputs, trashes=trashes)
59 if type_ == 'vector':
60 type_ = VectorType(inputs=inputs, outputs=outputs, trashes=trashes)
7161 elif inputs or outputs or trashes:
7262 raise SyntaxError("Cannot apply constraints to non-vector type")
7363
8676 if initial is not None and addr is not None:
8777 raise SyntaxError("Definition cannot have both initial value and explicit address")
8878
89 location = LocationRef(type, name)
79 location = LocationRef(type_, name)
9080
9181 return Defn(name=name, addr=addr, initial=initial, location=location)
82
83 def defn_type(self):
84 if self.scanner.consume('byte'):
85 if self.scanner.consume('table'):
86 return TYPE_BYTE_TABLE
87 return TYPE_BYTE
88 elif self.scanner.consume('word'):
89 if self.scanner.consume('table'):
90 return TYPE_WORD_TABLE
91 return TYPE_WORD
92 elif self.scanner.consume('vector'):
93 return 'vector' # will be resolved to a Type by caller
94 elif self.scanner.consume('buffer'):
95 self.scanner.expect('[')
96 self.scanner.check_type('integer literal')
97 size = int(self.scanner.token)
98 self.scanner.scan()
99 self.scanner.expect(']')
100 return BufferType(size)
101 else:
102 self.scanner.expect('pointer')
103 return PointerType()
92104
93105 def constraints(self):
94106 inputs = set()
2828 self.token = None
2929 self.type = 'EOF'
3030 return
31 if self.scan_pattern(r'\,|\@|\+|\:|\<|\>|\{|\}', 'operator'):
31 if self.scan_pattern(r'\,|\@|\+|\:|\<|\>|\{|\}|\[|\]', 'operator'):
3232 return
3333 if self.scan_pattern(r'\d+', 'integer literal'):
3434 return
122122 | }
123123 = ok
124124
125 User-defined memory addresses of different types.
126
127 | byte byt
128 | word wor
129 | vector vec
130 | byte table tab
131 |
132 | routine main {
133 | }
134 = ok
135
136 Buffer and pointer types.
137
138 | byte byt
139 | word wor
140 | vector vec
141 | byte table tab
142 | buffer[2048] buf
143 | pointer ptr
144 |
145 | routine main {
146 | }
147 = ok
148
125149 Explicit memory address.
126150
127151 | byte screen @ 1024