aboutsummaryrefslogtreecommitdiff
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
parent7a7dcefc447c4a4b20755fb6fb919a2f6328eafd (diff)
Extended text comprehensions to Lua constructors.
-rw-r--r--nomsu_compiler.lua18
-rw-r--r--nomsu_compiler.moon29
2 files changed, 31 insertions, 16 deletions
diff --git a/nomsu_compiler.lua b/nomsu_compiler.lua
index ada9c87..7decbf1 100644
--- a/nomsu_compiler.lua
+++ b/nomsu_compiler.lua
@@ -96,24 +96,24 @@ local compile = setmetatable({
if code.type ~= "Text" then
return LuaCode("LuaCode:from(", tostring(code.source):as_lua(), ", ", compile(code), ")")
end
- local add_bit_lua
- add_bit_lua = function(lua, bit_lua)
- local bit_leading_len = #(bit_lua:match("^[^\n]*"))
- lua:append(lua:trailing_line_len() + bit_leading_len > MAX_LINE and ",\n " or ", ")
- return lua:append(bit_lua)
- end
local operate_on_text
operate_on_text = function(text)
local lua = LuaCode:from(text.source, "LuaCode:from(", tostring(text.source):as_lua())
for _index_0 = 1, #text do
local bit = text[_index_0]
+ local bit_lua
if type(bit) == "string" then
- add_bit_lua(lua, bit:as_lua())
+ bit_lua = bit:as_lua()
elseif bit.type == "Text" then
- add_bit_lua(lua, operate_on_text(bit))
+ bit_lua = operate_on_text(bit)
+ elseif bit.type == "Block" then
+ 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)
end
+ local 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)
end
lua:append(")")
return lua
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)->