From adc03950e1d1f607ad6a073767d1a579e8429103 Mon Sep 17 00:00:00 2001 From: Eric Man Date: Mon, 7 Nov 2016 18:39:12 +1100 Subject: [PATCH] Add non-key table destructure recursion. #46 --- l2l/ext/let.lisp | 12 +++++++----- l2l/test.lua | 11 ++++++++++- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/l2l/ext/let.lisp b/l2l/ext/let.lisp index 8ec3595..eace1f9 100644 --- a/l2l/ext/let.lisp +++ b/l2l/ext/let.lisp @@ -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) @@ -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)) diff --git a/l2l/test.lua b/l2l/test.lua index 6a7ddfe..7ac0a87 100644 --- a/l2l/test.lua +++ b/l2l/test.lua @@ -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 @@ -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)