comparison python/ks/nodes.py @ 146:91af0e40f868

Moved several files
author Windel Bouwman
date Fri, 22 Feb 2013 10:31:58 +0100
parents python/ppci/frontends/ks/nodes.py@9e552d34bd60
children
comparison
equal deleted inserted replaced
145:c101826ffe2b 146:91af0e40f868
1 """
2 AST nodes for the K# language.
3 """
4
5 class Node:
6 location = None
7 def getChildren(self):
8 children = []
9 members = dir(self)
10 for member in members:
11 member = getattr(self, member)
12 if isinstance(member, Node):
13 children.append(member)
14 elif type(member) is list:
15 for mi in member:
16 if isinstance(mi, Node):
17 children.append(mi)
18 return children
19
20 class Symbol(Node):
21 pass
22
23 class Id(Node):
24 def __init__(self, name):
25 self.name = name
26 def __repr__(self):
27 return 'ID {0}'.format(self.name)
28
29 # Selectors:
30 class Field(Node):
31 def __init__(self, fieldname):
32 self.fieldname = fieldname
33 def __repr__(self):
34 return 'FIELD {0}'.format(self.fieldname)
35
36 class Index(Node):
37 def __init__(self, index, typ):
38 self.index = index
39 self.typ = typ
40 def __repr__(self):
41 return 'INDEX {0}'.format(self.index)
42
43 class Deref(Node):
44 pass
45
46 class Designator(Node):
47 def __init__(self, obj, selectors, typ):
48 self.obj = obj
49 self.selectors = selectors
50 self.typ = typ
51 def __repr__(self):
52 return 'DESIGNATOR {0}, selectors {1}, type {2}'.format(self.obj, self.selectors, self.typ)
53
54 """
55 Type classes
56 """
57 def isType(a, b):
58 """ Compare types a and b and check if they are equal """
59 if type(a) is type(b):
60 if type(a) is BaseType:
61 return (a.name == b.name) and (a.size == b.size)
62 elif type(a) is ArrayType:
63 return (a.dimension == b.dimension) and isType(a.elementType, b.elementType)
64 elif type(a) is ProcedureType:
65 if len(a.parameters) != len(b.parameters):
66 print('Number of parameters does not match')
67 return False
68 for aparam, bparam in zip(a.parameters, b.parameters):
69 if not isType(aparam.typ, bparam.typ):
70 print('Parameter {0} does not match parameter {1}'.format(aparam, bparam))
71 return False
72 if a.result is None:
73 # TODO: how to handle a None return type??
74 pass
75 if not isType(a.result, b.result):
76 print('Procedure return value mismatch {0} != {1}'.format(a.result, b.result))
77 return False
78 return True
79 else:
80 print(a)
81 print(b)
82 Error('Not implemented {0}'.format(a))
83 else:
84 return False
85
86 class Type:
87 def isType(self, b):
88 return isType(self, b)
89
90 class BaseType(Type):
91 def __init__(self, name, size):
92 self.name = name
93 self.size = size
94 def __repr__(self):
95 return '[TYPE {0}]'.format(self.name)
96
97 class NilType(Node):
98 # TODO: how to handle nil values??
99 def __repr__(self):
100 return 'NILTYPE'
101
102 class ArrayType(Type):
103 def __init__(self, dimension, elementType):
104 self.dimension = dimension
105 self.elementType = elementType
106 self.size = elementType.size * dimension
107 def __repr__(self):
108 return '[ARRAY {0} of {1}]'.format(self.dimension, self.elementType)
109
110 class RecordType(Type):
111 def __init__(self, fields):
112 self.fields = fields
113 self.size = 0
114 for fieldname in self.fields:
115 self.size += self.fields[fieldname].size
116 def __repr__(self):
117 return '[RECORD {0}]'.format(self.fields)
118
119 class PointerType(Type):
120 def __init__(self, pointedType):
121 self.pointedType = pointedType
122 self.size = 8
123 def __repr__(self):
124 return '[POINTER {0}]'.format(self.pointedType)
125
126 class ProcedureType(Type):
127 def __init__(self, parameters, returntype):
128 self.parameters = parameters
129 self.returntype = returntype
130 def __repr__(self):
131 return '[PROCTYPE {0} RET {1}]'.format(self.parameters, self.returntype)
132
133 class DefinedType(Type):
134 def __init__(self, name, typ):
135 self.name = name
136 self.typ = typ
137 def __repr__(self):
138 return 'Named type {0} of type {1}'.format(self.name, self.typ)
139
140 # Classes for constants like numbers and strings:
141 class StringConstant(Symbol):
142 def __init__(self, txt):
143 self.txt = txt
144 self.typ = 'string'
145 def __repr__(self):
146 return "STRING '{0}'".format(self.txt)
147
148 # Variables, parameters, local variables, constants:
149 class Constant(Symbol):
150 def __init__(self, value, typ, name=None, public=False):
151 self.name = name
152 self.value = value
153 self.typ = typ
154 self.public = public
155 def __repr__(self):
156 return 'CONSTANT {0} = {1}'.format(self.name, self.value)
157
158 class Variable(Symbol):
159 def __init__(self, name, typ, public):
160 self.name = name
161 self.typ = typ
162 self.public = public
163 self.isLocal = False
164 self.isReadOnly = False
165 self.isParameter = False
166 def __repr__(self):
167 txt = '[public] ' if self.public else ''
168 return '{2}VAR {0} : {1}'.format(self.name, self.typ, txt)
169
170 class Parameter(Node):
171 """ A parameter has a passing method, name and typ """
172 def __init__(self, kind, name, typ):
173 self.kind = kind
174 self.name = name
175 self.typ = typ
176 def __repr__(self):
177 return 'PARAM {0} {1} {2}'.format(self.kind, self.name, self.typ)
178
179 # Operations:
180 class Unop(Node):
181 def __init__(self, a, op, typ):
182 self.a = a
183 self.op = op # Operation: '+', '-', '*', '/', 'mod'
184 self.typ = typ
185 self.place = None
186 def __repr__(self):
187 return 'UNOP {0}'.format(self.op)
188
189 class Binop(Node):
190 def __init__(self, a, op, b, typ):
191 self.a = a
192 self.b = b
193 self.op = op # Operation: '+', '-', '*', '/', 'mod'
194 self.typ = typ # Resulting type :)
195 self.place = None
196 def __repr__(self):
197 return 'BINOP {0} {1}'.format(self.op, self.typ)
198
199 class Relop(Node):
200 def __init__(self, a, relop, b, typ):
201 self.a = a
202 self.relop = relop
203 self.b = b
204 self.typ = typ
205 def __repr__(self):
206 return 'RELOP {0}'.format(self.relop)
207
208 # Modules
209 class Module(Node):
210 def __init__(self, name):
211 self.name = name
212 def __repr__(self):
213 return 'MODULE {0}'.format(self.name)
214
215 # Imports and Exports:
216 class ImportedSymbol(Node):
217 def __init__(self, modname, name):
218 self.modname = modname
219 self.name = name
220 def __repr__(self):
221 return 'IMPORTED SYMBOL {0}'.format(self.name)
222
223 class ExportedSymbol(Node):
224 def __init__(self, name, typ):
225 self.name = name
226 self.typ = typ
227 def __repr__(self):
228 return 'EXPORTED PROCEDURE {0} : {1}'.format(self.name, self.typ)
229
230 # Procedure types
231 class BuiltinProcedure(Node):
232 def __init__(self, name, typ):
233 self.name = name
234 self.typ = typ
235 def __repr__(self):
236 return 'BUILTIN PROCEDURE {0} : {1}'.format(self.name, self.typ)
237
238 class Procedure(Symbol):
239 """ Actual implementation of a function """
240 def __init__(self, name, typ, block, symtable, retexpr):
241 self.name = name
242 self.block = block
243 self.symtable = symtable
244 self.typ = typ
245 self.retexpr = retexpr
246 def __repr__(self):
247 return 'PROCEDURE {0} {1}'.format(self.name, self.typ)
248
249 # Statements
250 class StatementSequence(Node):
251 def __init__(self, statements):
252 self.statements = statements
253 def __repr__(self):
254 return 'STATEMENTSEQUENCE'
255
256 class EmptyStatement(Node):
257 def __repr__(self):
258 return 'EMPTY STATEMENT'
259
260 class Assignment(Node):
261 def __init__(self, lval, rval):
262 self.lval = lval
263 self.rval = rval
264 def __repr__(self):
265 return 'ASSIGNMENT'
266
267 class ProcedureCall(Node):
268 def __init__(self, proc, args):
269 self.proc = proc
270 self.args = args
271 self.typ = proc.typ.returntype
272 def __repr__(self):
273 return 'CALL {0} '.format(self.proc)
274
275 class IfStatement(Node):
276 def __init__(self, condition, truestatement, falsestatement=None):
277 self.condition = condition
278 self.truestatement = truestatement
279 self.falsestatement = falsestatement
280 def __repr__(self):
281 return 'IF-statement'
282
283 class CaseStatement(Node):
284 def __init__(self, condition):
285 self.condition = condition
286 def __repr__(self):
287 return 'CASE-statement'
288
289 class WhileStatement(Node):
290 def __init__(self, condition, statements):
291 self.condition = condition
292 self.dostatements = statements
293 def __repr__(self):
294 return 'WHILE-statement'
295
296 class ForStatement(Node):
297 def __init__(self, variable, begin, end, increment, statements):
298 self.variable = variable
299 self.begin = begin
300 self.end = end
301 self.increment = increment
302 self.statements = statements
303 def __repr__(self):
304 return 'FOR-statement'
305
306 class AsmCode(Node):
307 def __init__(self, asmcode):
308 self.asmcode = asmcode
309 def __repr__(self):
310 return 'ASM CODE'
311