aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bitbucket@bruce-hill.com>2018-07-10 17:34:39 -0700
committerBruce Hill <bitbucket@bruce-hill.com>2018-07-10 17:34:50 -0700
commit194146d36591e6c3a964667ead9109bbc6e00524 (patch)
treefa2801b488f55f48bbd9a41f49585d125a54b180
parent26a6174f28191fe3364e30c76c1c7a5814e7c5ab (diff)
Improving codegen line wrapping.
-rw-r--r--code_obj.lua26
-rw-r--r--code_obj.moon19
-rw-r--r--nomsu_compiler.lua12
-rw-r--r--nomsu_compiler.moon13
4 files changed, 38 insertions, 32 deletions
diff --git a/code_obj.lua b/code_obj.lua
index cc2fb69..a822499 100644
--- a/code_obj.lua
+++ b/code_obj.lua
@@ -85,21 +85,27 @@ do
local _continue_0 = false
repeat
local b = select(i, ...)
- assert(b, "bit is nil")
+ assert(b, "code bit is nil")
if b == '' then
_continue_0 = true
break
end
bits[#bits + 1] = b
if type(b) == 'string' then
- do
- local spaces = match(b, "\n([ ]*)[^\n]*$")
- if spaces then
- self.current_indent = #spaces
- end
+ local trailing_text, spaces = match(b, "\n(([ ]*)[^\n]*)$")
+ if trailing_text then
+ self.current_indent = #spaces
+ self.trailing_line_len = #trailing_text
+ end
+ else
+ if #b.indents > 1 then
+ self.trailing_line_len = b.trailing_line_len
+ else
+ self.trailing_line_len = self.trailing_line_len + #tostring(b)
+ end
+ if self.current_indent ~= 0 then
+ indents[#bits] = self.current_indent
end
- elseif self.current_indent ~= 0 then
- indents[#bits] = self.current_indent
end
_continue_0 = true
until true
@@ -172,7 +178,9 @@ do
_class_0 = setmetatable({
__init = function(self, source, ...)
self.source = source
- self.bits, self.indents, self.current_indent = { }, { }, 0
+ self.bits = { }
+ self.indents, self.current_indent = { }, 0
+ self.trailing_line_len = 0
if type(self.source) == 'string' then
self.source = Source:from_string(self.source)
end
diff --git a/code_obj.moon b/code_obj.moon
index 8ac7146..1586d15 100644
--- a/code_obj.moon
+++ b/code_obj.moon
@@ -45,7 +45,9 @@ class Source
class Code
new: (@source, ...)=>
- @bits, @indents, @current_indent = {}, {}, 0
+ @bits = {}
+ @indents, @current_indent = {}, 0
+ @trailing_line_len = 0
if type(@source) == 'string'
@source = Source\from_string(@source)
assert(@source and Source\is_instance(@source), "Source has the wrong type")
@@ -57,14 +59,21 @@ class Code
match = string.match
for i=1,n
b = select(i, ...)
- assert(b, "bit is nil")
+ assert(b, "code bit is nil")
if b == '' then continue
bits[#bits+1] = b
if type(b) == 'string'
- if spaces = match(b, "\n([ ]*)[^\n]*$")
+ trailing_text, spaces = match(b, "\n(([ ]*)[^\n]*)$")
+ if trailing_text
@current_indent = #spaces
- elseif @current_indent != 0
- indents[#bits] = @current_indent
+ @trailing_line_len = #trailing_text
+ else
+ if #b.indents > 1
+ @trailing_line_len = b.trailing_line_len
+ else
+ @trailing_line_len += #tostring(b)
+ if @current_indent != 0
+ indents[#bits] = @current_indent
@__str = nil
concat_append: (values, joiner, wrapping_joiner)=>
diff --git a/nomsu_compiler.lua b/nomsu_compiler.lua
index 103a31e..3c1102f 100644
--- a/nomsu_compiler.lua
+++ b/nomsu_compiler.lua
@@ -150,7 +150,7 @@ local NomsuCompiler = setmetatable({ }, {
end
})
do
- NomsuCompiler.NOMSU_COMPILER_VERSION = 3
+ NomsuCompiler.NOMSU_COMPILER_VERSION = 4
NomsuCompiler.NOMSU_SYNTAX_VERSION = Parser.version
NomsuCompiler._ENV = NomsuCompiler
NomsuCompiler.nomsu = NomsuCompiler
@@ -259,20 +259,14 @@ do
end
local add_lua_string_bits
add_lua_string_bits = function(self, val_or_stmt, code)
- local line_len = 0
local cls_str = val_or_stmt == "value" and "LuaCode.Value(" or "LuaCode("
if code.type ~= "Text" then
return LuaCode(code.source, cls_str, repr(tostring(code.source)), ", ", self:compile(code), ")")
end
local add_bit_lua
add_bit_lua = function(lua, bit_lua)
- line_len = line_len + #tostring(bit_lua)
- if line_len > MAX_LINE then
- lua:append(",\n ")
- line_len = 4
- else
- lua:append(", ")
- end
+ local bit_leading_len = #(tostring(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
diff --git a/nomsu_compiler.moon b/nomsu_compiler.moon
index 747ddc6..5f766a8 100644
--- a/nomsu_compiler.moon
+++ b/nomsu_compiler.moon
@@ -92,7 +92,7 @@ dict = (t)-> setmetatable(t, _dict_mt)
MAX_LINE = 80 -- For beautification purposes, try not to make lines much longer than this value
NomsuCompiler = setmetatable({}, {__index: (k)=> if _self = rawget(@, "self") then _self[k] else nil})
with NomsuCompiler
- .NOMSU_COMPILER_VERSION = 3
+ .NOMSU_COMPILER_VERSION = 4
.NOMSU_SYNTAX_VERSION = Parser.version
._ENV = NomsuCompiler
.nomsu = NomsuCompiler
@@ -157,18 +157,13 @@ with NomsuCompiler
return operate_on_text code
add_lua_string_bits = (val_or_stmt, code)=>
- line_len = 0
cls_str = val_or_stmt == "value" and "LuaCode.Value(" or "LuaCode("
if code.type != "Text"
return LuaCode(code.source, cls_str, repr(tostring(code.source)), ", ", @compile(code), ")")
add_bit_lua = (lua, bit_lua)->
- line_len += #tostring(bit_lua)
- if line_len > MAX_LINE
- lua\append ",\n "
- line_len = 4
- else
- lua\append ", "
- lua\append bit_lua
+ bit_leading_len = #(tostring(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.Value(text.source, cls_str, repr(tostring(text.source)))
for bit in *text