DISABLE FDs (REMOVE ME).
[jackhill/mal.git] / es6 / step2_eval.mjs
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'
6
7 // read
8 const READ = str => read_str(str)
9
10 // eval
11 const eval_ast = (ast, env) => {
12 if (typeof ast === 'symbol') {
13 if (ast in env) {
14 return env[ast]
15 } else {
16 throw Error(`'${Symbol.keyFor(ast)}' not found`)
17 }
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)))
23 return new_hm
24 } else {
25 return ast
26 }
27 }
28
29 const EVAL = (ast, env) => {
30 if (!_list_Q(ast)) { return eval_ast(ast, env) }
31 if (ast.length === 0) { return ast }
32
33 const [f, ...args] = eval_ast(ast, env)
34 return f(...args)
35 }
36
37 // print
38 const PRINT = exp => pr_str(exp, true)
39
40 // repl
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))
46
47 while (true) {
48 let line = readline('user> ')
49 if (line == null) break
50 try {
51 if (line) { console.log(REP(line)) }
52 } catch (exc) {
53 if (exc instanceof BlankException) { continue }
54 if (exc instanceof Error) { console.warn(exc.stack) }
55 else { console.warn(`Error: ${exc}`) }
56 }
57 }