1 import rl from './node_readline.js'
2 const readline = rl.readline
3 import { _list_Q } from './types'
4 import { BlankException, read_str } from './reader'
5 import { pr_str } from './printer'
8 const READ = str => read_str(str)
11 const eval_ast = (ast, env) => {
12 if (typeof ast === 'symbol') {
16 throw Error(`'${Symbol.keyFor(ast)}' not found`)
18 } else if (ast instanceof Array) {
19 return ast.map(x => EVAL(x, env))
20 } else if (ast instanceof Map) {
21 let new_hm = new Map()
22 ast.forEach((v, k) => new_hm.set(EVAL(k, env), EVAL(v, env)))
29 const EVAL = (ast, env) => {
30 if (!_list_Q(ast)) { return eval_ast(ast, env) }
31 if (ast.length === 0) { return ast }
33 const [f, ...args] = eval_ast(ast, env)
38 const PRINT = exp => pr_str(exp, true)
41 var repl_env = {[Symbol.for('+')]: (a,b) => a+b,
42 [Symbol.for('-')]: (a,b) => a-b,
43 [Symbol.for('*')]: (a,b) => a*b,
44 [Symbol.for('/')]: (a,b) => a/b}
45 const REP = str => PRINT(EVAL(READ(str), repl_env))
48 let line = readline('user> ')
49 if (line == null) break
51 if (line) { console.log(REP(line)) }
53 if (exc instanceof BlankException) { continue }
54 if (exc instanceof Error) { console.warn(exc.stack) }
55 else { console.warn(`Error: ${exc}`) }