-
Notifications
You must be signed in to change notification settings - Fork 0
/
jisp.js
207 lines (177 loc) · 5.07 KB
/
jisp.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
Array.prototype.toString = function() {
return '(' + this.join(' ') + ')';
};
function pushEnv(env) {
var newEnv = function() { };
newEnv.prototype = env;
return new newEnv;
};
function evaluate(expr, env) {
env = pushEnv(env);
if (typeof (expr) === 'string') {
var value;
if (!isNaN((value = parseFloat(expr))))
return [value, env];
if (expr === 'true')
return [true, env];
if (expr === 'false')
return [false, env];
if (expr === 'null')
return [null, env];
return [env[expr], env];
}
var fn = expr[0];
var args = expr.slice(1);
if (fn === 'define') {
var r = evaluate(args[1], env);
env = r[1];
env[args[0]] = r[0];
return [null, env];
}
if (fn === 'list')
return [args.map(function(arg) {
return evaluate(arg, env).shift();
}), env];
if (fn === 'lambda') {
var lArgs = args[0];
var code = args[1];
return [['closure', lArgs, code, env], env];
}
if (fn === 'if') {
var cond = evaluate(args[0], env).shift();
if (cond === true)
return [evaluate(args[1], env), env];
return [evaluate(args[2], env), env];
}
return apply(
evaluate(fn, env).shift(),
args.map(function(arg) {
return evaluate(arg, env).shift();
}),
env
);
};
function merge(t, o) {
if (o)
for (var k in o)
if (t[k] === undefined)
t[k] = o[k];
return t;
};
function combine(keys, values) {
var result = {};
for (var key in keys)
if (values[key] !== undefined)
result[keys[key]] = values[key];
return result;
};
function apply(fn, args, env) {
var lArgs = fn[1];
var code = fn[2];
var cEnv = fn[3];
if (typeof (code) === 'function')
return code.apply(null, [args, env]);
return evaluateCode(code, merge(combine(lArgs, args), merge(env, cEnv)));
};
function evaluateCode(code, env, multipleResults) {
multipleResults = multipleResults || false;
var results = [];
code.forEach(function(codeInstruction) {
var r = evaluate(codeInstruction, env);
results.push(r[0]);
env = r[1];
});
if (multipleResults)
return results;
return results[results.length - 1];
};
function closure(fn) {
return [
'closure',
null,
function(args, env) {
return [
fn.apply(null, args),
env
];
},
[]
];
};
var env = {};
env['<'] = closure(function(a, b) { return a < b; });
env['='] = closure(function(a, b) { return a === b; });
env['<>'] = closure(function(a, b) { return a !== b; });
env['<='] = closure(function(a, b) { return a <= b; });
env['!'] = closure(function(a) { return !a; });
env['>'] = closure(function(a, b) { return a > b; });
env['>='] = closure(function(a, b) { return a >= b; });
env['+'] = closure(function(a, b) { if (b === undefined) return +a; return a + b; });
env['-'] = closure(function(a, b) { if (b === undefined) return -a; return a - b; });
env['/'] = closure(function(a, b) { return a / b; });
env['*'] = closure(function(a, b) { return a * b; });
env['car'] = closure(function(list) { return list.length > 0 ? list[0] : null; });
env['cdr'] = closure(function(list) { return list.length > 1 ? list.slice(1) : null; });
env['^'] = closure(function(a, b) { return Math.pow(a, b); });
env['exp'] = closure(function(a) { return Math.exp(a); });
/*
var fib = [
[ 'define', 'fib', [ 'lambda', [ 'n' ],
[[ 'if', [ '<', 'n', '2' ],
'n',
[ '+',
[ 'fib', [ '-', 'n', '1' ]],
[ 'fib', [ '-', 'n', '2' ]]
]
]]
]],
[ 'fib', '12' ]
];
(define fib (lambda n
((if (< n 2)
n
(+
(fib (- n 1))
(fib (- n 2))
)
))
))
(fib 12)
*/
var parenthesize = function(input, list) {
if (list === undefined) {
return parenthesize(input, []);
} else {
var token = input.shift();
if (token === undefined) {
return list;
} else if (token === "(") {
list.push(parenthesize(input, []));
return parenthesize(input, list);
} else if (token === ")") {
return list;
} else {
return parenthesize(input, list.concat(token));
}
}
};
var tokenize = function(input) {
return input.split('"')
.map(function(x, i) {
if (i % 2 === 0) { // not in string
return x.replace(/\(/g, ' ( ')
.replace(/\)/g, ' ) ');
} else { // in string
return x.replace(/ /g, "!whitespace!");
}
})
.join('"')
.trim()
.split(/\s+/)
.map(function(x) {
return x.replace(/!whitespace!/g, " ");
});
};
var parse = function(input) {
return parenthesize(tokenize(input));
};