From 163da759468164bc020fb4801bf06f1a190af7ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Sun, 24 Apr 2022 23:40:25 +0800 Subject: cleanup --- script/vm/runner.lua | 49 +++++++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 22 deletions(-) (limited to 'script/vm/runner.lua') diff --git a/script/vm/runner.lua b/script/vm/runner.lua index ad2b18ea..19233964 100644 --- a/script/vm/runner.lua +++ b/script/vm/runner.lua @@ -24,6 +24,7 @@ mt.index = 1 ---@field name? string ---@field tag? string ---@field copy? boolean +---@field new? boolean ---@field ref1? vm.runner.step ---@field ref2? vm.runner.step @@ -48,14 +49,10 @@ function mt:_compileNarrowByFilter(filter, outStep, blockStep) if filter.op.type == 'not' then local exp = filter[1] if exp.type == 'getlocal' and exp.node == self.loc then - self.steps[#self.steps+1] = { - type = 'push', - copy = true, - pos = filter.finish, - } self.steps[#self.steps+1] = { type = 'falsy', pos = filter.finish, + new = true, } self.steps[#self.steps+1] = { type = 'truthy', @@ -113,11 +110,6 @@ function mt:_compileNarrowByFilter(filter, outStep, blockStep) end if guide.isLiteral(exp) then if filter.op.type == '==' then - self.steps[#self.steps+1] = { - type = 'push', - copy = true, - pos = filter.finish, - } self.steps[#self.steps+1] = { type = 'remove', name = exp.type, @@ -128,14 +120,10 @@ function mt:_compileNarrowByFilter(filter, outStep, blockStep) type = 'as', name = exp.type, pos = filter.finish, + new = true, } end if filter.op.type == '~=' then - self.steps[#self.steps+1] = { - type = 'push', - copy = true, - pos = filter.finish, - } self.steps[#self.steps+1] = { type = 'as', name = exp.type, @@ -146,20 +134,17 @@ function mt:_compileNarrowByFilter(filter, outStep, blockStep) type = 'remove', name = exp.type, pos = filter.finish, + new = true, } end end end else if filter.type == 'getlocal' and filter.node == self.loc then - self.steps[#self.steps+1] = { - type = 'push', - copy = true, - pos = filter.finish, - } self.steps[#self.steps+1] = { type = 'truthy', pos = filter.finish, + new = true, } self.steps[#self.steps+1] = { type = 'falsy', @@ -331,15 +316,35 @@ function mt:launch(callback) for _, step in ipairs(self.steps) do local node = step.ref1 and step.ref1.node or topNode if step.type == 'truthy' then + if step.new then + node = node:copy() + topNode = node + end node:setTruthy() elseif step.type == 'falsy' then + if step.new then + node = node:copy() + topNode = node + end node:setFalsy() elseif step.type == 'as' then - node:clear() - node:merge(globalMgr.getGlobal('type', step.name)) + if step.new then + topNode = vm.createNode(globalMgr.getGlobal('type', step.name)) + else + node:clear() + node:merge(globalMgr.getGlobal('type', step.name)) + end elseif step.type == 'add' then + if step.new then + node = node:copy() + topNode = node + end node:merge(globalMgr.getGlobal('type', step.name)) elseif step.type == 'remove' then + if step.new then + node = node:copy() + topNode = node + end node:remove(step.name) elseif step.type == 'object' then topNode = callback(step.object, node) or node -- cgit v1.2.3