aboutsummaryrefslogtreecommitdiff
path: root/nomsu_compiler.moon
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2018-11-29 14:51:09 -0800
committerBruce Hill <bruce@bruce-hill.com>2018-11-29 14:51:26 -0800
commit09e571ffa67935f0ed29526768863f5338f91769 (patch)
tree417806b6fd5bc63897ce0e734648aab8ad8484ad /nomsu_compiler.moon
parent7a7dcefc447c4a4b20755fb6fb919a2f6328eafd (diff)
Extended text comprehensions to Lua constructors.
Diffstat (limited to 'nomsu_compiler.moon')
-rw-r--r--nomsu_compiler.moon29
1 files changed, 22 insertions, 7 deletions
diff --git a/nomsu_compiler.moon b/nomsu_compiler.moon
index 0266dab..9e77a13 100644
--- a/nomsu_compiler.moon
+++ b/nomsu_compiler.moon
@@ -68,21 +68,36 @@ compile = setmetatable({
return LuaCode("LuaCode()")
if code.type != "Text"
return LuaCode("LuaCode:from(", tostring(code.source)\as_lua!, ", ", compile(code), ")")
- add_bit_lua = (lua, bit_lua)->
- bit_leading_len = #(bit_lua\match("^[^\n]*"))
- lua\append(lua\trailing_line_len! + bit_leading_len > MAX_LINE and ",\n " or ", ")
- lua\append(bit_lua)
+
operate_on_text = (text)->
lua = LuaCode\from(text.source, "LuaCode:from(", tostring(text.source)\as_lua!)
for bit in *text
+ local bit_lua
if type(bit) == "string"
- add_bit_lua(lua, bit\as_lua!)
+ bit_lua = bit\as_lua!
elseif bit.type == "Text"
- add_bit_lua(lua, operate_on_text(bit))
+ bit_lua = operate_on_text(bit)
+ elseif bit.type == "Block"
+ bit_lua = LuaCode\from bit.source, "(function()",
+ "\n local _lua = LuaCode:from(", tostring(bit.source)\as_lua!, ")",
+ "\n local function add(bit) _lua:append(bit) end",
+ "\n local function join_with(glue)",
+ "\n local old_bits = _lua.bits",
+ "\n _lua = LuaCode:from(_lua.source)",
+ "\n _lua:concat_append(old_bits, glue)",
+ "\n end",
+ "\n ", compile(bit),
+ "\n return _lua",
+ "\nend)()"
else
- add_bit_lua(lua, compile(bit))
+ bit_lua = compile(bit)
+
+ bit_leading_len = #(bit_lua\match("^[^\n]*"))
+ lua\append(lua\trailing_line_len! + bit_leading_len > MAX_LINE and ",\n " or ", ")
+ lua\append(bit_lua)
lua\append ")"
return lua
+
return operate_on_text code
["lua >"]: (compile, code)->