Documentation
¶
Index ¶
- Constants
- Variables
- func WithFunctions(funcs Functions) treeOption
- func WithVariables(vars Variables) treeOption
- type Bool
- func (b Bool) And(other Value) Bool
- func (b Bool) AsString() String
- func (b Bool) Bool() Bool
- func (b Bool) Equal(other Bool) bool
- func (b Bool) EqualTo(other Value) Bool
- func (b Bool) Not() Bool
- func (b Bool) NotEqualTo(other Value) Bool
- func (b Bool) Or(other Value) Bool
- func (b Bool) String() string
- type Booler
- type Evaler
- type Function
- type FunctionalValue
- type Functions
- type MultiValue
- type Number
- func (n Number) Add(other Value) Value
- func (n Number) AsString() String
- func (n Number) Cos() Number
- func (n Number) Divide(other Value) Value
- func (n Number) Equal(other Number) bool
- func (n Number) EqualTo(other Value) Bool
- func (n Number) Factorial() Value
- func (n Number) Float64() float64
- func (n Number) Floor() Number
- func (n Number) GreaterThan(other Value) Bool
- func (n Number) GreaterThanOrEqual(other Value) Bool
- func (n Number) Int64() int64
- func (n Number) IntPart() Value
- func (n Number) LShift(other Value) Value
- func (n Number) LessThan(other Value) Bool
- func (n Number) LessThanOrEqual(other Value) Bool
- func (n Number) Ln(precision int32) Value
- func (n Number) Log(precision int32) Value
- func (n Number) Mod(other Value) Value
- func (n Number) Multiply(other Value) Value
- func (n Number) Neg() Number
- func (n Number) NotEqualTo(other Value) Bool
- func (n Number) Number() Number
- func (n Number) PowerOf(other Value) Value
- func (n Number) RShift(other Value) Value
- func (n Number) Sin() Number
- func (n Number) Sqrt() Value
- func (n Number) String() string
- func (n Number) Sub(other Value) Value
- func (n Number) Tan() Number
- func (n Number) Trunc(precision int32) Number
- type Numberer
- type Operator
- type String
- func (s String) Add(other Value) Value
- func (s String) AsString() String
- func (s String) Equal(other String) bool
- func (s String) EqualTo(other Value) Bool
- func (s String) Eval() Value
- func (s String) GreaterThan(other Value) Bool
- func (s String) GreaterThanOrEqual(other Value) Bool
- func (s String) LShift(Value) Value
- func (s String) LessThan(other Value) Bool
- func (s String) LessThanOrEqual(other Value) Bool
- func (s String) Multiply(other Value) Value
- func (s String) NotEqualTo(other Value) Bool
- func (s String) Number() Number
- func (s String) RShift(Value) Value
- func (s String) String() string
- type Stringer
- type Tree
- func (tree Tree) Calc(isOperatorInPrecedenceGroup func(Operator) bool, cfg *treeConfig) Tree
- func (tree Tree) CleanUp() Tree
- func (tree Tree) Eval(opts ...treeOption) Value
- func (tree Tree) FullLen() int
- func (tree Tree) Split() []Tree
- func (tree Tree) String(indents ...string) string
- func (tree Tree) TrunkLen() int
- type TreeBuilder
- type Undefined
- func (Undefined) Add(Value) Value
- func (Undefined) And(other Value) Bool
- func (u Undefined) AsString() String
- func (Undefined) Divide(Value) Value
- func (u Undefined) Equal(other Undefined) bool
- func (u Undefined) EqualTo(other Value) Bool
- func (u Undefined) GreaterThan(other Value) Bool
- func (u Undefined) GreaterThanOrEqual(other Value) Bool
- func (Undefined) LShift(Value) Value
- func (u Undefined) LessThan(other Value) Bool
- func (u Undefined) LessThanOrEqual(other Value) Bool
- func (Undefined) Mod(Value) Value
- func (Undefined) Multiply(Value) Value
- func (u Undefined) NotEqualTo(other Value) Bool
- func (Undefined) Or(other Value) Bool
- func (Undefined) PowerOf(Value) Value
- func (Undefined) RShift(Value) Value
- func (u Undefined) String() string
- func (Undefined) Sub(Value) Value
- type Value
- func Cos(args ...Value) Value
- func Eval(args ...Value) Value
- func Factorial(args ...Value) Value
- func Floor(args ...Value) Value
- func Ln(args ...Value) Value
- func Log(args ...Value) Value
- func Pi(args ...Value) Value
- func PiLong(args ...Value) Value
- func Sin(args ...Value) Value
- func Sqrt(args ...Value) Value
- func Tan(args ...Value) Value
- func Trunc(args ...Value) Value
- type Variable
- type Variables
Constants ¶
const Pi51199 = "" /* 51199-byte string literal not displayed */
Pi51197 returns Pi with 51197 decimal digits.
Variables ¶
var ( False = NewBool(false) True = NewBool(true) )
Functions ¶
func WithFunctions ¶
func WithFunctions(funcs Functions) treeOption
WithFunctions is a functional parameter for Tree evaluation. It provides user-defined functions.
func WithVariables ¶
func WithVariables(vars Variables) treeOption
WithVariables is a functional parameter for Tree evaluation. It provides user-defined variables.
Types ¶
type Bool ¶
type Bool struct {
Undefined
// contains filtered or unexported fields
}
func NewBoolFromString ¶ added in v8.1.0
TODO: another option would be to return a Value and hence allow Undefined when neither True nor False is provided.
func (Bool) Equal ¶
Equal satisfies the external Equaler interface such as in testify assertions and the cmp package
func (Bool) NotEqualTo ¶
type Function ¶
type Function struct {
Name string
BodyFn FunctionalValue
Args []Tree
}
func NewFunction ¶
func NewFunction(name string, bodyFn FunctionalValue, args ...Tree) Function
type FunctionalValue ¶
func BuiltInFunction ¶
func BuiltInFunction(name string) FunctionalValue
BuiltInFunction returns a built-in function body if known. It returns `nil` when no built-in function exists by the specified name. This signals the Evaluator to attempt to find a user defined function.
func UserDefinedFunction ¶
func UserDefinedFunction(name string, userFunctions Functions) FunctionalValue
UserDefinedFunction is a helper function that returns the definition of the provided function name from the supplied userFunctions.
func (FunctionalValue) String ¶
func (fv FunctionalValue) String() string
type Functions ¶
type Functions map[string]FunctionalValue
Functions holds the definition of user-defined functions.
func (Functions) Function ¶
func (f Functions) Function(name string) FunctionalValue
Function returns the function definition of the function of the specified name.
type MultiValue ¶
type MultiValue struct {
Undefined
// contains filtered or unexported fields
}
MultiValue is a container of zero or more Value's. TODO: impose a minimum of 1 value? For the time being, it is only usable and useful with functions. Functions can accept a MultiValue, and also return a MultiValue. This allows a function to effectively return multiple values as a MultiValue. TODO: we could add a syntax to instantiate a MultiValue within an expression. TODO: ... perhaps along the lines of [[v1 v2 ...]] or simply a built-in function such as TODO: ... MultiValue(...) - nothing stops the user from creating their own for now :-)
TODO: implement other methods such as Add, LessThan, etc (if meaningful)
func NewMultiValue ¶
func NewMultiValue(values ...Value) MultiValue
func (MultiValue) AsString ¶ added in v8.1.0
func (m MultiValue) AsString() String
func (MultiValue) Equal ¶
func (m MultiValue) Equal(other MultiValue) bool
Equal satisfies the external Equaler interface such as in testify assertions and the cmp package Note that the current implementation defines equality as values matching and in order they appear.
func (MultiValue) Get ¶
func (m MultiValue) Get(i int) Value
func (MultiValue) Size ¶
func (m MultiValue) Size() int
func (MultiValue) String ¶
func (m MultiValue) String() string
type Number ¶
type Number struct {
Undefined
// contains filtered or unexported fields
}
func NewNumberFromFloat ¶
func NewNumberFromString ¶
func (Number) Equal ¶
Equal satisfies the external Equaler interface such as in testify assertions and the cmp package
func (Number) GreaterThan ¶
func (Number) GreaterThanOrEqual ¶
func (Number) LessThanOrEqual ¶
func (Number) NotEqualTo ¶
type Operator ¶
type Operator string
const ( Plus Operator = "+" Minus Operator = "-" Multiply Operator = "*" Divide Operator = "/" Modulus Operator = "%" Power Operator = "**" LShift Operator = "<<" RShift Operator = ">>" LessThan Operator = "<" LessThanOrEqual Operator = "<=" EqualTo Operator = "==" NotEqualTo Operator = "!=" GreaterThan Operator = ">" GreaterThanOrEqual Operator = ">=" And Operator = "And" // TODO: case sentive for now And2 Operator = "&&" Or Operator = "Or" // TODO: case sentive for now Or2 Operator = "||" )
type String ¶
type String struct {
Undefined
// contains filtered or unexported fields
}
func (String) Equal ¶
Equal satisfies the external Equaler interface such as in testify assertions and the cmp package
func (String) GreaterThan ¶
func (String) GreaterThanOrEqual ¶
func (String) LessThanOrEqual ¶
func (String) NotEqualTo ¶
type Stringer ¶
type Stringer interface {
AsString() String // name is not String so to not clash with fmt.Stringer interface
}
type Tree ¶
type Tree []entry
func Parse ¶
Example: Parse("blah").Eval(WithVariables(...), WithFunctions(...)) This allows to parse an expression and then use the resulting Tree for multiple evaluations with different variables provided.
func (Tree) Calc ¶
Calc is a reduction operation that calculates the Value of sub-expressions contained in this Tree, based on operator precedence. When isOperatorInPrecedenceGroup returns true, the operator is calculated and the resultant Value is inserted in _replacement_ of the terms (elements) of this Tree that where calculated. For instance, a tree representing the expression '2 + 5 * 4 / 2' with an operator precedence of 'multiplicativeOperators' would read the Tree left to right and return a new Tree that represents: '2 + 10' where 10 was calculated (and reduced) from 5 * 4 = 20 / 2 = 10.
nolint: gocognit,gocyclo,cyclop
func (Tree) Eval ¶
Eval evaluates this tree and returns its value. It accepts optional functional parameters to supply user-defined entities such as functions and variables.
type TreeBuilder ¶
type TreeBuilder struct{}
func NewTreeBuilder ¶
func NewTreeBuilder() *TreeBuilder
type Undefined ¶
type Undefined struct {
// contains filtered or unexported fields
}
func NewUndefined ¶
func NewUndefined() Undefined
func NewUndefinedWithReasonf ¶
func (Undefined) Equal ¶
Equal satisfies the external Equaler interface such as in testify assertions and the cmp package
func (Undefined) GreaterThan ¶
func (Undefined) GreaterThanOrEqual ¶
func (Undefined) LessThanOrEqual ¶
func (Undefined) NotEqualTo ¶
type Value ¶
type Value interface {
// Calculation
Add(Value) Value
Sub(Value) Value
Multiply(Value) Value
Divide(Value) Value
PowerOf(Value) Value
Mod(Value) Value
LShift(Value) Value
RShift(Value) Value
// Logical
LessThan(Value) Bool
LessThanOrEqual(Value) Bool
EqualTo(Value) Bool
NotEqualTo(Value) Bool
GreaterThan(Value) Bool
GreaterThanOrEqual(Value) Bool
And(Value) Bool
Or(Value) Bool
// Helpers
Stringer
fmt.Stringer
// contains filtered or unexported methods
}