Merge pull request #406 from chr15m/lib-alias-hacks
[jackhill/mal.git] / lua / step5_tco.lua
index 237f5ea..9c28e4e 100755 (executable)
@@ -41,6 +41,7 @@ function EVAL(ast, env)
     if not types._list_Q(ast) then return eval_ast(ast, env) end
 
     local a0,a1,a2,a3 = ast[1], ast[2],ast[3],ast[4]
+    if not a0 then return ast end
     local a0sym = types._symbol_Q(a0) and a0.val or ""
     if 'def!' == a0sym then
         return env:set(a1, EVAL(a2, env))
@@ -57,13 +58,13 @@ function EVAL(ast, env)
     elseif 'if' == a0sym then
         local cond = EVAL(a1, env)
         if cond == types.Nil or cond == false then
-            if a3 then ast = a3 else return types.Nil end -- TCO
+            if #ast > 3 then ast = a3 else return types.Nil end -- TCO
         else
             ast = a2 -- TCO
         end
     elseif 'fn*' == a0sym then
         return types.MalFunc:new(function(...)
-            return EVAL(a2, Env:new(env, a1, arg))
+            return EVAL(a2, Env:new(env, a1, table.pack(...)))
         end, a2, env, a1)
     else
         local args = eval_ast(ast, env)
@@ -72,7 +73,7 @@ function EVAL(ast, env)
             ast = f.ast
             env = Env:new(f.env, f.params, args) -- TCO
         else
-            return f(unpack(args))
+            return f(table.unpack(args))
         end
     end
   end