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 ¶
- type A
- type AB
- func (x *AB) Append(ctx *Context, dst []byte) []byte
- func (x *AB) At(i int) byte
- func (x *AB) Clone() BV
- func (x *AB) DecrRC()
- func (x *AB) IncrRC()
- func (x *AB) IsBoolean() bool
- func (x *AB) Len() int
- func (x *AB) Less(i, j int) bool
- func (x *AB) LessT(y BV) bool
- func (x *AB) MarkImmutable()
- func (x *AB) Matches(y BV) bool
- func (x *AB) Slice() []byte
- func (x *AB) Swap(i, j int)
- func (x *AB) Type() string
- func (x *AB) VAt(i int) V
- type AF
- func (x *AF) Append(ctx *Context, dst []byte) []byte
- func (x *AF) At(i int) float64
- func (x *AF) Clone() BV
- func (x *AF) DecrRC()
- func (x *AF) IncrRC()
- func (x *AF) Len() int
- func (x *AF) Less(i, j int) bool
- func (x *AF) LessT(y BV) bool
- func (x *AF) MarkImmutable()
- func (x *AF) Matches(y BV) bool
- func (x *AF) Slice() []float64
- func (x *AF) Swap(i, j int)
- func (x *AF) Type() string
- func (x *AF) VAt(i int) V
- type AI
- func (x *AI) Append(ctx *Context, dst []byte) []byte
- func (x *AI) At(i int) int64
- func (x *AI) Clone() BV
- func (x *AI) DecrRC()
- func (x *AI) IncrRC()
- func (x *AI) Len() int
- func (x *AI) Less(i, j int) bool
- func (x *AI) LessT(y BV) bool
- func (x *AI) MarkImmutable()
- func (x *AI) Matches(y BV) bool
- func (x *AI) Slice() []int64
- func (x *AI) Swap(i, j int)
- func (x *AI) Type() string
- func (x *AI) VAt(i int) V
- type AS
- func (x *AS) Append(ctx *Context, dst []byte) []byte
- func (x *AS) At(i int) string
- func (x *AS) Clone() BV
- func (x *AS) DecrRC()
- func (x *AS) IncrRC()
- func (x *AS) Len() int
- func (x *AS) Less(i, j int) bool
- func (x *AS) LessT(y BV) bool
- func (x *AS) MarkImmutable()
- func (x *AS) Matches(y BV) bool
- func (x *AS) Slice() []string
- func (x *AS) Swap(i, j int)
- func (x *AS) Type() string
- func (x *AS) VAt(i int) V
- type AV
- func (x *AV) Append(ctx *Context, dst []byte) []byte
- func (x *AV) At(i int) V
- func (x *AV) Clone() BV
- func (x *AV) DecrRC()
- func (x *AV) IncrRC()
- func (x *AV) Len() int
- func (x *AV) Less(i, j int) bool
- func (x *AV) LessT(y BV) bool
- func (x *AV) MarkImmutable()
- func (x *AV) Matches(y BV) bool
- func (x *AV) Slice() []V
- func (x *AV) Swap(i, j int)
- func (x *AV) Type() string
- func (x *AV) VAt(i int) V
- type Array
- type BV
- 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(loc string, s string) error
- func (ctx *Context) Eval(s string) (V, error)
- func (ctx *Context) EvalPackage(s, loc, pfx string) (V, error)
- func (ctx *Context) GetGlobal(name string) (V, bool)
- func (ctx *Context) GetVariadic(name string) (V, VariadicFun)
- 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 D
- func (d *D) Append(ctx *Context, dst []byte) []byte
- func (d *D) Clone() BV
- func (d *D) DecrRC()
- func (d *D) IncrRC()
- func (d *D) Keys() V
- func (d *D) Len() int
- func (d *D) Less(i, j int) bool
- func (d *D) LessT(y BV) bool
- func (d *D) MarkImmutable()
- func (d *D) Matches(y BV) bool
- func (d *D) Swap(i, j int)
- func (d *D) Type() string
- func (d *D) Values() V
- type ErrPackageImported
- type IdentType
- type PanicError
- type RefCounter
- type S
- type Scanner
- type Token
- type TokenType
- type V
- func Canonical(x V) V
- func Errorf(format string, a ...interface{}) V
- func NewAB(x []byte) V
- func NewAF(x []float64) V
- func NewAI(x []int64) V
- func NewAS(x []string) V
- func NewAV(x []V) V
- func NewD(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(x BV) V
- func Panicf(format string, a ...interface{}) V
- func (x V) Append(ctx *Context, dst []byte) []byte
- func (x V) BV() BV
- func (x V) Clone() V
- func (x V) DecrRC()
- func (x V) Error() V
- func (x V) F() float64
- func (x V) HasRC() bool
- func (x V) I() int64
- func (x V) IncrRC()
- func (x V) IsBV() bool
- func (x V) IsCallable() bool
- func (x V) IsError() bool
- func (x V) IsF() bool
- func (x V) IsFalse() bool
- func (x V) IsFunction() bool
- func (x V) IsI() bool
- func (x V) IsPanic() bool
- func (x V) IsTrue() bool
- func (x V) Len() int
- func (x V) LessT(y V) bool
- func (x V) MarkImmutable()
- func (x V) Matches(y V) bool
- func (x V) Panic() string
- func (x V) Rank(ctx *Context) int
- func (x V) Sprint(ctx *Context) string
- func (x V) Type() string
- func (x V) VAt(i int) V
- type VariadicFun
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type A ¶ added in v0.19.0
type A[T any] struct { // contains filtered or unexported fields }
A is a generic type used to represent arrays. Only specific instantiations implement the BV and Array interfaces.
type AB ¶
AB represents an array of bytes. From Goal's perspective, it's the same as AI. It's used as an optimization to save space for small-integers, in particular for arrays of booleans (0s and 1s).
func (*AB) Append ¶ added in v0.6.0
Append appends a unique program representation of the value to dst, and returns the extended buffer.
func (*AB) Clone ¶ added in v0.19.0
Clone returns a clone of the value. Note that the cloned value might still share some structures with its parent if they're deemed reusable.
func (*AB) DecrRC ¶
func (x *AB) DecrRC()
DecrRC decrements the reference count by one, or zero if it is already non positive.
func (*AB) IsBoolean ¶ added in v0.16.0
IsBoolean returns true when the array of bytes is known to contain only 1s and 0s.
func (*AB) MarkImmutable ¶ added in v0.19.0
func (x *AB) MarkImmutable()
MarkImmutable marks the value as definitively non-reusable.
func (*AB) Slice ¶
Slice returns the underlying immutable slice of values. It should not be modified.
type AF ¶
AF represents an array of float64 values.
func (*AF) Append ¶ added in v0.6.0
Append appends a unique program representation of the value to dst, and returns the extended buffer.
func (*AF) Clone ¶ added in v0.19.0
Clone returns a clone of the value. Note that the cloned value might still share some structures with its parent if they're deemed reusable.
func (*AF) DecrRC ¶
func (x *AF) DecrRC()
DecrRC decrements the reference count by one, or zero if it is already non positive.
func (*AF) MarkImmutable ¶ added in v0.19.0
func (x *AF) MarkImmutable()
MarkImmutable marks the value as definitively non-reusable.
func (*AF) Slice ¶
Slice returns the underlying immutable slice of values. It should not be modified.
type AI ¶
AI represents an array of int64 values.
func (*AI) Append ¶ added in v0.6.0
Append appends a unique program representation of the value to dst, and returns the extended buffer.
func (*AI) Clone ¶ added in v0.19.0
Clone returns a clone of the value. Note that the cloned value might still share some structures with its parent if they're deemed reusable.
func (*AI) DecrRC ¶
func (x *AI) DecrRC()
DecrRC decrements the reference count by one, or zero if it is already non positive.
func (*AI) MarkImmutable ¶ added in v0.19.0
func (x *AI) MarkImmutable()
MarkImmutable marks the value as definitively non-reusable.
func (*AI) Slice ¶
Slice returns the underlying immutable slice of values. It should not be modified.
type AS ¶
AS represents an array of strings.
func (*AS) Append ¶ added in v0.6.0
Append appends a unique program representation of the value to dst, and returns the extended buffer.
func (*AS) Clone ¶ added in v0.19.0
Clone returns a clone of the value. Note that the cloned value might still share some structures with its parent if they're deemed reusable.
func (*AS) DecrRC ¶
func (x *AS) DecrRC()
DecrRC decrements the reference count by one, or zero if it is already non positive.
func (*AS) MarkImmutable ¶ added in v0.19.0
func (x *AS) MarkImmutable()
MarkImmutable marks the value as definitively non-reusable.
func (*AS) Slice ¶
Slice returns the underlying immutable slice of values. It should not be modified.
type AV ¶
AV represents a generic array. The elements of a generic array are marked as immutable, and they should not be representable together in a specialized array. In other words, it should be the canonical form of the array.
func (*AV) Append ¶ added in v0.6.0
Append appends a unique program representation of the value to dst, and returns the extended buffer.
func (*AV) Clone ¶ added in v0.19.0
Clone returns a clone of the value. Note that the cloned value might still share some structures with its parent if they're deemed reusable.
func (*AV) DecrRC ¶
func (x *AV) DecrRC()
DecrRC decrements the reference count by one, or zero if it is already non positive.
func (*AV) MarkImmutable ¶ added in v0.19.0
func (x *AV) MarkImmutable()
MarkImmutable marks the value as definitively non-reusable.
func (*AV) Slice ¶
Slice returns the underlying immutable slice of values. It should not be modified.
type Array ¶ added in v0.19.0
type Array interface { RefCounter sort.Interface // Len returns the value's length. Len() int // VAt returns the value at index i, assuming it's not out of bounds. VAt(i int) V // contains filtered or unexported methods }
Array interface is satisfied by the different kinds of supported array values.
type BV ¶ added in v0.19.0
type BV interface { // Matches returns true if the value matches another (in the sense of // the ~ operator). Matches(BV) bool // Append appends a unique program representation of the value to dst, // and returns the extended buffer. It should not store the returned // buffer elsewhere, so that it's possible to safely convert it to // string without allocations. Append(ctx *Context, dst []byte) []byte // Type returns the name of the value's type. It may be used by LessT to // sort non-comparable values using lexicographic order. This means // Type should return different values for non-comparable values. Type() string // LessT 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, // that is, irreflexive (~x<x), asymmetric (if x<y then ~y<x), // transitive, connected (different values are comparable, except // NaNs). LessT(BV) bool }
BV is the interface satisfied by all boxed values.
type Context ¶
type Context struct { Log io.Writer // output writer for logging with \expr and rt.log Prec int // floating point formatting precision (default: -1) OFS string // output field separator (default: " ") // contains filtered or unexported fields }
Context holds the state of the interpreter. Context values have to be created with NewContext.
func NewContext ¶
func NewContext() *Context
NewContext returns a new context for compiling and interpreting code, with default parameters.
func (*Context) ApplyN ¶
ApplyN calls a value with one or more arguments. The arguments should be provided in stack order, as in the right to left semantics used by the language: the first argument is the last element.
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 loc argument is the location used for error reporting and represents, usually a filename.
func (*Context) Eval ¶
Eval calls Compile with the given string and an empty location, and then Run. You cannot call it within a variadic function, as 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 s as source, loc as error location (used for caching too, usually a filename), pfx as prefix for global variables (usually a filename without the extension), and then Run. If a package with same location has already been evaluated, it returns ErrPackageImported. This means that even though Goal allows to evaluate (also via import) with the same location several times (which can be useful if separate files using the same package can be used together or alone), only the first one counts. 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) GetVariadic ¶ added in v0.10.0
func (ctx *Context) GetVariadic(name string) (V, VariadicFun)
GetVariadic returns the variadic value registered with a given keyword or symbol, along its associated variadic function. It returns a zero value and nil function if there is no registered variadic with such 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 D ¶ added in v0.19.0
type D struct {
// contains filtered or unexported fields
}
D represents a dictionary.
func (*D) Append ¶ added in v0.19.0
Append appends a unique program representation of the value to dst, and returns the extended buffer.
func (*D) Clone ¶ added in v0.19.0
Clone returns a clone of the value. Note that the cloned value might still share some structures with its parent if they're deemed reusable.
func (*D) DecrRC ¶ added in v0.19.0
func (d *D) DecrRC()
DecrRC decrements the reference count of both the key and value arrays by one, or zero if they are already non positive.
func (*D) IncrRC ¶ added in v0.19.0
func (d *D) IncrRC()
IncrRC increments the reference count of both the key and value arrays by one.
func (*D) Len ¶ added in v0.19.0
Len returns the length of the dictionary, that is the common length to its key and value arrays.
func (*D) MarkImmutable ¶ added in v0.19.0
func (d *D) MarkImmutable()
MarkImmutable marks the value as definitively non-reusable.
type ErrPackageImported ¶
type ErrPackageImported struct{}
ErrPackageImported is returned by EvalPackage for packages that have already been processed (same location).
func (ErrPackageImported) Error ¶
func (e ErrPackageImported) Error() string
type IdentType ¶ added in v0.10.0
type IdentType int
IdentType 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 RefCounter ¶
type RefCounter interface { BV // IncrRC increments the reference count by one. IncrRC() // DecrRC decrements the reference count by one, or zero if it is // already non-positive. DecrRC() // MarkImmutable marks the value as definitively non-reusable, even if // the reference counter is less than one. Extensions might use this // function to keep a value around without having to track its // reference count anymore. MarkImmutable() // Clone returns a clone of the value. Note that the cloned value might // still share some structures with its parent if they're deemed // reusable. Clone() BV }
RefCounter is implemented by values that use a reference count. In goal the refcount is not used for memory management, but only for optimization purposes. 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, unless it was marked as immutable.
When defining a new type implementing the BV 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.
func (S) Append ¶ added in v0.6.0
Append appends a unique program representation of the value to dst, and returns the extended buffer.
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 variadic functions have to return results in canonical form, so this function can be used to ensure that when defining new ones.
func NewAV ¶
NewAV returns a new array from a slice of generic values. The result value will be an array in canonical form.
func NewD ¶ added in v0.19.0
NewD returns a dictionary. Both keys and values should be arrays, and they should have the same length.
func (V) Append ¶ added in v0.6.0
Append appends a unique program representation of the value to dst, and returns the extended buffer.
func (V) BV ¶ added in v0.19.0
BV retrieves the boxed value, or nil if the value is not boxed. You can check whether the value is boxed with IsBV(v).
func (V) Clone ¶ added in v0.2.0
Clone returns a clone of the value. Note that the cloned value might still share some structures with its parent if they're deemed reusable.
func (V) HasRC ¶ added in v0.2.0
HasRC returns true if the value is boxed and implements RefCounter.
func (V) IsBV ¶ added in v0.19.0
IsBV returns true if the value is a boxed value satisfying the BV interface. You can then get the value with the BV method.
func (V) IsCallable ¶ added in v0.10.0
IsCallable returns true if the value can be called with one or more arguments. This is true for functions, arrays, strings and regexps, for example.
func (V) IsFalse ¶ added in v0.10.0
IsFalse returns true for false values, that is zero numbers, empty strings, zero-length values, and errors.
func (V) IsFunction ¶
IsFunction returns true if the value is some kind of function.
func (V) IsTrue ¶ added in v0.10.0
IsTrue returns true for true values, that is non-zero numbers, non-empty strings, and non-zero length values that are not errors.
func (V) LessT ¶ added in v0.10.0
LessT returns true if x is ordered before y. It represents a strict total order (except non-strict for NaNs). Values are ordered as follows: unboxed atoms first (numbers, 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) MarkImmutable ¶ added in v0.19.0
func (x V) MarkImmutable()
MarkImmutable marks the value as definitively non-reusable.
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 depends on the verb and adverb
type VariadicFun ¶
VariadicFun represents a variadic function. The array of arguments is in stack order: the first argument is its last element.
Source Files ¶
- adverbs.go
- adverbspecial.go
- amend.go
- amendspecial.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
- json.go
- less.go
- math.go
- mathm.go
- opcode.go
- parser.go
- radix.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
Directories ¶
Path | Synopsis |
---|---|
Package cmd provides a quick way to create derived interpreters.
|
Package cmd provides a quick way to create derived interpreters. |
Package os provides variadic function definitions for IO/OS builtins.
|
Package os provides variadic function definitions for IO/OS builtins. |