Added some training wheels for functions.

This commit is contained in:
Bruce Hill 2018-01-25 18:07:55 -08:00
parent fb22ba11ac
commit 0c0fb98389

View File

@ -26,3 +26,19 @@ parse [error!, panic!, fail!, abort!] as: barf!
parse [error %, panic %, fail %, abort %] as: barf %
parse [assert %condition %message] as: assume %condition or barf %message
parse [%cond ? %if_true %if_false] as: %if_true if %cond else %if_false
compile [function %args %body, lambda %args %body] to
%body_lua <- (%body as lua)
%statements <- ((%body_lua's "statements") or "return \(%body_lua's "expr");")
%locals <- (% for all ((%body_lua's "locals") or []))
for all (%args's "value")
lua> "utils.remove_from_list(\%locals, \(% as lua expr));"
if: (size of %locals) > 0
%statements <- "local \(%locals joined with ", ");\n\%statements"
return {..}
expr: ".."
(function(\(((% as lua expr) for all (%args's "value")) joined with ", "))
\%statements
end)
parse [function %name %args %body] as: %name <- (function %args %body)
compile [call %fn %args] to {expr:"\(%fn as lua expr)(unpack(\(%args as lua expr)))"}