-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstructs.py
More file actions
244 lines (208 loc) · 6.68 KB
/
constructs.py
File metadata and controls
244 lines (208 loc) · 6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# -*- coding: utf-8 -*-
# Copyright (c) 2008--2012 Peter Dinges <pdinges@acm.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# ==========
# Constructs
# ==========
#
# We represent constructs of syntax rules as instances of respective classes.
# Because the syntax rules use constructs of other rules as building blocks,
# the constructs form a tree. This abstract syntax tree (AST) is our internal
# representation of the program.
#
# We employ the visitor design pattern to separate algorithms that work on the
# AST from the AST's representation: each construct (that is, node in the AST)
# has a method accept() that calls a method in the visitor, depending on the
# construct's type.
#
# See chapter 2 in the thesis for a list of syntax rules; the syntactic
# categorys of these rules imply all classes in this module (and their design).
class Construct(object):
def accept(self, visitor): pass
def copy(self): return Construct()
class Name(Construct):
def __init__(self, n): self.name = n
def accept(self, visitor): visitor.visitName(self)
def copy(self): return Name(self.name)
class Variable(Construct):
def __init__(self, v): self.name = v
def accept(self, visitor): visitor.visitVariable(self)
def copy(self): return Variable(self.name)
class Bool(Construct): pass
class BoolEq(Bool):
def __init__(self, y1, y2):
self.var1 = y1
self.var2 = y2
def accept(self, visitor): visitor.visitBoolEq(self)
def copy(self): return BoolEq(self.var1.copy(), self.var2.copy())
class BoolNeq(Bool):
def __init__(self, y1, y2):
self.var1 = y1
self.var2 = y2
def accept(self, visitor): visitor.visitBoolNeq(self)
def copy(self): return BoolNeq(self.var1.copy(), self.var2.copy())
class Expression(Construct): pass
class VarExpression(Expression):
def __init__(self, y):
self.var = y
def accept(self, visitor): visitor.visitVarExpression(self)
def copy(self): return VarExpression(self.var.copy())
class New(Expression):
def __init__(self, c, a):
self.className = c
if a:
self.arguments = a
else:
self.arguments = []
def accept(self, visitor): visitor.visitNew(self)
def copy(self):
return New(
self.className.copy(),
[ a.copy() for a in self.arguments ]
)
class Call(Expression):
def __init__(self, y, m, a):
self.target = y
self.methodName = m
if a:
self.arguments = a
else:
self.arguments = []
def accept(self, visitor): visitor.visitCall(self)
def copy(self):
return Call(
self.target.copy(),
self.methodName.copy(),
[ a.copy() for a in self.arguments ]
)
class Statement(Construct): pass
class Assign(Statement):
def __init__(self, x, rhs):
self.target = x
self.rhs = rhs
def accept(self, visitor): visitor.visitAssign(self)
def copy(self): return Assign(self.target.copy(), self.rhs.copy())
class Skip(Statement):
def accept(self, visitor): visitor.visitSkip(self)
def copy(self): return Skip()
class Return(Statement):
def __init__(self, y):
self.var = y
def accept(self, visitor): visitor.visitReturn(self)
def copy(self): return Return(self.var.copy())
class Sequence(Construct):
def __init__(self, SS):
self.statements = SS
def accept(self, visitor): visitor.visitSequence(self)
def copy(self): return Sequence( [ s.copy() for s in self.statements ] )
class Block(Statement):
def __init__(self, dv, Q):
self.declaredVars = dv
self.sequence = Q
def accept(self, visitor): visitor.visitBlock(self)
def copy(self):
return Block(
[ v.copy() for v in self.declaredVars ],
self.sequence.copy()
)
class IfThenElse(Statement):
def __init__(self, b, S1, S2):
self.bool = b
self.trueStatement = S1
self.falseStatement = S2
def accept(self, visitor): visitor.visitIfThenElse(self)
def copy(self):
return IfThenElse(
self.bool.copy(),
self.trueStatement.copy(),
self.falseStatement.copy()
)
class While(Statement):
def __init__(self, b, S):
self.bool = b
self.bodyStatement = S
def accept(self, visitor): visitor.visitWhile(self)
def copy(self):
return While(
self.bool.copy(),
self.bodyStatement.copy()
)
class Declaration(Construct): pass
class VariableDeclaration(Declaration):
def __init__(self, v):
self.var = v
def accept(self, visitor): visitor.visitVariableDeclaration(self)
def copy(self): return VariableDeclaration(self.var.copy())
class MethodDeclaration(Declaration):
def __init__(self, m, p, S):
self.methodName = m
if p: self.parameters = p
else: self.parameters = []
self.body = S
def accept(self, visitor): visitor.visitMethodDeclaration(self)
def copy(self):
return MethodDeclaration(
self.methodName.copy(),
[ p.copy() for p in self.parameters ],
self.body.copy()
)
class ConstructorDeclaration(Declaration):
def __init__(self, p, S):
if p: self.parameters = p
else: self.parameters = []
self.body = S
def accept(self, visitor): visitor.visitConstructorDeclaration(self)
def copy(self):
return ConstructorDeclaration(
[ p.copy() for p in self.parameters ],
self.body.copy()
)
class ClassDeclaration(Declaration):
def __init__(self, c, dv, ct, dm):
self.className = c
if dv: self.memberVars = dv
else: self.memberVars = []
self.constructor = ct
if dm: self.methods = dm
else: self.methods = []
def accept(self, visitor): visitor.visitClassDeclaration(self)
def copy(self):
return ClassDeclaration(
self.className.copy(),
self.memberVars.copy(),
self.constructor.copy(),
[ m.copy() for m in self.methods ]
)
class Program(Construct):
def __init__(self, dc, S):
self.classDeclarations = dc
self.initialStatement = S
def accept(self, visitor): visitor.visitProgram(self)
def copy(self):
return Program(
[ c.copy for c in self.classDeclarations ],
self.initialStatement.copy()
)
class ScopedStatement(Construct): pass
class BlockScopedStatement(ScopedStatement):
def __init__(self, B):
self.body = B
def accept(self, visitor): visitor.visitBlockScopedStatement(self)
def copy(self): return BlockScopedStatement(self.body.copy())
class MethodScopedStatement(ScopedStatement):
def __init__(self, B):
self.body = B
def accept(self, visitor): visitor.visitMethodScopedStatement(self)
def copy(self): return MethodScopedStatement(self.body.copy())