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