comparison 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
comparison
equal deleted inserted replaced
299:674789d9ff37 300:158068af716c
1 """
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.
6 """
7
8 from ppci import SourceLocation
9
10
11 class Node:
12 pass
13
14
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
29 # Modules
30 class Package(Node):
31 def __init__(self, name, loc):
32 self.name = name
33 self.loc = loc
34 self.declarations = []
35 self.imports = []
36
37 def __repr__(self):
38 return 'MODULE {}'.format(self.name)
39
40
41 class Designator(Node):
42 def __init__(self, tname, loc):
43 self.tname = tname
44 self.loc = loc
45
46 def __repr__(self):
47 return 'DESIGNATOR {}'.format(self.tname)
48
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
59 """
60 Type classes
61
62 types must be comparable.
63
64 There are the following types:
65 - base type -> basic type (built in)
66 - struct type -> a composite type that contains a list of named fields
67 of other types
68 - function type
69 """
70
71
72 class Type(Node):
73 pass
74
75
76 class NamedType(Type, Symbol):
77 def __init__(self, name):
78 Symbol.__init__(self, name)
79
80
81 class BaseType(NamedType):
82 def __init__(self, name):
83 super().__init__(name)
84
85 def __repr__(self):
86 return '{}'.format(self.name)
87
88
89 class FunctionType(Type):
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)
97
98
99 class PointerType(Type):
100 """ A type that points to data of some other type """
101 def __init__(self, ptype):
102 assert isinstance(ptype, Type) or isinstance(ptype, Designator)
103 self.ptype = ptype
104
105 def __repr__(self):
106 return '({}*)'.format(self.ptype)
107
108
109 class StructField:
110 def __init__(self, name, typ):
111 self.name = name
112 self.typ = typ
113 self.offset = 0
114
115
116 class StructureType(Type):
117 def __init__(self, mems):
118 self.mems = mems
119 for mem in mems:
120 assert type(mem) is StructField
121 assert type(mem.name) is str
122
123 def hasField(self, name):
124 for mem in self.mems:
125 if name == mem.name:
126 return True
127 return False
128
129 def fieldType(self, name):
130 return self.findField(name).typ
131
132 def fieldOffset(self, name):
133 return self.findField(name).offset
134
135 def findField(self, name):
136 for mem in self.mems:
137 if name == mem.name:
138 return mem
139 raise KeyError(name)
140
141 def __repr__(self):
142 return 'STRUCT'
143
144
145 class DefinedType(NamedType):
146 """ A named type indicating another type """
147 def __init__(self, name, typ, loc):
148 assert isinstance(name, str)
149 super().__init__(name)
150 self.typ = typ
151 self.loc = loc
152
153 def __repr__(self):
154 return 'Named type {0} of type {1}'.format(self.name, self.typ)
155
156
157 class Constant(Symbol):
158 def __init__(self, name, typ, value):
159 super().__init__(name)
160 self.typ = typ
161 self.value = value
162
163 def __repr__(self):
164 return 'CONSTANT {0} = {1}'.format(self.name, self.value)
165
166
167 class Variable(Symbol):
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
191
192 # Procedure types
193 class Function(Symbol):
194 """ Actual implementation of a function """
195 def __init__(self, name, loc):
196 super().__init__(name)
197 self.loc = loc
198 self.declarations = []
199
200 def __repr__(self):
201 return 'Func {}'.format(self.name)
202
203
204 # Operations / Expressions:
205 class Expression(Node):
206 def __init__(self, loc):
207 self.loc = loc
208
209
210 class Deref(Expression):
211 def __init__(self, ptr, loc):
212 super().__init__(loc)
213 assert isinstance(ptr, Expression)
214 self.ptr = ptr
215
216 def __repr__(self):
217 return 'DEREF {}'.format(self.ptr)
218
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
225
226 def __repr__(self):
227 return 'TYPECAST {}'.format(self.to_type)
228
229
230 class FieldRef(Expression):
231 def __init__(self, base, field, loc):
232 super().__init__(loc)
233 assert isinstance(base, Expression)
234 assert isinstance(field, str)
235 self.base = base
236 self.field = field
237
238 def __repr__(self):
239 return 'FIELD {}.{}'.format(self.base, self.field)
240
241
242 class Unop(Expression):
243 def __init__(self, op, a, loc):
244 super().__init__(loc)
245 assert isinstance(a, Expression)
246 assert isinstance(op, str)
247 self.a = a
248 self.op = op
249
250 def __repr__(self):
251 return 'UNOP {}'.format(self.op)
252
253
254 class Binop(Expression):
255 def __init__(self, a, op, b, loc):
256 super().__init__(loc)
257 assert isinstance(a, Expression), type(a)
258 assert isinstance(b, Expression)
259 assert isinstance(op, str)
260 self.a = a
261 self.b = b
262 self.op = op # Operation: '+', '-', '*', '/', 'mod'
263
264 def __repr__(self):
265 return 'BINOP {}'.format(self.op)
266
267
268 class VariableUse(Expression):
269 def __init__(self, target, loc):
270 super().__init__(loc)
271 self.target = target
272
273 def __repr__(self):
274 return 'VAR USE {}'.format(self.target)
275
276
277 class Literal(Expression):
278 def __init__(self, val, loc):
279 super().__init__(loc)
280 self.val = val
281
282 def __repr__(self):
283 return 'LITERAL {}'.format(self.val)
284
285
286 class FunctionCall(Expression):
287 def __init__(self, proc, args, loc):
288 super().__init__(loc)
289 self.proc = proc
290 self.args = args
291
292 def __repr__(self):
293 return 'CALL {0} '.format(self.proc)
294
295
296 # Statements
297 class Statement(Node):
298 def __init__(self, loc):
299 self.loc = loc
300
301
302 class CompoundStatement(Statement):
303 def __init__(self, statements):
304 super().__init__(None)
305 self.statements = statements
306 for s in self.statements:
307 assert isinstance(s, Statement)
308
309 def __repr__(self):
310 return 'COMPOUND STATEMENT'
311
312
313 class ReturnStatement(Statement):
314 def __init__(self, expr, loc):
315 super().__init__(loc)
316 self.expr = expr
317
318 def __repr__(self):
319 return 'RETURN STATEMENT'
320
321
322 class Assignment(Statement):
323 def __init__(self, lval, rval, loc):
324 super().__init__(loc)
325 assert isinstance(lval, Node)
326 assert isinstance(rval, Node)
327 self.lval = lval
328 self.rval = rval
329
330 def __repr__(self):
331 return 'ASSIGNMENT'
332
333
334 class ExpressionStatement(Statement):
335 def __init__(self, ex, loc):
336 super().__init__(loc)
337 self.ex = ex
338
339 def __repr__(self):
340 return 'Epression'
341
342
343 class IfStatement(Statement):
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
353
354 class WhileStatement(Statement):
355 def __init__(self, condition, statement, loc):
356 super().__init__(loc)
357 self.condition = condition
358 self.statement = statement
359
360 def __repr__(self):
361 return 'WHILE-statement'