-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpret.spec.js
169 lines (141 loc) · 5.56 KB
/
interpret.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
var t = require('../lib/interpret').interpreter;
function isArray(value) {return toString.call(value) === '[object Array]';}
var aps = Array.prototype.slice, toString = Object.prototype.toString;
function simplify(node){
if (isArray(node)) {
var list = [];
node.forEach(function(e){
list.push(simplify(e));
});
return list;
} else {
return node.value;
}
}
describe('LispScript', function() {
describe('lexer', function() {
it('should lex a single atom', function() {
expect(t.scan("a")).toEqual(["a"]);
expect(t.tokenize("a")[0].value).toEqual("a");
});
it('should lex an atom in a list', function() {
expect(t.tokenize("()").length).toEqual(2);
expect(t.tokenize("()")[0].value).toEqual('(');
expect(t.tokenize("()")[1].value).toEqual(')');
});
});
describe('parser', function(){
it('should parse a single atom', function() {
expect(t.parse(t.tokenize('"a"')).value).toEqual("a");
expect(t.parse(t.tokenize('"a"')).type).toEqual("literal");
});
it('should lex an atom in a list', function() {
expect(t.parse(t.tokenize("()"))).toEqual([]);
});
it('should lex multi atom list', function() {
expect(simplify(t.parse(t.tokenize("(hi you)")))).toEqual(["hi", "you"]);
});
it('should lex list containing list', function() {
expect(simplify(t.parse(t.tokenize("((x))")))).toEqual([['x']]);
});
it('should lex list containing list', function() {
expect(simplify(t.parse(t.tokenize("(x (y) z)")))).toEqual(["x", ["y"], "z"]);
});
it('should lex list containing list', function() {
expect(simplify(t.parse(t.tokenize("(x (y) (a b c))")))).toEqual(["x", ["y"], ["a", "b", "c"]]);
});
describe('atoms', function() {
it('should parse out numbers', function() {
expect(simplify(t.parse(t.tokenize("(1 (a 2))")))).toEqual([1, ["a", 2]]);
});
});
describe('print', function(){
it('should print the syntax tree', function(){
t.print(t.parse(t.tokenize('(car (cdr 1 2) 1 (+ 1 3) 2)')));
t.print(t.parse(t.tokenize("(car '(1 2 3))")));
});
});
});
describe('interpret', function() {
var env = function(){
return {
get: function(id){
if (this.hasOwnProperty(id)) {
return this[id];
}
return '#' + id + '#';
},
put: function(id, value) {
this[id] = value;
}
}
}();
function interpret(input){
return t.eval(input, env);
}
describe('lists', function() {
it('should return empty list', function() {
expect(interpret('()')).toEqual([]);
});
it('should return list of strings', function() {
expect(interpret('("hi" "mary" "rose")')).toEqual(['hi', "mary", "rose"]);
});
it('should return list of numbers', function() {
expect(interpret('(1 2 3)')).toEqual([1, 2, 3]);
});
it('should return list of numbers in strings as strings', function() {
expect(interpret('("1" "2" "3")')).toEqual(["1", "2", "3"]);
});
});
describe('atoms', function() {
it('should return string atom', function() {
expect(interpret('"a"')).toEqual("a");
});
it('should return string with space atom', function() {
expect(interpret('"a b"')).toEqual("a b");
});
it('should return string with opening paren', function() {
expect(interpret('"(a"')).toEqual("(a");
});
it('should return string with closing paren', function() {
expect(interpret('")a"')).toEqual(")a");
});
it('should return string with parens', function() {
expect(interpret('"(a)"')).toEqual("(a)");
});
it('should return number atom', function() {
expect(interpret('123')).toEqual(123);
});
});
describe('invocation', function() {
it('should run print on an int', function() {
env.put('print', function(value){
return value;
});
expect(interpret("(print 1)")).toEqual(1);
});
it('should return first element of list', function() {
env.put('car', function(cons){
return cons[0];
});
expect(interpret("(car (1 2 3))")).toEqual(1);
});
it('should return rest of list', function() {
env.put('cdr', function(cons) {
return aps.call(cons, 1);
});
expect(interpret("(cdr (1 2 3))")).toEqual([2, 3]);
});
it('should return sum of list', function() {
env.put('+', function() {
var sum = 0;
for (var i = 0; i < arguments.length; i++) {
sum += arguments[i];
}
return sum;
});
expect(interpret("(+ 1 2 3)")).toEqual(6);
});
});
});
});