local MAJOR, MINOR = "LibLogic-1.0", 100000 + tonumber(("$Revision$"):match("(%d+)")) |
local LibLogic = LibStub:NewLibrary(MAJOR, MINOR) |
|
if not LibInventory then return end -- no need to update |
|
|
-- local upvalues |
local ipairs = _G.ipairs |
local next = _G.next |
|
local table_insert = _G.table.insert |
local table_remove = _G.table.remove |
|
|
|
-- individual handlers |
|
Local Typefunction = {} |
|
function Typefunction:or(logic,arguments) |
for i,sublogic in iparis(logic.args) do |
if self[sublogic.type](self,sublogic,arguments) then |
return true |
end |
end |
return false |
end |
|
function Typefunction:and(logic,arguments) |
for i,sublogic in ipairs(logic.args) do |
if not self[sublogic.type](self,sublogic,arguments) then |
return false |
end |
end |
return true |
end |
|
|
Typefunction["and"] = ANDfunc |
Typefunction["nor"] = NORfunc |
Typefunction["nand"] = NANDfunc |
Typefunction["not"] = NOTfunc |
Typefunction["xor"] = XORfunc |
Typefunction["value"] = VALUEfunc |
Typefunction["function"] = FUNCTIONfunc |
|
|
local function CompileLocal(logic,compiledLogic) |
local args = logic.args |
|
if logic.type == "value" then --boolean values |
if logic.value then |
table_insert(compiledLogic,{["type"]="value" , ["value"]= true }) |
else |
table_insert(compiledLogic,{["type"]="value" , ["value"]= false }) |
end |
end |
|
if logic.type == "function" then --functions |
if logic.function then |
table_insert(compiledLogic,{["type"]="function" , ["value"]= logic.function }) |
end |
end |
|
|
if logic.type == "and" then -- and |
if args then |
local counter = 0 |
for i,arg in ipairs(args) do |
CompileLocal(arg,compiledLogic) -- add all subexpressions |
counter++ |
end |
if counter > 0 then |
table_insert(compiledLogic,{["type"]="and" , ["value"]= counter }) |
end |
else |
table_insert(compiledLogic,{["type"]="value" , ["value"]= false }) -- no arguments , and is true |
end |
end |
|
|
if logic.type == "or" then -- or |
if args then |
local counter = 0 |
for i,arg in ipairs(args) do |
CompileLocal(arg,compiledLogic) -- add all subexpressions |
counter++ |
end |
if counter > 0 then |
table_insert(compiledLogic,{["type"]="and" , ["value"]= counter }) |
end |
else |
table_insert(compiledLogic,{["type"]="value" , ["value"]= false }) -- no arguments , or is false |
end |
end |
|
end |
|
-- API |
function LibLogic:Compile(logic) |
local compiledLogic = { |
["type"] = "compiled", |
["version"] = "1.0" |
["logic"] = {} |
} |
Compile_Local(logic,compiledLogic.logic) |
return compiledLogic |
end |