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