Mercurial > lcfOS
annotate python/ppci/c3/astnodes.py @ 300:158068af716c
yafm
author | Windel Bouwman |
---|---|
date | Tue, 03 Dec 2013 18:00:22 +0100 |
parents | python/c3/astnodes.py@bd2593de3ff8 |
children | 6753763d3bec |
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: |
288 | 12 pass |
13 | |
148 | 14 |
300 | 15 # Variables, parameters, local variables, constants and named types: |
16 class Symbol(Node): | |
17 def __init__(self, name): | |
18 self.name = name | |
19 self.refs = [] | |
20 | |
21 def addRef(self, r): | |
22 self.refs.append(r) | |
23 | |
24 @property | |
25 def References(self): | |
26 return self.refs | |
27 | |
28 | |
213 | 29 # Modules |
30 class Package(Node): | |
31 def __init__(self, name, loc): | |
32 self.name = name | |
33 self.loc = loc | |
34 self.declarations = [] | |
251
6ed3d3a82a63
Added another c3 example. First import attempt
Windel Bouwman
parents:
249
diff
changeset
|
35 self.imports = [] |
288 | 36 |
213 | 37 def __repr__(self): |
284 | 38 return 'MODULE {}'.format(self.name) |
39 | |
213 | 40 |
150 | 41 class Designator(Node): |
213 | 42 def __init__(self, tname, loc): |
43 self.tname = tname | |
44 self.loc = loc | |
288 | 45 |
213 | 46 def __repr__(self): |
47 return 'DESIGNATOR {}'.format(self.tname) | |
148 | 48 |
289 | 49 |
50 class ImportDesignator(Designator): | |
51 def __init__(self, tname, vname, loc): | |
52 super().__init__(tname, loc) | |
53 self.vname = vname | |
54 | |
55 def __repr__(self): | |
56 return 'IMPORT DESIGNATOR {}:{}'.format(self.tname, self.vname) | |
57 | |
58 | |
148 | 59 """ |
60 Type classes | |
213 | 61 |
62 types must be comparable. | |
63 | |
64 There are the following types: | |
228 | 65 - base type -> basic type (built in) |
288 | 66 - struct type -> a composite type that contains a list of named fields |
228 | 67 of other types |
213 | 68 - function type |
148 | 69 """ |
70 | |
288 | 71 |
149 | 72 class Type(Node): |
288 | 73 pass |
272 | 74 |
148 | 75 |
300 | 76 class NamedType(Type, Symbol): |
272 | 77 def __init__(self, name): |
300 | 78 Symbol.__init__(self, name) |
79 | |
80 | |
81 class BaseType(NamedType): | |
82 def __init__(self, name): | |
83 super().__init__(name) | |
272 | 84 |
85 def __repr__(self): | |
86 return '{}'.format(self.name) | |
87 | |
148 | 88 |
89 class FunctionType(Type): | |
288 | 90 def __init__(self, parametertypes, returntype): |
91 self.parametertypes = parametertypes | |
92 self.returntype = returntype | |
93 | |
94 def __repr__(self): | |
95 params = ', '.join([str(v) for v in self.parametertypes]) | |
96 return '{1} f({0})'.format(params, self.returntype) | |
148 | 97 |
272 | 98 |
213 | 99 class PointerType(Type): |
288 | 100 """ A type that points to data of some other type """ |
213 | 101 def __init__(self, ptype): |
225 | 102 assert isinstance(ptype, Type) or isinstance(ptype, Designator) |
213 | 103 self.ptype = ptype |
288 | 104 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
105 def __repr__(self): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
106 return '({}*)'.format(self.ptype) |
213 | 107 |
230 | 108 |
227 | 109 class StructField: |
110 def __init__(self, name, typ): | |
111 self.name = name | |
112 self.typ = typ | |
230 | 113 self.offset = 0 |
114 | |
227 | 115 |
213 | 116 class StructureType(Type): |
117 def __init__(self, mems): | |
118 self.mems = mems | |
227 | 119 for mem in mems: |
120 assert type(mem) is StructField | |
121 assert type(mem.name) is str | |
230 | 122 |
225 | 123 def hasField(self, name): |
227 | 124 for mem in self.mems: |
125 if name == mem.name: | |
225 | 126 return True |
127 return False | |
230 | 128 |
225 | 129 def fieldType(self, name): |
230 | 130 return self.findField(name).typ |
131 | |
132 def fieldOffset(self, name): | |
133 return self.findField(name).offset | |
134 | |
135 def findField(self, name): | |
227 | 136 for mem in self.mems: |
137 if name == mem.name: | |
230 | 138 return mem |
139 raise KeyError(name) | |
225 | 140 |
230 | 141 def __repr__(self): |
142 return 'STRUCT' | |
213 | 143 |
272 | 144 |
300 | 145 class DefinedType(NamedType): |
288 | 146 """ A named type indicating another type """ |
225 | 147 def __init__(self, name, typ, loc): |
148 assert isinstance(name, str) | |
300 | 149 super().__init__(name) |
225 | 150 self.typ = typ |
151 self.loc = loc | |
230 | 152 |
225 | 153 def __repr__(self): |
154 return 'Named type {0} of type {1}'.format(self.name, self.typ) | |
148 | 155 |
221 | 156 |
148 | 157 class Constant(Symbol): |
213 | 158 def __init__(self, name, typ, value): |
272 | 159 super().__init__(name) |
160 self.typ = typ | |
161 self.value = value | |
162 | |
213 | 163 def __repr__(self): |
272 | 164 return 'CONSTANT {0} = {1}'.format(self.name, self.value) |
165 | |
148 | 166 |
167 class Variable(Symbol): | |
272 | 168 def __init__(self, name, typ): |
169 super().__init__(name) | |
170 self.typ = typ | |
171 self.ival = None | |
172 self.isLocal = False | |
173 self.isReadOnly = False | |
174 self.isParameter = False | |
175 | |
176 def __repr__(self): | |
177 return 'Var {} [{}]'.format(self.name, self.typ) | |
178 | |
179 | |
180 class LocalVariable(Variable): | |
181 def __init__(self, name, typ): | |
182 super().__init__(name, typ) | |
183 self.isLocal = True | |
184 | |
185 | |
186 class FormalParameter(Variable): | |
187 def __init__(self, name, typ): | |
188 super().__init__(name, typ) | |
189 self.isParameter = True | |
190 | |
148 | 191 |
150 | 192 # Procedure types |
193 class Function(Symbol): | |
213 | 194 """ Actual implementation of a function """ |
195 def __init__(self, name, loc): | |
196 super().__init__(name) | |
197 self.loc = loc | |
215 | 198 self.declarations = [] |
213 | 199 |
200 def __repr__(self): | |
217 | 201 return 'Func {}'.format(self.name) |
148 | 202 |
288 | 203 |
163 | 204 # Operations / Expressions: |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
205 class Expression(Node): |
230 | 206 def __init__(self, loc): |
207 self.loc = loc | |
208 | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
209 |
225 | 210 class Deref(Expression): |
211 def __init__(self, ptr, loc): | |
230 | 212 super().__init__(loc) |
225 | 213 assert isinstance(ptr, Expression) |
214 self.ptr = ptr | |
288 | 215 |
225 | 216 def __repr__(self): |
288 | 217 return 'DEREF {}'.format(self.ptr) |
225 | 218 |
230 | 219 |
220 class TypeCast(Expression): | |
221 def __init__(self, to_type, x, loc): | |
222 super().__init__(loc) | |
223 self.to_type = to_type | |
224 self.a = x | |
288 | 225 |
230 | 226 def __repr__(self): |
227 return 'TYPECAST {}'.format(self.to_type) | |
228 | |
229 | |
225 | 230 class FieldRef(Expression): |
231 def __init__(self, base, field, loc): | |
230 | 232 super().__init__(loc) |
225 | 233 assert isinstance(base, Expression) |
234 assert isinstance(field, str) | |
235 self.base = base | |
236 self.field = field | |
288 | 237 |
225 | 238 def __repr__(self): |
288 | 239 return 'FIELD {}.{}'.format(self.base, self.field) |
225 | 240 |
230 | 241 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
242 class Unop(Expression): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
243 def __init__(self, op, a, 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) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
246 assert isinstance(op, str) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
247 self.a = a |
230 | 248 self.op = op |
288 | 249 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
250 def __repr__(self): |
288 | 251 return 'UNOP {}'.format(self.op) |
252 | |
148 | 253 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
254 class Binop(Expression): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
255 def __init__(self, a, op, b, loc): |
230 | 256 super().__init__(loc) |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
257 assert isinstance(a, Expression), type(a) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
258 assert isinstance(b, Expression) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
259 assert isinstance(op, str) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
260 self.a = a |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
261 self.b = b |
288 | 262 self.op = op # Operation: '+', '-', '*', '/', 'mod' |
148 | 263 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
264 def __repr__(self): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
265 return 'BINOP {}'.format(self.op) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
266 |
288 | 267 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
268 class VariableUse(Expression): |
228 | 269 def __init__(self, target, loc): |
230 | 270 super().__init__(loc) |
228 | 271 self.target = target |
288 | 272 |
228 | 273 def __repr__(self): |
288 | 274 return 'VAR USE {}'.format(self.target) |
163 | 275 |
284 | 276 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
277 class Literal(Expression): |
228 | 278 def __init__(self, val, loc): |
230 | 279 super().__init__(loc) |
228 | 280 self.val = val |
288 | 281 |
228 | 282 def __repr__(self): |
283 return 'LITERAL {}'.format(self.val) | |
150 | 284 |
284 | 285 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
286 class FunctionCall(Expression): |
228 | 287 def __init__(self, proc, args, loc): |
230 | 288 super().__init__(loc) |
228 | 289 self.proc = proc |
290 self.args = args | |
288 | 291 |
228 | 292 def __repr__(self): |
293 return 'CALL {0} '.format(self.proc) | |
148 | 294 |
284 | 295 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
296 # Statements |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
297 class Statement(Node): |
228 | 298 def __init__(self, loc): |
299 self.loc = loc | |
300 | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
301 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
302 class CompoundStatement(Statement): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
303 def __init__(self, statements): |
249 | 304 super().__init__(None) |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
305 self.statements = statements |
228 | 306 for s in self.statements: |
307 assert isinstance(s, Statement) | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
308 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
309 def __repr__(self): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
310 return 'COMPOUND STATEMENT' |
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 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
313 class ReturnStatement(Statement): |
228 | 314 def __init__(self, expr, loc): |
315 super().__init__(loc) | |
316 self.expr = expr | |
317 | |
318 def __repr__(self): | |
319 return 'RETURN STATEMENT' | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
320 |
284 | 321 |
222 | 322 class Assignment(Statement): |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
323 def __init__(self, lval, rval, loc): |
228 | 324 super().__init__(loc) |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
325 assert isinstance(lval, Node) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
326 assert isinstance(rval, Node) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
327 self.lval = lval |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
328 self.rval = rval |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
329 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
330 def __repr__(self): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
331 return 'ASSIGNMENT' |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
332 |
288 | 333 |
222 | 334 class ExpressionStatement(Statement): |
335 def __init__(self, ex, loc): | |
228 | 336 super().__init__(loc) |
222 | 337 self.ex = ex |
228 | 338 |
222 | 339 def __repr__(self): |
340 return 'Epression' | |
341 | |
228 | 342 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
343 class IfStatement(Statement): |
228 | 344 def __init__(self, condition, truestatement, falsestatement, loc): |
345 super().__init__(loc) | |
346 self.condition = condition | |
347 self.truestatement = truestatement | |
348 self.falsestatement = falsestatement | |
349 | |
350 def __repr__(self): | |
351 return 'IF-statement' | |
352 | |
148 | 353 |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
217
diff
changeset
|
354 class WhileStatement(Statement): |
228 | 355 def __init__(self, condition, statement, loc): |
356 super().__init__(loc) | |
357 self.condition = condition | |
358 self.statement = statement | |
148 | 359 |
228 | 360 def __repr__(self): |
361 return 'WHILE-statement' |