DISABLE FDs (REMOVE ME).
[jackhill/mal.git] / es6 / step5_tco.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 eval_ast = (ast, env) => {
14 if (typeof ast === 'symbol') {
15 return env_get(env, ast)
16 } else if (ast instanceof Array) {
17 return ast.map(x => EVAL(x, env))
18 } else if (ast instanceof Map) {
19 let new_hm = new Map()
20 ast.forEach((v, k) => new_hm.set(EVAL(k, env), EVAL(v, env)))
21 return new_hm
22 } else {
23 return ast
24 }
25 }
26
27 const EVAL = (ast, env) => {
28 while (true) {
29 //console.log('EVAL:', pr_str(ast, true))
30 if (!_list_Q(ast)) { return eval_ast(ast, env) }
31 if (ast.length === 0) { return ast }
32
33 const [a0, a1, a2, a3] = ast
34 switch (typeof a0 === 'symbol' ? Symbol.keyFor(a0) : Symbol(':default')) {
35 case 'def!':
36 return env_set(env, a1, EVAL(a2, env))
37 case 'let*':
38 let let_env = new_env(env)
39 for (let i=0; i < a1.length; i+=2) {
40 env_set(let_env, a1[i], EVAL(a1[i+1], let_env))
41 }
42 env = let_env
43 ast = a2
44 break // continue TCO loop
45 case 'do':
46 eval_ast(ast.slice(1,-1), env)
47 ast = ast[ast.length-1]
48 break // continue TCO loop
49 case 'if':
50 let cond = EVAL(a1, env)
51 if (cond === null || cond === false) {
52 ast = (typeof a3 !== 'undefined') ? a3 : null
53 } else {
54 ast = a2
55 }
56 break // continue TCO loop
57 case 'fn*':
58 return _malfunc((...args) => EVAL(a2, new_env(env, a1, args)),
59 a2, env, a1)
60 default:
61 let [f, ...args] = eval_ast(ast, env)
62 if (_malfunc_Q(f)) {
63 env = new_env(f.env, f.params, args)
64 ast = f.ast
65 break // continue TCO loop
66 } else {
67 return f(...args)
68 }
69 }
70 }
71 }
72
73 // print
74 const PRINT = exp => pr_str(exp, true)
75
76 // repl
77 let repl_env = new_env()
78 const REP = str => PRINT(EVAL(READ(str), repl_env))
79
80 // core.EXT: defined using ES6
81 for (let [k, v] of core_ns) { env_set(repl_env, Symbol.for(k), v) }
82
83 // core.mal: defined using language itself
84 REP('(def! not (fn* (a) (if a false true)))')
85
86 while (true) {
87 let line = readline('user> ')
88 if (line == null) break
89 try {
90 if (line) { console.log(REP(line)) }
91 } catch (exc) {
92 if (exc instanceof BlankException) { continue }
93 if (exc instanceof Error) { console.warn(exc.stack) }
94 else { console.warn(`Error: ${exc}`) }
95 }
96 }