Update JS impls: use ffi-napi and Ubuntu 18.04
[jackhill/mal.git] / es6 / step8_macros.mjs
1 import rl from './node_readline.js'
2 const readline = rl.readline
3 import { _list_Q, _malfunc, _malfunc_Q } from './types'
4 import { BlankException, read_str } from './reader'
5 import { pr_str } from './printer'
6 import { new_env, env_set, env_get } from './env'
7 import { core_ns } from './core'
8
9 // read
10 const READ = str => read_str(str)
11
12 // eval
13 const is_pair = x => Array.isArray(x) && x.length > 0
14
15 const quasiquote = ast => {
16 if (!is_pair(ast)) {
17 return [Symbol.for('quote'), ast]
18 } else if (ast[0] === Symbol.for('unquote')) {
19 return ast[1]
20 } else if (is_pair(ast[0]) && ast[0][0] === Symbol.for('splice-unquote')) {
21 return [Symbol.for('concat'),
22 ast[0][1],
23 quasiquote(ast.slice(1))]
24 } else {
25 return [Symbol.for('cons'),
26 quasiquote(ast[0]),
27 quasiquote(ast.slice(1))]
28 }
29 }
30
31 function macroexpand(ast, env) {
32 while (_list_Q(ast) && typeof ast[0] === 'symbol' && ast[0] in env) {
33 let f = env_get(env, ast[0])
34 if (!f.ismacro) { break }
35 ast = f(...ast.slice(1))
36 }
37 return ast
38 }
39
40
41 const eval_ast = (ast, env) => {
42 if (typeof ast === 'symbol') {
43 return env_get(env, ast)
44 } else if (ast instanceof Array) {
45 return ast.map(x => EVAL(x, env))
46 } else if (ast instanceof Map) {
47 let new_hm = new Map()
48 ast.forEach((v, k) => new_hm.set(EVAL(k, env), EVAL(v, env)))
49 return new_hm
50 } else {
51 return ast
52 }
53 }
54
55 const EVAL = (ast, env) => {
56 while (true) {
57 //console.log('EVAL:', pr_str(ast, true))
58 if (!_list_Q(ast)) { return eval_ast(ast, env) }
59
60 ast = macroexpand(ast, env)
61 if (!_list_Q(ast)) { return eval_ast(ast, env) }
62 if (ast.length === 0) { return ast }
63
64 const [a0, a1, a2, a3] = ast
65 switch (typeof a0 === 'symbol' ? Symbol.keyFor(a0) : Symbol(':default')) {
66 case 'def!':
67 return env_set(env, a1, EVAL(a2, env))
68 case 'let*':
69 let let_env = new_env(env)
70 for (let i=0; i < a1.length; i+=2) {
71 env_set(let_env, a1[i], EVAL(a1[i+1], let_env))
72 }
73 env = let_env
74 ast = a2
75 break // continue TCO loop
76 case 'quote':
77 return a1
78 case 'quasiquote':
79 ast = quasiquote(a1)
80 break // continue TCO loop
81 case 'defmacro!':
82 let func = EVAL(a2, env)
83 func.ismacro = true
84 return env_set(env, a1, func)
85 case 'macroexpand':
86 return macroexpand(a1, env)
87 case 'do':
88 eval_ast(ast.slice(1,-1), env)
89 ast = ast[ast.length-1]
90 break // continue TCO loop
91 case 'if':
92 let cond = EVAL(a1, env)
93 if (cond === null || cond === false) {
94 ast = (typeof a3 !== 'undefined') ? a3 : null
95 } else {
96 ast = a2
97 }
98 break // continue TCO loop
99 case 'fn*':
100 return _malfunc((...args) => EVAL(a2, new_env(env, a1, args)),
101 a2, env, a1)
102 default:
103 let [f, ...args] = eval_ast(ast, env)
104 if (_malfunc_Q(f)) {
105 env = new_env(f.env, f.params, args)
106 ast = f.ast
107 break // continue TCO loop
108 } else {
109 return f(...args)
110 }
111 }
112 }
113 }
114
115 // print
116 const PRINT = exp => pr_str(exp, true)
117
118 // repl
119 let repl_env = new_env()
120 const REP = str => PRINT(EVAL(READ(str), repl_env))
121
122 // core.EXT: defined using ES6
123 for (let [k, v] of core_ns) { env_set(repl_env, Symbol.for(k), v) }
124 env_set(repl_env, Symbol.for('eval'), a => EVAL(a, repl_env))
125 env_set(repl_env, Symbol.for('*ARGV*'), [])
126
127 // core.mal: defined using language itself
128 REP('(def! not (fn* (a) (if a false true)))')
129 REP('(def! load-file (fn* (f) (eval (read-string (str "(do " (slurp f) ")")))))')
130 REP('(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list \'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw "odd number of forms to cond")) (cons \'cond (rest (rest xs)))))))')
131 REP('(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))')
132
133 if (process.argv.length > 2) {
134 env_set(repl_env, Symbol.for('*ARGV*'), process.argv.slice(3))
135 REP(`(load-file "${process.argv[2]}")`)
136 process.exit(0)
137 }
138
139
140 while (true) {
141 let line = readline('user> ')
142 if (line == null) break
143 try {
144 if (line) { console.log(REP(line)) }
145 } catch (exc) {
146 if (exc instanceof BlankException) { continue }
147 if (exc.stack) { console.log(exc.stack) }
148 else { console.log(`Error: ${exc}`) }
149 }
150 }