diff options
| author | Bruce Hill <bitbucket@bruce-hill.com> | 2018-04-24 20:16:46 -0700 |
|---|---|---|
| committer | Bruce Hill <bitbucket@bruce-hill.com> | 2018-04-24 20:17:08 -0700 |
| commit | bff93d386c1d54504f0b8c930f246966d307a9e5 (patch) | |
| tree | 4e5a7cab137ab92c8790e8fc6878b55bf90dfdce /nomsu_tree.moon | |
| parent | 34a2f50ebc8b1d564b0061b5093b2736e365b29a (diff) | |
Refactor to improve indentation and move as_nomsu() into tree methods.
Diffstat (limited to 'nomsu_tree.moon')
| -rw-r--r-- | nomsu_tree.moon | 212 |
1 files changed, 210 insertions, 2 deletions
diff --git a/nomsu_tree.moon b/nomsu_tree.moon index 421e02a..fcef992 100644 --- a/nomsu_tree.moon +++ b/nomsu_tree.moon @@ -2,6 +2,7 @@ -- as well as the logic for converting them to Lua code. utils = require 'utils' re = require 're' +lpeg = require 'lpeg' {:repr, :stringify, :min, :max, :equivalent, :set, :is_list, :sum} = utils immutable = require 'immutable' {:insert, :remove, :concat} = table @@ -42,17 +43,33 @@ Tree "File", line_lua = line\as_lua(nomsu) if not line_lua error("No lua produced by #{repr line}", 0) - if i < #@value + if i > 1 lua\append "\n" lua\convert_to_statements! lua\append line_lua lua\declare_locals! return lua + as_nomsu: (inline=false)=> + return nil if inline + nomsu = Nomsu(@source) + for i, line in ipairs @value + nomsu\append assert(line\as_nomsu!, "Could not convert line to nomsu: #{line}") + if i < #@value + nomsu\append "\n" + return nomsu + Tree "Nomsu", as_lua: (nomsu)=> Lua.Value(@source, "nomsu:parse(Nomsu(",repr(@value.source),", ",repr(tostring(@value.source\get_text!)),")).value[1]") + as_nomsu: (inline=false)=> + nomsu = @value\as_nomsu(true) + if nomsu == nil and not inline + nomsu = @value\as_nomsu! + return nomsu and Nomsu(@source, "\\:\n ", nomsu) + return nomsu and Nomsu(@source, "\\(", nomsu, ")") + Tree "Block", as_lua: (nomsu)=> if #@value == 1 @@ -60,12 +77,24 @@ Tree "Block", lua = Lua(@source) for i,line in ipairs @value line_lua = line\as_lua(nomsu) - if i < #@value + if i > 1 lua\append "\n" line_lua\convert_to_statements! lua\append line_lua return lua + as_nomsu: (inline=false)=> + if inline + if #@value == 1 + return @value[1]\as_nomsu(true) + else return nil + nomsu = Nomsu(@source) + for i, line in ipairs @value + nomsu\append assert(line\as_nomsu!, "Could not convert line to nomsu: #{line}") + if i < #@value + nomsu\append "\n" + return nomsu + math_expression = re.compile [[ "%" (" " [*/^+-] " %")+ ]] Tree "Action", as_lua: (nomsu)=> @@ -127,6 +156,51 @@ Tree "Action", [(t.type == "Word" and t.value or "%#{t.value}") for t in *@value] else [(t.type == "Word" and t.value or "%") for t in *@value] return concat(bits, " ") + + as_nomsu: (inline=false)=> + if inline + nomsu = Nomsu(@source) + for i,bit in ipairs @value + if bit.type == "Word" + if i > 1 + nomsu\append " " + nomsu\append bit.value + else + arg_nomsu = bit\as_nomsu(true) + return nil unless arg_nomsu + unless i == 1 + nomsu\append " " + if bit.type == "Action" + arg_nomsu\parenthesize! + nomsu\append arg_nomsu + return nomsu + else + inline_version = @as_nomsu(true) + if inline_version and #inline_version <= 80 + return inline_version + nomsu = Nomsu(@source) + spacer = nil + for i,bit in ipairs @value + if spacer + nomsu\append spacer + + if bit.type == "Word" + nomsu\append bit.value + spacer = " " + else + arg_nomsu = bit\as_nomsu(true) + if arg_nomsu and #arg_nomsu < 80 + if bit.type == "Action" + arg_nomsu\parenthesize! + spacer = " " + else + arg_nomsu = bit\as_nomsu! + return nil unless nomsu + if bit.type == "Action" + nomsu\append "\n ", nomsu + spacer = "\n.." + nomsu\append arg_nomsu + return nomsu Tree "Text", @@ -158,6 +232,42 @@ Tree "Text", lua\parenthesize! return lua + as_nomsu: (inline=false)=> + if inline + nomsu = Nomsu(@source, '"') + for bit in *@value + if type(bit) == 'string' + -- Force indented text + return nil if bit\find("\n") + -- TODO: unescape better + nomsu\append (bit\gsub("\\","\\\\")\gsub("\n","\\n")) + else + interp_nomsu = bit\as_nomsu(true) + if interp_nomsu + if bit.type != "Word" and bit.type != "List" and bit.type != "Dict" and bit.type != "Text" + interp_nomsu\parenthesize! + nomsu\append "\\", interp_nomsu + else return nil + nomsu\append '"' + if #nomsu > 80 then return nil + else + nomsu = Nomsu(@source, '".."\n ') + for i, bit in ipairs @value + if type(bit) == 'string' + nomsu\append (bit\gsub("\\","\\\\")\gsub("\n","\n ")) + else + if interp_nomsu + if bit.type != "Word" and bit.type != "List" and bit.type != "Dict" and bit.type != "Text" + interp_nomsu\parenthesize! + nomsu\append "\\", interp_nomsu + else + interp_nomsu = bit\as_nomsu! + return nil unless interp_nomsu + nomsu\append "\\\n ", interp_nomsu + if i < #@value + nomsu\append "\n.." + return nomsu + Tree "List", as_lua: (nomsu)=> lua = Lua.Value @source, "{" @@ -183,6 +293,38 @@ Tree "List", lua\append "}" return lua + as_nomsu: (inline=false)=> + if inline + nomsu = Nomsu(@source, "[") + for i, item in ipairs @value + item_nomsu = item\as_nomsu(true) + return nil unless item_nomsu + if i > 1 + nomsu\append ", " + nomsu\append item_nomsu + nomsu\append "]" + return nomsu + else + nomsu = Nomsu(@source, "[..]") + line = Nomsu(@source, "\n ") + for item in *@value + item_nomsu = item\as_nomsu(true) + if item_nomsu and #line + #", " + #item_nomsu <= 80 + if #line.bits > 1 + line\append ", " + line\append item_nomsu + else + unless item_nomsu + item_nomsu = item\as_nomsu! + return nil unless item_nomsu + if #line.bits > 1 + nomsu\append line + line = Nomsu(bit.source, "\n ") + line\append item_nomsu + if #line.bits > 1 + nomsu\append line + return nomsu + Tree "Dict", as_lua: (nomsu)=> lua = Lua.Value @source, "{" @@ -223,6 +365,46 @@ Tree "Dict", lua\append "}" return lua + as_nomsu: (inline=false)=> + if inline + nomsu = Nomsu(@source, "{") + for i, entry in ipairs @value + key_nomsu = entry.key\as_nomsu(true) + return nil unless key_nomsu + if entry.key.type == "Action" + key_nomsu\parenthesize! + value_nomsu = entry.value\as_nomsu(true) + return nil unless value_nomsu + if i > 1 + nomsu\append ", " + nomsu\append key_nomsu,":",value_nomsu + nomsu\append "}" + return nomsu + else + nomsu = Nomsu(@source, "{..}") + line = Nomsu(@source, "\n ") + for entry in *@value + key_nomsu = entry.key\as_nomsu(true) + return nil unless key_nomsu + if entry.key.type == "Action" + key_nomsu\parenthesize! + value_nomsu = entry.value\as_nomsu(true) + if value_nomsu and #line + #", " + #key_nomsu + #":" + #value_nomsu <= 80 + if #line.bits > 1 + line\append ", " + line\append key_nomsu,":",value_nomsu + else + unless value_nomsu + value_nomsu = entry.value\as_nomsu! + return nil unless value_nomsu + if #line.bits > 1 + nomsu\append line + line = Nomsu(bit.source, "\n ") + line\append key_nomsu,":",value_nomsu + if #line.bits > 1 + nomsu\append line + return nomsu + Tree "IndexChain", as_lua: (nomsu)=> lua = @value[1]\as_lua(nomsu) @@ -251,9 +433,22 @@ Tree "IndexChain", lua\append "[",key_lua,"]" return lua + as_nomsu: (inline=false)=> + nomsu = Nomsu(@source) + for i, bit in ipairs @value + if i > 1 + nomsu\append "." + bit_nomsu = bit\as_nomsu(true) + return nil unless bit_nomsu + nomsu\append bit_nomsu + return nomsu + Tree "Number", as_lua: (nomsu)=> Lua.Value(@source, tostring(@value)) + + as_nomsu: (inline=false)=> + return Nomsu(@source, tostring(@value)) Tree "Var", as_lua: (nomsu)=> @@ -261,12 +456,25 @@ Tree "Var", if verboten == "_" then "__" else ("_%x")\format(verboten\byte!)) Lua.Value(@source, lua_id) + as_nomsu: (inline=false)=> + return Nomsu(@source, "%", @value) + Tree "Word", as_lua: (nomsu)=> error("Attempt to convert Word to lua") + as_nomsu: (inline=false)=> + return Nomsu(@source, @value) + Tree "Comment", as_lua: (nomsu)=> Lua(@source, "--"..@value\gsub("\n","\n--").."\n") + as_nomsu: (inline=false)=> + return nil if inline + if @value\match("\n") + return Nomsu(@source, "#..", @value\gsub("\n", "\n ")) + else + return Nomsu(@source, "#", @value) + return Types |
