2023-12-23 00:36:02 +08:00
|
|
|
|
local Macro = {}
|
|
|
|
|
Macro.__index = Macro
|
|
|
|
|
|
|
|
|
|
-- Macro 宏类
|
|
|
|
|
-- 提供宏定义和执行的功能
|
|
|
|
|
--
|
|
|
|
|
-- 宏命令支持以下类别:
|
|
|
|
|
-- {} 空表,占位符
|
|
|
|
|
-- { placeholder = 3 } 占位符,placeholder 表示占位符个数(这样不用重复塞多个空表了)
|
|
|
|
|
-- 其他类型都视为参数
|
|
|
|
|
--
|
|
|
|
|
-- 注意:表中含有 nil 可能会造成不可预料的问题
|
|
|
|
|
|
|
|
|
|
function Macro:new(macroSeries)
|
|
|
|
|
if not macroSeries or type(macroSeries) ~= "table" then
|
|
|
|
|
return nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local obj = {
|
|
|
|
|
rawSeries = macroSeries,
|
|
|
|
|
macroLen = 0,
|
|
|
|
|
compiledSeries = nil,
|
|
|
|
|
compiledLen = 0,
|
2023-12-23 19:55:28 +08:00
|
|
|
|
offset = 0
|
2023-12-23 00:36:02 +08:00
|
|
|
|
}
|
|
|
|
|
setmetatable(obj, Macro)
|
|
|
|
|
|
|
|
|
|
obj:compile()
|
|
|
|
|
return obj
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function Macro:compile()
|
|
|
|
|
self.compiledSeries = {}
|
|
|
|
|
for _, v in ipairs(self.rawSeries) do
|
|
|
|
|
if type(v) ~= "table" then
|
|
|
|
|
table.insert(self.compiledSeries, v)
|
|
|
|
|
goto continue
|
|
|
|
|
end
|
|
|
|
|
if v == nil or next(v) == nil then
|
|
|
|
|
-- 是nil或空表
|
|
|
|
|
table.insert(self.compiledSeries, {})
|
|
|
|
|
elseif v.placeholder then
|
|
|
|
|
for i = 1, v.placeholder do
|
|
|
|
|
table.insert(self.compiledSeries, {})
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
-- 非特殊指令
|
|
|
|
|
table.insert(self.compiledSeries, v)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
::continue::
|
|
|
|
|
end
|
|
|
|
|
self.compiledLen = #self.compiledSeries
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function Macro:getNext(loop)
|
2023-12-23 19:55:28 +08:00
|
|
|
|
if self.offset >= self.compiledLen then
|
2023-12-23 00:36:02 +08:00
|
|
|
|
if not loop then
|
|
|
|
|
return nil
|
|
|
|
|
end
|
|
|
|
|
self:reset()
|
|
|
|
|
end
|
|
|
|
|
|
2023-12-23 19:55:28 +08:00
|
|
|
|
self.offset = self.offset + 1
|
|
|
|
|
local cmd = self.compiledSeries[self.offset]
|
2023-12-23 00:36:02 +08:00
|
|
|
|
if cmd == nil then -- 理论不应该是nil,此处保险
|
|
|
|
|
return nil
|
|
|
|
|
end
|
|
|
|
|
if type(cmd) ~= "table" then
|
|
|
|
|
return cmd
|
|
|
|
|
end
|
|
|
|
|
if next(cmd) == nil then
|
|
|
|
|
return nil
|
|
|
|
|
end
|
|
|
|
|
return cmd
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function Macro:reset()
|
2023-12-23 19:55:28 +08:00
|
|
|
|
self.offset = 0
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function Macro:toEnd()
|
|
|
|
|
self.offset = self.compiledLen
|
2023-12-23 00:36:02 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function Macro:runNext(func, forceRun)
|
|
|
|
|
if not func or type(func) ~= "function" then
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
if type(forceRun) ~= "boolean" then
|
|
|
|
|
forceRun = false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local cmd = self:getNext(false)
|
|
|
|
|
if not cmd then
|
|
|
|
|
if forceRun then
|
|
|
|
|
func(nil)
|
|
|
|
|
end
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
func(cmd)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function Macro:runNextLoop(func, forceRun)
|
|
|
|
|
if not func or type(func) ~= "function" then
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
if type(forceRun) ~= "boolean" then
|
|
|
|
|
forceRun = false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local cmd = self:getNext(true)
|
|
|
|
|
if not cmd then
|
|
|
|
|
if forceRun then
|
|
|
|
|
func(nil)
|
|
|
|
|
end
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
func(cmd)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return Macro
|