-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvironment.py
More file actions
187 lines (131 loc) · 5.9 KB
/
Copy pathenvironment.py
File metadata and controls
187 lines (131 loc) · 5.9 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
#
# eval functions
#
import parser
import builtin
from lisp import *
class EvalError(LispError): pass
class Environment:
def __init__(self, symbols = None):
if symbols is None:
symbols = builtin.TABLE
self.symbols = symbols.copy()
#
# file loading
#
def import_file(self, fn):
with open(fn, "r") as f:
lisp_file = parser.file_parser.parse(f.read())
for item in lisp_file:
self.interpret(item)
#
# interpret
#
def interpret_single_line(self, line):
return self.interpret(parser.line_parser.parse(line), True)
def interpret(self, instruction, interactive = False):
if not instruction.is_atom() and type(instruction[0]) == LispSym:
cmd = instruction.head()
parameter = instruction.tail()
if cmd == "set":
if len(parameter) != 2:
raise EvalError("set expects exactly two parameter")
if type(parameter[0]) != LispSym:
raise EvalError("set expects symbol as first parameter")
value = self.evaluate(parameter[1])
self.symbols[parameter[0]] = value
return value
if cmd == "defun":
if len(parameter) != 3:
raise EvalError("defun expects three parameter")
symbol = parameter[0]
lambda_parameter = parameter[1]
lambda_body = parameter[2]
if type(symbol) != LispSym:
raise EvalError("defun expects symbol as first parameter")
if type(lambda_parameter) != LispList:
raise EvalError("defun expects list of symbols as second parameter")
# construct lambda
function = LispList([ LispSym("lambda"),
lambda_parameter,
lambda_body ])
value = LispLambda(function)
self.symbols[symbol] = value
return value
if cmd == "symbols":
return LispList(self.symbols)
if interactive:
return self.evaluate(instruction)
raise EvalError("illegal instruction: %s" % (instruction))
def evaluate(self, expr, stack = None, bindings = 0):
if stack is None:
stack = []
while True:
#print("eval: %s stack: %s bindings: %d" % (expr, stack, bindings))
if expr.is_atom():
if type(expr) == LispSym:
if expr in self.symbols:
return self.symbols[expr]
raise EvalError("%s: unknown symbol" % (expr))
if type(expr) == LispRef:
return stack[-int(expr)]
if type(expr) == LispLambda:
return expr
if type(expr) == LispClosure:
return expr.capture(stack)
return expr
function = expr.head()
parameter = expr.tail()
if type(function) == LispSym:
if function == 'quote':
if len(parameter) != 1:
raise EvalError('quote expects exactly one parameter')
return parameter[0]
elif function == 'if':
if len(parameter) != 3:
raise EvalError('if expects exactly 3 parameters')
if self.evaluate(parameter[0], stack).is_true():
expr = parameter[1]
else:
expr = parameter[2]
continue
elif function == 'eval':
if len(parameter) != 1:
raise EvalError('eval expects exactly one parameter')
expr = self.evaluate(parameter[0], stack)
continue
elif function == 'lambda':
return LispLambda(expr)
evalpar = [ self.evaluate(p, stack) for p in parameter ]
evalfun = self.evaluate(function, stack)
if type(evalfun) == LispBuiltin:
if evalfun.argc is not None and evalfun.argc != len(evalpar):
raise EvalError("%s: expects %d parameter, got %d"
% (function, evalfun.argc, len(evalpar)))
try:
return evalfun.function(*evalpar)
except TypeError:
raise EvalError("%s: illegal number of parameter to builtin function"
% (function))
if isinstance(evalfun, LispLambda):
if evalfun.argc != len(evalpar):
raise EvalError("%s: expects %d parameter, got %d" %
(function, evalfun.argc, len(evalpar)))
#print("[DEBUG] evalfun: %s, evalpar: %s, function: %s" % (evalfun, evalpar, function))
# check if we are executing an explicit lisp-lambda instead of an
# indirect one via symbol or call result. In case of a direct
# lambda, we are not allowed to clear current bindings, because
# it's body might still refer to them.
if type(function) != LispLambda and bindings > 0:
stack = stack[:-bindings]
bindings = 0
# add evaluated parameters to stack as new bindings
stack = stack + evalpar
bindings += len(evalpar)
# if we execute a closure, add captured values to stack
if type(evalfun) == LispClosure:
stack = stack + evalfun.capture_values
bindings += len(evalfun.capture_values)
expr = evalfun.body
continue
raise EvalError("%s: not executable" % (evalfun))