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
|
local util = require 'utility'
local guide = require 'parser.guide'
local globalBuilder = require 'vm.node.global'
---@class parser.object
---@field _globalNode vm.node.global
---@class vm.global-manager
local m = {}
---@type table<string, vm.node.global>
m.globals = {}
---@type table<uri, table<string, boolean>>
m.globalSubs = util.multiTable(2)
m.ID_SPLITE = '\x1F'
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.compileObject(uri, ref)
end
end
end)
: case 'setglobal'
: call(function (uri, source)
local name = guide.getKeyName(source)
local global = m.declareGlobal(name, uri)
global:addSet(uri, source)
source._globalNode = global
end)
: case 'getglobal'
: call(function (uri, source)
local name = guide.getKeyName(source)
local global = m.declareGlobal(name, uri)
global:addGet(uri, source)
source._globalNode = global
local nxt = source.next
if nxt then
m.compileObject(uri, nxt)
end
end)
: case 'setfield'
: case 'setmethod'
: case 'setindex'
---@param uri uri
---@param source parser.object
: call(function (uri, source)
local parent = source.node._globalNode
if not parent then
return
end
local name = parent:getName() .. m.ID_SPLITE .. guide.getKeyName(source)
local global = m.declareGlobal(name, uri)
global:addSet(uri, source)
source._globalNode = global
end)
: case 'getfield'
: case 'getmethod'
: case 'getindex'
---@param uri uri
---@param source parser.object
: call(function (uri, source)
local parent = source.node._globalNode
if not parent then
return
end
local name = parent:getName() .. m.ID_SPLITE .. guide.getKeyName(source)
local global = m.declareGlobal(name, uri)
global:addGet(uri, source)
source._globalNode = global
local nxt = source.next
if nxt then
m.compileObject(uri, nxt)
end
end)
: getMap()
---@param name string
---@param uri uri
---@return vm.node.global
function m.declareGlobal(name, uri)
m.globalSubs[uri][name] = true
if not m.globals[name] then
m.globals[name] = globalBuilder(name)
end
return m.globals[name]
end
---@param name string
---@param field? string
---@return vm.node.global?
function m.getGlobal(name, field)
if field then
name = name .. m.ID_SPLITE .. field
end
return m.globals[name]
end
---@param source parser.object
function m.compileObject(uri, source)
if source._globalNode ~= nil then
return
end
source._globalNode = false
local compiler = compilerGlobalMap[source.type]
if compiler then
compiler(uri, source)
end
end
---@param source parser.object
function m.compileAst(source)
local uri = guide.getUri(source)
local env = guide.getENV(source)
m.compileObject(uri, env)
end
---@return vm.node.global
function m.getNode(source)
if source.type == 'field'
or source.type == 'method' then
source = source.parent
end
return source._globalNode
end
---@param uri uri
function m.dropUri(uri)
local globalSub = m.globalSubs[uri]
m.globalSubs[uri] = nil
for name in pairs(globalSub) do
local global = m.globals[name]
global:dropUri(uri)
if not global:isAlive() then
m.globals[name] = nil
end
end
end
return m
|