annotate ide/compiler/nodes.py @ 18:6129643f5c34

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