diff --git a/bin/moonc b/bin/moonc index f18d43f..9410d2e 100755 --- a/bin/moonc +++ b/bin/moonc @@ -9,7 +9,7 @@ require "moonscript.util" require "alt_getopt" require "lfs" -local opts, ind = alt_getopt.get_opts(arg, "vhwt:pTb", { +local opts, ind = alt_getopt.get_opts(arg, "vhwt:pTXb", { print = "p", tree = "T", version = "v", help = "h" }) @@ -24,6 +24,7 @@ local help = [[Usage: %s [options] files... -t path Specify where to place compiled files -p Write output to standard out -T Write parse tree instead of code (to stdout) + -X Write line rewrite map instead of code (to stdout) -b Dump parse and compile time (doesn't write output) -v Print version @@ -122,10 +123,18 @@ function compile_file(text, fname) return "" else local compile_time = gettime() - local code, err, pos = compile.tree(tree) + local code, posmap_or_err, err_pos = compile.tree(tree) compile_time = gettime() - compile_time + if not code then - return nil, compile.format_error(err, pos, text) + return nil, compile.format_error(posmap_or_err, err_pos, text) + end + + if opts.X then + opts.p = true + print("Pos", "Lua", ">>", "Moon") + print(util.debug_posmap(posmap_or_err, text, code)) + return "" end if opts.b then diff --git a/moonscript/compile.lua b/moonscript/compile.lua index 1d47f57..075b473 100644 --- a/moonscript/compile.lua +++ b/moonscript/compile.lua @@ -594,47 +594,6 @@ Block = (function() end return _class_0 end)() -local debug_posmap -debug_posmap = function(posmap, fname, lua_code) - if fname == nil then - fname = error("pass in input file") - end - local moon_code = io.open(fname):read("*a") - local tuples = (function() - local _accum_0 = { } - local _len_0 = 0 - for k, v in pairs(posmap) do - _len_0 = _len_0 + 1 - _accum_0[_len_0] = { - k, - v - } - end - return _accum_0 - end)() - table.sort(tuples, function(a, b) - return a[1] < b[1] - end) - local lines = (function() - local _accum_0 = { } - local _len_0 = 0 - local _list_0 = tuples - for _index_0 = 1, #_list_0 do - local pair = _list_0[_index_0] - local lua_line, pos = unpack(pair) - local moon_line = pos_to_line(moon_code, pos) - local lua_text = get_line(lua_code, lua_line) - local moon_text = get_closest_line(moon_code, moon_line) - local _value_0 = tostring(pos) .. "\t " .. tostring(lua_line) .. ":[ " .. tostring(trim(lua_text)) .. " ] >> " .. tostring(moon_line) .. ":[ " .. tostring(trim(moon_text)) .. " ]" - if _value_0 ~= nil then - _len_0 = _len_0 + 1 - _accum_0[_len_0] = _value_0 - end - end - return _accum_0 - end)() - return concat(lines, "\n") .. "\n" -end RootBlock = (function() local _parent_0 = Block local _base_0 = { diff --git a/moonscript/compile.moon b/moonscript/compile.moon index 2c86a3f..6538c97 100644 --- a/moonscript/compile.moon +++ b/moonscript/compile.moon @@ -351,24 +351,6 @@ class Block @_lines = Lines! @stms fn lines -debug_posmap = (posmap, fname=error"pass in input file", lua_code) -> - moon_code = io.open(fname)\read "*a" - - tuples = [{k, v} for k, v in pairs posmap] - - table.sort tuples, (a, b) -> a[1] < b[1] - - lines = for pair in *tuples - lua_line, pos = unpack pair - moon_line = pos_to_line moon_code, pos - - lua_text = get_line lua_code, lua_line - moon_text = get_closest_line moon_code, moon_line - - "#{pos}\t #{lua_line}:[ #{trim lua_text} ] >> #{moon_line}:[ #{trim moon_text} ]" - - concat(lines, "\n") .. "\n" - class RootBlock extends Block new: (...) => @root = self @@ -419,6 +401,5 @@ tree = (tree, scope=RootBlock!) -> else lua_code = scope\render! posmap = scope._lines\flatten_posmap! - -- print debug_posmap posmap, "scrap.moon", lua_code lua_code, posmap diff --git a/moonscript/util.lua b/moonscript/util.lua index d131a12..e9845e9 100644 --- a/moonscript/util.lua +++ b/moonscript/util.lua @@ -99,3 +99,39 @@ dump = function(what) end return _dump(what) end +debug_posmap = function(posmap, moon_code, lua_code) + local tuples = (function() + local _accum_0 = { } + local _len_0 = 0 + for k, v in pairs(posmap) do + _len_0 = _len_0 + 1 + _accum_0[_len_0] = { + k, + v + } + end + return _accum_0 + end)() + table.sort(tuples, function(a, b) + return a[1] < b[1] + end) + local lines = (function() + local _accum_0 = { } + local _len_0 = 0 + local _list_0 = tuples + for _index_0 = 1, #_list_0 do + local pair = _list_0[_index_0] + local lua_line, pos = unpack(pair) + local moon_line = pos_to_line(moon_code, pos) + local lua_text = get_line(lua_code, lua_line) + local moon_text = get_closest_line(moon_code, moon_line) + local _value_0 = tostring(pos) .. "\t " .. tostring(lua_line) .. ":[ " .. tostring(trim(lua_text)) .. " ] >> " .. tostring(moon_line) .. ":[ " .. tostring(trim(moon_text)) .. " ]" + if _value_0 ~= nil then + _len_0 = _len_0 + 1 + _accum_0[_len_0] = _value_0 + end + end + return _accum_0 + end)() + return concat(lines, "\n") +end diff --git a/moonscript/util.moon b/moonscript/util.moon index 3a97ffe..7bbb8c1 100644 --- a/moonscript/util.moon +++ b/moonscript/util.moon @@ -4,7 +4,7 @@ module "moonscript.util", package.seeall export moon export pos_to_line, get_closest_line, get_line export reversed, trim, split -export dump +export dump, debug_posmap import concat from table @@ -75,3 +75,19 @@ dump = (what) -> _dump what +debug_posmap = (posmap, moon_code, lua_code) -> + tuples = [{k, v} for k, v in pairs posmap] + table.sort tuples, (a, b) -> a[1] < b[1] + + lines = for pair in *tuples + lua_line, pos = unpack pair + moon_line = pos_to_line moon_code, pos + + lua_text = get_line lua_code, lua_line + moon_text = get_closest_line moon_code, moon_line + + "#{pos}\t #{lua_line}:[ #{trim lua_text} ] >> #{moon_line}:[ #{trim moon_text} ]" + + concat(lines, "\n") + +