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