Skip to content

Commit fcafaa2

Browse files
committed
cleanup
1 parent 8052311 commit fcafaa2

File tree

2 files changed

+69
-75
lines changed

2 files changed

+69
-75
lines changed

script/vm/compiler.lua

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ local vm = require 'vm.vm'
1515
-- 该函数有副作用,会给source绑定node!
1616
---@param source parser.object
1717
---@return boolean
18-
function vm.bindDocs(source)
18+
local function bindDocs(source)
1919
local docs = source.bindDocs
2020
if not docs then
2121
return false
@@ -75,7 +75,7 @@ local function searchFieldByLocalID(source, key, pushResult)
7575
local hasMarkDoc = {}
7676
for _, src in ipairs(fields) do
7777
if src.bindDocs then
78-
if vm.bindDocs(src) then
78+
if bindDocs(src) then
7979
local skey = guide.getKeyName(src)
8080
if skey then
8181
hasMarkDoc[skey] = true
@@ -955,7 +955,7 @@ local function compileLocal(source)
955955

956956
local hasMarkDoc
957957
if source.bindDocs then
958-
hasMarkDoc = vm.bindDocs(source)
958+
hasMarkDoc = bindDocs(source)
959959
end
960960
local hasMarkParam
961961
if not hasMarkDoc then
@@ -1020,7 +1020,7 @@ local function compileLocal(source)
10201020
-- for x = ... do
10211021
if source.parent.type == 'loop' then
10221022
if source.parent.loc == source then
1023-
if vm.bindDocs(source) then
1023+
if bindDocs(source) then
10241024
return
10251025
end
10261026
vm.setNode(source, vm.declareGlobal('type', 'integer'))
@@ -1184,7 +1184,7 @@ local compilerSwitch = util.switch()
11841184
end)
11851185
: case 'setlocal'
11861186
: call(function (source)
1187-
if vm.bindDocs(source) then
1187+
if bindDocs(source) then
11881188
return
11891189
end
11901190
local locNode = vm.compileNode(source.node)
@@ -1284,7 +1284,7 @@ local compilerSwitch = util.switch()
12841284
---@cast key string
12851285
vm.compileByParentNode(source.node, key, function (src)
12861286
if src.value then
1287-
if vm.bindDocs(src) then
1287+
if bindDocs(src) then
12881288
variableNode:merge(vm.compileNode(src))
12891289
elseif src.value.type ~= 'nil' then
12901290
variableNode:merge(vm.compileNode(src.value))
@@ -1332,7 +1332,7 @@ local compilerSwitch = util.switch()
13321332
: call(function (source)
13331333
local hasMarkDoc
13341334
if source.bindDocs then
1335-
hasMarkDoc = vm.bindDocs(source)
1335+
hasMarkDoc = bindDocs(source)
13361336
end
13371337

13381338
if not hasMarkDoc then
@@ -1753,6 +1753,65 @@ local compilerSwitch = util.switch()
17531753
end
17541754
vm.binarySwitch(source.op.type, source)
17551755
end)
1756+
: case 'globalbase'
1757+
: call(function (source)
1758+
---@type vm.global
1759+
local global = source.global
1760+
local uri = guide.getUri(source)
1761+
local globalNode = vm.getNode(source)
1762+
if not globalNode then
1763+
return
1764+
end
1765+
globalNode:merge(global)
1766+
if global.cate == 'variable' then
1767+
local hasMarkDoc
1768+
for _, set in ipairs(global:getSets(uri)) do
1769+
if set.bindDocs and set.parent.type == 'main' then
1770+
if bindDocs(set) then
1771+
globalNode:merge(vm.compileNode(set))
1772+
hasMarkDoc = true
1773+
end
1774+
if vm.getNode(set) then
1775+
globalNode:merge(vm.compileNode(set))
1776+
end
1777+
end
1778+
end
1779+
-- Set all globals node first to avoid recursive
1780+
for _, set in ipairs(global:getSets(uri)) do
1781+
vm.setNode(set, globalNode, true)
1782+
end
1783+
for _, set in ipairs(global:getSets(uri)) do
1784+
if set.value and set.value.type ~= 'nil' and set.parent.type == 'main' then
1785+
if not hasMarkDoc or guide.isLiteral(set.value) then
1786+
globalNode:merge(vm.compileNode(set.value))
1787+
end
1788+
end
1789+
end
1790+
for _, set in ipairs(global:getSets(uri)) do
1791+
vm.setNode(set, globalNode, true)
1792+
end
1793+
end
1794+
if global.cate == 'type' then
1795+
for _, set in ipairs(global:getSets(uri)) do
1796+
if set.type == 'doc.class' then
1797+
if set.extends then
1798+
for _, ext in ipairs(set.extends) do
1799+
if ext.type == 'doc.type.table' then
1800+
if not vm.getGeneric(ext) then
1801+
globalNode:merge(vm.compileNode(ext))
1802+
end
1803+
end
1804+
end
1805+
end
1806+
end
1807+
if set.type == 'doc.alias' then
1808+
if not vm.getGeneric(set.extends) then
1809+
globalNode:merge(vm.compileNode(set.extends))
1810+
end
1811+
end
1812+
end
1813+
end
1814+
end)
17561815

17571816
---@param source parser.object
17581817
local function compileByNode(source)

script/vm/global.lua

Lines changed: 3 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ local vm = require 'vm.vm'
88
---@class parser.object
99
---@field package _globalBase parser.object
1010
---@field package _globalBaseMap table<string, parser.object>
11+
---@field global vm.global
1112

1213
---@class vm.global.link
1314
---@field sets parser.object[]
@@ -518,75 +519,8 @@ function vm.compileByGlobal(source)
518519
if not globalBase then
519520
return
520521
end
521-
local globalNode = vm.getNode(source._globalBase)
522-
if globalNode then
523-
vm.setNode(source, globalNode, true)
524-
return
525-
end
526-
local uri = guide.getUri(source)
527-
---@type vm.node
528-
globalNode = vm.createNode(global)
529-
vm.setNode(globalBase, globalNode, true)
522+
local globalNode = vm.compileNode(globalBase)
530523
vm.setNode(source, globalNode, true)
531-
532-
-- TODO:don't mix
533-
--local sets = global.links[uri].sets or {}
534-
--local gets = global.links[uri].gets or {}
535-
--for _, set in ipairs(sets) do
536-
-- vm.setNode(set, globalNode, true)
537-
--end
538-
--for _, get in ipairs(gets) do
539-
-- vm.setNode(get, globalNode, true)
540-
--end
541-
542-
if global.cate == 'variable' then
543-
local hasMarkDoc
544-
for _, set in ipairs(global:getSets(uri)) do
545-
if set.bindDocs and set.parent.type == 'main' then
546-
if vm.bindDocs(set) then
547-
globalNode:merge(vm.compileNode(set))
548-
hasMarkDoc = true
549-
end
550-
if vm.getNode(set) then
551-
globalNode:merge(vm.compileNode(set))
552-
end
553-
end
554-
end
555-
-- Set all globals node first to avoid recursive
556-
for _, set in ipairs(global:getSets(uri)) do
557-
vm.setNode(set, globalNode, true)
558-
end
559-
for _, set in ipairs(global:getSets(uri)) do
560-
if set.value and set.value.type ~= 'nil' and set.parent.type == 'main' then
561-
if not hasMarkDoc or guide.isLiteral(set.value) then
562-
globalNode:merge(vm.compileNode(set.value))
563-
end
564-
end
565-
end
566-
for _, set in ipairs(global:getSets(uri)) do
567-
vm.setNode(set, globalNode, true)
568-
end
569-
end
570-
if global.cate == 'type' then
571-
for _, set in ipairs(global:getSets(uri)) do
572-
if set.type == 'doc.class' then
573-
if set.extends then
574-
for _, ext in ipairs(set.extends) do
575-
if ext.type == 'doc.type.table' then
576-
if not vm.getGeneric(ext) then
577-
globalNode:merge(vm.compileNode(ext))
578-
end
579-
end
580-
end
581-
end
582-
end
583-
if set.type == 'doc.alias' then
584-
if not vm.getGeneric(set.extends) then
585-
globalNode:merge(vm.compileNode(set.extends))
586-
end
587-
end
588-
end
589-
end
590524
end
591525

592526
---@param source parser.object
@@ -609,6 +543,7 @@ function vm.getGlobalBase(source)
609543
root._globalBaseMap[name] = {
610544
type = 'globalbase',
611545
parent = root,
546+
global = global,
612547
}
613548
end
614549
source._globalBase = root._globalBaseMap[name]

0 commit comments

Comments
 (0)