vera

package module
v0.0.0-...-5aecfc3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 2, 2019 License: MIT Imports: 6 Imported by: 0

README

vera

Installation

go get https://github.com/Ro5bert/vera

The CLI binary can be built from the cli folder; for example:

cd $(go env GOPATH)/src/github.com/Ro5bert/vera/cli && go build -o vera main.go
Syntax
0 False
1 True
a-z, A-Z Statement
(...) Grouping/explicit binary operator precedence
! Negate
& AND
| OR
^ XOR
> Conditional/Implication
= Bi-conditional/Equality/IFF
Limitations
  • There is a maximum of 52 atomic statements (26 lowercase letters + 26 uppercase letter = 52). (Although, I am not sure why or how you would have 52 atomic statements... performance is O(2n) where n is number of atomic statements).
  • To avoid subjectivity in operator precedence, all binary operators are assigned equal precedence and must be parenthesized as necessary (even if the operators are all AND, for example); this may change in the future. (Negation, of course, still has higher precedence than all binary operators.)
Sample CLI Output
Sample CLI Output
Use as a Library Example
// Assuming "github.com/Ro5bert/vera" is imported

stmt, truth, err := vera.Parse("a > b")
if err != nil {
    // handle error
}
// Iterate over all sets of truth values and print out the stmt evaluated at each set:
for ; truth.Val < (1 << len(truth.Names)); truth.Val++ {
    fmt.Println(stmt.Eval(truth))
}

Documentation

Overview

vera is a package for parsing logical expressions.

Index

Constants

This section is empty.

Variables

View Source
var ASCIIBoxCS = &CharSet{
	RowSep:   "-",
	ColSep:   "|",
	Center:   "+",
	TopT:     "+",
	BottomT:  "+",
	LeftT:    "+",
	RightT:   "+",
	TLCorner: "+",
	TRCorner: "+",
	BLCorner: "+",
	BRCorner: "+",
}

ASCIIBoxCS is a CharSet using only ASCII characters.

View Source
var PrettyBoxCS = &CharSet{
	RowSep:   "─",
	ColSep:   "│",
	Center:   "┼",
	TopT:     "┬",
	BottomT:  "┴",
	LeftT:    "├",
	RightT:   "┤",
	TLCorner: "┌",
	TRCorner: "┐",
	BLCorner: "└",
	BRCorner: "┘",
}

PrettyBoxCS is a CharSet using Unicode box drawing characters.

Functions

func Parse

func Parse(input string) (Stmt, Truth, error)

Parse parses the given input string, returning a Stmt which can then be evaluated at certain sets of truth values using the given Truth. An error is also returned in the case of failure.

func RenderTT

func RenderTT(stmt Stmt, truth Truth, out io.Writer, cs *CharSet, colorize bool) error

RenderTT writes a truth table for the given Stmt/Truth pair to the given io.Writer. The appearance of the table is dictated by the given CharSet and colorize parameter.

Types

type CharSet

type CharSet struct {
	RowSep   string
	ColSep   string
	Center   string
	TopT     string
	BottomT  string
	LeftT    string
	RightT   string
	TLCorner string
	TRCorner string
	BLCorner string
	BRCorner string
}

CharSet is a set of characters for rendering a table via RenderTT.

type Stmt

type Stmt interface {
	fmt.Stringer
	Eval(Truth) bool
}

type Truth

type Truth struct {
	Val uint64

	Names []byte
	// contains filtered or unexported fields
}

Truth represents a set of truth values. The truth values are represented by Val, which is treated like a bit field where each bit represents whether the corresponding atomic statement is true (1) or false (0). The bits are in alphabetical order such that, if all 52 atomic statements are used, the 0th bit corresponds to 'z' and the 51st bit corresponds to 'A'. However, if some of the 52 possible atomic statements are not used, they will not be included in the bit field (e.g if only 'a' and 'G' are used, the 0th bit will correspond to 'a' and the 1st bit will correspond to 'G'; the remaining bits are meaningless). As a result of the truth values being represented as a uint64, it is very easy to iterate over all possible truth values for a statement; for example:

stmt, t, err := vera.Parse(...)
// check err
for t.Val = 0; t.Val < 1 << len(t.Names); t.Val++ {
	// Do something with t such as call stmt.Eval.
}

If the names associated with each bit value are needed, they are stored in the Names slice which uses the same indexing scheme as the bits in Val (e.g. t.Val&(1<<i)>0 accesses the value of the statement named t.Names[i] for some Truth t and integer i < len(t.Names)).

func (Truth) String

func (t Truth) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL