simplification
Download
535 lines 24.4 KiBMoonScriptModified 2026-11-09 raw
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
{:NomsuCode} = require "code_obj"
{:find, :sub, :match} = string
{:R,:P,:S} = require 'lpeg'
re = require 're'
pretty_error = require("pretty_errors")
unpack = unpack or table.unpack

MAX_LINE = 80
GOLDEN_RATIO = ((math.sqrt(5)-1)/2)

-- Parsing helper functions
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"))
operator_char = S("#'`~@^&*+=<>?/%!|\\-") + (P("\xE2") * (R("\x88\x8B")+R("\xA8\xAB")) * R("\128\191"))
operator_patt = operator_char^1 * -1
identifier_patt = (R("az","AZ","09") + P("_") + (-operator_char*utf8_char_patt))^1 * -1

is_operator = (s)->
    return type(s) == 'string' and not not operator_patt\match(s)

is_identifier = (s)->
    return type(s) == 'string' and not not identifier_patt\match(s)

can_be_unary = (t)->
    t.type == "Action" and #t == 2 and is_operator(t[1]) and type(t[2]) != 'string' and t[2].type != "Block" and not (t[2].type == "Number" and t[1] == "-")

-- Blocks with more than one statement can only be written on multiple lines
has_long_block = (tree)->
    for subtree in coroutine.wrap(-> (tree\with(coroutine.yield) and nil))
        return true if subtree.type == "Block" and #subtree > 1
    return false

-- A function called with no arguments, like Nomsu 7's ($fn()), is written (call $fn)
as_call = (tree)->
    if #tree == 1 and type(tree[1]) != 'string'
        return {type:"Action", source:tree.source, "call", tree[1]}
    return tree

inline_escaper = re.compile("{~ (%utf8_char / ('\"' -> '\\\"') / ('\n' -> '\\n') / ('\t' -> '\\t') / ('\b' -> '\\b') / ('\a' -> '\\a') / ('\v' -> '\\v') / ('\f' -> '\\f') / ('\r' -> '\\r') / ('\\' -> '\\\\') / ([^ -~] -> escape) / .)* ~}", {utf8_char: utf8_char_patt, escape:(=> ("\\%03d")\format(@byte!))})
inline_escape = (s)->
    return inline_escaper\match(s)

-- In multi-line text, a backslash only needs to be doubled when it could be read as
-- the start of an interpolation, and other characters are written as interpolated text
escaper = re.compile("{~ (%utf8_char / (('\\' &([$({\\] / %nl / !.)) -> '\\\\') / [\n\r\t -~] / (. -> escape))* ~}",
    {utf8_char: utf8_char_patt, escape:(=> ('\\("\\%03d")')\format(@byte!))})
escape = (s)->
    return escaper\match(s)

tree_to_inline_nomsu = (tree)->
    switch tree.type
        when "Action"
            tree = as_call(tree)
            nomsu = NomsuCode\from(tree.source)
            if can_be_unary(tree)
                nomsu\add tree[1]
                arg_nomsu = tree_to_inline_nomsu(tree[2])
                if tree[2].type == "MethodCall" or tree[2].type == "Action"
                    arg_nomsu\parenthesize!
                nomsu\add arg_nomsu
                return nomsu

            for i,bit in ipairs tree
                if type(bit) == "string"
                    clump_words = if type(tree[i-1]) == 'string'
                        is_operator(bit) != is_operator(tree[i-1])
                    else bit == "'"
                    nomsu\add " " if i > 1 and not clump_words
                    nomsu\add bit
                else
                    arg_nomsu = tree_to_inline_nomsu(bit)
                    if tree[i+1] == "'" and bit.type == "Action" and can_be_unary(bit)
                        arg_nomsu\parenthesize!
                    if bit.type == "Block"
                        if i != #tree
                            nomsu\add " " if i > 1
                            arg_nomsu\parenthesize!
                    else
                        nomsu\add " " if i > 1
                        if bit.type == "MethodCall"
                            arg_nomsu\parenthesize!
                        elseif bit.type == "Action" and not can_be_unary(bit)
                            arg_nomsu\parenthesize!
                    nomsu\add arg_nomsu
            return nomsu

        when "MethodCall"
            target_nomsu = tree_to_inline_nomsu(tree[1])
            if tree[1].type == "Block"
                target_nomsu\parenthesize!
            return NomsuCode\from(tree.source, target_nomsu, ", ", tree_to_inline_nomsu(tree[2]))

        when "Block"
            if #tree > 1
                error("A block with more than one statement can't be written on one line")
            nomsu = NomsuCode\from(tree.source, ":")
            if #tree == 1
                nomsu\add " ", tree_to_inline_nomsu(tree[1])
            return nomsu

        when "Text"
            add_text = (nomsu, tree)->
                for i, bit in ipairs tree
                    if type(bit) == 'string'
                        escaped = inline_escape(bit)
                        nomsu\add inline_escape(bit)
                    elseif bit.type == "Text"
                        add_text(nomsu, bit)
                    else
                        interp_nomsu = tree_to_inline_nomsu(bit)
                        if bit.type != "Var" and bit.type != "Table"
                            interp_nomsu\parenthesize!
                        elseif bit.type == "Var" and type(bit[1]) == 'string' and
                        type(tree[i+1]) == 'string' and not match(tree[i+1], "^[ \n\t,.:;#(){}[%]]")
                            interp_nomsu\parenthesize!
                        nomsu\add "\\", interp_nomsu
            nomsu = NomsuCode\from(tree.source)
            add_text(nomsu, tree)
            return NomsuCode\from(tree.source, '"', nomsu, '"')

        when "Table"
            nomsu = NomsuCode\from(tree.source, "{")
            for i, item in ipairs tree
                nomsu\add ", " if i > 1
                item_nomsu = tree_to_inline_nomsu(item, true)
                --if item.type == "Block" or item.type == "Action" or item.type == "MethodCall"
                --    item_nomsu\parenthesize!
                if item.type == "MethodCall" or (item.type == "Block" and i < #tree)
                    item_nomsu\parenthesize!
                nomsu\add item_nomsu
            nomsu\add "}"
            return nomsu
        
        when "TableEntry"
            key, value = tree[1], tree[2]
            nomsu = NomsuCode\from(tree.source)
            -- TODO: remove shim
            if key.type != "Index"
                key = {type:"Index", source:key.source, key}
            nomsu\add tree_to_inline_nomsu(key)
            if value
                nomsu\add " = "
                value_nomsu = tree_to_inline_nomsu(value)
                if value.type == "Block" or value.type == "MethodCall"
                    value_nomsu\parenthesize!
                nomsu\add value_nomsu
            return nomsu
        
        when "Index"
            key = tree[1]
            nomsu = NomsuCode\from(key.source, ".")
            key_nomsu = if key.type == "Text" and #key == 1 and is_identifier(key[1])
                key[1]
            else
                tree_to_inline_nomsu(key)
            switch key.type
                when "Block", "Action", "MethodCall", "IndexChain"
                    key_nomsu\parenthesize!
            return NomsuCode\from(key.source, ".", key_nomsu)
        
        when "IndexChain"
            nomsu = NomsuCode\from(tree.source)
            target = tree[1]
            target_nomsu = tree_to_inline_nomsu(target)
            switch target.type
                when "Action", "MethodCall"
                    target_nomsu\parenthesize!
                when "Number"
                    target_nomsu\parenthesize! if target_nomsu\text!\match("%.")
            nomsu\add target_nomsu
            for i=2,#tree
                -- TODO: remove shim?
                if tree[i].type != "Index"
                    tree[i] = {type:"Index", source:tree[i].source, tree[i]}
                nomsu\add tree_to_inline_nomsu(tree[i])
            return nomsu
        
        when "Number"
            -- Preserve original formatting, just make sure 0xdead_beef -> 0xDEAD_BEEF
            n = tostring(tree[1])
            s = if n\match("^-*0x")
                n\upper!\gsub("0X", "0x")
            elseif tree.hex and tonumber((n\gsub("_",""))) < 0
                ("-0x%X")\format(-tree[1])
            elseif tree.hex
                ("0x%X")\format(tree[1])
            else n
            return NomsuCode\from(tree.source, s)

        when "Var"
            varname = tree[1]
            if type(varname) == "string"
                return NomsuCode\from(tree.source, "$", varname)
            else
                return NomsuCode\from(tree.source, "$(", tree_to_inline_nomsu(varname), ")")

        when "File"
            error("Can't inline a whole file")
        
        when "Comment"
            -- TODO: implement?
            return NomsuCode\from(tree.source)
        
        when "Error"
            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}")

tree_to_nomsu = (tree, nomsu=NomsuCode\from(tree.source))->

    -- For concision:
    recurse = (t, argnum=nil)->
        space = MAX_LINE - nomsu\trailing_line_len!
        try_inline = true
        for subtree in coroutine.wrap(-> (t\with(coroutine.yield) and nil))
            switch subtree.type
                when "Comment"
                    try_inline = false
                when "Block"
                    if #subtree > 1
                        try_inline = false
                when "Text"
                    indented = tree_to_nomsu(subtree)
                    indented_lines = [line for line in *indented\text!\lines! when line\match("^    +([^ ].*)")]
                    for i=#indented_lines,1,-1
                        if indented_lines[i]\match('^ *\\%(" "%)$')
                            table.remove(indented_lines, i)
                    if #indented_lines > 1 or (#indented_lines == 1 and #indented_lines[1] > MAX_LINE + 8)
                        try_inline = false
        
        local inline_nomsu
        if try_inline
            inline_nomsu = tree_to_inline_nomsu(t)
            if t.type == "MethodCall"
                inline_nomsu\parenthesize!
            elseif t.type == "Action" and not can_be_unary(t)
                inline_nomsu\parenthesize!
            if #inline_nomsu\text! <= space or #inline_nomsu\text! <= 8
                if t.type != "Text"
                    return inline_nomsu
        indented = tree_to_nomsu(t)
        if t.type == "Action" or t.type == "MethodCall"
            if indented\is_multiline!
                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
                indented\parenthesize!
        indented_lines = [line for line in *indented\text!\lines! when line\match("^    +([^ ].*)")]
        if t.type == "Text"
            for i=#indented_lines,1,-1
                if indented_lines[i]\match('^ *\\%(" "%)$')
                    table.remove(indented_lines, i)
        if inline_nomsu and (#inline_nomsu\text! < MAX_LINE or #inline_nomsu\text! <= space) and
            #indented_lines <= 1
            return inline_nomsu
        return indented

    switch tree.type
        when "File"
            if tree.shebang
                nomsu\add tree.shebang, "\n"

            statements = {type:"Block", source:tree.source, unpack(tree)}
            if #statements > 0
                nomsu\add NomsuCode\from(tree.source, unpack(tree_to_nomsu(statements).bits, 2))

            return nomsu

        when "Action"
            tree = as_call(tree)
            if can_be_unary(tree) and not can_be_unary(tree[2])
                nomsu\add tree[1]
                nomsu\add recurse(tree[2])
                return nomsu

            next_space = ""
            word_buffer = {}
            for i,bit in ipairs tree
                -- TODO: properly wrap super long chains of words
                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 == " "
                        if nomsu\trailing_line_len! + #words > MAX_LINE and nomsu\trailing_line_len! > 8
                            next_space = "\n.."
                        elseif word_buffer[1] == "'"
                            next_space = ""
                    nomsu\add next_space, words
                    word_buffer = {}
                    next_space = " "

                bit_nomsu = recurse(bit, i)
                if tree[i+1] == "'" and bit.type == "Action" and not bit_nomsu\is_multiline! and can_be_unary(bit)
                    bit_nomsu\parenthesize!
                if bit.type == "Block"
                    -- Rule of thumb: nontrivial one-liner block arguments should be no more
                    -- than golden ratio * the length of the proceeding part of the line
                    if not bit_nomsu\is_multiline! and
                        (#bit_nomsu\text! > nomsu\trailing_line_len! * GOLDEN_RATIO and #bit_nomsu\text! > 8) or
                        #bit_nomsu\text! + nomsu\trailing_line_len! > MAX_LINE
                        bit_nomsu = tree_to_nomsu(bit)
                elseif (not bit_nomsu\is_multiline! and
                    nomsu\trailing_line_len! + #bit_nomsu\text! > MAX_LINE and
                    nomsu\trailing_line_len! > 8)
                    if next_space == " " and #bit_nomsu\text! < MAX_LINE
                        if i == #tree
                            bit_nomsu = tree_to_inline_nomsu(bit)
                            next_space = "\n    "
                        elseif bit.type == "Table"
                            bit_nomsu = tree_to_nomsu(bit)
                        else
                            next_space = "\n.."
                    elseif bit.type == 'Action' or bit.type == "MethodCall"
                        bit_nomsu = NomsuCode\from bit.source, "\n    ", tree_to_nomsu(bit)
                    else
                        bit_nomsu = tree_to_nomsu(bit)

                unless next_space == " " and bit_nomsu\text!\match("^[:\n]")
                    nomsu\add next_space

                nomsu\add bit_nomsu
                next_space = (bit.type == "Block" or bit_nomsu\text!\matches("\n    [^\n]*$")) and "\n.." or " "

            if #word_buffer > 0
                words = table.concat(word_buffer)
                if next_space == " "
                    if nomsu\trailing_line_len! + #words > MAX_LINE + 8 and nomsu\trailing_line_len! > 8
                        next_space = "\n.."
                    elseif word_buffer[1] == "'"
                        next_space = ""
                nomsu\add next_space, words
                next_space = " "
            return nomsu

        when "MethodCall"
            target_nomsu = recurse(tree[1])
            if tree[1].type == "Block" and not target_nomsu\is_multiline!
                target_nomsu\parenthesize!
            nomsu\add target_nomsu, ", "
            -- The method's action goes on the same line, so it wraps with the target in mind
            return tree_to_nomsu(tree[2], nomsu)

        when "Block"
            prev_line, needs_space = nil, {}
            for i, line in ipairs tree
                line_nomsu = tree_to_nomsu(line)
                if i > 1
                    nomsu\add "\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,
                    -- or if the last line starts with ".."
                    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] or
                            prev_line\text!\match("\n        [^\n]*$"))
                            nomsu\add "\n"
                nomsu\add line_nomsu
                prev_line = line_nomsu
            return NomsuCode\from(tree.source, ":\n    ", nomsu)

        when "Text"
            -- Multi-line text has more generous wrap margins
            max_line = MAX_LINE + 8
            -- Flatten nested text, and merge strings that end up next to each other
            pieces = {}
            flatten = (t)->
                for bit in *t
                    if type(bit) == 'string' and type(pieces[#pieces]) == 'string'
                        pieces[#pieces] ..= bit
                    elseif type(bit) != 'string' and bit.type == "Text"
                        flatten(bit)
                    else
                        pieces[#pieces+1] = bit
            flatten(tree)
            -- "\0" marks a line that continues the one before it, which is written with
            -- "...." in place of the text's indentation
            CONTINUE = "\n\0"
            pending_spaces = ""
            add_text = ->
                for i, bit in ipairs pieces
                    if type(bit) == 'string'
                        lines = bit\lines!
                        for j, line in ipairs lines
                            if j > 1
                                nomsu\add "\n"
                            elseif #line > 10 and nomsu\trailing_line_len! > max_line
                                nomsu\add CONTINUE

                            -- Spaces at the end of a line are interpolated, so they're visible
                            -- and don't get lost when trailing whitespace is trimmed
                            ends_line = j < #lines or i == #pieces
                            while #line > 0
                                space = max_line - nomsu\trailing_line_len!
                                split = find(line, "[%p%s]", space)
                                if not split or split > space + 16
                                    split = space + 16
                                if #line - split < 16
                                    split = #line
                                bite, line = sub(line, 1, split), sub(line, split+1, -1)
                                -- Spaces where a line is split go after the "....", where
                                -- they're visible
                                if #line > 0 and bite\match("[^ ]")
                                    spaces = bite\match(" *$")
                                    bite, line = sub(bite, 1, #bite - #spaces), spaces..line
                                bite = escape(bite)
                                if #line == 0 and ends_line
                                    bite = bite\gsub(" $", '\\(" ")')
                                elseif #line == 0
                                    -- Spaces before an interpolation wait for it, in case it
                                    -- starts a new line
                                    pending_spaces = bite\match(" *$")
                                    bite = sub(bite, 1, #bite - #pending_spaces)
                                nomsu\add bite
                                nomsu\add CONTINUE if #line > 0
                    else
                        interp_nomsu = recurse(bit)
                        prefix = ""
                        if interp_nomsu\is_multiline!
                            curr_indent = nomsu\text!\match("\n( *)[^\n]*$") or nomsu\text!\match("^( *)")
                            interp_nomsu = NomsuCode((interp_nomsu\text!\gsub("\n", "\n"..curr_indent)))
                        else
                            space = max_line - nomsu\trailing_line_len!
                            if bit.type == "Var"
                                next_str = pieces[i+1]
                                if type(next_str) == 'string' and not match(next_str, "^[ \n\t,.:;#(){}[%]]")
                                    interp_nomsu\parenthesize!
                            elseif #interp_nomsu\text! > space
                                interp_nomsu2 = if bit.type == "Action" or bit.type == "MethodCall"
                                    NomsuCode\from(bit.source, "(\n    ", tree_to_nomsu(bit), "\n)")
                                else
                                    tree_to_nomsu(bit)

                                if #interp_nomsu2\text!\lines! > 3 or #interp_nomsu2\text! >= MAX_LINE*GOLDEN_RATIO
                                    curr_indent = nomsu\text!\match("\n( *)[^\n]*$") or nomsu\text!\match("^( *)")
                                    interp_nomsu2 = NomsuCode((interp_nomsu2\text!\gsub("\n", "\n"..curr_indent)))
                                    interp_nomsu = interp_nomsu2
                                else
                                    prefix = CONTINUE
                                    if bit.type == "Block" or bit.type == "IndexChain"
                                        interp_nomsu\parenthesize!
                            elseif bit.type == "Block" or bit.type == "IndexChain"
                                interp_nomsu\parenthesize!
                            elseif bit.type == "Action" and can_be_unary(bit)
                                interp_nomsu\parenthesize!
                        nomsu\add prefix, pending_spaces, "\\", interp_nomsu
                        pending_spaces = ""
            add_text!
            text_lines = for line in (nomsu\text!.."\n")\gmatch("([^\n]*)\n")
                if sub(line, 1, 1) == "\0"
                    "...."..sub(line, 2)
                elseif #line == 0
                    line
                else
                    "    "..line
            return NomsuCode\from(tree.source, '("\n', table.concat(text_lines, "\n"), '\n")')

        when "Table"
            if #tree == 0
                nomsu\add "{}"
                return nomsu

            sep = ''
            prev_item, needs_space = nil, {}
            for i, item in ipairs tree
                local item_nomsu
                if item.type == 'MethodCall'
                    item_nomsu = recurse(item)
                elseif item.type == 'Comment'
                    item_nomsu = tree_to_nomsu(item)
                    sep = '\n' if i > 1
                else
                    item_nomsu = not has_long_block(item) and tree_to_inline_nomsu(item)
                    if not item_nomsu or nomsu\trailing_line_len! + #item_nomsu\text! > MAX_LINE
                        sep = '\n' if i > 1
                        item_nomsu = item.type == "Action" and tree_to_nomsu(item) or recurse(item)
                nomsu\add sep
                if sep == '\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,
                    -- or if the last line starts with ".."
                    if i > 1 and tree[i-1].type != "Comment"
                        needs_space[i] = (item_nomsu\is_multiline! and prev_item\is_multiline!)
                        if (tree[i].type == "Comment" or needs_space[i] or needs_space[i-1] or
                            prev_item\text!\match("\n        [^\n]*$"))
                            nomsu\add "\n"
                nomsu\add item_nomsu
                prev_item = item_nomsu
                if item_nomsu\is_multiline! or item.type == 'Comment' or item.type == "Block" or
                    nomsu\trailing_line_len! + #tostring(item_nomsu) >= MAX_LINE
                    sep = '\n'
                else
                    sep = ', '
            return NomsuCode\from(tree.source, "{\n    ", nomsu, "\n}")

        when "TableEntry"
            key, value = tree[1], tree[2]
            nomsu = NomsuCode\from(tree.source)
            -- TODO: remove shim
            if key.type != "Index"
                key = {type:"Index", source:key.source, key}
            nomsu\add tree_to_nomsu(key)
            if value
                value_nomsu = recurse(value)
                nomsu\add " = ", value_nomsu
            return nomsu

        when "Comment"
            nomsu\add "###", (tree[1]\gsub("\n([^\n])", "\n    %1"))
            return nomsu
        
        when "IndexChain", "Index", "Number", "Var", "Comment", "Error"
            return tree_to_inline_nomsu tree
        
        else
            error("Unknown type: #{tree.type}")

return {:tree_to_nomsu, :tree_to_inline_nomsu}