annotate python/ks/nodes.py @ 182:e9b27f7193e3

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