File tree Expand file tree Collapse file tree 3 files changed +156
-179
lines changed
Expand file tree Collapse file tree 3 files changed +156
-179
lines changed Original file line number Diff line number Diff line change 198198 return f
199199end })
200200
201+ local eachChildMap = setmetatable ({}, {__index = function (self , name )
202+ local defs = childMap [name ]
203+ if not defs then
204+ self [name ] = false
205+ return false
206+ end
207+ local text = {}
208+ text [# text + 1 ] = ' local obj, callback = ...'
209+ for _ , def in ipairs (defs ) do
210+ if def == ' #' then
211+ text [# text + 1 ] = [[
212+ for i = 1, #obj do
213+ callback(obj[i])
214+ end
215+ ]]
216+ elseif type (def ) == ' string' and def :sub (1 , 1 ) == ' #' then
217+ local key = def :sub (2 )
218+ text [# text + 1 ] = ([[
219+ local childs = obj.%s
220+ if childs then
221+ for i = 1, #childs do
222+ callback(childs[i])
223+ end
224+ end
225+ ]] ):format (key )
226+ elseif type (def ) == ' string' then
227+ text [# text + 1 ] = (' callback(obj.%s)' ):format (def )
228+ else
229+ text [# text + 1 ] = (' callback(obj[%q])' ):format (def )
230+ end
231+ end
232+ local buf = table.concat (text , ' \n ' )
233+ local f = load (buf , buf , ' t' )
234+ self [name ] = f
235+ return f
236+ end })
237+
201238m .actionMap = {
202239 [' main' ] = {' #' },
203240 [' repeat' ] = {' #' },
@@ -752,6 +789,16 @@ function m.eachSource(ast, callback)
752789 end
753790end
754791
792+ --- @param source parser.object
793+ --- @param callback fun ( src : parser.object )
794+ function m .eachChild (source , callback )
795+ local f = eachChildMap [source .type ]
796+ if not f then
797+ return
798+ end
799+ f (source , callback )
800+ end
801+
755802--- 获取指定的 special
756803function m .eachSpecialOf (ast , name , callback )
757804 local root = m .getRoot (ast )
Original file line number Diff line number Diff line change @@ -1313,14 +1313,6 @@ local compilerSwitch = util.switch()
13131313 else
13141314 vm .setNode (src , vm .compileNode (src .value ), true )
13151315 end
1316- elseif src .value
1317- and src .value .type == ' binary'
1318- and src .value .op and src .value .op .type == ' or'
1319- and src .value [1 ] and src .value [1 ].type == ' getlocal' and src .value [1 ].node == source then
1320- -- x = x or 1
1321- vm .setNode (src , vm .compileNode (src .value ))
1322- else
1323- vm .setNode (src , node , true )
13241316 end
13251317 return vm .getNode (src )
13261318 elseif src .type == ' getlocal' then
You can’t perform that action at this time.
0 commit comments