@lusc/truth-table

5.2.0 • Public • Published

@lusc/truth-table

@lusc/truth-table can parse truth statements and generate truth tables. Try it out.

A statement such as A AND B passed to generateTable will generate a table as seen below.

A B A ∧ B
true true true
true false false
false true false
false false false

Though generating tables is what this package was made for, it can easily be used to only parse a truth statement and generate an AST representation of the statement. See parseOperation.

Supported Operators

  • AND
  • OR
  • XOR
  • NOT
  • IFF
  • IFTHEN

See operator-aliases.md for all the aliases.

Variables

A variable is made up of at least one character. It must contain only latin letters (a-z). Variables are case-insensitive. and, or, and so on are reserved and will be treated as operators.

IndexedError

This extends Error and has from and to indicating were in the string there is an error. from is inclusive, to is exclusive. The message describes what the issue is and from and to can be used, for example, to highlight the problematic characters.

parseOperation('(a & b');
/*
IndexedError {
	message: "Unmatched opening parens at position 0.",
	from: 0,
	to: 1
}
*/

parseOperation

function parseOperation(input: string): AST;

If there are invalid characters, unmatched parens, two operators following each other or similar it throws IndexedError. For all other errors Error is thrown.

It returns AST.

operationToString

operationToString takes an AST as input and returns a string representation of the AST. It doesn't return the original input but instead returns a standardised form.

Example:

const ast = parseOperation('a & b');
operationToString(ast); // === "(A ∧ B)"

generateTable

function generateTable(input: string, includeSteps = true): ParsedTable;

type ParsedTable = {
	columns: readonly string[];
	rows: readonly boolean[][];
	ast: AST;
};

input is the operation that will be parsed.

By default, it will show each step to get to the result. For example with a statement like (a & b) | c, it will first contain the columns for the variables, then the column for A ∧ B, then the column for (A ∧ B) ∨ C.

If includeSteps is set to false, it will omit all steps and only return columns of the variables and the full statement.

The input is passed to parseOperation so it will throw the same errors.

Example:

generateTable('a != b');
// returns
{
	columns: ['A', 'B', 'A ↮ B'],
	rows: [
		[ true,  true,  false ],
		[ true,  false, true  ],
		[ false, true,  true  ],
		[ false, false, false ]
	],
	ast: {...}
}

Operator

This is an enum used to identify the type of operation.

It's implementation is pretty straightforward:

enum Operator {
	iff = 'iff',
	ifthen = 'ifthen',
	not = 'not',
	and = 'and',
	xor = 'xor',
	or = 'or',
}

AST

This is a representation of any given truth statement. The shape of the AST is as follows.

For variables

A variable is made of any latin letters. Variables are case insensitive. Additionally, _ is permitted.

A variable matches the regex /^[a-z_]+$/i.

type AST = {
	type: 'variable';
	variable: string;
};

Not-operator

type AST = {
	type: 'operator';
	operator: Operator.not;
	values: [AST];
};

Other operators

type AST = {
	type: 'operator';
	operator: Operator.and | Operator.or | ...; // All operators except Operator.not
	values: [AST, AST];
};

Readme

Keywords

none

Package Sidebar

Install

npm i @lusc/truth-table

Weekly Downloads

1

Version

5.2.0

License

MIT

Unpacked Size

50.4 kB

Total Files

55

Last publish

Collaborators

  • lusc