Skip to content

Commit

Permalink
Add non-key table destructure recursion. #46
Browse files Browse the repository at this point in the history
  • Loading branch information
meric committed Nov 7, 2016
1 parent 5b1622f commit adc0395
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
12 changes: 7 additions & 5 deletions l2l/ext/let.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ For each assignment,
(fn is_strictly_array (self)
(apply and
(map
(fn (field) (not (utils.hasmetatable field lua_field_name)))
(fn (field) (or (utils.hasmetatable field lua_name) (not (getmetatable field))))
self.fieldlist)))

(:where destructure lua_table (fn (self value)
Expand Down Expand Up @@ -63,11 +63,13 @@ For each assignment,

(:where destructure list (fn (self value)
(cond
(and (utils.hasmetatable value lua_table)
(is_strictly_array value))
(and (utils.hasmetatable value lua_table))
-- (a b c) {1,2,3}
`\local \,(lua_namelist (vector.cast self lua_name)) =
\,value.fieldlist
(do
(assert (is_strictly_array value)
"table with keys cannot be destructred into list")
`\local \,(lua_namelist (vector.cast self lua_name)) =
\,value.fieldlist)
(and (utils.hasmetatable value list)
(== (:car value) (symbol "quote")))
-- (d e f) '(4 5 (+ 6 8))
Expand Down
11 changes: 10 additions & 1 deletion l2l/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ local loadstring = _G["loadstring"] or _G["load"]

local function assert_exec_equal(source, ...)
local src = compiler.compile(source, "test")
local ret = {loadstring(src)()}
local f = loadstring(src)
if not f then
print(src)
end
local ret = {pcall(f)}
local ok = table.remove(ret, 1)
if not ok then
print(src)
end
for i=1, math.max(select("#", ...), #ret) do
t.assert_equal(select(i, ...), ret[i])
end
Expand Down Expand Up @@ -148,6 +156,7 @@ function test_let0()
local src = assert_exec_equal([[
(let (
{a, b, hello=c, world={f}} {1, 2, hello=4, world={5}}
{y, {z}} {1, {2}}
d 3
e 4)
\return a, b, c, d, e, f)
Expand Down

0 comments on commit adc0395

Please sign in to comment.