annotate ide/compiler/nodes.py @ 1:92df07bc2081

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