1 import { readline
} from './node_readline'
2 import { _symbol
, _symbol_Q
, _list_Q
, _vector
, _vector_Q
,
3 _hash_map_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
) => {
16 throw Error(`'${Symbol.keyFor(ast)}' not found`)
18 } else if (_list_Q(ast
)) {
19 return ast
.map((x
) => EVAL(x
, env
))
20 } else if (_vector_Q(ast
)) {
21 return _vector(...ast
.map((x
) => EVAL(x
, env
)))
22 } else if (_hash_map_Q(ast
)) {
23 let new_hm
= new Map()
24 for (let [k
, v
] of ast
) {
25 new_hm
.set(EVAL(k
, env
), EVAL(v
, env
))
33 const EVAL
= (ast
, env
) => {
34 if (!_list_Q(ast
)) { return eval_ast(ast
, env
) }
36 const [f
, ...args
] = eval_ast(ast
, env
)
41 const PRINT
= (exp
) => pr_str(exp
, true)
44 var repl_env
= {[_symbol('+')]: (a
,b
) => a
+b
,
45 [_symbol('-')]: (a
,b
) => a
-b
,
46 [_symbol('*')]: (a
,b
) => a
*b
,
47 [_symbol('/')]: (a
,b
) => a
/b
}
48 const REP
= (str
) => PRINT(EVAL(READ(str
), repl_env
))
51 let line
= readline('user> ')
52 if (line
== null) break
54 if (line
) { console
.log(REP(line
)); }
56 if (exc
instanceof BlankException
) { continue; }
57 if (exc
.stack
) { console
.log(exc
.stack
); }
58 else { console
.log(`Error: ${exc}`); }