• Function to evaluate a tokenized expression. This algorithm is an adaptation of the Shunting Yard algorithm to evaluate expressions with logical operators.

    There is no need to verify the correctness of the logical expression before calling the evaluate function, as this will be checked during the evaluation. If the expression is incorrect, an error will be thrown automatically.

    Example of correct logical expression: "true and false".

    Example of incorrect logical expression: "true true and false".

    Logical operators supported: and, or, not, xor. All other existing logical operators (nand, nor, xnor) can be generated using the supported logical operators.

    Parameters

    • tokens: string[]

      Tokens of the expression. The tokens can be boolean values, logical operators or parentheses.

    Returns boolean

    The boolean value after evaluating the expression.

    Example

    // Example usage:
    import { evaluate } from "@zk-kit/logical-expressions"

    const expression = ["true", "and", "false"]

    const result = evaluate(expression)

    console.log(result) // Output: false