summaryrefslogtreecommitdiff
path: root/script/vm/node/compiler.lua
blob: 422f464e8758a6a5ca7b0bcff772929324a6621b (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
local guide = require 'parser.guide'
local util  = require 'utility'
local state = require 'vm.state'
local union = require 'vm.node.union'

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

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

---@class vm.node.cross

m.GLOBAL_SPLITE = '\x1F'

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

---@param  ... string
---@return string
function m.getGlobalID(...)
    return table.concat({...}, m.GLOBAL_SPLITE)
end

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

local function compileValue(uri, source, value)
    if not value then
        return
    end
    if value.type == 'table'
    or value.type == 'integer'
    or value.type == 'number'
    or value.type == 'string'
    or value.type == 'function' then
        state.declareLiteral(uri, value)
        m.setNode(source, value)
    end
end

local compilerMap = util.switch()
    : case 'local'
    : call(function (uri, source)
        compileValue(uri, source, source.value)
        if source.ref then
            for _, ref in ipairs(source.ref) do
                if ref.type == 'setlocal' then
                    compileValue(uri, source, ref.value)
                end
            end
        end
    end)
    : case 'getlocal'
    : call(function (uri, source)
        m.setNode(source, m.compileNode(uri, source.node))
    end)
    : getMap()

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

local compilerGlobalMap = util.switch()
    : case 'local'
    : call(function (uri, source)
        if source.tag ~= '_ENV' then
            return
        end
        if source.ref then
            for _, ref in ipairs(source.ref) do
                m.compileGlobalNode(uri, ref)
            end
        end
    end)
    : case 'setglobal'
    : call(function (uri, source)
        local name = guide.getKeyName(source)
        source._globalID = state.declareGlobal(name, uri, source)
    end)
    : case 'getglobal'
    : call(function (uri, source)
        local name   = guide.getKeyName(source)
        local global = state.getGlobal(name)
        global:addGet(uri, source)
        source._globalID = global

        local nxt = source.next
        if nxt then
            m.compileGlobalNode(uri, nxt)
        end
    end)
    : case 'setfield'
    ---@param uri    uri
    ---@param source parser.object
    : call(function (uri, source)
        local parent = source.node._globalID
        if not parent then
            return
        end
        local name = m.getGlobalID(parent:getName(), guide.getKeyName(source))
        source._globalID = state.declareGlobal(name, uri, source)
    end)
    : case 'getfield'
    ---@param uri    uri
    ---@param source parser.object
    : call(function (uri, source)
        local parent = source.node._globalID
        if not parent then
            return
        end
        local name = m.getGlobalID(parent:getName(), guide.getKeyName(source))
        local global = state.getGlobal(name)
        global:addGet(uri, source)
        source._globalID = global

        local nxt = source.next
        if nxt then
            m.compileGlobalNode(uri, nxt)
        end
    end)
    : getMap()

---@param uri    uri
---@param source parser.object
function m.compileGlobalNode(uri, source)
    if source._globalID ~= nil then
        return
    end
    source._globalID = false
    local compiler = compilerGlobalMap[source.type]
    if compiler then
        compiler(uri, source)
    end
end

---编译全局变量的node
---@param  root parser.object
function m.compileGlobals(root)
    local uri = guide.getUri(root)
    local env = guide.getENV(root)
    m.compileGlobalNode(uri, env)
end

return m