Skip to content

Commit 5415cf7

Browse files
committed
cleanup
1 parent 45bf548 commit 5415cf7

File tree

2 files changed

+102
-98
lines changed

2 files changed

+102
-98
lines changed

script/vm/compiler.lua

Lines changed: 8 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ local vm = require 'vm.vm'
99
---@class parser.object
1010
---@field _compiledNodes boolean
1111
---@field _node vm.node
12-
---@field package _globalBase table
1312
---@field cindex integer
1413
---@field func parser.object
1514

1615
-- 该函数有副作用,会给source绑定node!
1716
---@param source parser.object
1817
---@return boolean
19-
local function bindDocs(source)
18+
function vm.bindDocs(source)
2019
local docs = source.bindDocs
2120
if not docs then
2221
return false
@@ -76,7 +75,7 @@ local function searchFieldByLocalID(source, key, pushResult)
7675
local hasMarkDoc = {}
7776
for _, src in ipairs(fields) do
7877
if src.bindDocs then
79-
if bindDocs(src) then
78+
if vm.bindDocs(src) then
8079
local skey = guide.getKeyName(src)
8180
if skey then
8281
hasMarkDoc[skey] = true
@@ -956,7 +955,7 @@ local function compileLocal(source)
956955

957956
local hasMarkDoc
958957
if source.bindDocs then
959-
hasMarkDoc = bindDocs(source)
958+
hasMarkDoc = vm.bindDocs(source)
960959
end
961960
local hasMarkParam
962961
if not hasMarkDoc then
@@ -1021,7 +1020,7 @@ local function compileLocal(source)
10211020
-- for x = ... do
10221021
if source.parent.type == 'loop' then
10231022
if source.parent.loc == source then
1024-
if bindDocs(source) then
1023+
if vm.bindDocs(source) then
10251024
return
10261025
end
10271026
vm.setNode(source, vm.declareGlobal('type', 'integer'))
@@ -1185,7 +1184,7 @@ local compilerSwitch = util.switch()
11851184
end)
11861185
: case 'setlocal'
11871186
: call(function (source)
1188-
if bindDocs(source) then
1187+
if vm.bindDocs(source) then
11891188
return
11901189
end
11911190
local locNode = vm.compileNode(source.node)
@@ -1285,7 +1284,7 @@ local compilerSwitch = util.switch()
12851284
---@cast key string
12861285
vm.compileByParentNode(source.node, key, function (src)
12871286
if src.value then
1288-
if bindDocs(src) then
1287+
if vm.bindDocs(src) then
12891288
variableNode:merge(vm.compileNode(src))
12901289
elseif src.value.type ~= 'nil' then
12911290
variableNode:merge(vm.compileNode(src.value))
@@ -1333,7 +1332,7 @@ local compilerSwitch = util.switch()
13331332
: call(function (source)
13341333
local hasMarkDoc
13351334
if source.bindDocs then
1336-
hasMarkDoc = bindDocs(source)
1335+
hasMarkDoc = vm.bindDocs(source)
13371336
end
13381337

13391338
if not hasMarkDoc then
@@ -1760,95 +1759,6 @@ local function compileByNode(source)
17601759
compilerSwitch(source.type, source)
17611760
end
17621761

1763-
---@param source parser.object
1764-
local function compileByGlobal(source)
1765-
local global = vm.getGlobalNode(source)
1766-
if not global then
1767-
return
1768-
end
1769-
---@cast source parser.object
1770-
local root = guide.getRoot(source)
1771-
local uri = guide.getUri(source)
1772-
if not root._globalBase then
1773-
root._globalBase = {}
1774-
end
1775-
local name = global:asKeyName()
1776-
if not root._globalBase[name] then
1777-
root._globalBase[name] = {
1778-
type = 'globalbase',
1779-
parent = root,
1780-
}
1781-
end
1782-
local globalNode = vm.getNode(root._globalBase[name])
1783-
if globalNode then
1784-
vm.setNode(source, globalNode, true)
1785-
return
1786-
end
1787-
---@type vm.node
1788-
globalNode = vm.createNode(global)
1789-
vm.setNode(root._globalBase[name], globalNode, true)
1790-
vm.setNode(source, globalNode, true)
1791-
1792-
-- TODO:don't mix
1793-
--local sets = global.links[uri].sets or {}
1794-
--local gets = global.links[uri].gets or {}
1795-
--for _, set in ipairs(sets) do
1796-
-- vm.setNode(set, globalNode, true)
1797-
--end
1798-
--for _, get in ipairs(gets) do
1799-
-- vm.setNode(get, globalNode, true)
1800-
--end
1801-
1802-
if global.cate == 'variable' then
1803-
local hasMarkDoc
1804-
for _, set in ipairs(global:getSets(uri)) do
1805-
if set.bindDocs and set.parent.type == 'main' then
1806-
if bindDocs(set) then
1807-
globalNode:merge(vm.compileNode(set))
1808-
hasMarkDoc = true
1809-
end
1810-
if vm.getNode(set) then
1811-
globalNode:merge(vm.compileNode(set))
1812-
end
1813-
end
1814-
end
1815-
-- Set all globals node first to avoid recursive
1816-
for _, set in ipairs(global:getSets(uri)) do
1817-
vm.setNode(set, globalNode, true)
1818-
end
1819-
for _, set in ipairs(global:getSets(uri)) do
1820-
if set.value and set.value.type ~= 'nil' and set.parent.type == 'main' then
1821-
if not hasMarkDoc or guide.isLiteral(set.value) then
1822-
globalNode:merge(vm.compileNode(set.value))
1823-
end
1824-
end
1825-
end
1826-
for _, set in ipairs(global:getSets(uri)) do
1827-
vm.setNode(set, globalNode, true)
1828-
end
1829-
end
1830-
if global.cate == 'type' then
1831-
for _, set in ipairs(global:getSets(uri)) do
1832-
if set.type == 'doc.class' then
1833-
if set.extends then
1834-
for _, ext in ipairs(set.extends) do
1835-
if ext.type == 'doc.type.table' then
1836-
if not vm.getGeneric(ext) then
1837-
globalNode:merge(vm.compileNode(ext))
1838-
end
1839-
end
1840-
end
1841-
end
1842-
end
1843-
if set.type == 'doc.alias' then
1844-
if not vm.getGeneric(set.extends) then
1845-
globalNode:merge(vm.compileNode(set.extends))
1846-
end
1847-
end
1848-
end
1849-
end
1850-
end
1851-
18521762
local nodeSwitch;nodeSwitch = util.switch()
18531763
: case 'field'
18541764
: case 'method'
@@ -1941,7 +1851,7 @@ function vm.compileNode(source)
19411851

19421852
---@cast source parser.object
19431853
vm.setNode(source, vm.createNode(), true)
1944-
compileByGlobal(source)
1854+
vm.compileByGlobal(source)
19451855
compileByNode(source)
19461856
compileByParentNode(source)
19471857
matchCall(source)

script/vm/global.lua

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ local files = require 'files'
55
---@class vm
66
local vm = require 'vm.vm'
77

8+
---@class parser.object
9+
---@field package _globalBase parser.object
10+
---@field package _globalBaseMap table<string, parser.object>
11+
812
---@class vm.global.link
913
---@field sets parser.object[]
1014
---@field hasGet boolean?
@@ -504,6 +508,96 @@ function vm.getEnums(source)
504508
return source._enums
505509
end
506510

511+
---@param source parser.object
512+
function vm.compileByGlobal(source)
513+
local global = vm.getGlobalNode(source)
514+
if not global then
515+
return
516+
end
517+
---@cast source parser.object
518+
local root = guide.getRoot(source)
519+
local uri = guide.getUri(source)
520+
if not root._globalBaseMap then
521+
root._globalBaseMap = {}
522+
end
523+
local name = global:asKeyName()
524+
if not root._globalBaseMap[name] then
525+
root._globalBaseMap[name] = {
526+
type = 'globalbase',
527+
parent = root,
528+
}
529+
end
530+
source._globalBase = root._globalBaseMap[name]
531+
local globalNode = vm.getNode(source._globalBase)
532+
if globalNode then
533+
vm.setNode(source, globalNode, true)
534+
return
535+
end
536+
---@type vm.node
537+
globalNode = vm.createNode(global)
538+
vm.setNode(source._globalBase, globalNode, true)
539+
vm.setNode(source, globalNode, true)
540+
541+
-- TODO:don't mix
542+
--local sets = global.links[uri].sets or {}
543+
--local gets = global.links[uri].gets or {}
544+
--for _, set in ipairs(sets) do
545+
-- vm.setNode(set, globalNode, true)
546+
--end
547+
--for _, get in ipairs(gets) do
548+
-- vm.setNode(get, globalNode, true)
549+
--end
550+
551+
if global.cate == 'variable' then
552+
local hasMarkDoc
553+
for _, set in ipairs(global:getSets(uri)) do
554+
if set.bindDocs and set.parent.type == 'main' then
555+
if vm.bindDocs(set) then
556+
globalNode:merge(vm.compileNode(set))
557+
hasMarkDoc = true
558+
end
559+
if vm.getNode(set) then
560+
globalNode:merge(vm.compileNode(set))
561+
end
562+
end
563+
end
564+
-- Set all globals node first to avoid recursive
565+
for _, set in ipairs(global:getSets(uri)) do
566+
vm.setNode(set, globalNode, true)
567+
end
568+
for _, set in ipairs(global:getSets(uri)) do
569+
if set.value and set.value.type ~= 'nil' and set.parent.type == 'main' then
570+
if not hasMarkDoc or guide.isLiteral(set.value) then
571+
globalNode:merge(vm.compileNode(set.value))
572+
end
573+
end
574+
end
575+
for _, set in ipairs(global:getSets(uri)) do
576+
vm.setNode(set, globalNode, true)
577+
end
578+
end
579+
if global.cate == 'type' then
580+
for _, set in ipairs(global:getSets(uri)) do
581+
if set.type == 'doc.class' then
582+
if set.extends then
583+
for _, ext in ipairs(set.extends) do
584+
if ext.type == 'doc.type.table' then
585+
if not vm.getGeneric(ext) then
586+
globalNode:merge(vm.compileNode(ext))
587+
end
588+
end
589+
end
590+
end
591+
end
592+
if set.type == 'doc.alias' then
593+
if not vm.getGeneric(set.extends) then
594+
globalNode:merge(vm.compileNode(set.extends))
595+
end
596+
end
597+
end
598+
end
599+
end
600+
507601
---@param source parser.object
508602
local function compileSelf(source)
509603
if source.parent.type ~= 'funcargs' then

0 commit comments

Comments
 (0)