calculator

package
v0.0.0-...-377a6f8 Latest Latest
Warning

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

Go to latest
Published: May 18, 2024 License: Apache-2.0 Imports: 6 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Swap = swap{}
	Dup  = dup{}
	Drop = drop{}
	Over = over{}
	Rot  = rot{}
	Dump = dump{}
)

Functions

func Add

func Add(a, b interface{}) (interface{}, error)

func Cast

func Cast(rv reflect.Value, as reflect.Type) (reflect.Value, error)

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 Equals

func Equals(a, b interface{}) (bool, error)

func GetBool

func GetBool(v interface{}) (bool, error)

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

func GetBoolRaw(v interface{}) (bool, bool)

GetBoolRaw returns a bool if v is a bool or implements Bool

func GetFloat

func GetFloat(v interface{}) (float64, error)

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

func GetFloatRaw(v interface{}) (float64, bool)

GetFloatRaw returns v as a float64 if it's a form of float.

func GetInt

func GetInt(v interface{}) (int, error)

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 GetIntRaw

func GetIntRaw(v interface{}) (int, bool)

GetIntRaw returns v as an int if v is a form of integer.

func GetString

func GetString(v interface{}) (string, error)

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

func GetStringRaw(v interface{}) (string, bool)

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

func IsStackEmpty(err error) bool

func Subtract

func Subtract(a, b interface{}) (interface{}, error)

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

func (op *BiOpDef) BiCalculate(a0, b0 interface{}) (interface{}, error)

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
}

func New

func New() Calculator

New Calculator

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 Int

type Int interface {
	Int() int
}

Int is an instance that can return its value as an int

type KeyValue

type KeyValue struct {
	// contains filtered or unexported fields
}

func GetKeyValue

func GetKeyValue(v interface{}) (*KeyValue, bool)

func NewKeyValue

func NewKeyValue(key string, value interface{}) *KeyValue

func (*KeyValue) Key

func (v *KeyValue) Key() string

func (*KeyValue) Value

func (v *KeyValue) Value() interface{}

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

func (op *MonoOpDef) MonoCalculate(a interface{}) (interface{}, error)

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

type String

type String interface {
	String() string
}

String is an instance that can return its value as a string

type Task

type Task func() error

func (Task) Do

func (t Task) Do() error

type Value

type Value interface {
	Value() interface{}
}

Value is an instance that can return it's value as an interface{}

Jump to

Keyboard shortcuts

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