comparison python/libs/compiler/nodes.py @ 63:32078200cdd6

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