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'
10 const READ = str => read_str(str)
13 const is_pair = x => Array.isArray(x) && x.length > 0
15 const quasiquote = ast => {
17 return [Symbol.for('quote'), ast]
18 } else if (ast[0] === Symbol.for('unquote')) {
20 } else if (is_pair(ast[0]) && ast[0][0] === Symbol.for('splice-unquote')) {
21 return [Symbol.for('concat'),
23 quasiquote(ast.slice(1))]
25 return [Symbol.for('cons'),
27 quasiquote(ast.slice(1))]
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))
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)))
55 const EVAL = (ast, env) => {
57 //console.log('EVAL:', pr_str(ast, true))
58 if (!_list_Q(ast)) { return eval_ast(ast, env) }
60 ast = macroexpand(ast, env)
61 if (!_list_Q(ast)) { return eval_ast(ast, env) }
62 if (ast.length === 0) { return ast }
64 const [a0, a1, a2, a3] = ast
65 switch (typeof a0 === 'symbol' ? Symbol.keyFor(a0) : Symbol(':default')) {
67 return env_set(env, a1, EVAL(a2, env))
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))
75 break // continue TCO loop
80 break // continue TCO loop
82 let func = EVAL(a2, env)
84 return env_set(env, a1, func)
86 return macroexpand(a1, env)
88 eval_ast(ast.slice(1,-1), env)
89 ast = ast[ast.length-1]
90 break // continue TCO loop
92 let cond = EVAL(a1, env)
93 if (cond === null || cond === false) {
94 ast = (typeof a3 !== 'undefined') ? a3 : null
98 break // continue TCO loop
100 return _malfunc((...args) => EVAL(a2, new_env(env, a1, args)),
103 let [f, ...args] = eval_ast(ast, env)
105 env = new_env(f.env, f.params, args)
107 break // continue TCO loop
116 const PRINT = exp => pr_str(exp, true)
119 let repl_env = new_env()
120 const REP = str => PRINT(EVAL(READ(str), repl_env))
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*'), [])
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))))))))')
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]}")`)
141 let line = readline('user> ')
142 if (line == null) break
144 if (line) { console.log(REP(line)) }
146 if (exc instanceof BlankException) { continue }
147 if (exc.stack) { console.log(exc.stack) }
148 else { console.log(`Error: ${exc}`) }