d60d02e
Branches > main > d60d02e > files
Add function arguments and table compilation
Commit: d60d02e79c92a69c7fcfecee5726f9a1d11d28d8
Parent: adbfe9f
1 changed file:
org/faddle/lang/compile.lua view file
@@ -28,13 +28,23 @@ return statement and "return " or "" 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(#fn_args == 0)+ assert(type(fn_args) == "table", "Function args must be a table") assert(count(body) == 1)- return ret(statement) .. "function() " .. compile(first(body), true) .. " end"+ 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" end -- If I'm just using the same lexical stuff as Lua, how do builtin Lua modules work?@@ -87,7 +97,14 @@ return ret(statement) .. tostring(form) elseif t == "string" then return ret(statement) .. string.format("%q", form)+ elseif t == "table" then+ local entries = {}+ for k, v in pairs(form) do+ entries[#entries+1] = "[" .. compile(k) .. "]=" .. compile(v)+ end+ return ret(statement) .. "{" .. table.concat(entries, ",").. "}" else+ print(lisp_print(form)) error("Cannot compile form of type '" .. t .. "'") end end