Documentation
¶
Index ¶
- Constants
- Variables
- func WithFunctions(funcs Functions) treeOption
- func WithObjects(objects Objects) 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) Number() Number
- func (b Bool) Or(other Value) Bool
- func (b Bool) String() string
- type Booler
- type Dot
- type Evaler
- type Function
- type FunctionalValue
- type Functions
- type Member
- type MultiValue
- type Number
- func (n Number) Add(other Value) Value
- func (n Number) AsString() String
- func (n Number) Bool() Bool
- 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 Object
- type ObjectValue
- type Objects
- 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) RawString() string
- 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 ObjectGetProperty(obj Object, name string) (Value, bool)
- 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 ToValue(value any) 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 WithObjects ¶
func WithObjects(objects Objects) treeOption
WithObjects is a functional parameter for Tree evaluation. It provides user-defined Objects. These objects can carry both properties and methods that can be accessed by gal in place of variables and 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 ¶
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 Dot ¶
type Dot[T Member] struct { Member T // must be a Method (i.e. Function) or a Property name (i.e. Variable) }
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 ObjectGetMethod ¶
func ObjectGetMethod(obj Object, name string) (FunctionalValue, bool)
func (FunctionalValue) String ¶
func (fv FunctionalValue) String() string
type Functions ¶
type Functions map[string]FunctionalValue
Functions holds the definition of user-defined functions.
type MultiValue ¶
type MultiValue struct { Undefined // contains filtered or unexported fields }
MultiValue is a container of zero or more Value's. 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. ... perhaps along the lines of [[v1 v2 ...]] or simply a built-in function such as ... 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 ¶
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 NewNumberFromInt ¶
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 Object ¶
type Object any
Object holds user-defined objects that can carry properties and functions that may be referenced within a gal expression during evaluation.
type ObjectValue ¶
type Objects ¶
Objects is a collection of Object's in the form of a map which keys are the name of the object and values are the actual Object's.
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(...), WithObjects(...)) 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.
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
}
Undefined is a special gal.Value that indicates an undefined evaluation outcome.
This can be as a first class citizen, when an error occurs (e.g. a '/' operator without the left hand side).
All implementors of gal.Value also encapsulate an Undefined value. This ensures a default behaviour as defined by "Undefined" when none is available on the implementor. For instance, Bool does not support RShift() and does not implement it. However, since Bool encapsulates an Undefined value, it will return an Undefined value when RShift() is called on it.
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 }
func ObjectGetProperty ¶
TODO: implement support for nested structs?