forked from kanaka/mal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
step7_quote.mjs
136 lines (125 loc) · 4.07 KB
/
step7_quote.mjs
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
import rl from './node_readline.js'
const readline = rl.readline
import { _list_Q, _malfunc, _malfunc_Q, Vector } from './types'
import { BlankException, read_str } from './reader'
import { pr_str } from './printer'
import { new_env, env_set, env_get } from './env'
import { core_ns } from './core'
// read
const READ = str => read_str(str)
// eval
const qq_loop = (acc, elt) => {
if (_list_Q(elt) && elt.length == 2
&& elt[0] === Symbol.for('splice-unquote')) {
return [Symbol.for('concat'), elt[1], acc]
} else {
return [Symbol.for('cons'), quasiquote (elt), acc]
}
}
const quasiquote = ast => {
if (_list_Q(ast)) {
if (ast.length == 2 && ast[0] === Symbol.for('unquote')) {
return ast[1]
} else {
return ast.reduceRight(qq_loop, [])
}
} else if (ast instanceof Vector) {
return [Symbol.for('vec'), ast.reduceRight(qq_loop, [])]
} else if (typeof ast === 'symbol' || ast instanceof Map) {
return [Symbol.for('quote'), ast]
} else {
return ast
}
}
const eval_ast = (ast, env) => {
if (typeof ast === 'symbol') {
return env_get(env, ast)
} else if (ast instanceof Array) {
return ast.map(x => EVAL(x, env))
} else if (ast instanceof Map) {
let new_hm = new Map()
ast.forEach((v, k) => new_hm.set(EVAL(k, env), EVAL(v, env)))
return new_hm
} else {
return ast
}
}
const EVAL = (ast, env) => {
while (true) {
//console.log('EVAL:', pr_str(ast, true))
if (!_list_Q(ast)) { return eval_ast(ast, env) }
if (ast.length === 0) { return ast }
const [a0, a1, a2, a3] = ast
switch (typeof a0 === 'symbol' ? Symbol.keyFor(a0) : Symbol(':default')) {
case 'def!':
return env_set(env, a1, EVAL(a2, env))
case 'let*':
let let_env = new_env(env)
for (let i=0; i < a1.length; i+=2) {
env_set(let_env, a1[i], EVAL(a1[i+1], let_env))
}
env = let_env
ast = a2
break // continue TCO loop
case 'quote':
return a1
case 'quasiquoteexpand':
return quasiquote(a1)
case 'quasiquote':
ast = quasiquote(a1)
break // continue TCO loop
case 'do':
eval_ast(ast.slice(1,-1), env)
ast = ast[ast.length-1]
break // continue TCO loop
case 'if':
let cond = EVAL(a1, env)
if (cond === null || cond === false) {
ast = (typeof a3 !== 'undefined') ? a3 : null
} else {
ast = a2
}
break // continue TCO loop
case 'fn*':
return _malfunc((...args) => EVAL(a2, new_env(env, a1, args)),
a2, env, a1)
default:
let [f, ...args] = eval_ast(ast, env)
if (_malfunc_Q(f)) {
env = new_env(f.env, f.params, args)
ast = f.ast
break // continue TCO loop
} else {
return f(...args)
}
}
}
}
// print
const PRINT = exp => pr_str(exp, true)
// repl
let repl_env = new_env()
const REP = str => PRINT(EVAL(READ(str), repl_env))
// core.EXT: defined using ES6
for (let [k, v] of core_ns) { env_set(repl_env, Symbol.for(k), v) }
env_set(repl_env, Symbol.for('eval'), a => EVAL(a, repl_env))
env_set(repl_env, Symbol.for('*ARGV*'), [])
// core.mal: defined using language itself
REP('(def! not (fn* (a) (if a false true)))')
REP('(def! load-file (fn* (f) (eval (read-string (str "(do " (slurp f) "\nnil)")))))')
if (process.argv.length > 2) {
env_set(repl_env, Symbol.for('*ARGV*'), process.argv.slice(3))
REP(`(load-file "${process.argv[2]}")`)
process.exit(0)
}
while (true) {
let line = readline('user> ')
if (line == null) break
try {
if (line) { console.log(REP(line)) }
} catch (exc) {
if (exc instanceof BlankException) { continue }
if (exc instanceof Error) { console.warn(exc.stack) }
else { console.warn(`Error: ${exc}`) }
}
}