Skip to content

Ashu11-A/truth-table

Repository files navigation

Truth Table

license-info stars-infoa Last-Comitt

Comitts Year reposize-info

npm package minimized gzipped size

πŸ” | What can this do?

This project is based on AST (Abstract Syntax Tree). It can both generate the AST and validate the syntax of the input, transforming it into a consumable data structure to generate any type of representation, such as CSV tables. In case of an error, it is possible to handle the error, allowing the display of the line and column where a syntax error is present. Below is an example of how to work with this library:

πŸ’« Main Features:

  • Syntax validation and error location reporting
  • Abstract Syntax Tree (AST) generation
  • Support for generating truth tables from parsed logic expressions
  • Flexible output formats (e.g., CSV)

πŸš€ | Getting Started

To get a local copy up and running, follow these simple steps:

Prerequisites Node.js (v20+ recommended)

Installation

npm i truth-table-ast
#OR
npm i -g truth-table-ast

πŸ“š | Usage

After installing this package globally, you can use the following commands:

πŸ“Ÿ | Terminal:

tt -p "p Λ… (p ^ q)"
# Or use this if your operating system doesn't detect the package
npx truth-table -p "p Λ… (p ^ q)" -o table.csv

πŸ“„ | Help:

Usage: tt [options]

  Options:

   -h --help        Show all available arguments
   -o --output      File where the truth table will be saved
   -d --display     How the data will be displayed in the table, supports: boolean, number
   -t --type        Type of file the table will be saved in (csv | text)
   -p --proposition Define proposition to generate truth table

πŸ‘¨β€πŸ’» | Code:

Here's an example of how to work with this library:

// ESM:
import { AST, Structure, Table } from 'truth-table-ast'
// Communjs:
// const { AST, Structure, Table } = require('truth-table-ast')

// In communjs, you'll need to put the code in an anonymous function for the asynchronous functions to work
// void (async () => {
  // code here
// })()

const input = 'p ^ (p v ~q)'
const parser = new AST(input)
const ast = parser.parse()

if (isError(ast)) throw new Error(JSON.stringify(ast, null, 2))
await parser.save('ast.json')

const structure = new Structure(ast).generate()
await structure.save('structure.json')

const table = new Table({
  structure,
  display: 'boolean',
  // type: 'csv'
})//.create('table.csv')

// const content = table.csv()
// const content = table.markdown()

table.type = 'markdown'
await table.create('table.md')

table.type = 'csv'
await table.create('table.csv')

✨ | Outputs

πŸ“œ | AST:

The AST (Abstract Syntax Tree) generated from the input of p ^ (p v ~q):

[
  {
    "value": "p",
    "type": "Proposition",
    "negatived": false,
    "loc": {
      "start": {
        "line": 1,
        "column": 0
      },
      "end": {
        "line": 1,
        "column": 0
      }
    }
  },
  {
    "type": "Operation",
    "value": "^",
    "key": "Conjunction",
    "loc": {...}
  },
  {
    "type": "SubExpression",
    "body": [
      {
        "value": "p",
        "type": "Proposition",
        "negatived": false,
        "loc": {...}
      },
      {
        "type": "Operation",
        "value": "v",
        "key": "Disjunction",
        "loc": {...}
      },
      {
        "value": "q",
        "type": "Proposition",
        "negatived": true,
        "loc": {...}
      }
    ],
    "negatived": false,
    "loc": {...}
  }
]

πŸ“ƒ | Structure:

The structured data generated from the AST to generate the truth table:

[
  {
    "type": "Variable",
    "element": "p",
    "value": true,
    "column": 0,
    "row": 0,
    "position": "0x0"
  },
  {
    "type": "Variable",
    "element": "q",
    "value": true,
    "column": 1,
    "row": 0,
    "position": "0x1"
  },
  {
    "type": "VariableNegative",
    "element": "~q",
    "value": false,
    "column": 2,
    "row": 0,
    "position": "0x2"
  },
  {
    "type": "Result",
    "element": "(p v ~q)",
    "value": true,
    "column": 3,
    "row": 0,
    "position": "0x3"
  },
  {
    "type": "Result",
    "element": "p ^ (p v ~q)",
    "value": true,
    "column": 4,
    "row": 0,
    "position": "0x4"
  },
  {...}
]

πŸ“‹ | Truth Table:

p q ~q (p v ~q) p ^ (p v ~q)
true true false true true
true false true true true
false true false false false
false false true true false

🀝 | Contributing

Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

πŸ“ | License

Distributed under the MIT License. See LICENSE.txt for more information.