diff --git a/nomsu.lua b/nomsu.lua index aecb6fe..ee7118e 100644 --- a/nomsu.lua +++ b/nomsu.lua @@ -467,7 +467,7 @@ end);]]):format(statements or "", expr or "ret") local ret; %s return ret; -end);]]):format(concat(buffer, "\n")) +end);]]):format(concat(buffer, "")) return return_value, lua_code, vars end, tree_to_value = function(self, tree, vars) @@ -481,6 +481,154 @@ end);]]):format(concat(buffer, "\n")) end return (lua_thunk())(self, vars or { }) end, + tree_to_nomsu = function(self, tree, force_inline) + if force_inline == nil then + force_inline = false + end + local indent + indent = function(s) + return s:gsub("\n", "\n ") + end + assert(tree, "No tree provided.") + if not tree.type then + self:errorln(debug.traceback()) + self:error("Invalid tree: " .. tostring(repr(tree))) + end + local _exp_0 = tree.type + if "File" == _exp_0 then + return concat((function() + local _accum_0 = { } + local _len_0 = 1 + local _list_0 = tree.value + for _index_0 = 1, #_list_0 do + local v = _list_0[_index_0] + _accum_0[_len_0] = self:tree_to_nomsu(v, force_inline) + _len_0 = _len_0 + 1 + end + return _accum_0 + end)(), "\n"), false + elseif "Nomsu" == _exp_0 then + local inside, inline = self:tree_to_nomsu(tree.value, force_inline) + return "\\" .. tostring(inside), inline + elseif "Thunk" == _exp_0 then + if force_inline then + return "{" .. tostring(concat((function() + local _accum_0 = { } + local _len_0 = 1 + local _list_0 = tree.value + for _index_0 = 1, #_list_0 do + local v = _list_0[_index_0] + _accum_0[_len_0] = self:tree_to_nomsu(v, true) + _len_0 = _len_0 + 1 + end + return _accum_0 + end)(), "; ")), true + else + return ":" .. indent("\n" .. concat((function() + local _accum_0 = { } + local _len_0 = 1 + local _list_0 = tree.value + for _index_0 = 1, #_list_0 do + local v = _list_0[_index_0] + _accum_0[_len_0] = self:tree_to_nomsu(v) + _len_0 = _len_0 + 1 + end + return _accum_0 + end)(), "\n")), false + end + elseif "FunctionCall" == _exp_0 then + local buff = "" + local sep = "" + local inline = true + local do_arg + do_arg = function(arg) end + local _list_0 = tree.value + for _index_0 = 1, #_list_0 do + local arg = _list_0[_index_0] + local arg_inline + nomsu, arg_inline = self:tree_to_nomsu(arg, force_inline) + buff = buff .. sep + if arg_inline then + sep = " " + else + inline = false + sep = "\n.." + end + if arg.type == 'FunctionCall' then + if arg_inline then + buff = buff .. "(" .. tostring(nomsu) .. ")" + else + buff = buff .. "(..)\n " .. tostring(indent(nomsu)) + end + else + buff = buff .. nomsu + end + end + return buff, inline + elseif "String" == _exp_0 then + local buff = "\"" + local longbuff = "\"..\"\n |" + local inline = true + local _list_0 = tree.value + for _index_0 = 1, #_list_0 do + local bit = _list_0[_index_0] + if type(bit) == "string" then + bit = bit:gsub("\\", "\\\\") + buff = buff .. bit:gsub("\n", "\\n"):gsub("\"", "\\\"") + longbuff = longbuff .. bit:gsub("\n", "\n |") + else + local inside, bit_inline = self:tree_to_nomsu(bit, force_inline) + inline = inline and bit_inline + buff = buff .. "\\(" .. tostring(inside) .. ")" + longbuff = longbuff .. "\\(" .. tostring(inside) .. ")" + end + end + buff = buff .. "\"" + if force_inline or (inline and #buff <= 90) then + return buff, true + else + return longbuff, false + end + elseif "List" == _exp_0 then + local buff = "[" + local longbuff = "[..]\n " + local longsep = "" + local longline = 0 + local inline = true + for i, bit in ipairs(tree.value) do + local bit_inline + nomsu, bit_inline = self:tree_to_nomsu(bit, force_inline) + inline = inline and bit_inline + if inline then + if i > 1 then + buff = buff .. ", " + end + buff = buff .. nomsu + end + longbuff = longbuff .. (longsep .. nomsu) + longline = longline + #nomsu + if bit_inline and longline <= 90 then + longsep = ", " + else + longsep = "\n " + end + end + buff = buff .. "]" + if force_inline or (inline and #buff <= 90) then + return buff, true + else + return longbuff, false + end + elseif "Number" == _exp_0 then + return repr(tree.value), true + elseif "Var" == _exp_0 then + return "%" .. tostring(tree.value), true + elseif "Word" == _exp_0 then + return tree.value, true + else + return self:error("Unknown/unimplemented thingy: " .. tostring(tree.type)) + end + end, tree_to_lua = function(self, tree) assert(tree, "No tree provided.") if not tree.type then @@ -491,7 +639,7 @@ end);]]):format(concat(buffer, "\n")) if "File" == _exp_0 then return error("Should not be converting File to lua through this function.") elseif "Nomsu" == _exp_0 then - return repr(tree.value), nil + return "nomsu:parse(" .. tostring(repr(tree.value.src)) .. ").value[1]", nil elseif "Thunk" == _exp_0 then local lua_bits = { } local _list_0 = tree.value diff --git a/nomsu.moon b/nomsu.moon index 9ea8353..eaee20e 100755 --- a/nomsu.moon +++ b/nomsu.moon @@ -360,7 +360,7 @@ return (function(nomsu, vars) local ret; %s return ret; -end);]])\format(concat(buffer, "\n")) +end);]])\format(concat(buffer, "")) return return_value, lua_code, vars tree_to_value: (tree, vars)=> @@ -372,6 +372,107 @@ end);]])\format(concat(buffer, "\n")) @error("Failed to compile generated code:\n#{colored.bright colored.blue colored.onblack code}\n\n#{colored.red err}") return (lua_thunk!)(self, vars or {}) + tree_to_nomsu: (tree, force_inline=false)=> + -- Return , + indent = (s)-> + s\gsub("\n","\n ") + assert tree, "No tree provided." + if not tree.type + @errorln debug.traceback() + @error "Invalid tree: #{repr(tree)}" + switch tree.type + when "File" + return concat([@tree_to_nomsu(v, force_inline) for v in *tree.value], "\n"), false + + when "Nomsu" + inside, inline = @tree_to_nomsu(tree.value, force_inline) + return "\\#{inside}", inline + + when "Thunk" + if force_inline + return "{#{concat([@tree_to_nomsu(v, true) for v in *tree.value], "; ")}", true + else + return ":"..indent("\n"..concat([@tree_to_nomsu v for v in *tree.value], "\n")), false + + when "FunctionCall" + buff = "" + sep = "" + inline = true + do_arg = (arg)-> + + for arg in *tree.value + nomsu, arg_inline = @tree_to_nomsu(arg, force_inline) + buff ..= sep + if arg_inline + sep = " " + else + inline = false + sep = "\n.." + if arg.type == 'FunctionCall' + if arg_inline + buff ..= "(#{nomsu})" + else + buff ..= "(..)\n #{indent nomsu}" + else + buff ..= nomsu + return buff, inline + + when "String" + buff = "\"" + longbuff = "\"..\"\n |" + inline = true + for bit in *tree.value + if type(bit) == "string" + bit = bit\gsub("\\","\\\\") + buff ..= bit\gsub("\n","\\n")\gsub("\"","\\\"") + longbuff ..= bit\gsub("\n","\n |") + else + inside, bit_inline = @tree_to_nomsu(bit, force_inline) + inline and= bit_inline + buff ..= "\\(#{inside})" + longbuff ..= "\\(#{inside})" + buff ..= "\"" + if force_inline or (inline and #buff <= 90) + return buff, true + else + return longbuff, false + + when "List" + buff = "[" + longbuff = "[..]\n " + longsep = "" + longline = 0 + inline = true + for i,bit in ipairs tree.value + nomsu, bit_inline = @tree_to_nomsu(bit, force_inline) + inline and= bit_inline + if inline + if i > 1 + buff ..= ", " + buff ..= nomsu + longbuff ..= longsep .. nomsu + longline += #nomsu + longsep = if bit_inline and longline <= 90 + ", " + else "\n " + buff ..= "]" + if force_inline or (inline and #buff <= 90) + return buff, true + else + return longbuff, false + + when "Number" + return repr(tree.value), true + + when "Var" + return "%#{tree.value}", true + + when "Word" + return tree.value, true + + else + @error("Unknown/unimplemented thingy: #{tree.type}") + tree_to_lua: (tree)=> -- Return , assert tree, "No tree provided." @@ -383,7 +484,7 @@ end);]])\format(concat(buffer, "\n")) error("Should not be converting File to lua through this function.") when "Nomsu" - return repr(tree.value), nil + return "nomsu:parse(#{repr tree.value.src}).value[1]", nil when "Thunk" lua_bits = {}