Skip to content

Commit 244a19d

Browse files
committed
add supports for merging with ifblock
1 parent 0ab880e commit 244a19d

File tree

2 files changed

+68
-20
lines changed

2 files changed

+68
-20
lines changed

script/vm/node.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,6 @@ end
188188

189189
---@return vm.node
190190
function mt:setFalsy()
191-
if self.optional == false then
192-
self.optional = nil
193-
end
194191
local hasBoolean
195192
for index = #self, 1, -1 do
196193
local c = self[index]
@@ -229,6 +226,10 @@ function mt:setFalsy()
229226
if hasBoolean then
230227
self:merge(vm.declareGlobal('type', 'false'))
231228
end
229+
if self.optional then
230+
self.optional = nil
231+
self:merge(vm.declareGlobal('type', 'nil'))
232+
end
232233
return self
233234
end
234235

script/vm/tracer.lua

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ end
3535
---@param mark table
3636
function mt:collectBlock(obj, mark)
3737
while true do
38-
obj = obj.parent
39-
if mark[obj] then
38+
local block = guide.getParentBlock(obj)
39+
if not block then
4040
return
4141
end
42-
if not guide.isBlockType(obj) then
42+
obj = block
43+
if mark[obj] then
4344
return
4445
end
4546
if obj == self.main then
@@ -63,6 +64,9 @@ function mt:collectLocal()
6364
self.assigns[#self.assigns+1] = obj
6465
self:collectBlock(obj, mark)
6566
end
67+
if obj.type == 'getlocal' then
68+
self:collectBlock(obj, mark)
69+
end
6670
end
6771

6872
local casts = self:getCasts()
@@ -112,10 +116,31 @@ function mt:narrow(source)
112116
end
113117

114118
if source.type == 'getlocal' then
115-
node = node:copy():setTruthy()
116-
return node
119+
node = node:copy()
120+
node:setTruthy()
117121
end
118122

123+
return node
124+
end
125+
126+
---@param source parser.object
127+
---@return vm.node?
128+
function mt:calcGet(source)
129+
local parent = source.parent
130+
if parent.type == 'filter' then
131+
return self:calcGet(parent)
132+
end
133+
if parent.type == 'ifblock' then
134+
local parentBlock = guide.getParentBlock(parent.parent)
135+
if parentBlock then
136+
local lastAssign = self:getLastAssign(parentBlock, parent.start)
137+
local node = self:getNode(lastAssign or parentBlock)
138+
return node
139+
end
140+
end
141+
if parent.type == 'unary' then
142+
return self:calcGet(parent)
143+
end
119144
return nil
120145
end
121146

@@ -135,19 +160,10 @@ function mt:calcNode(source)
135160
local node = self:getNode(lastAssign)
136161
return node
137162
end
138-
local parent = source.parent
139-
while true do
140-
if parent.type == 'filter'
141-
or parent.type == 'unary'
142-
or parent.type == 'ifblock'
143-
or parent.type == 'elseifblock' then
144-
parent = parent.parent
145-
else
146-
break
147-
end
163+
local node = self:calcGet(source)
164+
if node then
165+
return node
148166
end
149-
local node = self:getNode(parent)
150-
return node
151167
end
152168
if source.type == 'setlocal' then
153169
if source.node ~= self.source then
@@ -180,6 +196,37 @@ function mt:calcNode(source)
180196
return node
181197
end
182198
end
199+
if source.type == 'if' then
200+
local parentBlock = guide.getParentBlock(source)
201+
if not parentBlock then
202+
return nil
203+
end
204+
local lastAssign = self:getLastAssign(parentBlock, source.start)
205+
local outNode = self:getNode(lastAssign or source.parent) or vm.createNode()
206+
for _, block in ipairs(source) do
207+
local blockNode = self:getNode(block)
208+
if not blockNode then
209+
goto CONTINUE
210+
end
211+
if block.hasReturn
212+
or block.hasError
213+
or block.hasBreak then
214+
outNode:removeNode(blockNode)
215+
goto CONTINUE
216+
end
217+
local blockAssign = self:getLastAssign(block, block.finish)
218+
if not blockAssign then
219+
goto CONTINUE
220+
end
221+
local blockAssignNode = self:getNode(blockAssign)
222+
if not blockAssignNode then
223+
goto CONTINUE
224+
end
225+
outNode:removeNode(blockNode)
226+
outNode:merge(blockAssignNode)
227+
::CONTINUE::
228+
end
229+
end
183230
if source.type == 'unary' then
184231
if source.op.type == 'not' then
185232
local node = self:getNode(source[1])

0 commit comments

Comments
 (0)