-
Notifications
You must be signed in to change notification settings - Fork 3
/
lisp-eval.js
301 lines (262 loc) · 8.61 KB
/
lisp-eval.js
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Evaluation engine
// Evaluation context, as a list of strings
lisp.stackTrace = [];
// A lisp function value. run has to be a function(args)
// (or just a function(args)
lisp.Func = function(name, run) {
this.name = name;
this.run = run;
};
lisp.Func.prototype = {
type: 'function',
print: function() {
return '<function ' + this.name + '>';
},
eval: function() {
throw 'trying to evaluate ' + this.print() + ' again';
}
};
lisp.Number.prototype.eval = function() {
return this;
};
lisp.nil.eval = function() {
return this;
};
lisp.Symbol.prototype.eval = function(env) {
var v = env.get(this.s);
if (v)
return v;
else
throw 'undefined variable: ' + this.s;
};
lisp.Cons.prototype.eval = function(env) {
var args = lisp.termToList(this.cdr);
// check for special forms
if (this.car.type == 'symbol') {
var s = this.car.s;
switch (s) {
case 'if': {
lisp.checkNumArgs('if', 3, args);
var test = args[0].eval(env);
if (test.type == 'nil')
return args[2].eval(env);
else
return args[1].eval(env);
}
case 'when': {
if (args.length == 0)
throw 'too few arguments to ' + s;
var test = args[0].eval(env);
if (test.type == 'nil')
return lisp.nil;
else
return lisp.evalList(args, 1, env);
}
case 'quote': {
lisp.checkNumArgs('quote', 1, args);
return args[0];
}
case 'quasiquote':
return lisp.evalQuasi(this, 0, env);
case 'let': {
if (args.length == 0)
throw 'too few arguments to let';
// first, parse the bindings for let
// we expect a list: (name value name value...)
var bindings = lisp.termToList(args[0]);
if (bindings.length % 2 != 0)
throw 'bad bindings format: ' + args[0].print();
var vars = {};
for (var i = 0; i < bindings.length; i += 2) {
lisp.checkType(bindings[i], 'symbol');
var name = bindings[i].s;
var value = bindings[i + 1].eval(env);
vars[name] = value;
}
// now evaluate the rest of the form in a new environment
var newEnv = new lisp.Env(vars, env);
return lisp.evalList(args, 1, newEnv);
}
case 'do':
return lisp.evalList(args, 0, env);
case 'lambda': {
if (args.length == 0)
throw 'too few arguments to lambda';
return lisp.makeFuncFromDef(env, args, '(lambda)');
}
case 'define':
case 'defmacro': {
if (args.length == 0)
throw 'too few arguments to ' + s;
if (args[0].type == 'symbol') {
// form: (define x ...)
if (s == 'defmacro')
throw 'symbol macros are not supported';
var name = args[0].s;
var value = lisp.evalList(args, 1, env);
lisp.env.vars[name] = value;
return value;
} else {
// form: (define (f ...) ...)
lisp.checkType(args[0], 'cons');
lisp.checkType(args[0].car, 'symbol');
var name = args[0].car.s;
// pop the name from args[0]
args[0] = args[0].cdr;
var func = lisp.makeFuncFromDef(env, args, name);
if (s == 'define') {
lisp.env.vars[name] = func;
return func;
} else {
lisp.addMacro(name, func);
return new lisp.Symbol(name);
}
}
}
case 'set!': {
lisp.checkNumArgs('set!', 2, args);
lisp.checkType(args[0], 'symbol');
var name = args[0].s;
var value = args[1].eval(env);
if (!env.set(name, value))
throw 'undefined variable: ' + name;
return value;
}
default: // not a special form, do nothing (just proceed)
}
}
// ordinary function
var car = this.car.eval(env);
lisp.checkType(car, 'function');
for (var i = 0; i < args.length; ++i)
args[i] = args[i].eval(env);
lisp.stackTrace.push(this.print());
var result = car.run(args);
lisp.stackTrace.pop();
return result;
};
lisp.evalList = function(terms, start, env) {
var value = lisp.nil;
for (var i = start; i < terms.length; i++) {
value = terms[i].eval(env);
}
return value;
};
// Make a Func from arguments to (defun ...) or (lambda ...)
lisp.makeFuncFromDef = function(env, args, name) {
var paramNames = [];
var restParam = null;
var params = args[0];
// Parse the argument list
while (params.type == 'cons') {
lisp.checkType(params.car, 'symbol');
paramNames.push(params.car.s);
params = params.cdr;
}
// Last argument - either a 'rest' symbol, e.g. (a b . c),
// or nil
if (params.type == 'symbol')
restParam = params.s;
else
lisp.checkType(params, 'nil');
return new lisp.Func(
name, function(funcArgs) {
// bind the values to param names
if (restParam == null)
lisp.checkNumArgs(name, paramNames.length, funcArgs);
else {
if (funcArgs.length < paramNames.length)
throw 'too few arguments for ' + name;
}
var vars = {};
for (var i = 0; i < paramNames.length; i++)
vars[paramNames[i]] = funcArgs[i];
if (restParam != null) {
vars[restParam] = lisp.listToTerm(funcArgs.slice(paramNames.length));
}
// now evaluate the function body
var newEnv = new lisp.Env(vars, env);
return lisp.evalList(args, 1, newEnv);
});
};
// A basic Lisp variables environment.
lisp.Env = function(vars, parent) {
this.vars = vars;
this.parent = parent;
vars: {};
};
lisp.Env.prototype = {
// variable lookup
get: function(name) {
if (name in this.vars)
return this.vars[name];
else if (this.parent)
return this.parent.get(name);
return null;
},
set: function(name, value) {
if (name in this.vars) {
this.vars[name] = value;
return true;
} else if (this.parent)
return this.parent.set(name, value);
return false;
}
};
lisp.env = new lisp.Env({}, null);
lisp.evalQuasi = function(term, level, env) {
if (term.type == 'cons' && term.car.type == 'symbol') {
var s = term.car.s;
if (s == 'quasiquote' || s == 'unquote') {
var args = lisp.termToList(term.cdr);
lisp.checkNumArgs(s, 1, args);
if (s == 'quasiquote') {
if (level == 0)
return lisp.evalQuasi(args[0], level + 1, env);
else // level > 0
return lisp.form1('quasiquote',
lisp.evalQuasi(args[0], level + 1, env));
} else { // s == 'unquote'
if (level == 0)
throw 'unquote without quasiquote';
else if (level == 1)
return args[0].eval(env);
else // level > 1
return lisp.form1('unquote',
lisp.evalQuasi(args[0], level - 1, env));
}
}
}
// not a quasiquote or unquote
if (level == 0)
return term.eval(env);
else {
if (term.type == 'cons')
return new lisp.Cons(lisp.evalQuasi(term.car, level, env),
lisp.evalQuasi(term.cdr, level, env));
else
return term;
}
};
// Handle stack traces in code
lisp.runWithStackTrace = function(func) {
try {
func();
lisp.stackTrace = [];
} catch (err) {
while (lisp.stackTrace.length > 0)
err += '\nin ' + lisp.stackTrace.pop();
throw err;
}
};
// Macroexpand and evaluate code; handle stack traces
lisp.evalCode = function(term, env) {
lisp.runWithStackTrace(function() {
term = lisp.macroExpand(term);
});
var result;
lisp.runWithStackTrace(function() {
result = term.eval(env);
});
return result;
};