-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlisp_interpreter.py
188 lines (178 loc) · 7.46 KB
/
lisp_interpreter.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
from copy import deepcopy
debug = False
PrimitiveDataTypes = [int, float, bool]
class LispEnvironment():
"Lisp Statement Environment holding variable/value pairs"
def __init__(self, inherit=None):
self.inherit = inherit
self.env = {}
def __setitem__(self, variable, value):
self.env[variable] = value
def __getitem__(self, variable):
if variable in self.env:
return self.env[variable]
else:
if self.inherit is not None:
return self.inherit[variable]
else:
raise KeyError("No such variable, "+variable+".")
def __repr__(self):
if self.inherit is None:
return "GlobalEnv"
else:
return str(self.env)+"->"+str(self.inherit)
class LispProcedure():
"Defines a lisp procedure given by a lambda function"
def __init__(self, tree, outer_env):
"""Given a tree like (lambda (x) (* x x)), create an internal
representation of the procedure"""
if debug:
print "Creating procedure,", tree, "(%s)"%(str(outer_env))
self.tree = tree
assert self.tree[0] == "lambda"
assert len(self.tree) == 3
assert type(self.tree[1]) == list
# TODO: handle above cases more nicely
self.num_args = len(self.tree[1])
self.args = self.tree[1]
for a in self.args:
assert type(a) == str
self.return_statement = self.tree[2]
self.outer_env = outer_env
def apply(self, arg_values):
"""Applies the procedure to the given arguments, which are
already evaluated. arg_values should be list of primitive
numbers or procedures"""
self.internal_env = LispEnvironment(self.outer_env)
if debug:
print "Applying procedure,", self.tree, "(%s)"%(str(self.internal_env))
assert type(arg_values) == list
for x in arg_values:
assert (type(x) in PrimitiveDataTypes or
isinstance(x,LispProcedure))
# first check whether arguments match in number
assert len(arg_values) == self.num_args
# then unpack arguments floato environment
for (variable,value) in zip(self.args,arg_values):
self.internal_env[variable] = value
# return a LispStatement with a new environment
if debug:
print "returning from procedure",self.tree,arg_values,self.return_statement, self.internal_env
return LispStatement(self.return_statement,
LispEnvironment(self.internal_env))
class PrimitiveLispProcedure(LispProcedure):
def __init__(self, name, fxn):
self.name = name
self.fxn = fxn
def apply(self, arg_values):
if debug:
print "calling primitive procedure",self.name,"on", arg_values
return LispStatement(self.fxn(arg_values))
class LispStatement():
"The internal representation of a lisp statement"
def __init__(self, tree, env=None):
# if debug:
# print "Creating statement,", tree
# if env is not None:
# print "Environment:", env
self.tree = tree
if env is None:
env = globalEnv
self.environment = env
def evaluate(self):
"""
Evaluates the statement and returns its value.
This is done according to the following steps:
- Primitive Data elements return their own value
- Primitive Procedures evaluate to themselves
- Special forms are handled specially
- To evaluate an application:
1. Evaluate the operator
2. Evaluate the operands
3. Apply the procedure, by:
a. Copying the body of the procedure;
while substituting formal parameters
b. Evaluating the new body
"""
if debug:
print "Evaluating statement,", self.tree, "(%s)"%(str(self.environment))
if type(self.tree) == list:
# either spl form or application
operator = self.tree[0]
# if operator denotes spl form, use spl rules
if operator == "define":
# TODO: refactor floato another function or class
assert len(self.tree) == 3
a = self.tree[1]
b = self.tree[2]
globalEnv[a] = LispStatement(b).evaluate()
return self.environment[a]
elif operator == "lambda":
return LispProcedure(self.tree, self.environment)
elif operator == "if":
if_cond = self.tree[1]
if_true = self.tree[2]
if_false = self.tree[3]
if LispStatement(if_cond,
self.environment).evaluate():
return LispStatement(if_true,
self.environment).evaluate()
else:
return LispStatement(if_false,
self.environment).evaluate()
pass
else:
# otherwise find operator in environment
if isinstance(operator, LispProcedure):
pass
else:
operator = self.environment[operator]
if isinstance(operator, LispProcedure):
pass
elif type(operator) in PrimitiveDataTypes:
assert len(self.tree) == 1
return operator
# also check "upper" environments
# make sure it is a function
# evaluate arguments
operands = [LispStatement(
self.tree[i],
self.environment
).evaluate()
for i in range(1,len(self.tree))
]
# apply the function
new_statement = operator.apply(operands)
if debug:
print "New statement is evaluated to,",
print new_statement.tree
return new_statement.evaluate()
else:
# it is either primitive data or primitive procedure
# or it might be VARIABLE NAME denoting one of these
x = self.tree
if type(x) in PrimitiveDataTypes:
# primitive data ... return directly
if debug:
print "Statement evaluated to,",x
return x
elif type(x) == str:
if debug:
print "Statement evaluated to,",self.environment[x]
return self.environment[x]
###################################################################
# define global env
globalEnv = LispEnvironment()
globalEnv['+'] = PrimitiveLispProcedure('+',lambda l: sum(l))
globalEnv['-'] = PrimitiveLispProcedure('-',
lambda l: (l[0]-l[1] if len(l)==2 else -l[0])
)
globalEnv['*'] = PrimitiveLispProcedure('*',
lambda l: reduce(lambda x,y:x*y, l)
)
globalEnv['/'] = PrimitiveLispProcedure('/',lambda (x,y): float(x)/y)
globalEnv['>'] = PrimitiveLispProcedure('>',lambda (x,y): x>y)
globalEnv['<'] = PrimitiveLispProcedure('<',lambda (x,y): x<y)
globalEnv['>='] = PrimitiveLispProcedure('>=',lambda (x,y): x>=y)
globalEnv['<='] = PrimitiveLispProcedure('<=',lambda (x,y): x<=y)
globalEnv['abs'] = PrimitiveLispProcedure('abs',lambda (x,): abs(x))