aboutsummaryrefslogtreecommitdiff
path: root/nomsu_compiler.moon
diff options
context:
space:
mode:
authorBruce Hill <bitbucket@bruce-hill.com>2018-09-17 15:29:48 -0700
committerBruce Hill <bitbucket@bruce-hill.com>2018-09-17 15:30:24 -0700
commitc1cba45968b8d5e993fb12fcd7dae9192fbe6d79 (patch)
treeffa1ba6e6eb9b544bd28ce08e653d7467df226e8 /nomsu_compiler.moon
parente3bf10196ae42463db8d702fe90a222bb7cb4d54 (diff)
Added support for compile actions returning trees, and compiling blocks
into values.
Diffstat (limited to 'nomsu_compiler.moon')
-rw-r--r--nomsu_compiler.moon29
1 files changed, 24 insertions, 5 deletions
diff --git a/nomsu_compiler.moon b/nomsu_compiler.moon
index 2f79a34..19d4049 100644
--- a/nomsu_compiler.moon
+++ b/nomsu_compiler.moon
@@ -389,7 +389,7 @@ with NomsuCompiler
return run_lua_fn!
- .compile = (tree, compile_actions)=>
+ .compile = (tree, compile_actions, force_value=false)=>
compile_actions or= @environment.COMPILE_ACTIONS
if tree.version
if get_version = @[("Nomsu version")\as_lua_id!]
@@ -430,7 +430,7 @@ with NomsuCompiler
args = {}
for i, tok in ipairs tree
if type(tok) == "string" then continue
- arg_lua = @compile(tok, compile_actions)
+ arg_lua = @compile(tok, compile_actions, true)
unless arg_lua.is_value
if tok.type == "Block"
@compile_error tok,
@@ -469,9 +469,28 @@ with NomsuCompiler
return lua
when "Block"
- lua = LuaCode(tree.source)
- lua\concat_append([@compile(line, compile_actions)\as_statements! for line in *tree], "\n")
- return lua
+ if not force_value
+ lua = LuaCode(tree.source)
+ lua\concat_append([@compile(line, compile_actions)\as_statements! for line in *tree], "\n")
+ return lua
+ else
+ lua = LuaCode.Value(tree.source)
+ values = [@compile(line) for line in *tree]
+ all_values = true
+ for v in *values do all_values and= v.is_value
+ if all_values
+ return values[1] if #values == 1
+ lua\append "("
+ lua\concat_append(values, " and nil or ")
+ lua\append ")"
+ else
+ lua\append("((function()")
+ for i, v in ipairs(values)
+ if v.is_value
+ v = v\as_statements(i == #values and 'return ' or '')
+ lua\append "\n ", v
+ lua\append("\nend)())")
+ return lua
when "Text"
lua = LuaCode.Value(tree.source)