Merge pull request #281 from sebras/master
[jackhill/mal.git] / es6 / step4_if_fn_do.js
1 import { readline } from './node_readline'
2 import { _list_Q } from './types'
3 import { BlankException, read_str } from './reader'
4 import { pr_str } from './printer'
5 import { new_env, env_set, env_get } from './env'
6 import { core_ns } from './core'
7
8 // read
9 const READ = str => read_str(str)
10
11 // eval
12 const eval_ast = (ast, env) => {
13 if (typeof ast === 'symbol') {
14 return env_get(env, ast)
15 } else if (ast instanceof Array) {
16 return ast.map(x => EVAL(x, env))
17 } else if (ast instanceof Map) {
18 let new_hm = new Map()
19 ast.forEach((v, k) => new_hm.set(EVAL(k, env), EVAL(v, env)))
20 return new_hm
21 } else {
22 return ast
23 }
24 }
25
26 const EVAL = (ast, env) => {
27 //console.log('EVAL:', pr_str(ast, true))
28 if (!_list_Q(ast)) { return eval_ast(ast, env) }
29 if (ast.length === 0) { return ast }
30
31 const [a0, a1, a2, a3] = ast
32 switch (typeof a0 === 'symbol' ? Symbol.keyFor(a0) : Symbol(':default')) {
33 case 'def!':
34 return env_set(env, a1, EVAL(a2, env))
35 case 'let*':
36 let let_env = new_env(env)
37 for (let i=0; i < a1.length; i+=2) {
38 env_set(let_env, a1[i], EVAL(a1[i+1], let_env))
39 }
40 return EVAL(a2, let_env)
41 case 'do':
42 return eval_ast(ast.slice(1), env)[ast.length-2]
43 case 'if':
44 let cond = EVAL(a1, env)
45 if (cond === null || cond === false) {
46 return typeof a3 !== 'undefined' ? EVAL(a3, env) : null
47 } else {
48 return EVAL(a2, env)
49 }
50 case 'fn*':
51 return (...args) => EVAL(a2, new_env(env, a1, args))
52 default:
53 let [f, ...args] = eval_ast(ast, env)
54 return f(...args)
55 }
56 }
57
58 // print
59 const PRINT = exp => pr_str(exp, true)
60
61 // repl
62 let repl_env = new_env()
63 const REP = str => PRINT(EVAL(READ(str), repl_env))
64
65 // core.EXT: defined using ES6
66 for (let [k, v] of core_ns) { env_set(repl_env, Symbol.for(k), v) }
67
68 // core.mal: defined using language itself
69 REP('(def! not (fn* (a) (if a false true)))')
70
71 while (true) {
72 let line = readline('user> ')
73 if (line == null) break
74 try {
75 if (line) { console.log(REP(line)) }
76 } catch (exc) {
77 if (exc instanceof BlankException) { continue }
78 if (exc.stack) { console.log(exc.stack) }
79 else { console.log(`Error: ${exc}`) }
80 }
81 }