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