1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-20 10:07:45 +03:00
mal/es6/step7_quote.js

129 lines
3.8 KiB
JavaScript
Raw Normal View History

import { readline } from './node_readline'
import { _list_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
const READ = str => read_str(str)
2015-07-31 08:29:44 +03:00
// eval
const is_pair = x => Array.isArray(x) && x.length > 0
2015-07-31 08:29:44 +03:00
const quasiquote = ast => {
if (!is_pair(ast)) {
return [Symbol.for('quote'), ast]
} else if (ast[0] === Symbol.for('unquote')) {
return ast[1]
} else if (is_pair(ast[0]) && ast[0][0] === Symbol.for('splice-unquote')) {
return [Symbol.for('concat'),
ast[0][1],
quasiquote(ast.slice(1))]
2015-07-31 08:29:44 +03:00
} else {
return [Symbol.for('cons'),
quasiquote(ast[0]),
quasiquote(ast.slice(1))]
2015-07-31 08:29:44 +03:00
}
}
const eval_ast = (ast, env) => {
if (typeof ast === 'symbol') {
2015-07-31 08:29:44 +03:00
return env_get(env, ast)
} else if (ast instanceof Array) {
return ast.map(x => EVAL(x, env))
} else if (ast instanceof Map) {
let new_hm = new Map()
ast.forEach((v, k) => new_hm.set(EVAL(k, env), EVAL(v, env)))
return new_hm
2015-07-31 08:29:44 +03:00
} else {
return ast
2015-07-31 08:29:44 +03:00
}
}
const EVAL = (ast, env) => {
while (true) {
//console.log('EVAL:', pr_str(ast, true))
2015-07-31 08:29:44 +03:00
if (!_list_Q(ast)) { return eval_ast(ast, env) }
if (ast.length === 0) { return ast }
2015-07-31 08:29:44 +03:00
const [a0, a1, a2, a3] = ast
switch (typeof a0 === 'symbol' ? Symbol.keyFor(a0) : Symbol(':default')) {
case 'def!':
return env_set(env, a1, EVAL(a2, env))
2015-07-31 08:29:44 +03:00
case 'let*':
let let_env = new_env(env)
2015-07-31 08:29:44 +03:00
for (let i=0; i < a1.length; i+=2) {
env_set(let_env, a1[i], EVAL(a1[i+1], let_env))
2015-07-31 08:29:44 +03:00
}
env = let_env
ast = a2
break // continue TCO loop
case 'quote':
return a1
case 'quasiquote':
ast = quasiquote(a1)
break // continue TCO loop
case 'do':
eval_ast(ast.slice(1,-1), env)
ast = ast[ast.length-1]
break // continue TCO loop
case 'if':
let cond = EVAL(a1, env)
2015-07-31 08:29:44 +03:00
if (cond === null || cond === false) {
ast = (typeof a3 !== 'undefined') ? a3 : null
2015-07-31 08:29:44 +03:00
} else {
ast = a2
2015-07-31 08:29:44 +03:00
}
break // continue TCO loop
case 'fn*':
2015-07-31 08:29:44 +03:00
return _malfunc((...args) => EVAL(a2, new_env(env, a1, args)),
a2, env, a1)
2015-07-31 08:29:44 +03:00
default:
let [f, ...args] = eval_ast(ast, env)
2015-07-31 08:29:44 +03:00
if (_malfunc_Q(f)) {
env = new_env(f.env, f.params, args)
ast = f.ast
break // continue TCO loop
2015-07-31 08:29:44 +03:00
} else {
return f(...args)
2015-07-31 08:29:44 +03:00
}
}
}
}
// print
const PRINT = exp => pr_str(exp, true)
2015-07-31 08:29:44 +03:00
// repl
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
for (let [k, v] of core_ns) { env_set(repl_env, Symbol.for(k), v) }
env_set(repl_env, Symbol.for('eval'), a => EVAL(a, repl_env))
env_set(repl_env, Symbol.for('*ARGV*'), [])
2015-07-31 08:29:44 +03:00
// core.mal: defined using language itself
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) {
env_set(repl_env, Symbol.for('*ARGV*'), process.argv.slice(3))
REP(`(load-file "${process.argv[2]}")`)
process.exit(0)
2015-07-31 08:29:44 +03:00
}
while (true) {
let line = readline('user> ')
if (line == null) break
2015-07-31 08:29:44 +03:00
try {
if (line) { console.log(REP(line)) }
2015-07-31 08:29:44 +03:00
} catch (exc) {
if (exc instanceof BlankException) { continue }
if (exc.stack) { console.log(exc.stack) }
else { console.log(`Error: ${exc}`) }
2015-07-31 08:29:44 +03:00
}
}