Skip to content

Commit

Permalink
lua: style improvements, especially for try*
Browse files Browse the repository at this point in the history
  • Loading branch information
asarhaddon committed Aug 22, 2024
1 parent 8ff0c9b commit a5a7c89
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 22 deletions.
6 changes: 1 addition & 5 deletions impls/lua/env.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ function Env:new(outer, binds, exprs)
if binds then
for i, b in ipairs(binds) do
if binds[i].val == '&' then
local new_exprs = types.List:new()
for j = i, #exprs do
table.insert(new_exprs, exprs[j])
end
data[binds[i+1].val] = new_exprs
data[binds[i+1].val] = types.List.slice(exprs, i)
break
end
data[binds[i].val] = exprs[i]
Expand Down
2 changes: 1 addition & 1 deletion impls/lua/step8_macros.lua
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ function EVAL(ast, env)
local f = EVAL(a0, env)
local args = types.slice(ast, 2)
if types._macro_Q(f) then
ast = f.fn(table.unpack(args))
ast = f.fn(table.unpack(args)) -- TCO
else
args = utils.map(function(x) return EVAL(x,env) end, args)
if types._malfunc_Q(f) then
Expand Down
17 changes: 9 additions & 8 deletions impls/lua/step9_try.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,23 +99,24 @@ function EVAL(ast, env)
mac.ismacro = true
return env:set(a1.val, mac)
elseif 'try*' == a0sym then
if a2 == nil or a2[1].val ~= 'catch*' then
ast = a1 -- TCO
else
local exc, result = nil, nil
xpcall(function()
result = EVAL(a1, env)
end, function(err)
exc = err
end)
if exc ~= nil then
if exc == nil then
return result
else
if types._malexception_Q(exc) then
exc = exc.val
end
if a2 and a2[1].val == 'catch*' then
result = EVAL(a2[3], Env:new(env, {a2[2]}, {exc}))
else
types.throw(exc)
end
ast, env = a2[3], Env:new(env, {a2[2]}, {exc}) -- TCO
end
return result
end
elseif 'do' == a0sym then
utils.map(function(x) return EVAL(x, env) end, types.slice(ast, 2, #ast - 1))
ast = ast[#ast] -- TCO
Expand All @@ -134,7 +135,7 @@ function EVAL(ast, env)
local f = EVAL(a0, env)
local args = types.slice(ast, 2)
if types._macro_Q(f) then
ast = f.fn(table.unpack(args))
ast = f.fn(table.unpack(args)) -- TCO
else
args = utils.map(function(x) return EVAL(x,env) end, args)
if types._malfunc_Q(f) then
Expand Down
17 changes: 9 additions & 8 deletions impls/lua/stepA_mal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -100,23 +100,24 @@ function EVAL(ast, env)
mac.ismacro = true
return env:set(a1.val, mac)
elseif 'try*' == a0sym then
if a2 == nil or a2[1].val ~= 'catch*' then
ast = a1 -- TCO
else
local exc, result = nil, nil
xpcall(function()
result = EVAL(a1, env)
end, function(err)
exc = err
end)
if exc ~= nil then
if exc == nil then
return result
else
if types._malexception_Q(exc) then
exc = exc.val
end
if a2 and a2[1].val == 'catch*' then
result = EVAL(a2[3], Env:new(env, {a2[2]}, {exc}))
else
types.throw(exc)
end
ast, env = a2[3], Env:new(env, {a2[2]}, {exc}) -- TCO
end
return result
end
elseif 'do' == a0sym then
utils.map(function(x) return EVAL(x, env) end, types.slice(ast, 2, #ast - 1))
ast = ast[#ast] -- TCO
Expand All @@ -135,7 +136,7 @@ function EVAL(ast, env)
local f = EVAL(a0, env)
local args = types.slice(ast, 2)
if types._macro_Q(f) then
ast = f.fn(table.unpack(args))
ast = f.fn(table.unpack(args)) -- TCO
else
args = utils.map(function(x) return EVAL(x,env) end, args)
if types._malfunc_Q(f) then
Expand Down

0 comments on commit a5a7c89

Please sign in to comment.