Documentation ¶
Index ¶
- func Execute(args ...string) (*calculathor.Token, error)
- func GetVarTypeString(t TVarType) string
- func RandInt(args ...string) (*calculathor.Token, error)
- func Tokenize(strToParse string) ([]calculathor.Token, [2]uint8)
- type TVarType
- type TVariable
- type TVariableList
- func (tv *TVariableList) AddVariable(name, value string) error
- func (tv *TVariableList) BulkAddVariables(input [][2]string) error
- func (tv *TVariableList) ExecFunctionString(v *TVariable) error
- func (tv *TVariableList) GetProcessedVarmanCopy() (*TVariableList, error)
- func (tv *TVariableList) GetVarmanCopy() (*TVariableList, error)
- func (tvl *TVariableList) Hash() (string, error)
- func (tv *TVariableList) ProcessVariable(v *TVariable) error
- func (tv *TVariableList) RenderStringWithVariables(str string) (string, error)
- func (tv *TVariableList) ReplaceVariable(tvar *TVariable) (int8, error)
- func (tv *TVariableList) ReplaceVarsInTokenString(v *TVariable) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Execute ¶
func Execute(args ...string) (*calculathor.Token, error)
Execute function from token - array of [name, arg1, arg2, ...]
@param args []string Array of [funcName, arg1, arg2, ...]
func GetVarTypeString ¶
Get variable type as string to print out
@param t TVarType Variable structure type
func RandInt ¶
func RandInt(args ...string) (*calculathor.Token, error)
Random integer value in range from 0 to max
@param args []string 1st arg is max value, 2nd arg is min value (optional)
func Tokenize ¶
func Tokenize(strToParse string) ([]calculathor.Token, [2]uint8)
Execute tokenizer on string, create stream and pass it to processToken
@param strToParse string String to parse
@return []Token Array of tokens, nil if error
@return [2]uint8 Count of variables ([0]) and functions ([1]) in token string
Types ¶
type TVariable ¶
type TVariable struct { Unique bool // should generate unique value for each instance FlagNotExec bool // don't execute function FlagNotSum bool // don't sum polynomial FlagStatic bool // static, only generate once and copy the value to all descendants Skip bool // skip this variable processing, keep as is ToReplace uint8 // number of variable tokens used in expression ToExecute uint8 // executed all functions, no variables or functions left Evaluated bool // math expression evaluated Generated bool // variable was generated VarType TVarType Content []calculathor.Token }
TVariable: Store single variable data during processing
func (*TVariable) PrintVariable ¶
Print variable type and print all tokens
@param v *TVariable Verbose print
@param verbose bool
type TVariableList ¶
type TVariableList struct { Variables map[string]*TVariable MathMode bool // turn off math mode parsing (polynomials, fractions) Count int // current number of variables }
func NewVarman ¶
func NewVarman() *TVariableList
NewVarman
Create new variable manager structure ¶
@return *TVariableList
func (*TVariableList) AddVariable ¶
func (tv *TVariableList) AddVariable(name, value string) error
AddVariable
Add new variable (name, value pair) to the variable list ¶
Value is tokenized by `github.com/bzick/tokenizer`, analyzed and processed later
Variable name can contain flags after ':' like this:"variable:ng"
- o - keep in original shape, don't process anything, mark as processed
- r - keep as string, don't apply
- s - don't sum polynomials, keep as is
- p - randomize polynomial position
@example vlist.addVariable("name", "2*{{randInt(42)}} + 1")
@param name string Variable name with flags `variable:ng`
@param value string Variable value with functions and variables, translated into string of tokens `random(1,10) + $variable + x + 42`
@return error
func (*TVariableList) BulkAddVariables ¶
func (tv *TVariableList) BulkAddVariables(input [][2]string) error
BulkAddVariables
Executes `AddVariable` for multiple variables in format [][2]string{{"name", "value"}, {"name2", "value2"}} ¶
See `AddVariable` for more information ¶
@param input [][2]string [Variable name, variable value]
@example
err := vlist.BulkAddVariables([][2]string{ {"v4riabl3:o", "2*{{randInt(42)}} + 1"}, {"prom3nn4:o", "42 + {{v4riabl3}}"}, })
@return error
func (*TVariableList) ExecFunctionString ¶
func (tv *TVariableList) ExecFunctionString(v *TVariable) error
ExecFunctionString
Replace function calls (e.g. generate values) in variable token list ¶
Edits variable content in place ¶
@param tv `*TVariableList` Variable list
@param v `*TVariable` Variable to process
@return error unknown function or invalid arguments
func (*TVariableList) GetProcessedVarmanCopy ¶
func (tv *TVariableList) GetProcessedVarmanCopy() (*TVariableList, error)
GetProcessedVarmanCopy
Process all variables in list by rules defined in options (replace variables, sum polynomials, etc.) ¶
@receiver tv `*TVariableList` Variable list
@return (*varman, error) Return duplicate of variable list with processed variables or error if something went wrong (like missing variable)
func (*TVariableList) GetVarmanCopy ¶
func (tv *TVariableList) GetVarmanCopy() (*TVariableList, error)
GetVarmanCopy
Create deep memory copy of variable list ¶
@receiver tv `*TVariableList` Variable list
@return *TVariableList
@return error
func (*TVariableList) Hash ¶
func (tvl *TVariableList) Hash() (string, error)
Create hash using variable name and content pairs
@return string Hash
func (*TVariableList) ProcessVariable ¶
func (tv *TVariableList) ProcessVariable(v *TVariable) error
ProcessVariable
Process single variable by rules defined in options (replace variables, sum polynomials, etc.) ¶
@receiver tv `*TVariableList` Variable list
@param v `*TVariable` Variable to process
@return error
func (*TVariableList) RenderStringWithVariables ¶
func (tv *TVariableList) RenderStringWithVariables(str string) (string, error)
RenderStringWithVariables
Render generated and calculated variables to given string ¶
@return (string, error) Processed string with variables replaced or error if something went wrong
func (*TVariableList) ReplaceVariable ¶
func (tv *TVariableList) ReplaceVariable(tvar *TVariable) (int8, error)
ReplaceVariable
Replace variable tokens with existing variables ¶
@param tv `*TVariableList` Variable list
@param tvar `*TVariable` Variable to replace
@return int8 Number of replaced variables
@return (int8, error) Number of replaced variables and error if something went wrong
func (*TVariableList) ReplaceVarsInTokenString ¶
func (tv *TVariableList) ReplaceVarsInTokenString(v *TVariable) error
ReplaceVarsInTokenString
Find variable tokens in string of tokens and replace them with their values ¶
@receiver tv `*TVariableList` Variable list
@param t []Token Array of tokens
@return []Token New array of tokens with replaced variables or nil if no changes were made
@return error