@@ -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
5250end
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 )
8080end
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
105103end
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 ?
109124function 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
126146end
127147
128148--- @param source parser.object
0 commit comments