1c96790
Branches > main > 1c96790 > files
Add variable-length bodies to function calls
Commit: 1c9679014b1633d34ef10877b659831dce1a63c1
Parent: d60d02e
2 changed files:
org/faddle/lang/compile.lua view file
@@ -28,23 +28,43 @@ return statement and "return " or "" end +local function compile_body(body)+ 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)+ end+ i = i + 1+ lst = rest(lst)+ end+ return table.concat(lines, ";")+end++local function build_fn_args(fn_args)+ assert(type(fn_args) == "table", "Function args must be a table")+ local compiled_args = {}+ local i = 1+ while fn_args[i] ~= nil do+ local arg = fn_args[i]+ assert(is_symbol(arg) and not module(arg), "Function arguments must be a simple symbol")+ table.insert(compiled_args, compile(arg))+ i = i + 1+ end+ return table.concat(compiled_args, ",")+end+ -- TODO should missing args error? local function compile_fn(form, statement) local args = rest(form) local fn_args = first(args) local body = rest(args)- assert(type(fn_args) == "table", "Function args must be a table")- assert(count(body) == 1)- local compiled_args_t = {}- local i = 1- while fn_args[i] ~= nil do- local arg = fn_args[i]- assert(is_symbol(arg) and not module(arg), "Function arguments must be a simple symbol")- table.insert(compiled_args_t, compile(arg))- i = i + 1- end- local compiled_args = table.concat(compiled_args_t, ",")- return ret(statement) .. "function(" .. compiled_args .. ") " .. compile(first(body), true) .. " end"+ return ret(statement) .. "function(" .. build_fn_args(fn_args) .. ") " .. compile_body(body) .. " end" end -- If I'm just using the same lexical stuff as Lua, how do builtin Lua modules work?test.lua view file
@@ -165,9 +165,9 @@ -- Add more arities? teq(read_eval("((fn [x y] [x y]) 1 2)"), {1, 2}) 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 []))"), nil)+teq(read_eval("((fn [] 1 2 3))"), 3) -- teq(read_eval("((let []))"), nil)--- teq(read_eval("((fn [] 1 2 3))"), 3) -- teq(read_eval("((let [] 1 2 3))"), 3) -- Feels like do could be implemented in terms of let or fn idk.