diff options
| author | Bruce Hill <bitbucket@bruce-hill.com> | 2017-08-22 02:52:05 -0700 |
|---|---|---|
| committer | Bruce Hill <bitbucket@bruce-hill.com> | 2017-08-22 02:52:05 -0700 |
| commit | a5863030ec0f729080f7407dbfa9970c8829fe69 (patch) | |
| tree | 8105317a40e63fec7c8fc752cd54149b3ec2bdb5 /core.moon | |
| parent | 0e916161b1b47758e0bb15051028d8c92c6446e7 (diff) | |
Simple macros work.
Diffstat (limited to 'core.moon')
| -rwxr-xr-x | core.moon | 94 |
1 files changed, 90 insertions, 4 deletions
@@ -28,9 +28,13 @@ g\defmacro "let %varname = %value", (vars, helpers, ftype)=> .lua "vars[#{.ded(.transform(vars.varname))} = #{.ded(.transform(vars.value))}" return nil -g\defmacro {"true", "yes"}, (vars,helpers,ftype)=> helpers.lua("true") -g\defmacro {"false", "no"}, (vars,helpers,ftype)=> helpers.lua("false") -g\defmacro "nil", (vars,helpers,ftype)=> helpers.lua("nil") +singleton = (aliases, value)-> + g\defmacro aliases, (vars,helpers,ftype)=> + if ftype == "Expression" + helpers.lua(value) + else + helpers.lua("ret = #{value}") + infix = (ops)-> for op in *ops alias = op @@ -50,6 +54,10 @@ unary = (ops)-> elseif ftype == "Expression" helpers.lua("#{op}(#{helpers.var('x')})") else error("Unknown: #{ftype}") + +singleton {"true","yes"}, "true" +singleton {"false","no"}, "false" +singleton {"nil","null"}, "nil" infix{"+","-","*","/","==",{"!=","~="},"<","<=",">",">=","^","and","or"} unary{"-","#","not"} g\def [[%x == %y]], (args)=> utils.equivalent(args.x, args.y) @@ -108,5 +116,83 @@ g\def {[[# %list]], [[length of %list]], [[size of %list]]}, (args)=> return return #(.list) +g\defmacro "if %condition %if_body else %else_body", (vars,helpers,ftype)=> + with helpers + switch ftype + when "Expression" + .lua "((#{.ded(.transform(vars.condition))}) and" + .indented -> + .lua "("..(.ded(.transform(vars.if_body)))..")" + .lua "or ("..(.ded(.transform(vars.if_body))).."))(game, vars)" + when "Statement" + .lua("if (#{.ded(.transform(vars.condition))}) then") + .indented -> + if_body = vars.if_body + while if_body.type != "Block" + if_body = if_body.value + if if_body == nil then error("Failed to find body.") + for statement in *if_body.value + .lua(.ded(.transform(statement))) + .lua("else") + .indented -> + else_body = vars.else_body + while else_body.type != "Block" + else_body = else_body.value + if else_body == nil then error("Failed to find body.") + for statement in *else_body.value + .lua(.ded(.transform(statement))) + .lua("end") + return nil + +g\defmacro "for %varname in %iterable %body", (vars,helpers,ftype)=> + with helpers + switch ftype + when "Expression" + .lua "(function(game, vars)" + .indented -> + .lua "local comprehension, vars = {}, setmetatable({}, {__index=vars})" + .lua "for i, value in ipairs(#{.ded(.transform(vars.iterable))}) do" + .indented -> + .lua "local comp_value" + .lua "vars[#{.ded(.transform(vars.varname))}] = value" + body = vars.body + while body.type != "Block" + body = body.value + if body == nil then error("Failed to find body.") + for statement in *body.value + -- TODO: Clean up this ugly bit + .lua("comp_value = "..(.ded(.transform(statement.value, {type:"Expression"})))) + .lua "table.insert(comprehension, comp_value)" + .lua "end" + .lua "return comprehension" + .lua "end)(game,vars)" + when "Statement" + .lua "do" + .indented -> + .lua "local vars = setmetatable({}, {__index=vars})" + .lua "for i, value in ipairs(#{.ded(.transform(vars.iterable))}) do" + .indented -> + .lua "vars[#{.ded(.transform(vars.varname))}] = value" + body = vars.body + while body.type != "Block" + body = body.value + if body == nil then error("Failed to find body.") + for statement in *body.value + .lua(.ded(.transform(statement))) + .lua "end" + .lua "end" + return nil + +g\simplemacro "if %condition %body", [[ +if %condition %body +..else: nil +]] + +g\simplemacro "unless %condition %body", [[ +if (not %condition) %body +..else: nil]] + +g\def [[do %action]], (vars)=> return vars.action(self,vars) + -return game +return g |
