-
Notifications
You must be signed in to change notification settings - Fork 0
/
representation.py
171 lines (120 loc) · 3.8 KB
/
representation.py
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
# data
class Int:
def __init__(self, literal):
self.value = int(literal)
def __str__(self):
return str(self.value)
def eval(self, env):
return self.value
class Float:
def __init__(self, literal):
self.value = float(literal)
def __str__(self):
return str(self.value)
def eval(self, env):
return self.value
class Symbol:
def __init__(self, literal):
self.value = literal
def __str__(self):
return "'%s'" % self.value
def eval(self, env):
return env.lookup(self.value)
class List:
def __init__(self, s_exp):
self.s_exp = s_exp
def __str__(self):
return str(self.s_exp)
def eval(self, env):
return self.s_exp
# expressions
class Assignment:
def __init__(self, symbol, value_exp):
self.identifier = symbol.value
self.value_exp = value_exp
def __str__(self):
return "%s := %s" % (str(self.identifier), str(self.value_exp))
def eval(self, env):
env.find_env(self.identifier)[self.identifier] = self.value_exp.eval(env)
return None
class Program:
def __init__(self, expressions):
self.exps = expressions
def __str__(self):
return '\n'.join([str(e) for e in self.exps])
def eval(self, env):
v = None
for e in self.exps:
v = e.eval(env)
return v
class If:
def __init__(self, cond, when_true, when_false):
self.cond = cond
self.when_true = when_true
self.when_false = when_false
def __str__(self):
return 'if (%s) then (%s) else (%s)' % (
str(self.cond), str(self.when_true), str(self.when_false))
def eval(self, env):
if self.cond.eval(env):
return self.when_true.eval(env)
else:
return self.when_false.eval(env)
class Application:
def __init__(self, proc_to_be, args):
self.proc_to_be = proc_to_be
self.args = args
def __str__(self):
return '(%s)(%s)' % (
str(self.proc_to_be), ', '.join(list(map(str, self.args))))
def eval(self, env):
procedure = self.proc_to_be.eval(env)
evaluated_args = [a.eval(env) for a in self.args]
# if isinstance(self.proc_to_be, Symbol):
# print('applying', self.proc_to_be.value, 'to', evaluated_args)
# else:
# print('applying to', evaluated_args)
return procedure(evaluated_args)
class Define:
def __init__(self, name, value):
self.name = name.value
self.value = value
def __str__(self):
return '%s = (%s)' % (self.name, str(self.value))
def eval(self, env):
env.set(self.name, self.value.eval(env))
return None
class DefineProc:
def __init__(self, name, params, body):
self.name = name.value
self.params = params
self.body = body
def __str__(self):
return '%s = <proc object>' % self.name
def eval(self, env):
env.set(self.name, Procedure(self.params, self.body, env))
return None
class Lambda:
def __init__(self, params, body):
self.params = params
self.body = body
def __str__(self):
return '<lambda object>'
def eval(self, env):
return Procedure(self.params, [self.body], env)
# procedure
class Procedure:
def __init__(self, params, body, env):
self.params = params
self.body = body
self.env = env
def __repr__(self):
return "<proc object>"
# apply
def __call__(self, args):
nested_env = self.env.nested()
for symbol, value in zip(self.params, args):
nested_env.set(symbol.value, value)
for expression in self.body[:-1]:
expression.eval(nested_env)
return self.body[-1].eval(nested_env)