2015-08-05 06:20:58 +03:00
|
|
|
import { readline } from './node_readline'
|
|
|
|
import { _symbol, _symbol_Q, _list_Q, _vector, _vector_Q,
|
|
|
|
_hash_map_Q, _sequential_Q, _malfunc, _malfunc_Q } from './types'
|
|
|
|
import { BlankException, read_str } from './reader'
|
|
|
|
import { pr_str } from './printer'
|
|
|
|
import { new_env, env_set, env_get } from './env'
|
|
|
|
import { core_ns } from './core'
|
2015-07-31 08:29:44 +03:00
|
|
|
|
|
|
|
// read
|
2015-08-05 06:20:58 +03:00
|
|
|
const READ = (str) => read_str(str)
|
2015-07-31 08:29:44 +03:00
|
|
|
|
|
|
|
// eval
|
|
|
|
const is_pair = x => _sequential_Q(x) && x.length > 0
|
|
|
|
|
|
|
|
const quasiquote = ast => {
|
|
|
|
if (!is_pair(ast)) {
|
2015-08-05 06:20:58 +03:00
|
|
|
return [_symbol('quote'), ast]
|
|
|
|
} else if (ast[0] === _symbol('unquote')) {
|
|
|
|
return ast[1]
|
|
|
|
} else if (is_pair(ast[0]) && ast[0][0] === _symbol('splice-unquote')) {
|
|
|
|
return [_symbol('concat'), ast[0][1], quasiquote(ast.slice(1))]
|
2015-07-31 08:29:44 +03:00
|
|
|
} else {
|
2015-08-05 06:20:58 +03:00
|
|
|
return [_symbol('cons'), quasiquote(ast[0]), quasiquote(ast.slice(1))]
|
2015-07-31 08:29:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const eval_ast = (ast, env) => {
|
2015-08-05 06:20:58 +03:00
|
|
|
if (_symbol_Q(ast)) {
|
2015-07-31 08:29:44 +03:00
|
|
|
return env_get(env, ast)
|
|
|
|
} else if (_list_Q(ast)) {
|
2015-08-05 06:20:58 +03:00
|
|
|
return ast.map((x) => EVAL(x, env))
|
2015-08-02 06:29:44 +03:00
|
|
|
} else if (_vector_Q(ast)) {
|
2015-08-05 06:20:58 +03:00
|
|
|
return _vector(...ast.map((x) => EVAL(x, env)))
|
2015-08-02 06:29:44 +03:00
|
|
|
} else if (_hash_map_Q(ast)) {
|
2015-08-05 06:20:58 +03:00
|
|
|
let new_hm = new Map()
|
2015-08-02 06:29:44 +03:00
|
|
|
for (let [k, v] of ast) {
|
2015-08-05 06:20:58 +03:00
|
|
|
new_hm.set(EVAL(k, env), EVAL(v, env))
|
2015-08-02 06:29:44 +03:00
|
|
|
}
|
2015-08-05 06:20:58 +03:00
|
|
|
return new_hm
|
2015-07-31 08:29:44 +03:00
|
|
|
} else {
|
2015-08-05 06:20:58 +03:00
|
|
|
return ast
|
2015-07-31 08:29:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const EVAL = (ast, env) => {
|
|
|
|
while (true) {
|
2015-08-05 06:20:58 +03:00
|
|
|
//console.log('EVAL:', pr_str(ast, true))
|
2015-07-31 08:29:44 +03:00
|
|
|
if (!_list_Q(ast)) { return eval_ast(ast, env) }
|
2016-03-30 18:28:53 +03:00
|
|
|
if (ast.length === 0) { return ast }
|
2015-07-31 08:29:44 +03:00
|
|
|
|
2015-08-05 06:20:58 +03:00
|
|
|
const [a0, a1, a2, a3] = ast
|
|
|
|
const a0sym = _symbol_Q(a0) ? Symbol.keyFor(a0) : Symbol(':default')
|
2015-07-31 08:29:44 +03:00
|
|
|
switch (a0sym) {
|
|
|
|
case 'def!':
|
2015-08-05 06:20:58 +03:00
|
|
|
return env_set(env, a1, EVAL(a2, env))
|
2015-07-31 08:29:44 +03:00
|
|
|
case 'let*':
|
2015-08-05 06:20:58 +03:00
|
|
|
let let_env = new_env(env)
|
2015-07-31 08:29:44 +03:00
|
|
|
for (let i=0; i < a1.length; i+=2) {
|
2015-08-05 06:20:58 +03:00
|
|
|
env_set(let_env, a1[i], EVAL(a1[i+1], let_env))
|
2015-07-31 08:29:44 +03:00
|
|
|
}
|
2015-08-05 06:20:58 +03:00
|
|
|
env = let_env
|
|
|
|
ast = a2
|
2015-07-31 08:29:44 +03:00
|
|
|
break; // continue TCO loop
|
2015-08-05 06:20:58 +03:00
|
|
|
case 'quote':
|
|
|
|
return a1
|
|
|
|
case 'quasiquote':
|
|
|
|
ast = quasiquote(a1)
|
2015-07-31 08:29:44 +03:00
|
|
|
break; // continue TCO loop
|
2015-08-05 06:20:58 +03:00
|
|
|
case 'do':
|
|
|
|
eval_ast(ast.slice(1,-1), env)
|
|
|
|
ast = ast[ast.length-1]
|
2015-07-31 08:29:44 +03:00
|
|
|
break; // continue TCO loop
|
2015-08-05 06:20:58 +03:00
|
|
|
case 'if':
|
|
|
|
let cond = EVAL(a1, env)
|
2015-07-31 08:29:44 +03:00
|
|
|
if (cond === null || cond === false) {
|
2015-08-05 06:20:58 +03:00
|
|
|
ast = (typeof a3 !== 'undefined') ? a3 : null
|
2015-07-31 08:29:44 +03:00
|
|
|
} else {
|
2015-08-05 06:20:58 +03:00
|
|
|
ast = a2
|
2015-07-31 08:29:44 +03:00
|
|
|
}
|
|
|
|
break; // continue TCO loop
|
2015-08-05 06:20:58 +03:00
|
|
|
case 'fn*':
|
2015-07-31 08:29:44 +03:00
|
|
|
return _malfunc((...args) => EVAL(a2, new_env(env, a1, args)),
|
2015-08-05 06:20:58 +03:00
|
|
|
a2, env, a1)
|
2015-07-31 08:29:44 +03:00
|
|
|
default:
|
2015-08-05 06:20:58 +03:00
|
|
|
let [f, ...args] = eval_ast(ast, env)
|
2015-07-31 08:29:44 +03:00
|
|
|
if (_malfunc_Q(f)) {
|
2015-08-05 06:20:58 +03:00
|
|
|
env = new_env(f.env, f.params, args)
|
|
|
|
ast = f.ast
|
2015-07-31 08:29:44 +03:00
|
|
|
break; // continue TCO loop
|
|
|
|
} else {
|
2015-08-05 06:20:58 +03:00
|
|
|
return f(...args)
|
2015-07-31 08:29:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// print
|
2015-08-05 06:20:58 +03:00
|
|
|
const PRINT = (exp) => pr_str(exp, true)
|
2015-07-31 08:29:44 +03:00
|
|
|
|
|
|
|
// repl
|
2015-08-05 06:20:58 +03:00
|
|
|
let repl_env = new_env()
|
|
|
|
const REP = (str) => PRINT(EVAL(READ(str), repl_env))
|
2015-07-31 08:29:44 +03:00
|
|
|
|
|
|
|
// core.EXT: defined using ES6
|
2015-08-05 06:20:58 +03:00
|
|
|
for (let [k, v] of core_ns) { env_set(repl_env, _symbol(k), v) }
|
|
|
|
env_set(repl_env, _symbol('eval'), a => EVAL(a, repl_env))
|
|
|
|
env_set(repl_env, _symbol('*ARGV*'), [])
|
2015-07-31 08:29:44 +03:00
|
|
|
|
|
|
|
// core.mal: defined using language itself
|
2015-08-05 06:20:58 +03:00
|
|
|
REP('(def! not (fn* (a) (if a false true)))')
|
|
|
|
REP('(def! load-file (fn* (f) (eval (read-string (str "(do " (slurp f) ")")))))')
|
2015-07-31 08:29:44 +03:00
|
|
|
|
|
|
|
if (process.argv.length > 2) {
|
2016-03-28 00:45:20 +03:00
|
|
|
env_set(repl_env, _symbol('*ARGV*'), process.argv.slice(3))
|
2015-08-05 06:20:58 +03:00
|
|
|
REP(`(load-file "${process.argv[2]}")`)
|
|
|
|
process.exit(0)
|
2015-07-31 08:29:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
while (true) {
|
2015-08-05 06:20:58 +03:00
|
|
|
let line = readline('user> ')
|
|
|
|
if (line == null) break
|
2015-07-31 08:29:44 +03:00
|
|
|
try {
|
|
|
|
if (line) { console.log(REP(line)); }
|
|
|
|
} catch (exc) {
|
|
|
|
if (exc instanceof BlankException) { continue; }
|
|
|
|
if (exc.stack) { console.log(exc.stack); }
|
2015-08-05 06:20:58 +03:00
|
|
|
else { console.log(`Error: ${exc}`); }
|
2015-07-31 08:29:44 +03:00
|
|
|
}
|
|
|
|
}
|