2015-01-09 08:25:40 +03:00
|
|
|
#!/usr/bin/env lua
|
|
|
|
|
|
|
|
local table = require('table')
|
|
|
|
|
|
|
|
local readline = require('readline')
|
|
|
|
local utils = require('utils')
|
|
|
|
local types = require('types')
|
|
|
|
local reader = require('reader')
|
|
|
|
local printer = require('printer')
|
|
|
|
local Env = require('env')
|
|
|
|
local core = require('core')
|
|
|
|
local List, Vector, HashMap = types.List, types.Vector, types.HashMap
|
|
|
|
|
|
|
|
-- read
|
|
|
|
function READ(str)
|
|
|
|
return reader.read_str(str)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- eval
|
2020-07-21 19:01:48 +03:00
|
|
|
function starts_with(ast, sym)
|
|
|
|
return 0 < #ast and types._symbol_Q(ast[1]) and ast[1].val == sym
|
|
|
|
end
|
|
|
|
|
|
|
|
function quasiquote_loop(ast)
|
|
|
|
local acc = types.List:new({})
|
|
|
|
for i = #ast,1,-1 do
|
|
|
|
local elt = ast[i]
|
|
|
|
if types._list_Q(elt) and starts_with(elt, "splice-unquote") then
|
|
|
|
acc = types.List:new({types.Symbol:new("concat"), elt[2], acc})
|
|
|
|
else
|
|
|
|
acc = types.List:new({types.Symbol:new("cons"), quasiquote(elt), acc})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return acc
|
2015-01-09 08:25:40 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
function quasiquote(ast)
|
2020-07-21 19:01:48 +03:00
|
|
|
if types._list_Q(ast) then
|
|
|
|
if starts_with(ast, "unquote") then
|
|
|
|
return ast[2]
|
|
|
|
else
|
|
|
|
return quasiquote_loop(ast)
|
|
|
|
end
|
|
|
|
elseif types._vector_Q(ast) then
|
|
|
|
return types.List:new({types.Symbol:new("vec"), quasiquote_loop(ast)})
|
|
|
|
elseif types._symbol_Q(ast) or types._hash_map_Q(ast) then
|
2015-01-09 08:25:40 +03:00
|
|
|
return types.List:new({types.Symbol:new("quote"), ast})
|
|
|
|
else
|
2020-07-21 19:01:48 +03:00
|
|
|
return ast
|
2015-01-09 08:25:40 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-08-22 02:51:28 +03:00
|
|
|
function EVAL(ast, env)
|
|
|
|
while true do
|
2024-08-22 06:53:59 +03:00
|
|
|
|
|
|
|
local dbgeval = env:get("DEBUG-EVAL")
|
|
|
|
if dbgeval ~= nil and dbgeval ~= types.Nil and dbgeval ~= false then
|
|
|
|
print("EVAL: " .. printer._pr_str(ast, true))
|
|
|
|
env:debug()
|
2015-01-09 08:25:40 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
if types._symbol_Q(ast) then
|
2024-08-22 06:53:59 +03:00
|
|
|
local result = env:get(ast.val)
|
|
|
|
if result == nil then
|
|
|
|
types.throw("'" .. ast.val .. "' not found")
|
|
|
|
end
|
|
|
|
return result
|
2015-01-09 08:25:40 +03:00
|
|
|
elseif types._vector_Q(ast) then
|
|
|
|
return Vector:new(utils.map(function(x) return EVAL(x,env) end,ast))
|
|
|
|
elseif types._hash_map_Q(ast) then
|
|
|
|
local new_hm = {}
|
|
|
|
for k,v in pairs(ast) do
|
2021-08-21 20:08:17 +03:00
|
|
|
new_hm[k] = EVAL(v, env)
|
2015-01-09 08:25:40 +03:00
|
|
|
end
|
|
|
|
return HashMap:new(new_hm)
|
2024-08-22 02:51:28 +03:00
|
|
|
elseif not types._list_Q(ast) or #ast == 0 then
|
2015-01-09 08:25:40 +03:00
|
|
|
return ast
|
|
|
|
end
|
|
|
|
|
|
|
|
-- apply list
|
|
|
|
|
|
|
|
local a0,a1,a2,a3 = ast[1], ast[2],ast[3],ast[4]
|
|
|
|
local a0sym = types._symbol_Q(a0) and a0.val or ""
|
|
|
|
if 'def!' == a0sym then
|
2024-08-22 06:53:59 +03:00
|
|
|
return env:set(a1.val, EVAL(a2, env))
|
2015-01-09 08:25:40 +03:00
|
|
|
elseif 'let*' == a0sym then
|
|
|
|
local let_env = Env:new(env)
|
|
|
|
for i = 1,#a1,2 do
|
2024-08-22 06:53:59 +03:00
|
|
|
let_env:set(a1[i].val, EVAL(a1[i+1], let_env))
|
2015-01-09 08:25:40 +03:00
|
|
|
end
|
|
|
|
env = let_env
|
|
|
|
ast = a2 -- TCO
|
|
|
|
elseif 'quote' == a0sym then
|
|
|
|
return a1
|
|
|
|
elseif 'quasiquote' == a0sym then
|
|
|
|
ast = quasiquote(a1) -- TCO
|
|
|
|
elseif 'defmacro!' == a0sym then
|
2024-08-22 03:07:19 +03:00
|
|
|
local mac = types.copy(EVAL(a2, env))
|
2015-01-09 08:25:40 +03:00
|
|
|
mac.ismacro = true
|
2024-08-22 06:53:59 +03:00
|
|
|
return env:set(a1.val, mac)
|
2015-01-09 08:25:40 +03:00
|
|
|
elseif 'try*' == a0sym then
|
2024-08-22 07:47:11 +03:00
|
|
|
if a2 == nil or a2[1].val ~= 'catch*' then
|
|
|
|
ast = a1 -- TCO
|
|
|
|
else
|
2015-01-09 08:25:40 +03:00
|
|
|
local exc, result = nil, nil
|
|
|
|
xpcall(function()
|
|
|
|
result = EVAL(a1, env)
|
|
|
|
end, function(err)
|
|
|
|
exc = err
|
|
|
|
end)
|
2024-08-22 07:47:11 +03:00
|
|
|
if exc == nil then
|
|
|
|
return result
|
|
|
|
else
|
2015-01-09 08:25:40 +03:00
|
|
|
if types._malexception_Q(exc) then
|
|
|
|
exc = exc.val
|
|
|
|
end
|
2024-08-22 07:47:11 +03:00
|
|
|
ast, env = a2[3], Env:new(env, {a2[2]}, {exc}) -- TCO
|
2015-01-09 08:25:40 +03:00
|
|
|
end
|
2024-08-22 07:47:11 +03:00
|
|
|
end
|
2015-01-09 08:25:40 +03:00
|
|
|
elseif 'do' == a0sym then
|
2024-08-22 02:51:28 +03:00
|
|
|
utils.map(function(x) return EVAL(x, env) end, types.slice(ast, 2, #ast - 1))
|
2015-01-09 08:25:40 +03:00
|
|
|
ast = ast[#ast] -- TCO
|
|
|
|
elseif 'if' == a0sym then
|
|
|
|
local cond = EVAL(a1, env)
|
|
|
|
if cond == types.Nil or cond == false then
|
2019-01-28 22:31:28 +03:00
|
|
|
if #ast > 3 then ast = a3 else return types.Nil end -- TCO
|
2015-01-09 08:25:40 +03:00
|
|
|
else
|
|
|
|
ast = a2 -- TCO
|
|
|
|
end
|
|
|
|
elseif 'fn*' == a0sym then
|
|
|
|
return types.MalFunc:new(function(...)
|
2019-01-28 22:31:28 +03:00
|
|
|
return EVAL(a2, Env:new(env, a1, table.pack(...)))
|
2015-01-09 08:25:40 +03:00
|
|
|
end, a2, env, a1)
|
|
|
|
else
|
2024-08-22 02:51:28 +03:00
|
|
|
local f = EVAL(a0, env)
|
|
|
|
local args = types.slice(ast, 2)
|
|
|
|
if types._macro_Q(f) then
|
2024-08-22 07:47:11 +03:00
|
|
|
ast = f.fn(table.unpack(args)) -- TCO
|
2024-08-22 02:51:28 +03:00
|
|
|
else
|
|
|
|
args = utils.map(function(x) return EVAL(x,env) end, args)
|
2015-01-09 08:25:40 +03:00
|
|
|
if types._malfunc_Q(f) then
|
|
|
|
ast = f.ast
|
|
|
|
env = Env:new(f.env, f.params, args) -- TCO
|
|
|
|
else
|
2019-01-28 22:31:28 +03:00
|
|
|
return f(table.unpack(args))
|
2015-01-09 08:25:40 +03:00
|
|
|
end
|
2024-08-22 02:51:28 +03:00
|
|
|
end
|
2015-01-09 08:25:40 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- print
|
|
|
|
function PRINT(exp)
|
|
|
|
return printer._pr_str(exp, true)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- repl
|
|
|
|
local repl_env = Env:new()
|
|
|
|
function rep(str)
|
|
|
|
return PRINT(EVAL(READ(str),repl_env))
|
|
|
|
end
|
|
|
|
|
|
|
|
-- core.lua: defined using Lua
|
|
|
|
for k,v in pairs(core.ns) do
|
2024-08-22 06:53:59 +03:00
|
|
|
repl_env:set(k, v)
|
2015-01-09 08:25:40 +03:00
|
|
|
end
|
2024-08-22 06:53:59 +03:00
|
|
|
repl_env:set('eval',
|
2015-01-09 08:25:40 +03:00
|
|
|
function(ast) return EVAL(ast, repl_env) end)
|
2024-08-22 06:53:59 +03:00
|
|
|
repl_env:set('*ARGV*', types.List:new(types.slice(arg,2)))
|
2015-01-09 08:25:40 +03:00
|
|
|
|
|
|
|
-- core.mal: defined using mal
|
|
|
|
rep("(def! not (fn* (a) (if a false true)))")
|
2019-07-16 00:57:02 +03:00
|
|
|
rep("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \"\nnil)\")))))")
|
2015-01-09 08:25:40 +03:00
|
|
|
rep("(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))")
|
|
|
|
|
2015-03-01 00:58:35 +03:00
|
|
|
function print_exception(exc)
|
|
|
|
if exc then
|
|
|
|
if types._malexception_Q(exc) then
|
|
|
|
exc = printer._pr_str(exc.val, true)
|
|
|
|
end
|
|
|
|
print("Error: " .. exc)
|
|
|
|
print(debug.traceback())
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if #arg > 0 and arg[1] == "--raw" then
|
|
|
|
readline.raw = true
|
|
|
|
table.remove(arg,1)
|
|
|
|
end
|
|
|
|
|
2015-01-09 08:25:40 +03:00
|
|
|
if #arg > 0 then
|
2015-03-01 00:58:35 +03:00
|
|
|
xpcall(function() rep("(load-file \""..arg[1].."\")") end,
|
|
|
|
print_exception)
|
2015-01-09 08:25:40 +03:00
|
|
|
os.exit(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
while true do
|
|
|
|
line = readline.readline("user> ")
|
|
|
|
if not line then break end
|
2015-03-01 00:58:35 +03:00
|
|
|
xpcall(function() print(rep(line)) end,
|
|
|
|
print_exception)
|
2015-01-09 08:25:40 +03:00
|
|
|
end
|