-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval.js
48 lines (44 loc) · 1.35 KB
/
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
const globalEnv = require("./globalEnv")
function eval(x, env = globalEnv) {
if (env.isSymbol.call(env, x)) return env[x]
if (env.isNumber(x)) return x
if (!Array.isArray(x) || !x.length) return x
let [operator, ...args] = x
if (operator === "if") {
const [test, conseq, alt] = args
return eval(test, env) ? eval(conseq, env) : eval(alt, env)
}
if (operator === "define") {
const [symbol, exp] = args
env[symbol] = eval(exp, env)
return typeof env[symbol] === "function" ? symbol : env[symbol]
}
if (operator === "quote") {
return args[0]
}
if (operator === "begin") {
return args.reduce((_, exp) => eval(exp, env), null)
}
if (operator === "lambda") {
const [params, body] = args
return function (...args) {
const procedureEnv = Object.create(env)
params.forEach((param, i) => {
procedureEnv[param] = eval(args[i], env)
})
return eval(body, procedureEnv)
}
}
if (operator === "set!") {
const [symbol, exp] = args
const prevValue = env[symbol]
if (!prevValue) throw `ReferenceError: '${symbol}' is not found`
env[symbol] = eval(exp, env)
return prevValue
}
if (typeof env[operator] === "function" || Array.isArray(operator)) {
const procedure = eval(operator, env)
return procedure(...args.map((arg) => eval(arg, env)))
}
}
module.exports = eval