-
Notifications
You must be signed in to change notification settings - Fork 84
/
littlelisp.spec.js
168 lines (135 loc) · 5.5 KB
/
littlelisp.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
var t = require('./littlelisp').littleLisp;
var is = function(input, type) {
return Object.prototype.toString.call(input) === '[object ' + type + ']';
};
// takes an AST and replaces type annotated nodes with raw values
var unannotate = function(input) {
if (is(input, 'Array')) {
if (input[0] === undefined) {
return [];
} else if (is(input[0], 'Array')) {
return [unannotate(input[0])].concat(unannotate(input.slice(1)));
} else {
return unannotate(input[0]).concat(unannotate(input.slice(1)));
}
} else {
return [input.value];
}
};
describe('littleLisp', function() {
describe('parse', function() {
it('should lex a single atom', function() {
expect(t.parse("a").value).toEqual("a");
});
it('should lex an atom in a list', function() {
expect(unannotate(t.parse("()"))).toEqual([]);
});
it('should lex multi atom list', function() {
expect(unannotate(t.parse("(hi you)"))).toEqual(["hi", "you"]);
});
it('should lex list containing list', function() {
expect(unannotate(t.parse("((x))"))).toEqual([["x"]]);
});
it('should lex list containing list', function() {
expect(unannotate(t.parse("(x (x))"))).toEqual(["x", ["x"]]);
});
it('should lex list containing list', function() {
expect(unannotate(t.parse("(x y)"))).toEqual(["x", "y"]);
});
it('should lex list containing list', function() {
expect(unannotate(t.parse("(x (y) z)"))).toEqual(["x", ["y"], "z"]);
});
it('should lex list containing list', function() {
expect(unannotate(t.parse("(x (y) (a b c))"))).toEqual(["x", ["y"], ["a", "b", "c"]]);
});
describe('atoms', function() {
it('should parse out numbers', function() {
expect(unannotate(t.parse("(1 (a 2))"))).toEqual([1, ["a", 2]]);
});
});
});
describe('interpret', function() {
describe('lists', function() {
it('should return empty list', function() {
expect(t.interpret(t.parse('()'))).toEqual([]);
});
it('should return list of strings', function() {
expect(t.interpret(t.parse('("hi" "mary" "rose")'))).toEqual(['hi', "mary", "rose"]);
});
it('should return list of numbers', function() {
expect(t.interpret(t.parse('(1 2 3)'))).toEqual([1, 2, 3]);
});
it('should return list of numbers in strings as strings', function() {
expect(t.interpret(t.parse('("1" "2" "3")'))).toEqual(["1", "2", "3"]);
});
});
describe('atoms', function() {
it('should return string atom', function() {
expect(t.interpret(t.parse('"a"'))).toEqual("a");
});
it('should return string with space atom', function() {
expect(t.interpret(t.parse('"a b"'))).toEqual("a b");
});
it('should return string with opening paren', function() {
expect(t.interpret(t.parse('"(a"'))).toEqual("(a");
});
it('should return string with closing paren', function() {
expect(t.interpret(t.parse('")a"'))).toEqual(")a");
});
it('should return string with parens', function() {
expect(t.interpret(t.parse('"(a)"'))).toEqual("(a)");
});
it('should return number atom', function() {
expect(t.interpret(t.parse('123'))).toEqual(123);
});
});
describe('invocation', function() {
it('should run print on an int', function() {
expect(t.interpret(t.parse("(print 1)"))).toEqual(1);
});
it('should return first element of list', function() {
expect(t.interpret(t.parse("(first (1 2 3))"))).toEqual(1);
});
it('should return rest of list', function() {
expect(t.interpret(t.parse("(rest (1 2 3))"))).toEqual([2, 3]);
});
});
describe('lambdas', function() {
it('should return correct result when invoke lambda w no params', function() {
expect(t.interpret(t.parse("((lambda () (rest (1 2))))"))).toEqual([2]);
});
it('should return correct result for lambda that takes and returns arg', function() {
expect(t.interpret(t.parse("((lambda (x) x) 1)"))).toEqual(1);
});
it('should return correct result for lambda that returns list of vars', function() {
expect(t.interpret(t.parse("((lambda (x y) (x y)) 1 2)"))).toEqual([1, 2]);
});
it('should get correct result for lambda that returns list of lits + vars', function() {
expect(t.interpret(t.parse("((lambda (x y) (0 x y)) 1 2)"))).toEqual([0, 1, 2]);
});
it('should return correct result when invoke lambda w params', function() {
expect(t.interpret(t.parse("((lambda (x) (first (x))) 1)")))
.toEqual(1);
});
});
describe('let', function() {
it('should eval inner expression w names bound', function() {
expect(t.interpret(t.parse("(let ((x 1) (y 2)) (x y))"))).toEqual([1, 2]);
});
it('should not expose parallel bindings to each other', function() {
// Expecting undefined for y to be consistent with normal
// identifier resolution in littleLisp.
expect(t.interpret(t.parse("(let ((x 1) (y x)) (x y))"))).toEqual([1, undefined]);
});
it('should accept empty binding list', function() {
expect(t.interpret(t.parse("(let () 42)"))).toEqual(42);
});
});
describe('if', function() {
it('should choose the right branch', function() {
expect(t.interpret(t.parse("(if 1 42 4711)"))).toEqual(42);
expect(t.interpret(t.parse("(if 0 42 4711)"))).toEqual(4711);
});
});
});
});