a85f72c
Branches > main > a85f72c > files
Add variable-length bodies to let bindings
Commit: a85f72ca89886a1fc19559efde8437f7081a0c20
Parent: 1c96790
2 changed files:
org/faddle/lang/compile.lua view file
@@ -32,18 +32,22 @@ local lines = {} local i = 1 local lst = body- while count(lst) ~= 0 do- local form = first(lst)- local last_form = i == count(body)- -- TODO not sure if this is correct- -- Basically `2` is not a valid lua statement, only function calls are...- if last_form or (is_list(form) and count(form) > 0) then- lines[#lines+1] = compile(form, last_form)+ if count(body) == 0 then+ return "return"+ else+ while count(lst) ~= 0 do+ local form = first(lst)+ local last_form = i == count(body)+ -- TODO not sure if this is correct+ -- Basically `2` is not a valid lua statement, only function calls are...+ if last_form or (is_list(form) and count(form) > 0) then+ lines[#lines+1] = compile(form, last_form)+ end+ i = i + 1+ lst = rest(lst) end- i = i + 1- lst = rest(lst)+ return table.concat(lines, ";") end- return table.concat(lines, ";") end local function build_fn_args(fn_args)@@ -81,11 +85,10 @@ local body = rest(args) assert(type(bindings) == "table" and #bindings % 2 == 0, "Let requires an even number of binding forms") assert(#bindings == 2)- assert(count(body) == 1) local sym = bindings[1] local binding = bindings[2] if statement then- return "local " .. munge(sym) .. "=" .. compile(binding) .. ";" .. compile(first(body), true)+ return "local " .. munge(sym) .. "=" .. compile(binding) .. ";" .. compile_body(body) else return "return (function()" .. compile_let(form, not statement) .. " end)()" endtest.lua view file
@@ -167,8 +167,9 @@ teq(read_eval("((fn [x y z] [x y z]) 1 2 3)"), {1, 2, 3}) teq(read_eval("((fn []))"), nil) teq(read_eval("((fn [] 1 2 3))"), 3)--- teq(read_eval("((let []))"), nil)--- teq(read_eval("((let [] 1 2 3))"), 3)+-- TODO still need variable amounts of let bindings!+teq(read_eval("(let [x 1])"), nil)+teq(read_eval("(let [x 2] 1 2 3)"), 3) -- Feels like do could be implemented in terms of let or fn idk. -- teq(read_eval("(do)"), nil)