Documentation
¶
Index ¶
- Variables
- func Add(a, b interface{}) (interface{}, error)
- func Cast(rv reflect.Value, as reflect.Type) (reflect.Value, error)
- func Convert(a, b interface{}) (interface{}, interface{}, error)
- func Equals(a, b interface{}) (bool, error)
- func GetBool(v interface{}) (bool, error)
- func GetBoolRaw(v interface{}) (bool, bool)
- func GetFloat(v interface{}) (float64, error)
- func GetFloatRaw(v interface{}) (float64, bool)
- func GetInt(v interface{}) (int, error)
- func GetIntRaw(v interface{}) (int, bool)
- func GetString(v interface{}) (string, error)
- func GetStringRaw(v interface{}) (string, bool)
- func GetValue(v interface{}) interface{}
- func IsStackEmpty(err error) bool
- func Subtract(a, b interface{}) (interface{}, error)
- type BiCalculation
- type BiOpDef
- type BiOpDefBuilder
- type Bool
- type Calculator
- type Float
- type Instruction
- type Int
- type KeyValue
- type MonoCalculation
- type MonoOpDef
- type MonoOpDefBuilder
- type String
- type Task
- type Value
Constants ¶
This section is empty.
Variables ¶
var ( Swap = swap{} Dup = dup{} Drop = drop{} Over = over{} Rot = rot{} Dump = dump{} )
Functions ¶
func Cast ¶
Cast takes a Value and attempt to cast it to a specific Type. This will attempt to use our conversions for Int, Float, Bool, String etc. as part of that conversion.
func Convert ¶
func Convert(a, b interface{}) (interface{}, interface{}, error)
Convert converts 'a' and 'b' so that they are of the same type. e.g. if 'a' is a float then this will ensure both are float's. Same for int, string or bool.
If the conversion cannot take place then this will return an error.
Special case: If 'a' is an int but 'b' is a float then this will convert 'a' to a float.
This is to allow "for i=0; i<10; i=i+0.5" to work because in the increment "i=i+0.5" "i" is an int and if we convert 0.5 to an int then we get 0 and an infinite loop.
func GetBool ¶
GetBool converts v to a bool, returning an error if it cannot do the conversion.
For int this returns true if it's not 0.
For float this returns true if |float| > 1e-9 (to account for rounding errors)
For string this returns true if "true", "yes", "t" or "y", and false if "false", "no", "f" or "n".
func GetBoolRaw ¶
GetBoolRaw returns a bool if v is a bool or implements Bool
func GetFloat ¶
GetFloat returns v as a float64. If v is a float64 it will return it. If v is an int or int64 it will return that value as a float64. If v implements the Float interface then it will use that for the result. If v is a string it will parse it. The bool is false if a float64 cannot be returned.
func GetFloatRaw ¶
GetFloatRaw returns v as a float64 if it's a form of float.
func GetInt ¶
GetInt returns v as an int. If v is an int or int64 it will return that value. If v implements the Int interface then it will use that for the result. If v is a float64 it will return it as an int. If v is a string it will parse it. The bool is false if an integer cannot be returned.
func GetString ¶
GetString returns v as a string. If v is string or implements the String interface then it will use that result. If an int, int64, float64 then it will return that value as a string. If a bool then "true" or "false" is returned. Returns "",false if the value could not be converted to a string.
func GetStringRaw ¶
GetStringRaw returns v as a string if it's a string or implements String. Returns "",false if the value is not a string.
func GetValue ¶
func GetValue(v interface{}) interface{}
GetValue returns v calling v.Value() if it supports the Value interface
func IsStackEmpty ¶
Types ¶
type BiCalculation ¶
type BiCalculation interface {
BiCalculate(a, b interface{}) (interface{}, error)
}
BiCalculation performs an operation against two values
type BiOpDef ¶
type BiOpDef struct {
// contains filtered or unexported fields
}
BiOpDef implements an operation whose behaviour depends on the type of the left hand side
func (*BiOpDef) BiCalculate ¶
type BiOpDefBuilder ¶
type BiOpDefBuilder interface { Int(func(a, b int) (interface{}, error)) BiOpDefBuilder Float(func(a, b float64) (interface{}, error)) BiOpDefBuilder String(func(a, b string) (interface{}, error)) BiOpDefBuilder Bool(func(a, b bool) (interface{}, error)) BiOpDefBuilder Build() *BiOpDef }
func NewBiOpDef ¶
func NewBiOpDef() BiOpDefBuilder
NewBiOpDef creates a new NewBiOp based on the provided
type Bool ¶
type Bool interface {
Bool() bool
}
Bool is an instance that can return its value as a bool
type Calculator ¶
type Calculator interface { Reset() Calculator // Push a value onto the stack Push(v interface{}) Calculator // Pop a value from the stack. Return an error if the stack is empty // Returns an error if the stack is empty. Pop() (interface{}, error) // Pop2 returns two values from the stack. // The order returned (a,b) is if b was the top value on the stack whilst a was below it. // Returns an error if the stack is empty. Pop2() (interface{}, interface{}, error) // Peek returns the top value on the stack, the stack is unchanged // Returns an error if the stack is empty. Peek() (interface{}, error) // Swap the top two entries on the stack. // Return an error if the stack does not have two entries to swap. Swap() error // Rot rotates third itm to top [n3 n2 n1] -> [n2 n1 n3] Rot() error // Over copies second item to top [n2 n1] -> [n2 n1 n2] Over() error // Dup duplicates the top entry on the stack // Returns an error if the stack is empty. Dup() error // Drop removes the top entry on the stack // Returns an error if the stack is empty. Drop() error // Op1 performs a named operation using the top entry on the stack, returning // the result to the stack. // // Returns an error if the stack doesn't have an entry or the calculation fails Op1(op string) error // Op2 performs a named operation using the top two entries on the stack, returning // the result to the stack. // // Returns an error if the stack doesn't have two entries or the calculation fails Op2(op string) error // Calculate executes a Calculation against this calculator. // // It ensures that the stack is valid for just this calculation, preserving // any existing stack for the calculation that is executing this one. // Returns an error if the calculation fails. // // Returns the top value of the stack at the end of the calculation, // or nil if the stack was empty. // // The boolean returned is true if a value was returned, false if not, allowing for nil // value to be returned from the calculation. Calculate(t Task) (interface{}, bool, error) // MustCalculate is the same as Calculate but an error is returned if no result was returned MustCalculate(t Task) (interface{}, error) // Exec is similar to Calculate but does not return a result. Exec(t Task) error // Process processes a series of Instruction's to perform a calculation Process(instructions ...Instruction) error // Dump returns the current stack as a string. // Used for debugging Dump() string }
type Float ¶
type Float interface {
Float() float64
}
Float is an instance that can return its value as a float64
type Instruction ¶
type Instruction interface {
Invoke(c Calculator) error
}
func Op2 ¶
func Op2(op string) Instruction
func Push ¶
func Push(v interface{}) Instruction
type KeyValue ¶
type KeyValue struct {
// contains filtered or unexported fields
}
func GetKeyValue ¶
func NewKeyValue ¶
type MonoCalculation ¶
type MonoCalculation interface {
MonoCalculate(a interface{}) (interface{}, error)
}
MonoCalculation performs an operation against two values
type MonoOpDef ¶
type MonoOpDef struct {
// contains filtered or unexported fields
}
MonoOpDef implements an operation whose behaviour depends on the type of the left hand side
func (*MonoOpDef) MonoCalculate ¶
type MonoOpDefBuilder ¶
type MonoOpDefBuilder interface { Int(func(a int) (interface{}, error)) MonoOpDefBuilder Float(func(a float64) (interface{}, error)) MonoOpDefBuilder String(func(a string) (interface{}, error)) MonoOpDefBuilder Bool(func(a bool) (interface{}, error)) MonoOpDefBuilder Build() *MonoOpDef }
func NewMonoOpDef ¶
func NewMonoOpDef() MonoOpDefBuilder
NewMonoOpDef creates a new NewBiOp based on the provided