boolal

package module
v0.0.0-...-b1bfa0f Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2023 License: Apache-2.0 Imports: 1 Imported by: 1

README

boolean algebra

test Go Report Card codecov

Evaluate a boolean expression.

Usage

import ba "github.com/lesomnus/boolal"

func Expression() {
	data := map[string]bool{"t": true}
	expr, err := ba.ParseString("t & f | !(t | f)")
	if err != nil {
		panic(err)
	}

	ok := expr.Eval(data)
	// ok == false
}

func Manipulation() {
	data := map[string]bool{"t": true}
	expr := ba.And("t", "f").Or(ba.Not(ba.Or("t", "f")))

	ok := expr.Eval(data)
	// ok == false
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BinaryOp

type BinaryOp struct {
	And []UnaryOp `parser:"  ( '&' @@ )+"`
	Or  []UnaryOp `parser:"| ( '|' @@ )+"`
}

type EvalOption

type EvalOption struct {
	FallbackValue bool
}

type EvalOptionModifier

type EvalOptionModifier func(o *EvalOption)

func WithFallbackAs

func WithFallbackAs(v bool) EvalOptionModifier

func WithOption

func WithOption(opt EvalOption) EvalOptionModifier

type Expr

type Expr struct {
	Lhs UnaryOp    `parser:"  @@"`
	Rhs []BinaryOp `parser:"@@*"`
}

func And

func And(lhs any, rhs any, terms ...any) *Expr

func Or

func Or(lhs any, rhs any, terms ...any) *Expr

func ParseString

func ParseString(expr string) (*Expr, error)

func (*Expr) And

func (a *Expr) And(term any, terms ...any) *Expr

func (*Expr) Eval

func (e *Expr) Eval(data map[string]bool, modifiers ...EvalOptionModifier) bool

func (*Expr) Or

func (a *Expr) Or(term any, terms ...any) *Expr

type UnaryOp

type UnaryOp struct {
	Name string `parser:"@( '!' )?"`
	Var  string `parser:"(   @Ident"`
	Al   *Expr  `parser:"  | '(' @@ ')' )"`
}

func Falsity

func Falsity() UnaryOp

func Not

func Not(term any) UnaryOp

func Truth

func Truth() UnaryOp

func Var

func Var(v string) UnaryOp

Jump to

Keyboard shortcuts

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