comparison python/ppci/c3/astnodes.py @ 306:b145f8e6050b

Start on c3 rewrite
author Windel Bouwman
date Mon, 09 Dec 2013 19:00:21 +0100
parents 6753763d3bec
children e609d5296ee9
comparison
equal deleted inserted replaced
305:0615b5308710 306:b145f8e6050b
7 7
8 from ppci import SourceLocation 8 from ppci import SourceLocation
9 9
10 10
11 class Node: 11 class Node:
12 """ Base class of all nodes in a AST """
12 pass 13 pass
13 14
14 15
15 # Variables, parameters, local variables, constants and named types: 16 # Variables, parameters, local variables, constants and named types:
16 class Symbol(Node): 17 class Symbol(Node):
18 """ Symbol is the base class for all named things like variables,
19 functions, constants and types and modules """
17 def __init__(self, name): 20 def __init__(self, name):
18 self.name = name 21 self.name = name
19 self.refs = [] 22 self.refs = []
20 23
21 def addRef(self, r): 24 def addRef(self, r):
36 39
37 def __repr__(self): 40 def __repr__(self):
38 return 'MODULE {}'.format(self.name) 41 return 'MODULE {}'.format(self.name)
39 42
40 43
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): 44 class Type(Node):
45 """ Base class of all types """
73 pass 46 pass
74 47
75 48
76 class NamedType(Type, Symbol): 49 class NamedType(Type, Symbol):
50 """ Some types are named, for example a user defined type (typedef)
51 and built in types. That is why this class derives from both Type
52 and Symbol. """
77 def __init__(self, name): 53 def __init__(self, name):
78 Symbol.__init__(self, name) 54 Symbol.__init__(self, name)
79 55
80 56
81 class BaseType(NamedType): 57 class BaseType(NamedType):
58 """ Built in type """
82 def __init__(self, name): 59 def __init__(self, name):
83 super().__init__(name) 60 super().__init__(name)
84 61
85 def __repr__(self): 62 def __repr__(self):
86 return '{}'.format(self.name) 63 return '{}'.format(self.name)
87 64
88 65
89 class FunctionType(Type): 66 class FunctionType(Type):
67 """ Function blueprint, defines argument types and return type """
90 def __init__(self, parametertypes, returntype): 68 def __init__(self, parametertypes, returntype):
91 self.parametertypes = parametertypes 69 self.parametertypes = parametertypes
92 self.returntype = returntype 70 self.returntype = returntype
93 71
94 def __repr__(self): 72 def __repr__(self):
97 75
98 76
99 class PointerType(Type): 77 class PointerType(Type):
100 """ A type that points to data of some other type """ 78 """ A type that points to data of some other type """
101 def __init__(self, ptype): 79 def __init__(self, ptype):
102 assert isinstance(ptype, Type) or isinstance(ptype, Designator) 80 assert isinstance(ptype, Type) or isinstance(ptype, Expression)
103 self.ptype = ptype 81 self.ptype = ptype
104 82
105 def __repr__(self): 83 def __repr__(self):
106 return '({}*)'.format(self.ptype) 84 return '({}*)'.format(self.ptype)
107 85
108 86
109 class StructField: 87 class StructField:
110 def __init__(self, name, typ): 88 def __init__(self, name, typ):
89 assert type(name) is str
111 self.name = name 90 self.name = name
112 self.typ = typ 91 self.typ = typ
113 self.offset = 0 92 self.offset = 0
114 93
115 94
116 class StructureType(Type): 95 class StructureType(Type):
96 """ Struct type consisting of several named members """
117 def __init__(self, mems): 97 def __init__(self, mems):
118 self.mems = mems 98 self.mems = mems
119 for mem in mems: 99 assert all(type(mem) is StructField for mem in mems)
120 assert type(mem) is StructField
121 assert type(mem.name) is str
122 100
123 def hasField(self, name): 101 def hasField(self, name):
124 for mem in self.mems: 102 for mem in self.mems:
125 if name == mem.name: 103 if name == mem.name:
126 return True 104 return True
166 144
167 class Variable(Symbol): 145 class Variable(Symbol):
168 def __init__(self, name, typ): 146 def __init__(self, name, typ):
169 super().__init__(name) 147 super().__init__(name)
170 self.typ = typ 148 self.typ = typ
171 self.ival = None
172 self.isLocal = False 149 self.isLocal = False
173 self.isReadOnly = False 150 self.isReadOnly = False
174 self.isParameter = False 151 self.isParameter = False
175 152
176 def __repr__(self): 153 def __repr__(self):
225 202
226 def __repr__(self): 203 def __repr__(self):
227 return 'TYPECAST {}'.format(self.to_type) 204 return 'TYPECAST {}'.format(self.to_type)
228 205
229 206
230 class FieldRef(Expression): 207 class Member(Expression):
208 """ Field reference of some object, can also be package selection """
231 def __init__(self, base, field, loc): 209 def __init__(self, base, field, loc):
232 super().__init__(loc) 210 super().__init__(loc)
233 assert isinstance(base, Expression) 211 assert isinstance(base, Expression)
234 assert isinstance(field, str) 212 assert isinstance(field, str)
235 self.base = base 213 self.base = base
236 self.field = field 214 self.field = field
237 215
238 def __repr__(self): 216 def __repr__(self):
239 return 'FIELD {}.{}'.format(self.base, self.field) 217 return 'MEMBER {}.{}'.format(self.base, self.field)
240 218
241 219
242 class Unop(Expression): 220 class Unop(Expression):
221 """ Operation on one operand """
243 def __init__(self, op, a, loc): 222 def __init__(self, op, a, loc):
244 super().__init__(loc) 223 super().__init__(loc)
245 assert isinstance(a, Expression) 224 assert isinstance(a, Expression)
246 assert isinstance(op, str) 225 assert isinstance(op, str)
247 self.a = a 226 self.a = a
250 def __repr__(self): 229 def __repr__(self):
251 return 'UNOP {}'.format(self.op) 230 return 'UNOP {}'.format(self.op)
252 231
253 232
254 class Binop(Expression): 233 class Binop(Expression):
234 """ Expression taking two operands and one operator """
255 def __init__(self, a, op, b, loc): 235 def __init__(self, a, op, b, loc):
256 super().__init__(loc) 236 super().__init__(loc)
257 assert isinstance(a, Expression), type(a) 237 assert isinstance(a, Expression), type(a)
258 assert isinstance(b, Expression) 238 assert isinstance(b, Expression)
259 assert isinstance(op, str) 239 assert isinstance(op, str)
263 243
264 def __repr__(self): 244 def __repr__(self):
265 return 'BINOP {}'.format(self.op) 245 return 'BINOP {}'.format(self.op)
266 246
267 247
268 class VariableUse(Expression): 248 class Identifier(Expression):
249 """ Reference to some identifier, can be anything from package, variable
250 function or type, any named thing! """
269 def __init__(self, target, loc): 251 def __init__(self, target, loc):
270 super().__init__(loc) 252 super().__init__(loc)
271 self.target = target 253 self.target = target
272 254
273 def __repr__(self): 255 def __repr__(self):
274 return 'VAR USE {}'.format(self.target) 256 return 'ID {}'.format(self.target)
275 257
276 258
277 class Literal(Expression): 259 class Literal(Expression):
260 """ Constant value or string """
278 def __init__(self, val, loc): 261 def __init__(self, val, loc):
279 super().__init__(loc) 262 super().__init__(loc)
280 self.val = val 263 self.val = val
281 264
282 def __repr__(self): 265 def __repr__(self):
283 return 'LITERAL {}'.format(self.val) 266 return 'LITERAL {}'.format(self.val)
284 267
285 268
286 class FunctionCall(Expression): 269 class FunctionCall(Expression):
270 """ Call to a some function """
287 def __init__(self, proc, args, loc): 271 def __init__(self, proc, args, loc):
288 super().__init__(loc) 272 super().__init__(loc)
289 self.proc = proc 273 self.proc = proc
290 self.args = args 274 self.args = args
291 275
293 return 'CALL {0} '.format(self.proc) 277 return 'CALL {0} '.format(self.proc)
294 278
295 279
296 # Statements 280 # Statements
297 class Statement(Node): 281 class Statement(Node):
282 """ Base class of all statements """
298 def __init__(self, loc): 283 def __init__(self, loc):
299 self.loc = loc 284 self.loc = loc
300 285
301 286
287 class EmptyStatement(Statement):
288 """ Empty statement which does nothing! """
289 def __init__(self):
290 super().__init__(None)
291
292 def __repr__(self):
293 return 'NOP'
294
295
302 class CompoundStatement(Statement): 296 class CompoundStatement(Statement):
297 """ Statement consisting of a sequence of other statements """
303 def __init__(self, statements): 298 def __init__(self, statements):
304 super().__init__(None) 299 super().__init__(None)
305 self.statements = statements 300 self.statements = statements
306 for s in self.statements: 301 for s in self.statements:
307 assert isinstance(s, Statement) 302 assert isinstance(s, Statement)
320 315
321 316
322 class Assignment(Statement): 317 class Assignment(Statement):
323 def __init__(self, lval, rval, loc): 318 def __init__(self, lval, rval, loc):
324 super().__init__(loc) 319 super().__init__(loc)
325 assert isinstance(lval, Node) 320 assert isinstance(lval, Expression)
326 assert isinstance(rval, Node) 321 assert isinstance(rval, Expression)
327 self.lval = lval 322 self.lval = lval
328 self.rval = rval 323 self.rval = rval
329 324
330 def __repr__(self): 325 def __repr__(self):
331 return 'ASSIGNMENT' 326 return 'ASSIGNMENT'