Skip to content

Commit ed97a9e

Browse files
committed
redesign
1 parent 73f8157 commit ed97a9e

File tree

2 files changed

+51
-20
lines changed

2 files changed

+51
-20
lines changed

script/vm/tracer.lua

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ function mt:collectBlock(obj, mark)
4545
return
4646
end
4747
mark[obj] = true
48-
if obj.type ~= 'do' then
49-
self.assigns[#self.assigns+1] = obj
50-
end
48+
self.assigns[#self.assigns+1] = obj
5149
end
5250
end
5351

@@ -57,6 +55,8 @@ function mt:collectLocal()
5755

5856
local mark = {}
5957

58+
self.assigns[#self.assigns+1] = self.source
59+
6060
for _, obj in ipairs(self.source.ref) do
6161
if obj.type == 'setlocal' then
6262
self.assigns[#self.assigns+1] = obj
@@ -79,50 +79,70 @@ function mt:collectLocal()
7979
end)
8080
end
8181

82-
---@param source parser.object
82+
---@param block parser.object
83+
---@param pos integer
8384
---@return parser.object?
84-
function mt:getLastAssign(source)
85-
local assign = self.source
86-
local block = guide.getParentBlock(source)
85+
function mt:getLastAssign(block, pos)
8786
if not block then
8887
return nil
8988
end
89+
local assign
9090
for _, obj in ipairs(self.assigns) do
91-
if obj.start >= source.start then
91+
if obj.start >= pos then
9292
break
9393
end
9494
local objBlock = guide.getParentBlock(obj)
9595
if not objBlock then
9696
break
9797
end
98-
if objBlock == block
99-
or objBlock.type == 'do'
100-
or objBlock.finish > block.finish then
98+
if objBlock == block then
10199
assign = obj
102100
end
103101
end
104102
return assign
105103
end
106104

105+
---@param source parser.object
106+
---@return vm.node?
107+
function mt:calcNode(source)
108+
if guide.isSet(source) then
109+
local node = vm.compileNode(source)
110+
return node
111+
end
112+
if source.type == 'do' then
113+
local lastAssign = self:getLastAssign(source, source.finish)
114+
if lastAssign then
115+
return self:getNode(lastAssign)
116+
else
117+
return nil
118+
end
119+
end
120+
end
121+
107122
---@param source parser.object
108123
---@return vm.node?
109124
function mt:getNode(source)
110125
if self.nodes[source] ~= nil then
111126
return self.nodes[source] or nil
112127
end
113-
local lastAssign = self:getLastAssign(source)
114-
if not lastAssign then
128+
local parentBlock = guide.getParentBlock(source)
129+
if not parentBlock then
130+
self.nodes[source] = false
131+
return nil
132+
end
133+
if source == self.main then
115134
self.nodes[source] = false
116135
return nil
117136
end
118-
if guide.isSet(lastAssign) then
119-
local lastNode = vm.compileNode(lastAssign)
120-
self.nodes[source] = lastNode
121-
return lastNode
137+
local node = self:calcNode(source)
138+
if node then
139+
self.nodes[source] = node
140+
return node
122141
end
123-
local lastNode = self:getNode(lastAssign)
124-
self.nodes[source] = lastNode
125-
return lastNode
142+
local lastAssign = self:getLastAssign(parentBlock, source.start)
143+
local parentNode = self:getNode(lastAssign or parentBlock)
144+
self.nodes[source] = parentNode or false
145+
return parentNode
126146
end
127147

128148
---@param source parser.object

test/type_inference/init.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,6 +1767,17 @@ end
17671767
print(<?x?>)
17681768
]]
17691769

1770+
TEST 'boolean' [[
1771+
local x
1772+
x = true
1773+
function XX()
1774+
do
1775+
x = 1
1776+
end
1777+
end
1778+
print(<?x?>)
1779+
]]
1780+
17701781
TEST 'integer?' [[
17711782
---@type integer?
17721783
local <?x?>

0 commit comments

Comments
 (0)