boolean-expression-ast
v1.0.1
Published
A library for working with boolean expression ASTs.
Maintainers
Readme
boolean-expression-ast
Parse, manipulate, and evaluate boolean expressions as Abstract Syntax Trees (AST) in TypeScript/JavaScript.
Features
- Parse boolean expressions (supports variables, constants, AND, OR, XOR, NAND, NOR, XNOR, parentheses)
- Generate ASTs for further analysis or transformation
- Evaluate expressions with variable assignments
- Output expressions as strings or LaTeX
Installation
npm install boolean-expression-astUsage
import AST from "boolean-expression-ast";
const expr = "A & (B | !C)";
const ast = new AST(expr);
console.log(ast.toString()); // Outputs: A & (B | !C)
console.log(ast.toLatex()); // Outputs: A \& (B \lor \overline{C})
console.log(ast.evaluate({ A: 1, B: 0, C: 1 })); // Outputs: 0API
Constructor
new AST(expression: string)
Methods
toString(): string— Returns a human-readable string representationtoLatex(): string— Returns a LaTeX representationevaluate(variableValues: Record<string, 0 | 1>): number— Evaluates the expression
Supported Operators
| Symbol | Name | Example | |--------|-----------|--------------| | & | AND | A & B | | | | OR | A | B | | ^ | XOR | A ^ B | | # | NAND | A # B | | $ | NOR | A $ B | | = | XNOR | A = B | | ! | NOT | !A |
License
MIT
