ivyshims

package
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2023 License: MIT, BSD-3-Clause Imports: 17 Imported by: 0

README

This is a stripped down version of https://github.com/robpike/ivy , copied over on December 12th, 2022.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BinaryOps = make(map[string]BinaryOp)
View Source
var DebugFlags = [...]string{
	"cpu",
	"panic",
	"parse",
	"tokens",
	"types",
}

Order here determines order in the Config.debug array.

View Source
var IvyEval func(context Context, s string) Value

Implemented in package run, handled as a func to avoid a dependency loop.

View Source
var (
	MaxBigInt63 = big.NewInt(int64(^uint64(0) >> 1)) // Used in ../parse/special.go

)
View Source
var UnaryOps = make(map[string]UnaryOp)

Functions

func Atan

func Atan(q *big.Float) *big.Float

func ComplexSqrt

func ComplexSqrt(r, i *big.Float) (*big.Float, *big.Float)

func Cos

func Cos(q *big.Float) *big.Float

func Errorf

func Errorf(format string, args ...interface{})

Errorf panics with the formatted string, with type Error.

func IndexAssign

func IndexAssign(context Context, top, left Expr, index []Expr, right Expr, rhs Value)

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 MaxParallelismForTesting

func MaxParallelismForTesting()

func ParseString

func ParseString(s string) string

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 Pow

func Pow(x *big.Float, e *big.Float) *big.Float

func Sin

func Sin(q *big.Float) *big.Float

Types

type BigFloat

type BigFloat struct {
	*big.Float
}

func Consts

func Consts(c Context) (e, pi BigFloat)

func (BigFloat) Eval

func (f BigFloat) Eval(Context) Value

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) Inner

func (f BigFloat) Inner() Value

func (BigFloat) ProgString

func (f BigFloat) ProgString() string

func (BigFloat) Rank

func (f BigFloat) Rank() int

func (BigFloat) Sprint

func (f BigFloat) Sprint(conf *Config) string

func (BigFloat) String

func (f BigFloat) String() string

type BigInt

type BigInt struct {
	*big.Int
}

func (BigInt) BitLen

func (i BigInt) BitLen() int64

func (BigInt) Eval

func (i BigInt) Eval(Context) Value

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) Inner

func (i BigInt) Inner() Value

func (BigInt) ProgString

func (i BigInt) ProgString() string

func (BigInt) Rank

func (i BigInt) Rank() int

func (BigInt) Sprint

func (i BigInt) Sprint(conf *Config) string

func (BigInt) String

func (i BigInt) String() string

type BigRat

type BigRat struct {
	*big.Rat
}

func (BigRat) Eval

func (r BigRat) Eval(Context) Value

func (BigRat) Inner

func (r BigRat) Inner() Value

func (BigRat) ProgString

func (r BigRat) ProgString() string

func (BigRat) Rank

func (r BigRat) Rank() int

func (BigRat) Sprint

func (r BigRat) Sprint(conf *Config) string

func (BigRat) String

func (r BigRat) String() string

type BinaryOp

type BinaryOp interface {
	EvalBinary(c Context, right, left Value) Value
}

BinaryOp is the interface implemented by a simple binary operator.

type Char

type Char rune

func (Char) Eval

func (c Char) Eval(Context) Value

func (Char) Inner

func (c Char) Inner() Value

func (Char) ProgString

func (c Char) ProgString() string

func (Char) Rank

func (c Char) Rank() int

func (Char) Sprint

func (c Char) Sprint(conf *Config) string

func (Char) String

func (c Char) String() string

type Complex

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

func (Complex) Components

func (c Complex) Components() (Value, Value)

func (Complex) Eval

func (c Complex) Eval(Context) Value

func (Complex) Inner

func (c Complex) Inner() Value

func (Complex) ProgString

func (c Complex) ProgString() string

func (Complex) Rank

func (c Complex) Rank() int

func (Complex) Sprint

func (c Complex) Sprint(conf *Config) string

func (Complex) String

func (c Complex) String() string

type Config

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

A Config holds information about the configuration of the system. The zero value of a Config represents the default values for all settings.

func (*Config) Base

func (c *Config) Base() (inputBase, outputBase int)

Base returns the input and output bases.

func (*Config) BigOrigin

func (c *Config) BigOrigin() *big.Int

BigOrigin returns the index origin as a *big.Int.

func (*Config) CPUTime

func (c *Config) CPUTime() (real, user, sys time.Duration)

CPUTime returns the duration of the last interactive operation.

func (*Config) Debug

func (c *Config) Debug(flag string) bool

Debug returns the value of the specified boolean debugging flag.

func (*Config) ErrOutput

func (c *Config) ErrOutput() io.Writer

ErrOutput returns the writer to be used for error output.

func (*Config) FloatFormat

func (c *Config) FloatFormat() (verb byte, prec int, ok bool)

FloatFormat returns the parsed information about the format, if it's a floating-point format.

func (*Config) FloatPrec

func (c *Config) FloatPrec() uint

FloatPrec returns the floating-point precision in bits. The exponent size is fixed by math/big.

func (*Config) Format

func (c *Config) Format() string

Format returns the formatting string. If empty, the default formatting is used, as defined by the bases.

func (*Config) InputBase

func (c *Config) InputBase() int

InputBase returns the input base.

func (*Config) MaxBits

func (c *Config) MaxBits() uint

MaxBits returns the maximum integer size to store, in bits.

func (*Config) MaxDigits

func (c *Config) MaxDigits() uint

MaxDigits returns the maximum integer size to print as integer, in digits.

func (*Config) MaxStack

func (c *Config) MaxStack() uint

MaxStack returns the maximum call stack depth.

func (*Config) Mobile

func (c *Config) Mobile() bool

Mobile reports whether we are running on a mobile platform.

func (*Config) Origin

func (c *Config) Origin() int

Origin returns the index origin, default 1.

func (*Config) Output

func (c *Config) Output() io.Writer

Output returns the writer to be used for program output.

func (*Config) OutputBase

func (c *Config) OutputBase() int

OutputBase returns the output base.

func (*Config) PrintCPUTime

func (c *Config) PrintCPUTime() string

PrintCPUTime returns a nicely formatted version of the CPU time.

func (*Config) Prompt

func (c *Config) Prompt() string

Prompt returns the interactive prompt.

func (*Config) Random

func (c *Config) Random() *rand.Rand

Random returns the generator for random numbers.

func (*Config) RandomSeed

func (c *Config) RandomSeed() int64

RandomSeed returns the seed used to initialize the random number generator.

func (*Config) RatFormat

func (c *Config) RatFormat() string

Format returns the formatting string for rationals.

func (*Config) SetBase

func (c *Config) SetBase(inputBase, outputBase int)

SetBase sets the input and output bases.

func (*Config) SetCPUTime

func (c *Config) SetCPUTime(real, user, sys time.Duration)

SetCPUTime sets the duration of the last interactive operation.

func (*Config) SetDebug

func (c *Config) SetDebug(flag string, state bool) bool

SetDebug sets the value of the specified boolean debugging flag. It returns false if the flag is unknown.

func (*Config) SetErrOutput

func (c *Config) SetErrOutput(output io.Writer)

SetErrOutput sets the writer to which error output is printed; default is os.Stderr.

func (*Config) SetFloatPrec

func (c *Config) SetFloatPrec(prec uint)

SetFloatPrec sets the floating-point precision in bits.

func (*Config) SetFormat

func (c *Config) SetFormat(s string)

SetFormat sets the formatting string. Rational formatting is just this format applied twice with a / in between.

func (*Config) SetMaxBits

func (c *Config) SetMaxBits(digits uint)

MaxBits sets the maximum integer size to store, in bits.

func (*Config) SetMaxDigits

func (c *Config) SetMaxDigits(digits uint)

SetMaxDigits sets the maximum integer size to print as integer, in digits.

func (*Config) SetMaxStack

func (c *Config) SetMaxStack(depth uint)

SetMaxStack sets the maximum call stack depth.

func (*Config) SetMobile

func (c *Config) SetMobile(mobile bool)

SetMobile sets the Mobile bit as specified.

func (*Config) SetOrigin

func (c *Config) SetOrigin(origin int)

SetOrigin sets the index origin.

func (*Config) SetOutput

func (c *Config) SetOutput(output io.Writer)

SetOutput sets the writer to which program output is printed; default is os.Stdout.

func (*Config) SetPrompt

func (c *Config) SetPrompt(prompt string)

SetPrompt sets the interactive prompt.

func (*Config) SetRandomSeed

func (c *Config) SetRandomSeed(seed int64)

SetRandomSeed sets the seed for the random number generator.

type Context

type Context interface {
	// Lookup returns the configuration state for evaluation.
	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

	// EvalUnaryFn 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
}

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.

func NewContext

func NewContext(conf *Config) Context

NewContext returns a new execution context: the stack and variables, plus the execution configuration.

type Decomposable

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 Error

type Error string

Error is the type we recognize as a recoverable run-time error.

func (Error) Error

func (err Error) Error() string

type ExecContext

type ExecContext struct {
	Globals Symtab

	//  UnaryFn maps the names of unary functions (ops) to their implemenations.
	UnaryFn map[string]*Function
	//  BinaryFn maps the names of binary functions (ops) to their implemenations.
	BinaryFn map[string]*Function
	// Defs is a list of defined ops, in time order.  It is used when saving the
	// Context to a file.
	Defs []OpDef
	// contains filtered or unexported fields
}

Context holds execution context, specifically the binding of names to values and operators. It is the only implementation of ../Context, but since it references the value package, there would be a cycle if that package depended on this type definition.

func (*ExecContext) AssignGlobal

func (c *ExecContext) AssignGlobal(name string, val Value)

Assign assigns the global variable the value. The variable must be defined either in the current function or globally. Inside a function, new variables become locals.

func (*ExecContext) AssignLocal

func (c *ExecContext) AssignLocal(i int, value Value)

AssignLocal assigns the local variable with the given index the value.

func (*ExecContext) Binary

func (c *ExecContext) Binary(op string) BinaryOp

func (*ExecContext) Config

func (c *ExecContext) Config() *Config

func (*ExecContext) Declare

func (c *ExecContext) Declare(name string)

Declare makes the name a variable while parsing the next function.

func (*ExecContext) Define

func (c *ExecContext) Define(fn *Function)

Define defines the function and installs it. It also performs some error checking and adds the function to the sequencing information used by the save method.

func (*ExecContext) Eval

func (c *ExecContext) Eval(exprs []Expr) []Value

Eval evaluates a list of expressions.

func (*ExecContext) EvalBinary

func (c *ExecContext) EvalBinary(left Value, op string, right Value) Value

EvalBinary evaluates a binary operator, including products.

func (*ExecContext) EvalUnary

func (c *ExecContext) EvalUnary(op string, right Value) Value

EvalUnary evaluates a unary operator, including reductions and scans.

func (*ExecContext) ForgetAll

func (c *ExecContext) ForgetAll()

ForgetAll forgets the declared variables.

func (*ExecContext) Global

func (c *ExecContext) Global(name string) Value

Global returns the value of a global symbol, or nil if the symbol is not defined globally.

func (*ExecContext) Local

func (c *ExecContext) Local(i int) Value

Local returns the value of the local variable with index i.

func (*ExecContext) SetConstants

func (c *ExecContext) SetConstants()

SetConstants re-assigns the fundamental constant values using the current setting of floating-point precision.

func (*ExecContext) Unary

func (c *ExecContext) Unary(op string) UnaryOp

func (*ExecContext) UserDefined

func (c *ExecContext) UserDefined(op string, isBinary bool) bool

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 Function

type Function struct {
	IsBinary bool
	Name     string
	Left     string
	Right    string
	Body     []Expr
	Locals   []string
	Globals  []string
}

Function represents a unary or binary user-defined operator.

func (*Function) EvalBinary

func (fn *Function) EvalBinary(context Context, left, right Value) Value

func (*Function) EvalUnary

func (fn *Function) EvalUnary(context Context, right Value) Value

func (*Function) String

func (fn *Function) String() string

type Int

type Int int64

func (Int) Eval

func (i Int) Eval(Context) Value

func (Int) Inner

func (i Int) Inner() Value

func (Int) ProgString

func (i Int) ProgString() string

func (Int) Rank

func (i Int) Rank() int

func (Int) Sprint

func (i Int) Sprint(conf *Config) string

func (Int) String

func (i Int) String() string

func (Int) ToBool

func (i Int) ToBool() bool

type Matrix

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

func NewMatrix

func NewMatrix(shape []int, data []Value) *Matrix

NewMatrix makes a new matrix. The number of elements must fit in an Int.

func (*Matrix) Copy

func (m *Matrix) Copy() *Matrix

func (*Matrix) Data

func (m *Matrix) Data() Vector

Data returns the data of the matrix as a vector.

func (*Matrix) ElemSize

func (m *Matrix) ElemSize() int64

ElemSize returns the size of each top-level element of the matrix. Given shape [a, b, c, ...] it is b*c*....

func (*Matrix) Eval

func (m *Matrix) Eval(Context) Value

func (*Matrix) Inner

func (m *Matrix) Inner() Value

func (*Matrix) ProgString

func (m *Matrix) ProgString() string

func (*Matrix) Rank

func (m *Matrix) Rank() int

func (*Matrix) Shape

func (m *Matrix) Shape() []int

Shape returns the shape of the matrix.

func (*Matrix) Size

func (m *Matrix) Size() int64

Size returns number of elements of the matrix. Given shape [a, b, c, ...] it is a*b*c*....

func (*Matrix) Sprint

func (m *Matrix) Sprint(conf *Config) string

func (*Matrix) String

func (m *Matrix) String() string

type OpDef

type OpDef struct {
	Name     string
	IsBinary bool
}

OpDef is just a record of an op's name and arg count. It is held in execContext.defs to control writing the ops out in the right order during save. See comment above.

type Symtab

type Symtab map[string]Value

Symtab is a symbol table, a map of names to values.

type UnaryOp

type UnaryOp interface {
	EvalUnary(c Context, right Value) Value
}

UnaryOp is the interface implemented by a simple unary operator.

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) string
	Eval(Context) 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 EvalFunctionBody

func EvalFunctionBody(context Context, fnName string, body []Expr) Value

EvalFunctionBody evaluates the list of expressions inside a function, possibly with conditionals that generate an early return.

func Index

func Index(context Context, top, left Expr, index []Expr) Value

Index returns left[index]. Left and index will be evaluated (right to left), while top is only for its ProgString method.

func Parse

func Parse(conf *Config, s string) (Value, error)

func Product

func Product(c Context, u Value, op string, v Value) Value

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 Reduce

func Reduce(c Context, op string, v Value) Value

Reduce computes a reduction such as +/. The slash has been removed.

func Scan

func Scan(c Context, op string, v Value) Value

Scan computes a scan of the op; the \ has been removed. It gives the successive values of reducing op through v. We must be right associative; that is the grammar.

type Vector

type Vector []Value

func NewIntVector

func NewIntVector(elems []int) Vector

func NewVector

func NewVector(elems []Value) Vector

func (Vector) AllChars

func (v Vector) AllChars() bool

AllChars reports whether the vector contains only Chars.

func (Vector) AllInts

func (v Vector) AllInts() bool

AllInts reports whether the vector contains only Ints.

func (Vector) Copy

func (v Vector) Copy() Vector

func (Vector) Eval

func (v Vector) Eval(Context) Value

func (Vector) Inner

func (v Vector) Inner() Value

func (Vector) ProgString

func (v Vector) ProgString() string

func (Vector) Rank

func (v Vector) Rank() int

func (Vector) Sprint

func (v Vector) Sprint(conf *Config) string

func (Vector) String

func (v Vector) String() string

Jump to

Keyboard shortcuts

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