Documentation ¶
Overview ¶
Package goal provides an API to goal's interpreter.
In order to evaluate code in the goal programming language, first a new context has to be created.
ctx := goal.NewContext()
This context can then be used to Compile some code, and then Run it. It is possible to customize the context by registering new unary and binary operators using the RegisterMonad and RegisterDyad methods.
See tests in context_test.go, as well as cmd/goal/main.go, for usage examples.
Index ¶
- func Length(x V) int
- func Match(x, y V) bool
- type AB
- func (x *AB) At(i int) bool
- func (x *AB) CloneWithRC(rc *int) Value
- func (x *AB) DecrRC()
- func (x *AB) Fprint(ctx *Context, w ValueWriter) (n int, err error)
- func (x *AB) IncrRC()
- func (x *AB) InitWithRC(rc *int)
- func (x *AB) Len() int
- func (xv *AB) Less(y Value) bool
- func (x *AB) Matches(y Value) bool
- func (x *AB) RC() *int
- func (x *AB) Type() string
- type AF
- func (x *AF) At(i int) float64
- func (x *AF) CloneWithRC(rc *int) Value
- func (x *AF) DecrRC()
- func (x *AF) Fprint(ctx *Context, w ValueWriter) (n int, err error)
- func (x *AF) IncrRC()
- func (x *AF) InitWithRC(rc *int)
- func (x *AF) Len() int
- func (xv *AF) Less(y Value) bool
- func (x *AF) Matches(y Value) bool
- func (x *AF) RC() *int
- func (x *AF) Type() string
- type AI
- func (x *AI) At(i int) int64
- func (x *AI) CloneWithRC(rc *int) Value
- func (x *AI) DecrRC()
- func (x *AI) Fprint(ctx *Context, w ValueWriter) (n int, err error)
- func (x *AI) IncrRC()
- func (x *AI) InitWithRC(rc *int)
- func (x *AI) Len() int
- func (xv *AI) Less(y Value) bool
- func (x *AI) Matches(y Value) bool
- func (x *AI) RC() *int
- func (x *AI) Type() string
- type AS
- func (x *AS) At(i int) string
- func (x *AS) CloneWithRC(rc *int) Value
- func (x *AS) DecrRC()
- func (x *AS) Fprint(ctx *Context, w ValueWriter) (n int, err error)
- func (x *AS) IncrRC()
- func (x *AS) InitWithRC(rc *int)
- func (x *AS) Len() int
- func (xv *AS) Less(y Value) bool
- func (x *AS) Matches(y Value) bool
- func (x *AS) RC() *int
- func (x *AS) Type() string
- type AV
- func (x *AV) At(i int) V
- func (x *AV) CloneWithRC(rc *int) Value
- func (x *AV) DecrRC()
- func (x *AV) Fprint(ctx *Context, w ValueWriter) (n int, err error)
- func (x *AV) IncrRC()
- func (x *AV) InitWithRC(rc *int)
- func (x *AV) Len() int
- func (xv *AV) Less(y Value) bool
- func (x *AV) Matches(y Value) bool
- func (x *AV) RC() *int
- func (x *AV) Type() string
- type Context
- func (ctx *Context) Apply(x, y V) V
- func (ctx *Context) Apply2(x, y, z V) V
- func (ctx *Context) ApplyN(x V, args []V) V
- func (ctx *Context) AssignGlobal(name string, x V)
- func (ctx *Context) AssignedLast() bool
- func (ctx *Context) Compile(name string, s string) error
- func (ctx *Context) Eval(s string) (V, error)
- func (ctx *Context) EvalPackage(s, name, prefix string) (V, error)
- func (ctx *Context) GetGlobal(name string) (V, bool)
- func (ctx *Context) RegisterDyad(name string, vf VariadicFun) V
- func (ctx *Context) RegisterMonad(name string, vf VariadicFun) V
- func (ctx *Context) Run() (V, error)
- func (ctx *Context) Show() string
- type Dict
- func (d *Dict) CloneWithRC(rc *int) Value
- func (d *Dict) DecrRC()
- func (d *Dict) Fprint(ctx *Context, w ValueWriter) (n int, err error)
- func (d *Dict) IncrRC()
- func (d *Dict) InitWithRC(rc *int)
- func (d *Dict) Keys() V
- func (d *Dict) Len() int
- func (d *Dict) Less(y Value) bool
- func (d *Dict) Matches(y Value) bool
- func (d *Dict) Type() string
- func (d *Dict) Values() V
- type ErrPackageImported
- type NameType
- type PanicError
- type RefCountHolder
- type RefCounter
- type S
- type Scanner
- type Token
- type TokenType
- type V
- func Canonical(x V) V
- func CanonicalRec(x V) V
- func Errorf(format string, a ...interface{}) V
- func NewAB(x []bool) V
- func NewABWithRC(x []bool, rc *int) V
- func NewAF(x []float64) V
- func NewAFWithRC(x []float64, rc *int) V
- func NewAI(x []int64) V
- func NewAIWithRC(x []int64, rc *int) V
- func NewAS(x []string) V
- func NewASWithRC(x []string, rc *int) V
- func NewAV(x []V) V
- func NewAVWithRC(x []V, rc *int) V
- func NewDict(keys, values V) V
- func NewError(x V) V
- func NewF(f float64) V
- func NewI(i int64) V
- func NewPanic(s string) V
- func NewS(s string) V
- func NewV(bv Value) V
- func Panicf(format string, a ...interface{}) V
- func VAbs(ctx *Context, args []V) V
- func VAcos(ctx *Context, args []V) V
- func VAdd(ctx *Context, args []V) V
- func VAnd(ctx *Context, args []V) V
- func VApply(ctx *Context, args []V) V
- func VApplyN(ctx *Context, args []V) V
- func VAsin(ctx *Context, args []V) V
- func VAtan(ctx *Context, args []V) V
- func VBytes(ctx *Context, args []V) V
- func VCSV(ctx *Context, args []V) V
- func VCast(ctx *Context, args []V) V
- func VCeil(ctx *Context, args []V) V
- func VCos(ctx *Context, args []V) V
- func VDivide(ctx *Context, args []V) V
- func VDrop(ctx *Context, args []V) V
- func VEach(ctx *Context, args []V) V
- func VEqual(ctx *Context, args []V) V
- func VError(ctx *Context, args []V) V
- func VEval(ctx *Context, args []V) V
- func VExp(ctx *Context, args []V) V
- func VFind(ctx *Context, args []V) V
- func VFirsts(ctx *Context, args []V) V
- func VFold(ctx *Context, args []V) V
- func VICount(ctx *Context, args []V) V
- func VIn(ctx *Context, args []V) V
- func VJoin(ctx *Context, args []V) V
- func VKey(ctx *Context, args []V) V
- func VLess(ctx *Context, args []V) V
- func VList(ctx *Context, args []V) V
- func VLog(ctx *Context, args []V) V
- func VMatch(ctx *Context, args []V) V
- func VMax(ctx *Context, args []V) V
- func VMin(ctx *Context, args []V) V
- func VMod(ctx *Context, args []V) V
- func VMore(ctx *Context, args []V) V
- func VMultiply(ctx *Context, args []V) V
- func VNaN(ctx *Context, args []V) V
- func VOCount(ctx *Context, args []V) V
- func VOr(ctx *Context, args []V) V
- func VPanic(ctx *Context, args []V) V
- func VRShift(ctx *Context, args []V) V
- func VRight(ctx *Context, args []V) V
- func VRotate(ctx *Context, args []V) V
- func VRoundToEven(ctx *Context, args []V) V
- func VRx(ctx *Context, args []V) V
- func VScan(ctx *Context, args []V) V
- func VSeed(ctx *Context, args []V) V
- func VSet(ctx *Context, args []V) V
- func VShift(ctx *Context, args []V) V
- func VSign(ctx *Context, args []V) V
- func VSin(ctx *Context, args []V) V
- func VSqrt(ctx *Context, args []V) V
- func VSub(ctx *Context, args []V) V
- func VSubtract(ctx *Context, args []V) V
- func VTake(ctx *Context, args []V) V
- func VTan(ctx *Context, args []V) V
- func VTime(ctx *Context, args []V) V
- func VUTF8RCount(ctx *Context, args []V) V
- func VUTF8Valid(ctx *Context, args []V) V
- func VWithout(ctx *Context, args []V) V
- func (x V) Clone() V
- func (x V) CloneWithRC(rc *int) V
- func (x V) DecrRC()
- func (x V) Error() V
- func (x V) F() float64
- func (v V) Fprint(ctx *Context, w ValueWriter) (n int, err error)
- func (x V) HasRC() bool
- func (x V) I() int64
- func (x V) IncrRC()
- func (x V) InitRC()
- func (x V) InitWithRC(rc *int)
- func (x V) IsError() bool
- func (x V) IsF() bool
- func (x V) IsFunction() bool
- func (x V) IsI() bool
- func (x V) IsPanic() bool
- func (x V) IsValue() bool
- func (x V) Less(y V) bool
- func (x V) Rank(ctx *Context) int
- func (v V) Sprint(ctx *Context) string
- func (x V) Type() string
- func (x V) Value() Value
- type Value
- type ValueWriter
- type VariadicFun
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type AB ¶
type AB struct { Slice []bool // contains filtered or unexported fields }
AB represents an array of booleans.
func (*AB) CloneWithRC ¶ added in v0.2.0
func (*AB) InitWithRC ¶ added in v0.2.0
type AF ¶
type AF struct { Slice []float64 // contains filtered or unexported fields }
AF represents an array of reals.
func (*AF) CloneWithRC ¶ added in v0.2.0
func (*AF) InitWithRC ¶ added in v0.2.0
type AI ¶
type AI struct { Slice []int64 // contains filtered or unexported fields }
AI represents an array of integers.
func (*AI) CloneWithRC ¶ added in v0.2.0
func (*AI) InitWithRC ¶ added in v0.2.0
type AS ¶
type AS struct { Slice []string // string array // contains filtered or unexported fields }
AS represents an array of strings.
func (*AS) CloneWithRC ¶ added in v0.2.0
func (*AS) InitWithRC ¶ added in v0.2.0
type AV ¶
type AV struct { Slice []V // contains filtered or unexported fields }
AV represents a generic array.
func (*AV) CloneWithRC ¶ added in v0.2.0
func (*AV) InitWithRC ¶ added in v0.2.0
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Context holds the state of the interpreter.
func NewContext ¶
func NewContext() *Context
NewContext returns a new context for compiling and interpreting code.
func (*Context) ApplyN ¶
ApplyN calls a value with one or more arguments. The arguments should be provided in reverse order, given the stack-based right to left semantics used by the language.
func (*Context) AssignGlobal ¶
AssignGlobal assigns a value to a global variable name.
func (*Context) AssignedLast ¶
AssignedLast returns true if the last compiled expression was an assignment.
func (*Context) Compile ¶
Compile parses and compiles code from the given source string. The name argument is used for error reporting and represents, usually, the filename.
func (*Context) Eval ¶
Eval calls Compile with the given string as unnamed source, and then Run. You cannot call it within a variadic function, as it the evaluation is done on the current context, so it would interrupt compilation of current file. Use EvalPackage for that.
func (*Context) EvalPackage ¶
EvalPackage calls Compile with the string as source, name (for error location and caching, usually a filename), prefix (for global variables, usually a filename without the extension), and then Run. If a package with same name has already been evaluated, it returns ErrPackageImported. Current implementation has the following limitation: if a prefix is provided, it is only used the first time a same package is evaluated (so all imports have to share the same prefix). The package is evaluated in a derived context that is then merged on successful completion, so this function can be called within a variadic function.
func (*Context) GetGlobal ¶
GetGlobal returns the value attached to a global variable with the given name.
func (*Context) RegisterDyad ¶
func (ctx *Context) RegisterDyad(name string, vf VariadicFun) V
RegisterDyad adds a variadic function to the context, and generates a new dyadic keyword for that variadic (parsing will search for a left argument). The variadic is also returned as a value.
func (*Context) RegisterMonad ¶
func (ctx *Context) RegisterMonad(name string, vf VariadicFun) V
RegisterMonad adds a variadic function to the context, and generates a new monadic keyword for that variadic (parsing will not search for a left argument). The variadic is also returned as a value. Note that while that a keyword defined in such a way will not take a left argument, it is still possible to pass several arguments to it with bracket indexing, like for any value.
type Dict ¶ added in v0.5.0
type Dict struct {
// contains filtered or unexported fields
}
Dict represents a dictionnary.
func (*Dict) CloneWithRC ¶ added in v0.5.0
func (*Dict) Fprint ¶ added in v0.5.0
func (d *Dict) Fprint(ctx *Context, w ValueWriter) (n int, err error)
func (*Dict) InitWithRC ¶ added in v0.5.0
type ErrPackageImported ¶
type ErrPackageImported struct{}
ErrPackageImported is returned by EvalPackage for packages that have already been processed.
func (ErrPackageImported) Error ¶
func (e ErrPackageImported) Error() string
type NameType ¶
type NameType int
NameType represents the different kinds of special roles for alphanumeric identifiers that act as keywords.
type PanicError ¶
type PanicError struct { Msg string // error message (without location) // contains filtered or unexported fields }
PanicError represents a fatal error returned by any Context method.
func (*PanicError) Error ¶
func (e *PanicError) Error() string
Error returns the default string representation. It makes uses of position information obtained from its running context.
type RefCountHolder ¶ added in v0.2.0
type RefCountHolder interface { RefCounter // RC returns the value's root reference count pointer. RC() *int }
RefCountHolder is a RefCounter that has a root refcount pointer. When such values are returned from a variadic function, if the refcount pointer is still nil, InitWithRC is automatically called with a newly allocated refcount pointer to a zero count.
type RefCounter ¶
type RefCounter interface { Value // IncrRC increments the reference count by one. It can panic if the // value's refcount pointer has not been properly initialized. IncrRC() // DecrRC decrements the reference count by one, or zero if it is // already zero. DecrRC() // InitWithRC recursively sets the refcount pointer for reusable // values, and increments by 2 the refcount of non-reusable values, to // ensure immutability of non-reusable children without cloning them. InitWithRC(rc *int) // CloneWithRC returns a clone of the value, with rc as new refcount // pointer. If the current value's current refcount pointer is nil or // equal to the passed one, the same value is returned after updating // the refcount pointer as needed, instead of doing a full clone. CloneWithRC(rc *int) Value }
RefCounter is implemented by values that use a reference count. In goal the refcount is not used for memory management, but only for optimization of memory allocations. Refcount is increased by each assignement, and each push operation on the stack, except for pushes corresponding to the last use of a variable (as approximated conservatively). It is reduced after each drop. If refcount is equal or less than one, then the value is considered reusable.
When defining a new type implementing the Value interface, it is only necessary to also implement RefCounter if the type definition makes use of a type implementing it (for example an array type or a generic V).
type S ¶
type S string
S represents (immutable) strings of bytes.
type Scanner ¶
type Scanner struct {
// contains filtered or unexported fields
}
Scanner represents the state of the scanner.
func NewScanner ¶
NewScanner returns a scanner for the given source string.
type Token ¶
type Token struct { Type TokenType // token type Pos int // token's offset in the source Text string // content text (identifier, string, number) }
Token represents a token information.
type TokenType ¶
type TokenType int
TokenType represents the different kinds of tokens.
type V ¶
type V struct {
// contains filtered or unexported fields
}
V contains a boxed or unboxed value.
func Canonical ¶
Canonical returns the canonical form of a given value, that is the most specialized form, assuming it's already canonical at depth > 1. In practice, if the value is a generic array, but a more specialized version could represent the value, it returns the specialized value. All goal variadic functions have to return results in canonical form, so this function can be used to ensure that.
func CanonicalRec ¶ added in v0.2.0
CanonicalRec returns the canonical form of a given value, that is the most specialized form. In practice, if the value is a generic array, but a more specialized version could represent the value, it returns the specialized value. All goal variadic functions have to return results in canonical form, so this function can be used to ensure that.
func NewABWithRC ¶ added in v0.2.0
NewABWithRC returns a new boolean array.
func NewAFWithRC ¶ added in v0.2.0
NewAFWithRC returns a new array of reals.
func NewAIWithRC ¶ added in v0.2.0
NewAIWithRC returns a new int array.
func NewASWithRC ¶ added in v0.2.0
NewASWithRC returns a new array of strings.
func NewAVWithRC ¶ added in v0.2.0
NewAVWithRC returns a new generic array.
func NewDict ¶ added in v0.5.0
NewDict returns a dictionnary. Both keys and values should be arrays, and they should have the same length.
func VRoundToEven ¶
VRoundToEven implements the round variadic.
func VUTF8RCount ¶
VUTF8RCount implements the "utf8.rcount" variadic verb.
func VUTF8Valid ¶
VUTF8Valid implements the "utf8.valid" variadic verb.
func (V) Clone ¶ added in v0.2.0
Clone creates an identical deep copy of a value, or the value itself if it is reusable. It initializes refcount if necessary.
func (V) CloneWithRC ¶ added in v0.2.0
func (V) Fprint ¶
func (v V) Fprint(ctx *Context, w ValueWriter) (n int, err error)
Fprint writes a matching program string representation of the value.
func (V) HasRC ¶ added in v0.2.0
HasRC returns true if the value is boxed and implements RefCounter.
func (V) InitRC ¶ added in v0.2.0
func (x V) InitRC()
InitRC initializes refcount if the value is a RefCountHolder with nil refcount.
func (V) InitWithRC ¶ added in v0.2.0
InitWithRC calls the method of the same name on boxed values.
func (V) IsFunction ¶
IsFunction returns true if the value is some kind of function.
func (V) IsValue ¶
IsValue returns true if the value is a boxed value satisfying the Value interface. You can then get the value with the Value method.
func (V) Less ¶ added in v0.4.0
Less returns true if x is ordered before y. It represents a strict total order. Values are ordered as follows: unboxed atoms first (nums, variadics, then lambdas), then boxed values. Otherwise, values are compared with < and > when comparable, and otherwise using their Type string value. As a special case, comparable arrays are compared first by length, or lexicographically if they are of equal length.
func (V) Rank ¶
Rank returns the default rank of the value, that is the number of arguments it normally takes. It returns 0 for non-function values. This default rank is used when a function is used in an adverbial expression that has different semantics depending on the function arity. Currently, ranks are as follows:
variadic 2 lambda number of arguments projections number of gaps derived verb 1
type Value ¶
type Value interface { // Matches returns true if the value matches another (in the sense of // the ~ operator). Matches(Value) bool // Fprint writes a unique program string representation of the value. Fprint(*Context, ValueWriter) (n int, err error) // Type returns the name of the value's type. It may be used by Less to // sort non-comparable values using lexicographic order. This means // Type should return different values for non-comparable values. Type() string // Less returns true if the value should be orderer before the given // one. It is used for sorting values, but not for element-wise // comparison with < and >. It should produce a strict total order, so, // in particular, if x < y, then we do not have y > x, and one of them // should hold unless both values match. Less(Value) bool }
Value is the interface satisfied by all boxed values.
type ValueWriter ¶
type ValueWriter interface { io.Writer io.ByteWriter io.StringWriter }
ValueWriter is the interface used when formatting values with Fprint.
type VariadicFun ¶
VariadicFun represents a variadic function.
Source Files ¶
- adverbs.go
- adverbspecial.go
- amend.go
- apply.go
- arithd.go
- arithm.go
- arrays.go
- ast.go
- clone.go
- cmpsearch.go
- compiler.go
- context.go
- csv.go
- dicts.go
- encdec.go
- errors.go
- flip.go
- fns.go
- group.go
- join.go
- less.go
- math.go
- mathm.go
- opcode.go
- parser.go
- rand.go
- refcount.go
- regexp.go
- scanner.go
- sfns.go
- sort.go
- stringer.go
- strings.go
- time.go
- utf8.go
- utils.go
- valstring.go
- value.go
- variadics.go
- vm.go