-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree.py
More file actions
196 lines (134 loc) · 4.91 KB
/
Copy pathTree.py
File metadata and controls
196 lines (134 loc) · 4.91 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
from typing import Dict, List
class EvaluationException(Exception):
''' Indicates some error while evaluating a term. '''
pass
class UnboundVarException(EvaluationException):
''' Indicates that a variable should be resolved while it is not defined/in scope. '''
def __init__(self, symbol):
super().__init__(f"Undefined variable {symbol}")
class UnevaluatedArgumentException(EvaluationException):
''' Indicates that an argument to a lambda has not been evaluated to a value (lambda or numeral). Note: This is call by value. '''
def __init__(self, arg):
super().__init__(f"Argument must be a value but is {arg}")
class NotCallableException(EvaluationException):
''' Indicates that the left hand side of an application is not (evaluated to )a lambda expression and cannot be called. '''
def __init__(self, func):
super().__init__(f"{func} is not callable.")
class BinOpOperandException(EvaluationException):
''' Indicates that a binary operation should operate on something that is not a number. '''
def __init__(self, opSymbol, pos, arg):
if pos == 0:
posStr = "First"
else:
posStr = "Second"
super().__init__(f"{posStr} argument of {opSymbol} is not a number: {arg}")
class Node:
''' This abstract class represents any node of the syntax tree '''
def __init__(self, *children: "Node"):
self._children: List[Node] = list(children)
def is_value(self) -> bool:
return False
def evaluate(self, ctx: Dict[str, "Node"]) -> "Node":
raise NotImplementedError()
def parse(self, pi) -> "Node":
raise NotImplementedError()
def debug(self, ctx):
# print(f"Evaluating {self} with ctx={ctx}")
pass
class Numeral(Node):
def __init__(self, value: int):
super().__init__()
self.value = value
def evaluate(self, ctx):
self.debug(ctx)
return self
def is_value(self):
return True
def __str__(self):
return str(self.value)
class Var(Node):
def __init__(self, symbol: str):
super().__init__()
self.symbol = symbol
def evaluate(self, ctx):
self.debug(ctx)
if self.symbol not in ctx:
raise UnboundVarException(self.symbol)
return ctx[self.symbol]
def __str__(self):
return self.symbol
# Convenience variables
X = Var("x")
Y = Var("y")
F = Var("f")
class Lambda(Node):
def __init__(self, param: Var, body: Node):
super().__init__(body)
self.param = param.symbol
self._closure = None
def evaluate(self, ctx):
# return a closure (copy of this lambda with its context captured)
result = Lambda(
Var(self.param),
self._children[0]
)
result._closure = ctx.copy()
return result
def call(self, arg: Node):
if not arg.is_value():
raise UnevaluatedArgumentException(arg)
new_ctx = self._closure.copy() | { self.param: arg }
return self._children[0].evaluate(new_ctx)
def is_value(self):
return True
def __str__(self):
return f"(λ{self.param}.{self._children[0]})"
class LambdaX(Lambda):
def __init__(self, body):
super().__init__(X, body)
class LambdaY(Lambda):
def __init__(self, body):
super().__init__(Y, body)
class LambdaF(Lambda):
def __init__(self, body):
super().__init__(F, body)
class Application(Node):
def evaluate(self, ctx):
self.debug(ctx)
func = self._children[0].evaluate(ctx)
if not isinstance(func, Lambda):
raise NotCallableException(func)
arg = self._children[1].evaluate(ctx)
return func.call(arg)
def __str__(self):
return f"({self._children[0]} {self._children[1]})"
class IfStmt(Node):
def evaluate(self, ctx):
self.debug(ctx)
cond = self._children[0].evaluate(ctx)
branch = 1 if not isinstance(cond, Numeral) or cond.value != 0 else 2
return self._children[branch].evaluate(ctx)
def __str__(self):
return "(if {} then {} else {})".format(*self._children[0:3])
class BinOp(Node):
def op(self, a, b):
raise NotImplementedError()
def evaluate(self, ctx):
self.debug(ctx)
a = self._children[0].evaluate(ctx)
if not isinstance(a, Numeral):
raise BinOpOperandException(self._opSymbol, 0, a)
b = self._children[1].evaluate(ctx)
if not isinstance(b, Numeral):
raise BinOpOperandException(self._opSymbol, 1, b)
return Numeral(self.op(a.value, b.value))
def __str__(self):
return f"({self._children[0]} {self._opSymbol} {self._children[1]})"
class Sum(BinOp):
_opSymbol = "+"
def op(self, a, b):
return a + b
class Product(BinOp):
_opSymbol = "*"
def op(self, a, b):
return a * b