Documentation
¶
Index ¶
- Variables
- func Assign(context Context, left, right Expr, rhs Value)
- func Errorf(format string, args ...interface{})
- func IndexAssign(context Context, top, left Expr, index []Expr, right Expr, rhs Value)
- func IsCompound(x interface{}) bool
- func IsScalarType(v Value) bool
- func MaxParallelismForTesting()
- func OrderedCompare(c Context, u, v Value) int
- func ParseString(s string) string
- func TraceBinary(c Context, level int, u Value, op string, v Value)
- func TraceUnary(c Context, level int, op string, v Value)
- type Assignment
- type BigFloat
- type BigInt
- type BigRat
- type BinaryExpr
- type BinaryOp
- type Char
- type Complex
- func (c Complex) Components() (Value, Value)
- func (c Complex) Copy() Value
- func (c Complex) Eval(Context) Value
- func (c Complex) Inner() Value
- func (c Complex) ProgString() string
- func (c Complex) Rank() int
- func (c Complex) Signum(ctx Context) Complex
- func (c Complex) Sprint(conf *config.Config) string
- func (c Complex) String() string
- type CondExpr
- type Context
- type Decomposable
- type Error
- type Expr
- type IndexExpr
- type Int
- type Matrix
- func (m *Matrix) Copy() Value
- func (m *Matrix) Data() *Vector
- func (m *Matrix) ElemSize() int
- func (m *Matrix) Eval(Context) Value
- func (m *Matrix) Inner() Value
- func (m *Matrix) ProgString() string
- func (m *Matrix) Rank() int
- func (m *Matrix) Shape() []int
- func (m *Matrix) Size() int
- func (m *Matrix) Sprint(conf *config.Config) string
- func (m *Matrix) String() string
- type UnaryExpr
- type UnaryOp
- type Value
- func BinaryEach(c Context, lv Value, op string, rv Value) Value
- func Each(c Context, op string, v Value) Value
- func EvalCharEqual(u Value, isEqualOp bool, v Value) (Value, bool)
- func EvalFunctionBody(context Context, fnName string, body []Expr) Value
- func Index(context Context, top, left Expr, index []Expr) Value
- func Parse(conf *config.Config, s string) (Value, error)
- func Product(c Context, u Value, op string, v Value) Value
- func QuoRem(op string, c Context, a, b Value) (div, rem Value)
- func Reduce(c Context, op string, v Value) Value
- func ReduceFirst(c Context, op string, v Value) Value
- func Scan(c Context, op string, v Value) Value
- func ScanFirst(c Context, op string, v Value) Value
- type VarExpr
- type Vector
- func (v *Vector) All() []Value
- func (v *Vector) AllChars() bool
- func (v *Vector) AllInts() bool
- func (v *Vector) At(i int) Value
- func (v *Vector) Copy() Value
- func (v *Vector) Eval(Context) Value
- func (v *Vector) Inner() Value
- func (v *Vector) Len() int
- func (v *Vector) ProgString() string
- func (v *Vector) Rank() int
- func (v *Vector) Set(i int, x Value)
- func (v *Vector) Slice(i, j int) []Value
- func (v *Vector) Sprint(conf *config.Config) string
- func (v *Vector) String() string
- func (v *Vector) Writable() []Value
- type VectorExpr
Constants ¶
This section is empty.
Variables ¶
var BinaryOps = make(map[string]BinaryOp)
var IvyEval func(context Context, s string) Value
Implemented in package run, handled as a func to avoid a dependency loop.
var (
MaxBigInt63 = big.NewInt(int64(^uint64(0) >> 1)) // Used in ../parse/special.go
)
var UnaryOps = make(map[string]UnaryOp)
Functions ¶
func Errorf ¶
func Errorf(format string, args ...interface{})
Errorf panics with the formatted string, with type Error.
func IndexAssign ¶ added in v0.1.12
IndexAssign handles general assignment to indexed expressions on the LHS. Left and index will be evaluated (right to left), while top is only for its ProgString method. The caller must check that left is a variable expression, so that the assignment is not being written into a temporary.
func IsCompound ¶ added in v0.3.8
func IsCompound(x interface{}) bool
IsCompound reports whether the item is a non-trivial expression tree, one that may require parentheses around it when printed to maintain correct evaluation order.
func IsScalarType ¶ added in v0.3.0
IsScalarType reports whether u is an actual scalar, an int or float etc.
func MaxParallelismForTesting ¶ added in v0.1.12
func MaxParallelismForTesting()
func OrderedCompare ¶ added in v0.3.0
OrderedCompare returns -1, 0, or 1 according to whether u is less than, equal to, or greater than v, according to total ordering rules. Total ordering is not the usual mathematical definition, as we honor things like 1.0 == 1, comparison of int and char is forbidden, and complex numbers do not implement <. Plus other than for scalars we don't need to follow any particular rules at all, just what works for sorting sets.
Thus we amend the usual orderings: - Char is below all other types - All other scalars are compared directly, except... - ...Complex is above all other scalars, unless on the real line: 1j0 == 1. - Vector is above all other types except... - ...Matrix, which is above all other types.
When comparing identically-typed values:
- Complex is ordered first by real component, then by imaginary.
- Vector and Matrix are ordered first by number of elements, then in lexical order of elements.
These are unusual rules, but they are provide a unique ordering of elements sufficient for set membership. // Exported for testing, which is done by the parent directory to avoid a dependency cycle.
func ParseString ¶
ParseString parses a string. Single quotes and double quotes are both allowed (but must be consistent.) The result must contain only valid Unicode code points.
func TraceBinary ¶ added in v0.3.10
TraceBinary prints a trace line for a binary operator.
Types ¶
type Assignment ¶ added in v0.3.8
type Assignment struct {
Value
}
Assignment is an implementation of Value that is created as the result of an assignment. It can be type-asserted to discover whether the returned value was created by assignment, such as is done in the interpreter to avoid printing the results of assignment expressions.
type BigFloat ¶
func (BigFloat) Format ¶
func (f BigFloat) Format()
The fmt package looks for Formatter before Stringer, but we want to use Stringer only. big.Float implements Formatter, and we embed it in our BigFloat type. To make sure that our String gets called rather than the inner Format, we put a non-matching stub Format method into this interface. This is ugly but very simple and cheap.
func (BigFloat) ProgString ¶
type BigInt ¶
func (BigInt) Format ¶
func (i BigInt) Format()
The fmt package looks for Formatter before Stringer, but we want to use Stringer only. big.Int and big.Rat implement Formatter, and we embed them in our BigInt and BigRat types. To make sure that our String gets called rather than the inner Format, we put a non-matching stub Format method into this interface. This is ugly but very simple and cheap.
func (BigInt) ProgString ¶
type BigRat ¶
func (BigRat) ProgString ¶
type BinaryExpr ¶ added in v0.3.8
func (*BinaryExpr) Eval ¶ added in v0.3.8
func (b *BinaryExpr) Eval(context Context) Value
func (*BinaryExpr) ProgString ¶ added in v0.3.8
func (b *BinaryExpr) ProgString() string
type Complex ¶ added in v0.2.0
type Complex struct {
// contains filtered or unexported fields
}
func NewComplex ¶ added in v0.3.0
func (Complex) Components ¶ added in v0.2.0
func (Complex) ProgString ¶ added in v0.2.0
type CondExpr ¶ added in v0.3.8
type CondExpr struct {
Cond *BinaryExpr
}
CondExpr is a CondExpr executor: expression ":" expression
func (*CondExpr) ProgString ¶ added in v0.3.8
type Context ¶
type Context interface { // Config returns the configuration state for evaluation. Config() *config.Config // Local returns the value of the i'th local variable. Local(i int) Value // AssignLocal assigns to the i'th local variable. AssignLocal(i int, value Value) // Global returns the value of the named global variable. Global(name string) Value // AssignGlobal assigns to the named global variable. AssignGlobal(name string, value Value) // Eval evaluates a list of expressions. Eval(exprs []Expr) []Value // EvalUnary evaluates a unary operator. EvalUnary(op string, right Value) Value // EvalBinary evaluates a binary operator. EvalBinary(left Value, op string, right Value) Value // UserDefined reports whether the specified op is user-defined. UserDefined(op string, isBinary bool) bool // TraceIndent returns an indentation marker showing the depth of the stack. TraceIndent() string }
Context is the execution context for evaluation. The only implementation is ../exec/Context, but the interface is defined separately, here, because of the dependence on Expr and the import cycle that would otherwise result.
type Decomposable ¶ added in v0.1.1
type Decomposable interface { // Operator returns the operator, or "" for a singleton. Operator() string // Operands returns the left and right operands, or nil if absent. // For singletons, both will be nil, but ProgString can // give the underlying name or value. Operands() (left, right Expr) }
Decomposable allows one to pull apart a parsed expression. Only implemented by Expr types that need to be decomposed in function evaluation.
type Expr ¶
type Expr interface { // ProgString returns the unambiguous representation of the // expression to be used in program source. ProgString() string Eval(Context) Value }
Expr is the interface for a parsed expression. Also implemented by Value.
type IndexExpr ¶ added in v0.3.8
func (*IndexExpr) ProgString ¶ added in v0.3.8
type Matrix ¶
type Matrix struct {
// contains filtered or unexported fields
}
func (*Matrix) ElemSize ¶
ElemSize returns the size of each top-level element of the matrix. Given shape [a, b, c, ...] it is b*c*....
func (*Matrix) ProgString ¶
type UnaryExpr ¶ added in v0.3.8
func (*UnaryExpr) ProgString ¶ added in v0.3.8
type Value ¶
type Value interface { // String is for internal debugging only. It uses default configuration // and puts parentheses around every value so it's clear when it is used. // All user output should call Sprint instead. String() string Sprint(*config.Config) string // Eval evaluates (simplifies) the Value. Eval(Context) Value // Copy returns a copy of the Value. Copy() Value // Inner retrieves the value, without evaluation. But for Assignments, // it returns the right-hand side. Inner() Value // Rank returns the rank of the value: 0 for scalar, 1 for vector, etc. Rank() int // ProgString is like String, but suitable for program listing. // For instance, it ignores the user format for numbers and // puts quotes on chars, guaranteeing a correct representation. ProgString() string // contains filtered or unexported methods }
func BinaryEach ¶ added in v0.3.7
BinaryEach computes the result of applying op to lv and rv, applying the "each" expansion depending on how many times @ appears on the left and right ends of op.
func Each ¶ added in v0.3.7
Each computes the result of running op on each element of v. The trailing @ has been removed.
func EvalCharEqual ¶ added in v0.3.0
EvalCharEqual handles == and != in a special case: If comparing a scalar against a Char, avoid the conversion. The logic of type promotion in EvalBinary otherwise interferes with comparison because it tries to force scalar types to be the same, and char doesn't convert to any other type.
func EvalFunctionBody ¶ added in v0.1.1
EvalFunctionBody evaluates the list of expressions inside a function, possibly with conditionals that generate an early return.
func Index ¶ added in v0.1.12
Index returns left[index]. Left and index will be evaluated (right to left), while top is only for its ProgString method.
func Product ¶
Product computes a compound product, such as an inner product "+.*" or outer product "o.*". The op is known to contain a period. The operands are all at least vectors, and for inner product they must both be vectors.
func QuoRem ¶ added in v0.3.0
QuoRem uses Euclidean division to return the quotient and remainder for a/b. The quotient will be an integer, possibly negative; the remainder is always positive and may be fractional. Returned values satisfy the identity that
quo = a div b such that rem = a - b*quo with 0 <= rem < |y|
See comment for math/big.Int.DivMod for details. Exported for testing.
func ReduceFirst ¶ added in v0.3.0
ReduceFirst computes a reduction such as +/% along the first axis. The slash-percent has been removed.
type VarExpr ¶ added in v0.3.8
VarExpr identifies a variable to be looked up and evaluated.
func (*VarExpr) ProgString ¶ added in v0.3.8
type Vector ¶
type Vector struct {
// contains filtered or unexported fields
}
func NewIntVector ¶
func (*Vector) ProgString ¶
type VectorExpr ¶ added in v0.3.8
type VectorExpr []Expr
VectorExpr holds a syntactic vector to be verified and evaluated.
func (VectorExpr) Eval ¶ added in v0.3.8
func (e VectorExpr) Eval(context Context) Value
func (VectorExpr) ProgString ¶ added in v0.3.8
func (e VectorExpr) ProgString() string