summaryrefslogtreecommitdiff
path: root/script/vm/node/compiler.lua
blob: dfe4bc0ce9c519e76790f430d3bb94c38162e368 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
local guide    = require 'parser.guide'
local util     = require 'utility'
local state    = require 'vm.state'
local union    = require 'vm.node.union'
local localID  = require 'vm.local-id'

---@class parser.object
---@field _compiledNodes  boolean
---@field _node           vm.node

---@class vm.node.compiler
local m = {}

---@class vm.node.cross

---@alias vm.node parser.object | vm.node.union | vm.node.cross | vm.node.global

function m.setNode(source, node)
    if not node then
        return
    end
    local me = source._node
    if not me then
        source._node = node
        return
    end
    if me.type == 'union'
    or me.type == 'cross' then
        me:merge(node)
        return
    end
    source._node = union(me, node)
end

function m.eachNode(node)
    if node.type == 'union' then
        return node:eachNode()
    end
    local first = true
    return function ()
        if first then
            first = false
            return node
        end
        return nil
    end
end

local function getReturnOfFunction(func, index)
    if not func._returns then
        func._returns = util.defaultTable(function ()
            return {
                type   = 'function.return',
                parent = func,
                index  = index,
            }
        end)
    end
    return m.compileNode(func._returns[index])
end

local function getReturn(func, index)
    local node = m.compileNode(func)
    if not node then
        return
    end
    for cnode in m.eachNode(node) do
        if cnode.type == 'function' then
            return getReturnOfFunction(cnode, index)
        end
    end
end

local function compileByLocalID(source)
    local sources = localID.getSources(source)
    if not sources then
        return
    end
    for _, src in ipairs(sources) do
        if src.value then
            m.setNode(source, m.compileNode(src.value))
        end
    end
end

local searchFieldMap = util.switch()
    : case 'table'
    : call(function (node, key, pushResult)
        for _, field in ipairs(node) do
            if field.type == 'tablefield'
            or field.type == 'tableindex' then
                if guide.getKeyName(field) == key then
                    pushResult(field)
                end
            end
        end
    end)
    : getMap()

local function compileByParentNode(source)
    local parentNode = m.compileNode(source.node)
    if not parentNode then
        return
    end
    local key = guide.getKeyName(source)
    local f = searchFieldMap[parentNode.type]
    if f then
        f(parentNode, key, function (field)
            m.setNode(source, m.compileNode(field.value))
        end)
    end
end

local compilerMap = util.switch()
    : case 'boolean'
    : case 'table'
    : case 'integer'
    : case 'number'
    : case 'string'
    : case 'function'
    : call(function (source)
        m.setNode(source, state.declareLiteral(source))
    end)
    : case 'local'
    : call(function (source)
        if source.value then
            m.setNode(source, m.compileNode(source.value))
        end
        if source.ref then
            for _, ref in ipairs(source.ref) do
                if ref.type == 'setlocal' then
                    m.setNode(source, m.compileNode(ref.value))
                end
            end
        end
    end)
    : case 'getlocal'
    : call(function (source)
        m.setNode(source, m.compileNode(source.node))
    end)
    : case 'setfield'
    : case 'setmethod'
    : case 'setindex'
    : call(function (source)
        compileByLocalID(source)
    end)
    : case 'getfield'
    : case 'getmethod'
    : case 'getindex'
    : call(function (source)
        compileByLocalID(source)
        compileByParentNode(source)
    end)
    : case 'function.return'
    : call(function (source)
        local func  = source.parent
        local index = source.index
        if func.returns then
            for _, rtn in ipairs(func.returns) do
                if rtn[index] then
                    m.setNode(source, m.compileNode(rtn[index]))
                end
            end
        end
    end)
    : case 'select'
    : call(function (source, value)
        local vararg = value.vararg
        if vararg.type == 'call' then
            m.setNode(source, getReturn(vararg.node, value.sindex))
        end
    end)
    : getMap()

---@param source parser.object
---@return vm.node
function m.compileNode(source)
    if source._node then
        return source._node
    end
    source._node = false
    local compiler = compilerMap[source.type]
    if compiler then
        compiler(source)
    end
    state.subscribeLiteral(source, source._node)
    return source._node
end

return m