simplification
Download
171 lines 5.9 KiBMoonScriptModified 2026-11-08 raw
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
-- This file contains the datastructures used to represent parsed Nomsu syntax trees,
-- as well as the logic for converting them to Lua code.
{:insert, :remove, :concat} = table
{:Source} = require "code_obj"
Files = require 'files'
unpack or= table.unpack

local SyntaxTree
class SyntaxTree
    __tostring: =>
        bits = [type(b) == 'string' and b\as_lua! or tostring(b) for b in *@]
        for k,v in pairs(@)
            unless bits[k] or k == 'type' or k == 'source'
                table.insert(bits, "#{k}=#{type(v) == 'string' and v\as_lua! or v}")
        return "#{@type}{#{table.concat(bits, ", ")}}"

    __eq: (other)=>
        return false if type(@) != type(other) or #@ != #other or getmetatable(@) != getmetatable(other)
        return false if @type != other.type
        for i=1,#@
            return false if @[i] != other[i]
        return true

    @source_code_for_tree: setmetatable({}, {
        __index:(t)=>
            s = t.source
            f = Files.read(s.filename)
            return f
        __mode: "k"
    })
    get_source_file: => @@source_code_for_tree[@]
    get_source_code: => @@source_code_for_tree[@]\sub(@source.start, @source.stop-1)

    add: (...)=>
        n = #@
        for i=1,select('#', ...)
            @[n+i] = select(i, ...)
        @stub = nil

    with: (fn)=>
        if type(fn) == 'table'
            replacements = fn
            fn = (t)->
                if t.type == "Var"
                    if r = replacements[t\as_var!]
                        return r

        replacement = fn(@)
        if replacement == false then return nil
        if replacement
            -- Clone the replacement, so we can give it a proper source/comments
            if SyntaxTree\is_instance(replacement)
                replacement = {k,v for k,v in pairs replacement}
                replacement.source = @source
                replacement.comments = {unpack(@comments)} if @comments
                replacement = SyntaxTree(replacement)
        else
            replacement = {source:@source, comments:@comments and {unpack(@comments)}}
            changes = false
            for k,v in pairs(@)
                replacement[k] = v
                if SyntaxTree\is_instance(v)
                    r = v\with(fn)
                    continue if r == v or r == nil
                    changes = true
                    replacement[k] = r
            return @ unless changes
            replacement = SyntaxTree(replacement)
        return replacement

    contains: (subtree)=>
        if subtree == @ then return true
        for k,v in pairs(@)
            if SyntaxTree\is_instance(v)
                return true if v\contains(subtree)
        return false

    get_args: =>
        assert(@type == "Action" or @type == "MethodCall", "Only actions and method calls have arguments")
        args = {}
        if @type == "MethodCall"
            args[1] = @[1]
            for tok in *@[2]
                if type(tok) != 'string' then args[#args+1] = tok
        else
            for tok in *@
                if type(tok) != 'string' then args[#args+1] = tok
        return args

    get_stub: =>
        switch @type
            when "Action"
                stub_bits = {}
                arg_i = 1
                for a in *@
                    if type(a) == 'string'
                        stub_bits[#stub_bits+1] = a
                    else
                        stub_bits[#stub_bits+1] = arg_i
                        arg_i += 1
                while type(stub_bits[#stub_bits]) == 'number'
                    stub_bits[#stub_bits] = nil
                return concat stub_bits, " "
            when "MethodCall"
                return "0, "..@[2]\get_stub!
            else
                error("#{@type}s do not have stubs")

    as_var: =>
        assert(@type == "Var")
        if type(@[1]) == 'string'
            return @[1]
        else
            return @[1]\get_stub!

    -- Match this tree against a pattern tree, and return what each of the pattern's
    -- variables matched, or nil if it doesn't match. A variable matches any subtree,
    -- and a variable that's used more than once has to match the same thing each
    -- time. Variables named in `literals` only match themselves. If the variable named
    -- `rest` is the last part of the pattern, it matches a list of the remaining parts.
    matching: (patt, literals={}, rest=nil, match={})=>
        if patt.type == "Var" and not literals[patt\as_var!]
            name = patt\as_var!
            return nil if match[name] != nil and match[name] != @
            match[name] = @
            return match
        return nil if patt.type != @type
        for i=1,#patt
            p, v = patt[i], @[i]
            if i == #patt and SyntaxTree\is_instance(p) and p.type == "Var" and p\as_var! == rest
                match[rest] = [@[j] for j=i,#@]
                return match
            if SyntaxTree\is_instance(p)
                return nil unless SyntaxTree\is_instance(v) and v\matching(p, literals, rest, match)
            elseif v != p
                return nil
        return nil if #@ != #patt
        return match

    _breadth_first: =>
        coroutine.yield @
        for child in *@
            if getmetatable(child) == SyntaxTree.__base
                child\_breadth_first!
        return
    breadth_first: => coroutine.create(-> @_breadth_first!)

    _depth_first: =>
        coroutine.yield @
        for child in *@
            if getmetatable(child) == SyntaxTree.__base
                child\_depth_first!
        return
    depth_first: => coroutine.create(-> @_depth_first!)

    @is_instance: (t)=>
        type(t) == 'table' and getmetatable(t) == @__base

SyntaxTree.__base.__type = "a Syntax Tree"

getmetatable(SyntaxTree).__call = (t, ...)=>
    if type(t.source) == 'string'
        t.source = Source\from_string(t.source)
    setmetatable(t, @__base)
    for i=1,select("#", ...)
        t[i] = select(i, ...)
    if t.type == 'Action'
        t.stub = t\get_stub!
    return t

return SyntaxTree