git @ Cat's Eye Technologies SixtyPical / 74f8675
Start of typedef support. Chris Pressey 3 years ago
2 changed file(s) with 42 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
1818 def __init__(self, text):
1919 self.scanner = Scanner(text)
2020 self.symbols = {} # token -> SymEntry
21 self.typedefs = {} # token -> Type AST
2122 for token in ('a', 'x', 'y'):
2223 self.symbols[token] = SymEntry(None, LocationRef(TYPE_BYTE, token))
2324 for token in ('c', 'z', 'n', 'v'):
3435 def program(self):
3536 defns = []
3637 routines = []
37 while self.scanner.on('byte', 'word', 'table', 'vector', 'buffer', 'pointer'): # 'routine',
38 while self.scanner.on('typedef'):
39 typedef = self.typedef()
40 typenames = ['byte', 'word', 'table', 'vector', 'buffer', 'pointer'] # 'routine',
41 typenames.extend(self.typedefs.keys())
42 while self.scanner.on(*typenames):
3843 defn = self.defn()
3944 name = defn.name
4045 if name in self.symbols:
7782
7883 return Program(defns=defns, routines=routines)
7984
85 def typedef(self):
86 self.scanner.expect('typedef')
87 type_ = self.defn_type()
88 name = self.defn_name()
89 if name in self.typedefs:
90 raise SyntaxError('Type "%s" already declared' % name)
91 self.typedefs[name] = type_
92 return type_
93
8094 def defn(self):
8195 type_ = self.defn_type()
8296 name = self.defn_name()
130144 elif self.scanner.consume('buffer'):
131145 size = self.defn_size()
132146 return BufferType(size)
133 else:
134 self.scanner.expect('pointer')
147 elif self.scanner.consume('pointer'):
135148 return PointerType()
149 else:
150 type_name = self.scanner.token
151 self.scanner.scan()
152 if type_name not in self.typedefs:
153 raise SyntaxError("Undefined type '%s'" % type_name)
154 return self.typedefs[type_name]
136155
137156 def defn_name(self):
138157 self.scanner.check_type('identifier')
152152 | }
153153 = ok
154154
155 Typedefs of different types.
156
157 | typedef byte octet
158 | typedef table[256] octet twokay
159 | typedef routine trashes a game_routine
160 | vector game_routine start_game
161 |
162 | routine main {
163 | }
164 = ok
165
166 Can't have two typedefs with the same name.
167
168 | typedef byte frank
169 | typedef word frank
170 |
171 | routine main {
172 | }
173 ? SyntaxError
174
155175 Explicit memory address.
156176
157177 | byte screen @ 1024