aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2019-03-04 14:25:56 -0800
committerBruce Hill <bruce@bruce-hill.com>2019-03-04 14:26:03 -0800
commitde1f80fe51ea3a760966a304500de75325e1b70b (patch)
treeae7fae14a23372f6e9aa708225de79772419cc8f
parent026f7bf0e41c8d2c310a9795e8ed4e147927146e (diff)
Better codegen and error reporting
-rw-r--r--nomsu_decompiler.lua33
-rw-r--r--nomsu_decompiler.moon27
2 files changed, 47 insertions, 13 deletions
diff --git a/nomsu_decompiler.lua b/nomsu_decompiler.lua
index 3c703a5..41c109b 100644
--- a/nomsu_decompiler.lua
+++ b/nomsu_decompiler.lua
@@ -11,6 +11,7 @@ do
R, P, S = _obj_0.R, _obj_0.P, _obj_0.S
end
local re = require('re')
+local pretty_error = require("pretty_errors")
local MAX_LINE = 80
local GOLDEN_RATIO = ((math.sqrt(5) - 1) / 2)
local utf8_char_patt = (R("\194\223") * R("\128\191") + R("\224\239") * R("\128\191") * R("\128\191") + R("\240\244") * R("\128\191") * R("\128\191") * R("\128\191"))
@@ -259,7 +260,16 @@ tree_to_inline_nomsu = function(tree)
elseif "Comment" == _exp_0 then
return NomsuCode:from(tree.source)
elseif "Error" == _exp_0 then
- return error("Can't compile errors")
+ local err_msg = pretty_error({
+ title = "Parse error",
+ error = tree.error,
+ hint = tree.hint,
+ source = tree:get_source_file(),
+ start = tree.source.start,
+ stop = tree.source.stop,
+ filename = tree.source.filename
+ })
+ return error(err_msg)
else
return error("Unknown type: " .. tostring(tree.type))
end
@@ -327,12 +337,10 @@ tree_to_nomsu = function(tree)
local indented = tree_to_nomsu(t)
if t.type == "Action" or t.type == "MethodCall" then
if indented:is_multiline() then
- if not (indented:text():match("\n%S[^\n ]*$")) then
- if argnum == nil or argnum == 1 then
- return NomsuCode:from(t.source, "(\n ", indented, "\n)")
- else
- return NomsuCode:from(t.source, "\n ", indented)
- end
+ if argnum == nil or argnum == 1 then
+ return NomsuCode:from(t.source, "(\n ", indented, "\n)")
+ else
+ return NomsuCode:from(t.source, "\n ", indented)
end
elseif argnum and argnum > 1 then
return NomsuCode:from(t.source, "\n ", indented)
@@ -599,6 +607,17 @@ tree_to_nomsu = function(tree)
nomsu:add(tree.type == "List" and "[]" or "{}")
return nomsu
end
+ if #tree == 1 and tree[1].type == "Block" then
+ local block_lua = recurse(tree[1])
+ if block_lua:is_multiline() then
+ block_lua:add("\n")
+ end
+ if tree.type == "List" then
+ return NomsuCode:from(tree.source, "[", block_lua, "]")
+ else
+ return NomsuCode:from(tree.source, "{", block_lua, "}")
+ end
+ end
local sep = ''
local prev_item, needs_space = nil, { }
for i, item in ipairs(tree) do
diff --git a/nomsu_decompiler.moon b/nomsu_decompiler.moon
index ede9b91..3016a80 100644
--- a/nomsu_decompiler.moon
+++ b/nomsu_decompiler.moon
@@ -2,6 +2,7 @@
{:find, :sub, :match} = string
{:R,:P,:S} = require 'lpeg'
re = require 're'
+pretty_error = require("pretty_errors")
MAX_LINE = 80
GOLDEN_RATIO = ((math.sqrt(5)-1)/2)
@@ -201,7 +202,13 @@ tree_to_inline_nomsu = (tree)->
return NomsuCode\from(tree.source)
when "Error"
- error("Can't compile errors")
+ err_msg = pretty_error{
+ title:"Parse error"
+ error:tree.error, hint:tree.hint, source:tree\get_source_file!
+ start:tree.source.start, stop:tree.source.stop, filename:tree.source.filename
+ }
+ -- Coroutine yield here?
+ error(err_msg)
else
error("Unknown type: #{tree.type}")
@@ -242,11 +249,10 @@ tree_to_nomsu = (tree)->
indented = tree_to_nomsu(t)
if t.type == "Action" or t.type == "MethodCall"
if indented\is_multiline!
- unless indented\text!\match("\n%S[^\n ]*$")
- if argnum == nil or argnum == 1
- return NomsuCode\from(t.source, "(\n ", indented, "\n)")
- else
- return NomsuCode\from(t.source, "\n ", indented)
+ if argnum == nil or argnum == 1
+ return NomsuCode\from(t.source, "(\n ", indented, "\n)")
+ else
+ return NomsuCode\from(t.source, "\n ", indented)
elseif argnum and argnum > 1
return NomsuCode\from(t.source, "\n ", indented)
else
@@ -458,6 +464,15 @@ tree_to_nomsu = (tree)->
if #tree == 0
nomsu\add(tree.type == "List" and "[]" or "{}")
return nomsu
+
+ if #tree == 1 and tree[1].type == "Block"
+ block_lua = recurse(tree[1])
+ if block_lua\is_multiline! then block_lua\add "\n"
+ return if tree.type == "List" then
+ NomsuCode\from(tree.source, "[", block_lua, "]")
+ else
+ NomsuCode\from(tree.source, "{", block_lua, "}")
+
sep = ''
prev_item, needs_space = nil, {}
for i, item in ipairs tree