Mercurial > lcfOS
annotate python/ppci/c3/astnodes.py @ 346:3bb7dcfe5529
expanded arm target
author | Windel Bouwman |
---|---|
date | Fri, 07 Mar 2014 17:05:32 +0100 |
parents | d1ecc493384e |
children | 5477e499b039 |
rev | line source |
---|---|
148 | 1 """ |
213 | 2 AST (abstract syntax tree) nodes for the c3 language. |
3 The tree is build by the parser. | |
4 Then it is checked | |
5 Finally code is generated from it. | |
148 | 6 """ |
7 | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
8 from ppci import SourceLocation |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
9 |
288 | 10 |
148 | 11 class Node: |
306 | 12 """ Base class of all nodes in a AST """ |
288 | 13 pass |
14 | |
148 | 15 |
300 | 16 # Variables, parameters, local variables, constants and named types: |
17 class Symbol(Node): | |
308 | 18 """ Symbol is the base class for all named things like variables, |
306 | 19 functions, constants and types and modules """ |
300 | 20 def __init__(self, name): |
21 self.name = name | |
22 self.refs = [] | |
23 | |
24 def addRef(self, r): | |
25 self.refs.append(r) | |
26 | |
27 @property | |
28 def References(self): | |
29 return self.refs | |
30 | |
31 | |
213 | 32 # Modules |
301 | 33 class Package(Symbol): |
213 | 34 def __init__(self, name, loc): |
301 | 35 super().__init__(name) |
213 | 36 self.loc = loc |
37 self.declarations = [] | |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
38 self.imports = [] |
288 | 39 |
336
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
316
diff
changeset
|
40 def add_declaration(self, decl): |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
316
diff
changeset
|
41 self.declarations.append(decl) |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
316
diff
changeset
|
42 if isinstance(decl, Function): |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
316
diff
changeset
|
43 decl.package = self |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
316
diff
changeset
|
44 |
213 | 45 def __repr__(self): |
284 | 46 return 'MODULE {}'.format(self.name) |
47 | |
213 | 48 |
149 | 49 class Type(Node): |
306 | 50 """ Base class of all types """ |
288 | 51 pass |
272 | 52 |
148 | 53 |
300 | 54 class NamedType(Type, Symbol): |
308 | 55 """ Some types are named, for example a user defined type (typedef) |
306 | 56 and built in types. That is why this class derives from both Type |
57 and Symbol. """ | |
272 | 58 def __init__(self, name): |
300 | 59 Symbol.__init__(self, name) |
60 | |
61 | |
62 class BaseType(NamedType): | |
306 | 63 """ Built in type """ |
300 | 64 def __init__(self, name): |
65 super().__init__(name) | |
272 | 66 |
67 def __repr__(self): | |
68 return '{}'.format(self.name) | |
69 | |
148 | 70 |
71 class FunctionType(Type): | |
306 | 72 """ Function blueprint, defines argument types and return type """ |
288 | 73 def __init__(self, parametertypes, returntype): |
74 self.parametertypes = parametertypes | |
75 self.returntype = returntype | |
76 | |
77 def __repr__(self): | |
78 params = ', '.join([str(v) for v in self.parametertypes]) | |
79 return '{1} f({0})'.format(params, self.returntype) | |
148 | 80 |
272 | 81 |
213 | 82 class PointerType(Type): |
288 | 83 """ A type that points to data of some other type """ |
213 | 84 def __init__(self, ptype): |
306 | 85 assert isinstance(ptype, Type) or isinstance(ptype, Expression) |
213 | 86 self.ptype = ptype |
288 | 87 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
88 def __repr__(self): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
89 return '({}*)'.format(self.ptype) |
213 | 90 |
230 | 91 |
227 | 92 class StructField: |
316 | 93 """ Field of a struct type """ |
227 | 94 def __init__(self, name, typ): |
306 | 95 assert type(name) is str |
227 | 96 self.name = name |
97 self.typ = typ | |
230 | 98 |
227 | 99 |
213 | 100 class StructureType(Type): |
306 | 101 """ Struct type consisting of several named members """ |
213 | 102 def __init__(self, mems): |
103 self.mems = mems | |
306 | 104 assert all(type(mem) is StructField for mem in mems) |
230 | 105 |
225 | 106 def hasField(self, name): |
227 | 107 for mem in self.mems: |
108 if name == mem.name: | |
225 | 109 return True |
110 return False | |
230 | 111 |
225 | 112 def fieldType(self, name): |
230 | 113 return self.findField(name).typ |
114 | |
115 def fieldOffset(self, name): | |
116 return self.findField(name).offset | |
117 | |
118 def findField(self, name): | |
227 | 119 for mem in self.mems: |
120 if name == mem.name: | |
230 | 121 return mem |
122 raise KeyError(name) | |
225 | 123 |
230 | 124 def __repr__(self): |
125 return 'STRUCT' | |
213 | 126 |
272 | 127 |
300 | 128 class DefinedType(NamedType): |
288 | 129 """ A named type indicating another type """ |
225 | 130 def __init__(self, name, typ, loc): |
131 assert isinstance(name, str) | |
300 | 132 super().__init__(name) |
225 | 133 self.typ = typ |
134 self.loc = loc | |
230 | 135 |
225 | 136 def __repr__(self): |
137 return 'Named type {0} of type {1}'.format(self.name, self.typ) | |
148 | 138 |
221 | 139 |
148 | 140 class Constant(Symbol): |
316 | 141 """ Constant definition """ |
213 | 142 def __init__(self, name, typ, value): |
272 | 143 super().__init__(name) |
144 self.typ = typ | |
145 self.value = value | |
146 | |
213 | 147 def __repr__(self): |
272 | 148 return 'CONSTANT {0} = {1}'.format(self.name, self.value) |
149 | |
148 | 150 |
151 class Variable(Symbol): | |
272 | 152 def __init__(self, name, typ): |
153 super().__init__(name) | |
154 self.typ = typ | |
155 self.isLocal = False | |
156 self.isParameter = False | |
157 | |
158 def __repr__(self): | |
159 return 'Var {} [{}]'.format(self.name, self.typ) | |
160 | |
161 | |
162 class LocalVariable(Variable): | |
163 def __init__(self, name, typ): | |
164 super().__init__(name, typ) | |
165 self.isLocal = True | |
166 | |
167 | |
168 class FormalParameter(Variable): | |
169 def __init__(self, name, typ): | |
170 super().__init__(name, typ) | |
171 self.isParameter = True | |
172 | |
148 | 173 |
150 | 174 # Procedure types |
175 class Function(Symbol): | |
213 | 176 """ Actual implementation of a function """ |
177 def __init__(self, name, loc): | |
178 super().__init__(name) | |
179 self.loc = loc | |
215 | 180 self.declarations = [] |
213 | 181 |
336
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
316
diff
changeset
|
182 def add_declaration(self, decl): |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
316
diff
changeset
|
183 self.declarations.append(decl) |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
316
diff
changeset
|
184 |
213 | 185 def __repr__(self): |
217 | 186 return 'Func {}'.format(self.name) |
148 | 187 |
288 | 188 |
163 | 189 # Operations / Expressions: |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
190 class Expression(Node): |
230 | 191 def __init__(self, loc): |
192 self.loc = loc | |
193 | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
194 |
225 | 195 class Deref(Expression): |
196 def __init__(self, ptr, loc): | |
230 | 197 super().__init__(loc) |
225 | 198 assert isinstance(ptr, Expression) |
199 self.ptr = ptr | |
288 | 200 |
225 | 201 def __repr__(self): |
288 | 202 return 'DEREF {}'.format(self.ptr) |
225 | 203 |
230 | 204 |
205 class TypeCast(Expression): | |
206 def __init__(self, to_type, x, loc): | |
207 super().__init__(loc) | |
208 self.to_type = to_type | |
209 self.a = x | |
288 | 210 |
230 | 211 def __repr__(self): |
212 return 'TYPECAST {}'.format(self.to_type) | |
213 | |
214 | |
306 | 215 class Member(Expression): |
216 """ Field reference of some object, can also be package selection """ | |
225 | 217 def __init__(self, base, field, loc): |
230 | 218 super().__init__(loc) |
225 | 219 assert isinstance(base, Expression) |
220 assert isinstance(field, str) | |
221 self.base = base | |
222 self.field = field | |
288 | 223 |
225 | 224 def __repr__(self): |
306 | 225 return 'MEMBER {}.{}'.format(self.base, self.field) |
225 | 226 |
230 | 227 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
228 class Unop(Expression): |
306 | 229 """ Operation on one operand """ |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
230 def __init__(self, op, a, loc): |
230 | 231 super().__init__(loc) |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
232 assert isinstance(a, Expression) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
233 assert isinstance(op, str) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
234 self.a = a |
230 | 235 self.op = op |
288 | 236 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
237 def __repr__(self): |
288 | 238 return 'UNOP {}'.format(self.op) |
239 | |
148 | 240 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
241 class Binop(Expression): |
306 | 242 """ Expression taking two operands and one operator """ |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
243 def __init__(self, a, op, b, loc): |
230 | 244 super().__init__(loc) |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
245 assert isinstance(a, Expression), type(a) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
246 assert isinstance(b, Expression) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
247 assert isinstance(op, str) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
248 self.a = a |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
249 self.b = b |
288 | 250 self.op = op # Operation: '+', '-', '*', '/', 'mod' |
148 | 251 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
252 def __repr__(self): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
253 return 'BINOP {}'.format(self.op) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
254 |
288 | 255 |
306 | 256 class Identifier(Expression): |
257 """ Reference to some identifier, can be anything from package, variable | |
258 function or type, any named thing! """ | |
228 | 259 def __init__(self, target, loc): |
230 | 260 super().__init__(loc) |
228 | 261 self.target = target |
288 | 262 |
228 | 263 def __repr__(self): |
306 | 264 return 'ID {}'.format(self.target) |
163 | 265 |
284 | 266 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
267 class Literal(Expression): |
306 | 268 """ Constant value or string """ |
228 | 269 def __init__(self, val, loc): |
230 | 270 super().__init__(loc) |
228 | 271 self.val = val |
288 | 272 |
228 | 273 def __repr__(self): |
274 return 'LITERAL {}'.format(self.val) | |
150 | 275 |
284 | 276 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
277 class FunctionCall(Expression): |
306 | 278 """ Call to a some function """ |
228 | 279 def __init__(self, proc, args, loc): |
230 | 280 super().__init__(loc) |
228 | 281 self.proc = proc |
282 self.args = args | |
288 | 283 |
228 | 284 def __repr__(self): |
285 return 'CALL {0} '.format(self.proc) | |
148 | 286 |
284 | 287 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
288 # Statements |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
289 class Statement(Node): |
306 | 290 """ Base class of all statements """ |
228 | 291 def __init__(self, loc): |
292 self.loc = loc | |
293 | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
294 |
307 | 295 class Empty(Statement): |
306 | 296 """ Empty statement which does nothing! """ |
297 def __init__(self): | |
298 super().__init__(None) | |
299 | |
300 def __repr__(self): | |
301 return 'NOP' | |
302 | |
303 | |
307 | 304 class Compound(Statement): |
306 | 305 """ Statement consisting of a sequence of other statements """ |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
306 def __init__(self, statements): |
249 | 307 super().__init__(None) |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
308 self.statements = statements |
228 | 309 for s in self.statements: |
310 assert isinstance(s, Statement) | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
311 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
312 def __repr__(self): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
313 return 'COMPOUND STATEMENT' |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
314 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
315 |
307 | 316 class Return(Statement): |
228 | 317 def __init__(self, expr, loc): |
318 super().__init__(loc) | |
319 self.expr = expr | |
320 | |
321 def __repr__(self): | |
322 return 'RETURN STATEMENT' | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
323 |
284 | 324 |
222 | 325 class Assignment(Statement): |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
326 def __init__(self, lval, rval, loc): |
228 | 327 super().__init__(loc) |
306 | 328 assert isinstance(lval, Expression) |
329 assert isinstance(rval, Expression) | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
330 self.lval = lval |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
331 self.rval = rval |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
332 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
333 def __repr__(self): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
334 return 'ASSIGNMENT' |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
335 |
288 | 336 |
222 | 337 class ExpressionStatement(Statement): |
338 def __init__(self, ex, loc): | |
228 | 339 super().__init__(loc) |
222 | 340 self.ex = ex |
228 | 341 |
222 | 342 def __repr__(self): |
343 return 'Epression' | |
344 | |
228 | 345 |
307 | 346 class If(Statement): |
228 | 347 def __init__(self, condition, truestatement, falsestatement, loc): |
348 super().__init__(loc) | |
349 self.condition = condition | |
350 self.truestatement = truestatement | |
351 self.falsestatement = falsestatement | |
352 | |
353 def __repr__(self): | |
354 return 'IF-statement' | |
355 | |
148 | 356 |
307 | 357 class While(Statement): |
228 | 358 def __init__(self, condition, statement, loc): |
359 super().__init__(loc) | |
360 self.condition = condition | |
361 self.statement = statement | |
148 | 362 |
228 | 363 def __repr__(self): |
364 return 'WHILE-statement' | |
315 | 365 |
366 | |
367 class For(Statement): | |
368 def __init__(self, init, condition, final, statement, loc): | |
369 super().__init__(loc) | |
370 self.init = init | |
371 self.condition = condition | |
372 self.final = final | |
373 self.statement = statement | |
374 | |
375 def __repr__(self): | |
376 return 'FOR-statement' |