Typefunction["function"] = FUNCTIONfunc |
|
|
-- for faster comparison: |
-- types: |
-- 0 value |
-- 1 function |
-- 2 and |
-- 3 or |
|
|
local function CompileLocal(logic,compiledLogic) |
local args = logic.args |
local args = logic.arguments |
|
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 |
if not logic.type then -- do nothing if malformed |
return nil |
end |
|
if logic.type == "function" then --functions |
if logic.function then |
table_insert(compiledLogic,{["type"]="function" , ["value"]= logic.function }) |
end |
if logic.type == "value" then |
table_insert(compiledLogic,{[1]=0 , [2]=logic.value}) |
return "value" |
end |
|
if logic.type == "function" then |
table_insert(compiledLogic,{[1]=1 , [2]=logic.function}) |
return "function" |
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++ |
-- building up the return |
local complexBuffer = nil |
local simpleBuffer = nil |
local operator = nil |
|
if logic.type == "and" then |
if not args then |
table_insert(compiledLogic,{[1]=0 , [2]=true}) |
return "value" |
else |
operator={[1]=2,[2]=0} |
simpleBuffer = {} |
complexBuffer = {} |
simpleCount = 0 |
complexCount = 0 |
for i,arg in pairs(args) do |
local buffer = {} |
local typebuffer = CompileLocal(arg,buffer) |
if typebuffer then |
if typebuffer == "value" then |
if not buffer[1].value then -- one false and the and is false |
table_insert(compiledLogic,{[1]=0 , [2]=false}) |
return "value" |
end -- yes we can ignore true values here |
end |
if typebuffer == "function" then -- functions should later be evaluated first! |
operator[2]++ |
simpleCount++ |
table_insert(simpleBuffer,table_remove(buffer)) -- shift it to the simple buffer |
end |
if typebuffer == "complex" then |
operator[2]++ --yes this is correct, as the and only wants one argument |
complexCount++ |
table_insert(complexBuffer,buffer) |
buffer = {} -- need a new one |
end |
end |
end |
if counter > 0 then |
table_insert(compiledLogic,{["type"]="and" , ["value"]= counter }) |
end |
end |
|
if logic.type == "or" then |
if not args then |
table_insert(compiledLogic,{[1]=0 , [2]=false}) |
return "value" |
else |
operator={[1]=3,[2]=0} |
simpleBuffer = {} |
complexBuffer = {} |
for i,arg in pairs(args) do |
local buffer = {} |
local typebuffer = CompileLocal(arg,buffer) |
if typebuffer then |
if typebuffer == "value" then |
if buffer[1].value then -- one true and the or is true |
table_insert(compiledLogic,{[1]=0 , [2]=true}) |
return "value" |
end -- yes we can ignore false values here |
end |
if typebuffer == "function" then -- functions should later be evaluated first! |
operator[2]++ |
table_insert(simpleBuffer,table_remove(buffer)) -- shift it to the simple buffer |
end |
if typebuffer == "complex" then |
operator[2]++ --yes this is correct, as the or only wants one argument |
table_insert(complexBuffer,buffer) |
buffer = {} -- and a new one |
end |
end |
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++ |
--so you're still here? eh? |
--then we should have loadet up quite some info now, so get ready to return |
|
-- if there is only one entry we don't need to throw logic at it... |
if operator[2]==1 then --dump the operator |
if simpleCount != 0 then |
table_insert(compiledLogic,simpleBuffer[1]) |
return "function" -- can't be anything else |
else |
for i,val in ipairs(complexBuffer[1]) |
table_insert(compiledLogic,val) |
return "complex" |
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 |
--ok now the really complex stuff |
table_insert(compiledLogic,operator) -- we use prefix notation !! |
for i,val in simpleBuffer do |
table_insert(compiledLogic,val) |
end |
for i,val in ipairs(complexBuffer) do |
for i2,cval in ipairs(val) do |
table_insert(compiledLogic,cval) |
end |
end |
return "complex" |
end |
|
|
|
|
local function CompileFunction(logic,compiledLogic) |
end |
|
local function CompileAnd(logic,compiledLogic) |
end |
|
local function CompileOr(logic,compiledLogic) |
end |
|
|
local function CompileLocal(logic,compiledLogic) |
if logic.type then |
if logic.type == "value" then |
table.insert(compiledLogic,{[1]=0 , [2]=logic.value}) |
end |
|
end |
end |
|
-- API |