-
Notifications
You must be signed in to change notification settings - Fork 0
/
lisp-spec.js
297 lines (252 loc) · 7.58 KB
/
lisp-spec.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
var assert = require('assert');
var interpreter = require('../src/interpreter');
var env = require('../src/env');
var lisp = require('../src/lisp');
describe('lispjs', function () {
function identity(arg) {
return arg;
}
function constantly(value) {
return function () {
return value;
};
}
var getDefaultEnv = env.getDefaultEnv;
function evaluate(expr, env) {
env = env || getDefaultEnv();
return interpreter.evaluate(expr, env);
}
describe('simple evaluation', function () {
it('evaluates simple values as themselves', function () {
var exprs = [1, 'foo', true, {foo: 'bar'}];
exprs.forEach(function (expr) {
assert.strictEqual(evaluate(expr), expr);
});
});
it('evaluates bound symbols to their values', function () {
var expr = 'foo';
var env = {foo: 'bar'};
assert.equal(evaluate(expr, env), 'bar');
});
});
describe('function invocation', function () {
it('invokes a function without arguments', function () {
var expr = ['foo'];
var env = {foo: constantly('bar')};
assert.equal(evaluate(expr, env), 'bar');
});
it('invokes a function with arguments', function () {
var expr = ['foo', 'bar'];
var env = {foo: identity};
assert.equal(evaluate(expr, env), 'bar');
});
it('evaluates function arguments before invoking a function', function () {
var expr = ['foo', 'bar'];
var env = {foo: identity, bar: 'baz'};
assert.equal(evaluate(expr, env), 'baz');
});
it('invokes anonymous functions', function () {
var expr = [['lambda', ['a', 'b'], ['+', 'a', 'b']], 1, 2];
assert.equal(evaluate(expr), 3);
});
});
describe('define', function () {
it('binds values in environment', function () {
var prog = [
['define', 'foo', 'bar'],
'foo'
];
assert.equal(lisp.run(prog), 'bar');
});
it('evaluates values', function () {
var prog = [
['define', 'bar', 'baz'],
['define', 'foo', 'bar'],
'foo'
];
assert.equal(lisp.run(prog), 'baz');
});
});
describe('if', function () {
it('returns first form if condition evaluates to true', function () {
var expr = ['if', true, 'foo', 'bar'];
assert.equal(evaluate(expr), 'foo');
});
it('returns second form if condition evaluates to false', function () {
var expr = ['if', false, 'foo', 'bar'];
assert.equal(evaluate(expr), 'bar');
});
it('evaluates first form if condition evaluates to true', function () {
var prog = [
['define', 'then', 'foo'],
['if', true, 'then', 'otherwise']
];
assert.equal(lisp.run(prog), 'foo');
});
it('evaluates first form if condition evaluates to true', function () {
var prog = [
['define', 'otherwise', 'bar'],
['if', false, 'then', 'otherwise']
];
assert.equal(lisp.run(prog), 'bar');
});
it('evaluates condition before branching', function () {
var prog = [
['define', 'cond', false],
['if', 'cond', 'foo', 'bar']
];
assert.equal(lisp.run(prog), 'bar');
});
});
describe('quote', function () {
it('returns passed value without evaluation', function () {
var prog = [
['define', 'foo', 'bar'],
['quote', ['foo']]
];
assert.deepEqual(lisp.run(prog), ['foo']);
});
});
describe('lambdas', function () {
it('defines simple function without arguments', function () {
var prog = [
['define', 'foo', ['lambda', [], 'bar']],
['foo']
];
assert.equal(lisp.run(prog), 'bar');
});
it('binds arguments to call values', function () {
var prog = [
['define', 'foo', ['lambda', ['arg'], 'arg']],
['foo', 'bar']
];
assert.equal(lisp.run(prog), 'bar');
});
it('supports lexical scope', function () {
var prog = [
['define', 'foo', 'bar'],
['define', 'bar', ['lambda', [], 'foo']],
['bar']
];
assert.equal(lisp.run(prog), 'bar');
});
it('supports currying', function () {
var prog = [
['define', 'foo',
['lambda', ['a'],
['lambda', ['b'],
['+', 'a', 'b']]]],
[['foo', 1], 2]
];
assert.equal(lisp.run(prog), 3);
});
it('gives local scope higher priority', function () {
var prog = [
['define', 'foo', 'bar'],
['define', 'bar', ['lambda', ['foo'], 'foo']],
['bar', 'baz']
];
assert.equal(lisp.run(prog), 'baz');
});
it('throws when trying to invoke a symbol which is not bound to a function', function () {
function block() {
var prog = [
['define', 'foo', 'bar'],
['foo']
];
lisp.run(prog);
}
assert.throws(block, /'foo' is not a function/);
});
});
describe('macros', function () {
it('enables simple macros', function () {
var prog = [
['defmacro', 'infix', ['expr'],
['list',
['nth', 'expr', 1],
['nth', 'expr', 0],
['nth', 'expr', 2]]],
['infix', [1, '+', 2]]
];
assert.equal(lisp.run(prog), 3);
});
it('defines the let bindings macro in core environment', function () {
var expr = ['let', ['a', 1, 'b', 2], ['+', 'a', 'b']];
assert.equal(evaluate(expr), 3);
});
});
describe('higher order functions', function () {
it('supports map', function () {
var prog = [
// Define some collection
['define', 'coll', ['list', 1, 2, 3]],
// Map collection with 'inc'
['map', 'inc', 'coll']
];
assert.deepEqual(lisp.run(prog), [2, 3, 4]);
});
it('supports filter', function () {
var prog = [
// Define some collection
['define', 'coll', ['list', 1, 2, 3]],
// Filter odd numbers
['filter', 'odd', 'coll']
];
assert.deepEqual(lisp.run(prog), [1, 3]);
});
it('supports reduce', function () {
var prog = [
// Define some collection
['define', 'coll', ['list', 1, 2, 3]],
// Sum numbers
['reduce', 'coll', '+', 0]
];
assert.equal(lisp.run(prog), 6);
});
it('supports the Y-combinator', function () {
var prog = [
// Define the Y-combinator
['define', 'Y',
['lambda', ['le'],
[['lambda', ['f'], ['f', 'f']],
['lambda', ['f'],
['le', ['lambda', ['x'], [['f', 'f'], 'x']]]]]]],
// Define factorial without self-reference
['define', 'fact',
['lambda', ['f'],
['lambda', ['n'],
['if', ['zero', 'n'],
1,
['*', 'n', ['f', ['dec', 'n']]]]]]],
// Apply factorial using the Y-combinator
[['Y', 'fact'], 5]
];
assert.equal(lisp.run(prog), 120);
});
});
describe('programs tests', function () {
it('calculates fibonacci recursively', function () {
var fib =
['defun', 'fib', ['n'],
['if', ['<', 'n', 2],
1,
['+',
['fib', ['-', 'n', 1]],
['fib', ['-', 'n', 2]]]]];
var env = getDefaultEnv();
evaluate(fib, env);
var tests = [
{input: 0, output: 1},
{input: 1, output: 1},
{input: 2, output: 2},
{input: 3, output: 3},
{input: 4, output: 5},
{input: 5, output: 8}
];
tests.forEach(function (test) {
assert.equal(evaluate(['fib', test.input], env), test.output);
});
});
});
});