aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2018-11-10 00:29:08 -0800
committerBruce Hill <bruce@bruce-hill.com>2018-11-10 00:29:20 -0800
commit1b9d57844ede419488ae62ba94c081f6cf2e8a05 (patch)
treefdccbc9ac3e0f00a497964f2c0775d56b7b93066
parent771007bc4dbcde1698fd86d87374df21651585a5 (diff)
Tweaks to line clumping
-rw-r--r--nomsu_decompiler.lua98
-rw-r--r--nomsu_decompiler.moon91
2 files changed, 109 insertions, 80 deletions
diff --git a/nomsu_decompiler.lua b/nomsu_decompiler.lua
index 17ac7eb..2deaa7f 100644
--- a/nomsu_decompiler.lua
+++ b/nomsu_decompiler.lua
@@ -202,7 +202,7 @@ tree_to_nomsu = function(tree)
return (t:map(coroutine.yield) and nil)
end) do
if subtree.type == "Block" then
- if #subtree > 1 or #tree_to_inline_nomsu(subtree):text() > 20 then
+ if #subtree > 1 then
inline = false
end
end
@@ -231,36 +231,12 @@ tree_to_nomsu = function(tree)
if tree.shebang then
nomsu:append(tree.shebang, "\n")
end
- local should_clump
- should_clump = function(prev_line, line)
- if prev_line.type == "Action" and line.type == "Action" then
- if prev_line.stub == "use" then
- return line.stub == "use"
- end
- if prev_line.stub == "test" then
- return true
- end
- if line.stub == "test" then
- return false
- end
- end
- return not tree_to_nomsu(prev_line):is_multiline()
- end
for chunk_no, chunk in ipairs(tree) do
if chunk_no > 1 then
nomsu:append("\n\n" .. tostring(("~"):rep(80)) .. "\n\n")
end
if chunk.type == "Block" then
- for line_no, line in ipairs(chunk) do
- if line_no > 1 then
- if should_clump(chunk[line_no - 1], line) then
- nomsu:append("\n")
- else
- nomsu:append("\n\n")
- end
- end
- nomsu:append(tree_to_nomsu(line))
- end
+ nomsu:append(NomsuCode:from(chunk.source, unpack(tree_to_nomsu(chunk).bits, 2)))
else
nomsu:append(tree_to_nomsu(chunk))
end
@@ -279,25 +255,36 @@ tree_to_nomsu = function(tree)
nomsu:append(target_nomsu)
nomsu:append(target_nomsu:is_multiline() and "\n..::" or "::")
end
+ local word_buffer = { }
for i, bit in ipairs(tree) do
- if type(bit) == "string" then
- if next_space == " " then
- local clump_words
- if type(tree[i - 1]) == 'string' then
- clump_words = is_operator(bit) ~= is_operator(tree[i - 1])
- else
- clump_words = bit == "'"
+ local _continue_0 = false
+ repeat
+ if type(bit) == "string" then
+ if #word_buffer > 0 and is_operator(bit) == is_operator(word_buffer[#word_buffer]) then
+ table.insert(word_buffer, " ")
end
- if clump_words then
- next_space = ""
+ table.insert(word_buffer, bit)
+ _continue_0 = true
+ break
+ end
+ if #word_buffer > 0 then
+ local words = table.concat(word_buffer)
+ if next_space == " " then
+ if nomsu:trailing_line_len() + #words > MAX_LINE then
+ next_space = " \\\n.."
+ elseif word_buffer[1] == "'" then
+ next_space = ""
+ end
end
+ nomsu:append(next_space, words)
+ word_buffer = { }
+ next_space = " "
end
- nomsu:append(next_space, bit)
- next_space = nomsu:trailing_line_len() > MAX_LINE and " \\\n.." or " "
- else
local bit_nomsu = recurse(bit)
- if i < #tree and bit.type == "Block" and not bit_nomsu:is_multiline() then
- bit_nomsu:parenthesize()
+ if bit.type == "Block" and not bit_nomsu:is_multiline() then
+ if #bit_nomsu:text() > nomsu:trailing_line_len() then
+ bit_nomsu = tree_to_nomsu(bit)
+ end
end
if next_space == " " and not bit_nomsu:is_multiline() and nomsu:trailing_line_len() + #bit_nomsu:text() > MAX_LINE then
if bit.type == 'Action' then
@@ -310,8 +297,23 @@ tree_to_nomsu = function(tree)
nomsu:append(next_space)
end
nomsu:append(bit_nomsu)
- next_space = bit_nomsu:is_multiline() and "\n.." or " "
+ next_space = (bit_nomsu:is_multiline() or bit.type == 'Block') and "\n.." or " "
+ _continue_0 = true
+ until true
+ if not _continue_0 then
+ break
+ end
+ end
+ if #word_buffer > 0 then
+ local words = table.concat(word_buffer)
+ if next_space == " " then
+ if nomsu:trailing_line_len() + #words > MAX_LINE then
+ next_space = " \\\n.."
+ elseif word_buffer[1] == "'" then
+ next_space = ""
+ end
end
+ nomsu:append(next_space, words)
end
return nomsu
elseif "EscapedNomsu" == _exp_0 then
@@ -321,12 +323,20 @@ tree_to_nomsu = function(tree)
end
return NomsuCode:from(tree.source, "\\", nomsu)
elseif "Block" == _exp_0 then
+ local prev_line, needs_space = nil, { }
for i, line in ipairs(tree) do
local line_nomsu = tree_to_nomsu(line)
- nomsu:append(line_nomsu)
- if i < #tree then
- nomsu:append(line_nomsu:match('\n[^\n]*\n') and "\n\n" or "\n")
+ if i > 1 then
+ nomsu:append("\n")
+ if tree[i - 1].type ~= "Comment" then
+ needs_space[i] = (line_nomsu:is_multiline() and prev_line:is_multiline())
+ if tree[i].type == "Comment" or needs_space[i] or needs_space[i - 1] then
+ nomsu:append("\n")
+ end
+ end
end
+ nomsu:append(line_nomsu)
+ prev_line = line_nomsu
end
return NomsuCode:from(tree.source, ":\n ", nomsu)
elseif "Text" == _exp_0 then
diff --git a/nomsu_decompiler.moon b/nomsu_decompiler.moon
index 474d61f..fa50eb2 100644
--- a/nomsu_decompiler.moon
+++ b/nomsu_decompiler.moon
@@ -156,7 +156,7 @@ tree_to_nomsu = (tree)->
inline = true
for subtree in coroutine.wrap(-> (t\map(coroutine.yield) and nil))
if subtree.type == "Block"
- if #subtree > 1 or #tree_to_inline_nomsu(subtree)\text! > 20
+ if #subtree > 1
inline = false
if inline
@@ -176,25 +176,14 @@ tree_to_nomsu = (tree)->
when "FileChunks"
if tree.shebang
nomsu\append tree.shebang, "\n"
- should_clump = (prev_line, line)->
- if prev_line.type == "Action" and line.type == "Action"
- if prev_line.stub == "use" then return line.stub == "use"
- if prev_line.stub == "test" then return true
- if line.stub == "test" then return false
- return not tree_to_nomsu(prev_line)\is_multiline!
for chunk_no, chunk in ipairs tree
nomsu\append "\n\n#{("~")\rep(80)}\n\n" if chunk_no > 1
if chunk.type == "Block"
- for line_no, line in ipairs chunk
- if line_no > 1
- if should_clump(chunk[line_no-1], line)
- nomsu\append "\n"
- else
- nomsu\append "\n\n"
- nomsu\append tree_to_nomsu(line)
+ nomsu\append NomsuCode\from(chunk.source, table.unpack(tree_to_nomsu(chunk).bits, 2))
else
nomsu\append tree_to_nomsu(chunk)
+
nomsu\append('\n') unless nomsu\match("\n$")
return nomsu
@@ -207,30 +196,53 @@ tree_to_nomsu = (tree)->
nomsu\append target_nomsu
nomsu\append(target_nomsu\is_multiline! and "\n..::" or "::")
+ word_buffer = {}
for i,bit in ipairs tree
if type(bit) == "string"
+ if #word_buffer > 0 and is_operator(bit) == is_operator(word_buffer[#word_buffer])
+ table.insert word_buffer, " "
+ table.insert word_buffer, bit
+ continue
+
+ if #word_buffer > 0
+ words = table.concat(word_buffer)
if next_space == " "
- clump_words = if type(tree[i-1]) == 'string'
- is_operator(bit) != is_operator(tree[i-1])
- else bit == "'"
- next_space = "" if clump_words
- nomsu\append next_space, bit
- next_space = nomsu\trailing_line_len! > MAX_LINE and " \\\n.." or " "
- else
- bit_nomsu = recurse(bit)
- if i < #tree and bit.type == "Block" and not bit_nomsu\is_multiline!
- bit_nomsu\parenthesize!
-
- if next_space == " " and not bit_nomsu\is_multiline! and nomsu\trailing_line_len! + #bit_nomsu\text! > MAX_LINE
- if bit.type == 'Action'
- bit_nomsu = NomsuCode\from bit.source, "(..)\n ", tree_to_nomsu(bit)
- else
+ if nomsu\trailing_line_len! + #words > MAX_LINE
next_space = " \\\n.."
- unless next_space == " " and bit.type == "Block"
- nomsu\append next_space
+ elseif word_buffer[1] == "'"
+ next_space = ""
+ nomsu\append next_space, words
+ word_buffer = {}
+ next_space = " "
+
+ bit_nomsu = recurse(bit)
+ if bit.type == "Block" and not bit_nomsu\is_multiline!
+ -- Rule of thumb: one-liner block arguments should be shorter
+ -- than the proceeding part of the line
+ if #bit_nomsu\text! > nomsu\trailing_line_len!
+ bit_nomsu = tree_to_nomsu(bit)
+ --else
+ -- bit_nomsu\parenthesize!
- nomsu\append bit_nomsu
- next_space = bit_nomsu\is_multiline! and "\n.." or " "
+ if next_space == " " and not bit_nomsu\is_multiline! and nomsu\trailing_line_len! + #bit_nomsu\text! > MAX_LINE
+ if bit.type == 'Action'
+ bit_nomsu = NomsuCode\from bit.source, "(..)\n ", tree_to_nomsu(bit)
+ else
+ next_space = " \\\n.."
+ unless next_space == " " and bit.type == "Block"
+ nomsu\append next_space
+
+ nomsu\append bit_nomsu
+ next_space = (bit_nomsu\is_multiline! or bit.type == 'Block') and "\n.." or " "
+
+ if #word_buffer > 0
+ words = table.concat(word_buffer)
+ if next_space == " "
+ if nomsu\trailing_line_len! + #words > MAX_LINE
+ next_space = " \\\n.."
+ elseif word_buffer[1] == "'"
+ next_space = ""
+ nomsu\append next_space, words
return nomsu
@@ -241,12 +253,19 @@ tree_to_nomsu = (tree)->
return NomsuCode\from tree.source, "\\", nomsu
when "Block"
+ prev_line, needs_space = nil, {}
for i, line in ipairs tree
line_nomsu = tree_to_nomsu(line)
+ if i > 1
+ nomsu\append "\n"
+ -- Rule of thumb: add a blank line between two lines if both are
+ -- multi-line non-comments, or if a comment comes after a non-comment.
+ if tree[i-1].type != "Comment"
+ needs_space[i] = (line_nomsu\is_multiline! and prev_line\is_multiline!)
+ if tree[i].type == "Comment" or needs_space[i] or needs_space[i-1]
+ nomsu\append "\n"
nomsu\append line_nomsu
- if i < #tree
- -- number of lines > 2 (TODO: improve this)
- nomsu\append(line_nomsu\match('\n[^\n]*\n') and "\n\n" or "\n")
+ prev_line = line_nomsu
return NomsuCode\from(tree.source, ":\n ", nomsu)
when "Text"