boolean-expression-ast
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

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-ast

Usage

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: 0

API

Constructor

new AST(expression: string)

Methods

  • toString(): string — Returns a human-readable string representation
  • toLatex(): string — Returns a LaTeX representation
  • evaluate(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

Package Sidebar

Install

npm i boolean-expression-ast

Weekly Downloads

11

Version

1.0.1

License

MIT

Unpacked Size

35.2 kB

Total Files

7

Last publish

Collaborators

  • henri_schulz