lua-boolexpr

Boolean Expression library for Lua

This project is maintained by A-G-D

Compiling Boolean Expressions

Back to: Table of Contents


About Compilation

Compiling boolean expressions returns a new and simplified version of that expresion. This can be done using the BoolExpr:compile() method.


Pros and Cons

Simplifying expressions can reduce a fair amount of overhead during evaluation. This is especially true when the leaf expresions - meaning the function - only does light operations and the overall level of nesting of boolean operators are deep. On the other hand, the compilation itself is a complex process and creates a new boolean expression which further takes up memory. Therefore, it is usually enough to only compile really complex expressions that are often evaluated in a performance sensitive parts of the script, and leave the seldom used or simple boolean expressions as is.


Cases for Simplifications

These are a list of all cases for simplification currently supported by the library. The following are written in a simplified tree representation. Each line (except for the braces) represents an element, which is either an operator - written with words in capital letters, or an expression - written in numbers - which also represents the order in which each expression is evaluated within the boolean expression tree.


Chained AND Operations

{
    AND
    {
        AND
        {
            AND
            {
                1
            }
            2
        }
        3
    }
    4
}

->

{
    ALL
    1
    2
    3
    4
}


Chained OR Operations

{
    OR
    {
        OR
        {
            OR
            {
                1
            }
            2
        }
        3
    }
    4
}

->

{
    ANY
    1
    2
    3
    4
}


Chained ALL-AND Operations (and Vice-Versa)

{
    ALL
    {
        AND
        {
            ALL
            1
            2
            3
        }
        {
            ALL
            4
            5
            6
        }
    }
    {
        AND
        7
        8
    }
    {
        AND
        9
        10
    }
}

->

{
    ALL
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
}


Chained ANY-OR Operations (and Vice-Versa)

{
    ANY
    {
        OR
        {
            ANY
            1
            2
            3
        }
        {
            4
            5
            6
        }
    }
    {
        OR
        7
        8
    }
    {
        OR
        9
        10
    }
}

->

{
    ANY
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
}


Operator Negation

{
    NOT
    {
        AND
        1
        2
    }
}

->

{
    NAND
    1
    2
}
{
    NOT
    {
        OR
        1
        2
    }
}

->

{
    NOR
    1
    2
}
{
    NOT
    {
        XNOR
        1
        2
    }
}

->

{
    XOR
    1
    2
}
{
    NOT
    {
        XOR
        1
        2
    }
}

->

{
    XNOR
    1
    2
}
{
    NOT
    {
        NAND
        1
        2
    }
}

->

{
    AND
    1
    2
}
{
    NOT
    {
        NOR
        1
        2
    }
}

->

{
    OR
    1
    2
}


Previous: Expression Evaluation Arguments

Next: Glossary

Back to top