package goal
import "codeberg.org/anaseto/goal"
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 or keywords using the RegisterMonad and RegisterDyad methods.
See programs under cmd/* and extension packages like os or io/fs for usage examples. You may have a look at tests in context_test.go too. Also, the documentation provides a "Writing an extension" tutorial chapter.
Index ¶
- type A
- type AB
- func (x *AB) Append(_ *Context, dst []byte, _ bool) []byte
- func (x *AB) At(i int) V
- 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) MarshalJSON() ([]byte, error)
- func (x *AB) Matches(y BV) bool
- func (x *AB) Reusable() bool
- func (x *AB) Swap(i, j int)
- func (x *AB) Type() string
- type AF
- func (x *AF) Append(ctx *Context, dst []byte, _ bool) []byte
- func (x *AF) At(i int) V
- 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) MarshalJSON() ([]byte, error)
- func (x *AF) Matches(y BV) bool
- func (x *AF) Reusable() bool
- func (x *AF) Swap(i, j int)
- func (x *AF) Type() string
- type AI
- func (x *AI) Append(_ *Context, dst []byte, _ bool) []byte
- func (x *AI) At(i int) V
- 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) MarshalJSON() ([]byte, error)
- func (x *AI) Matches(y BV) bool
- func (x *AI) Reusable() bool
- func (x *AI) Swap(i, j int)
- func (x *AI) Type() string
- type AS
- func (x *AS) Append(_ *Context, dst []byte, _ bool) []byte
- func (x *AS) At(i int) V
- 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) MarshalJSON() ([]byte, error)
- func (x *AS) Matches(y BV) bool
- func (x *AS) Reusable() bool
- func (x *AS) Swap(i, j int)
- func (x *AS) Type() string
- type AV
- func (x *AV) Append(ctx *Context, dst []byte, compact bool) []byte
- func (x *AV) At(i int) V
- 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) MarshalJSON() ([]byte, error)
- func (x *AV) Matches(y BV) bool
- func (x *AV) Reusable() bool
- func (x *AV) Swap(i, j int)
- func (x *AV) Type() string
- type Array
- type BV
- type Callable
- type Context
- func NewContext() *Context
- func (ctx *Context) AssignGlobal(name string, x V)
- func (ctx *Context) AssignedLast() bool
- func (ctx *Context) Compile(s, loc, pfx string) error
- func (ctx *Context) Eval(s string) (V, error)
- func (ctx *Context) EvalPackage(s, loc, pfx string) (V, error)
- func (ctx *Context) Get(key string) any
- func (ctx *Context) GetGlobal(name string) (V, bool)
- func (ctx *Context) GetVariadic(name string) (V, VariadicFunc)
- func (ctx *Context) GlobalNames(names []string) []string
- func (ctx *Context) Keywords(names []string) []string
- func (ctx *Context) RegisterDyad(name string, vf VariadicFunc) V
- func (ctx *Context) RegisterExtension(name, version string)
- func (ctx *Context) RegisterMonad(name string, vf VariadicFunc) V
- func (ctx *Context) Run() (V, error)
- func (ctx *Context) Set(key string, value any)
- func (ctx *Context) String() string
- func (ctx *Context) Version() string
- type D
- func (d *D) Append(ctx *Context, dst []byte, compact bool) []byte
- func (d *D) Get(x V) (V, bool)
- func (d *D) GetS(x string) (V, bool)
- func (d *D) KeyArray() Array
- func (d *D) Keys() V
- func (d *D) Len() int
- func (d *D) LessT(y BV) bool
- func (d *D) MarkImmutable()
- func (d *D) MarshalJSON() ([]byte, error)
- func (d *D) Matches(y BV) bool
- func (d *D) Type() string
- func (d *D) ValueArray() Array
- func (d *D) Values() V
- type ErrPackageImported
- type Error
- func (e *Error) Append(ctx *Context, dst []byte, compact bool) []byte
- func (e *Error) LessT(y BV) bool
- func (e *Error) Matches(y BV) bool
- func (e *Error) Msg(ctx *Context) string
- func (e *Error) Type() string
- func (e *Error) Value() V
- type OrderedBV
- type Panic
- func (e *Panic) Append(_ *Context, dst []byte, _ bool) []byte
- func (e *Panic) Error() string
- func (e *Panic) ErrorStack() string
- func (e *Panic) LessT(y BV) bool
- func (e *Panic) Matches(y BV) bool
- func (e *Panic) Type() string
- func (e *Panic) Unwrap() error
- func (e *Panic) WithPrefix(pfx string) *Panic
- type R
- func (rx *R) Append(_ *Context, dst []byte, _ bool) []byte
- func (rx *R) LessT(y BV) bool
- func (rx *R) Matches(x BV) bool
- func (rx *R) Regexp() *regexp.Regexp
- func (rx *R) Type() string
- type S
- func (s S) Append(_ *Context, dst []byte, _ bool) []byte
- func (s S) LessT(y BV) bool
- func (s S) MarshalJSON() ([]byte, error)
- func (s S) Matches(y BV) bool
- func (s S) Type() string
- type V
- func Apply2FloatFunc(x, y V, f func(float64, float64) float64) V
- func ApplyFloatFunc(x V, f func(float64) float64) V
- func Errorf(format string, a ...any) 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 NewDict(keys, values Array) V
- func NewError(x V) V
- func NewF(f float64) V
- func NewGap() V
- func NewI(i int64) V
- func NewPanic(msg string) V
- func NewPanicError(err error) V
- func NewPanicV(x *Panic) V
- func NewProjection(x V, args []V) V
- func NewR(re *regexp.Regexp) V
- func NewS(s string) V
- func NewV(x BV) V
- func Panicf(format string, a ...any) V
- func (x V) Append(ctx *Context, dst []byte, compact bool) []byte
- func (x V) Apply(ctx *Context, args ...V) V
- func (x V) ApplyAt(ctx *Context, y V) V
- func (x V) BV() BV
- func (x V) Error() *Error
- func (x V) F() float64
- func (x V) I() int64
- 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) IsGap() bool
- func (x V) IsI() bool
- func (x V) IsPanic() bool
- func (x V) IsProjection() bool
- func (x V) IsTrue() bool
- func (x V) Len() int
- func (x V) LessT(y V) bool
- func (x V) MarkImmutable() V
- func (x V) MarshalJSON() ([]byte, error)
- func (x V) Matches(y V) bool
- func (x V) Panic() *Panic
- func (x V) Rank(ctx *Context) int
- func (x V) Sprint(ctx *Context, compact bool) string
- func (x V) String() string
- func (x V) Type() string
- type VariadicFunc
Types ¶
type A ¶
type A[T any] struct { // Slice is the underlying immutable slice of values. It should not be // modified unless Reusable returns true, and in such a case, you should mark // any new stored values with MarkImmutable (except for values that are // always immutable, like strings or unboxed values). Slice []T // 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 ¶
Append appends a unique program representation of the value to dst, and returns the extended buffer.
func (*AB) At ¶
At returns the value at index i, assuming it's not out of bounds.
func (*AB) IsBoolean ¶
IsBoolean reports whether the array of bytes is known to contain only 1s and 0s.
func (*AB) Len ¶
Len returns the length of the array.
func (*AB) Less ¶
Less satisfies the specification of sort.Interface.
func (*AB) LessT ¶
LessT satisfies the specification of the BV interface.
func (*AB) MarkImmutable ¶
func (x *AB) MarkImmutable()
MarkImmutable marks the value as definitively non-reusable. You can use this method to tell the optimizer that it cannot reuse its memory anymore. In particular, you should call this method before storing a value within a user-defined boxed value type.
func (*AB) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
func (*AB) Matches ¶
Matches reports whether the two values match like in x~y.
func (*AB) Reusable ¶
Reusable reports whether the array value and its underlying slice are reusable. Note that reusable values can be modified in-place by some operations, so use MarkImmutable if you want to avoid any such mutations. Always check Reusable before modifying an Array.
func (*AB) Swap ¶
Swap satisfies the specification of sort.Interface.
func (*AB) Type ¶
Type returns the name of the value's type ("I").
type AF ¶
AF represents an array of float64 values.
func (*AF) Append ¶
Append appends a unique program representation of the value to dst, and returns the extended buffer.
func (*AF) At ¶
At returns the value at index i, assuming it's not out of bounds.
func (*AF) Len ¶
Len returns the length of the array.
func (*AF) Less ¶
Less satisfies the specification of sort.Interface.
func (*AF) LessT ¶
LessT satisfies the specification of the BV interface.
func (*AF) MarkImmutable ¶
func (x *AF) MarkImmutable()
MarkImmutable marks the value as definitively non-reusable. You can use this method to tell the optimizer that it cannot reuse its memory anymore. In particular, you should call this method before storing a value within a user-defined boxed value type.
func (*AF) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
func (*AF) Matches ¶
Matches reports whether the two values match like in x~y.
func (*AF) Reusable ¶
Reusable reports whether the array value and its underlying slice are reusable. Note that reusable values can be modified in-place by some operations, so use MarkImmutable if you want to avoid any such mutations. Always check Reusable before modifying an Array.
func (*AF) Swap ¶
Swap satisfies the specification of sort.Interface.
func (*AF) Type ¶
Type returns the name of the value's type ("N").
type AI ¶
AI represents an array of int64 values.
func (*AI) Append ¶
Append appends a unique program representation of the value to dst, and returns the extended buffer.
func (*AI) At ¶
At returns the value at index i, assuming it's not out of bounds.
func (*AI) Len ¶
Len returns the length of the array.
func (*AI) Less ¶
Less satisfies the specification of sort.Interface.
func (*AI) LessT ¶
LessT satisfies the specification of the BV interface.
func (*AI) MarkImmutable ¶
func (x *AI) MarkImmutable()
MarkImmutable marks the value as definitively non-reusable. You can use this method to tell the optimizer that it cannot reuse its memory anymore. In particular, you should call this method before storing a value within a user-defined boxed value type.
func (*AI) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
func (*AI) Matches ¶
Matches reports whether the two values match like in x~y.
func (*AI) Reusable ¶
Reusable reports whether the array value and its underlying slice are reusable. Note that reusable values can be modified in-place by some operations, so use MarkImmutable if you want to avoid any such mutations. Always check Reusable before modifying an Array.
func (*AI) Swap ¶
Swap satisfies the specification of sort.Interface.
func (*AI) Type ¶
Type returns the name of the value's type ("I").
type AS ¶
AS represents an array of strings.
func (*AS) Append ¶
Append appends a unique program representation of the value to dst, and returns the extended buffer.
func (*AS) At ¶
At returns the value at index i, assuming it's not out of bounds.
func (*AS) Len ¶
Len returns the length of the array.
func (*AS) Less ¶
Less satisfies the specification of sort.Interface.
func (*AS) LessT ¶
LessT satisfies the specification of the BV interface.
func (*AS) MarkImmutable ¶
func (x *AS) MarkImmutable()
MarkImmutable marks the value as definitively non-reusable. You can use this method to tell the optimizer that it cannot reuse its memory anymore. In particular, you should call this method before storing a value within a user-defined boxed value type.
func (*AS) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
func (*AS) Matches ¶
Matches reports whether the two values match like in x~y.
func (*AS) Reusable ¶
Reusable reports whether the array value and its underlying slice are reusable. Note that reusable values can be modified in-place by some operations, so use MarkImmutable if you want to avoid any such mutations. Always check Reusable before modifying an Array.
func (*AS) Swap ¶
Swap satisfies the specification of sort.Interface.
func (*AS) Type ¶
Type returns the name of the value's type ("S").
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 ¶
Append appends a unique program representation of the value to dst, and returns the extended buffer.
func (*AV) At ¶
At returns the value at index i, assuming it's not out of bounds.
func (*AV) Len ¶
Len returns the length of the array.
func (*AV) Less ¶
Less satisfies the specification of sort.Interface.
func (*AV) LessT ¶
LessT satisfies the specification of the BV interface.
func (*AV) MarkImmutable ¶
func (x *AV) MarkImmutable()
MarkImmutable marks the value as definitively non-reusable. You can use this method to tell the optimizer that it cannot reuse its memory anymore. In particular, you should call this method before storing a value within a user-defined boxed value type.
func (*AV) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
func (*AV) Matches ¶
Matches reports whether the two values match like in x~y.
func (*AV) Reusable ¶
Reusable reports whether the array value and its underlying slice are reusable. Note that reusable values can be modified in-place by some operations, so use MarkImmutable if you want to avoid any such mutations. Always check Reusable before modifying an Array.
func (*AV) Swap ¶
Swap satisfies the specification of sort.Interface.
func (*AV) Type ¶
Type returns the name of the value's type ("A").
type Array ¶
type Array interface { OrderedBV sort.Interface // At returns the value at index i, assuming it's not out of bounds. At(i int) V // Len returns the value's length. Len() int // 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() // Reusable reports whether the array value and its underlying slice are // reusable. Note that reusable values can be modified in-place by some // operations, so use MarkImmutable if you want to avoid any such mutations. // Always check Reusable before modifying an Array. Reusable() bool // contains filtered or unexported methods }
Array interface is satisfied by the different kinds of supported built-in array values. It can be used to handle arrays in a generic way when appropriate. Note that Array values also satisfy the BV interface.
func NewArray ¶
NewArray returns a new array from a slice of generic values. The result value will be an array in canonical form. If the values are all of the same specialized type, like integers, floats, or strings, the underlying array will use a specialized representation, instead of a generic one.
It is the same as NewAV, but returns an Array value instead of a generic value.
type BV ¶
type BV interface { // 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. The compact boolean determines whether // insignificant whitespace should be removed or not. If passed a nil // context, Append should produce a unique string suitable for hashing. Append(ctx *Context, dst []byte, compact bool) []byte // Matches reports whether the two values match like in x~y. Matches(BV) bool // Type returns the name of the value's type. It may be used to compare // unordered values using lexicographic order. This means Type should // return different values for non-comparable values. Type() string }
BV is the interface satisfied by all boxed values. It can be satisfied by user-defined types.
Ordered boxed values can also satisfy the OrderedBV interface, so that they are supported by sort and bins.
Types that are not comparable or ordered in a meaningful way may implement Append with nil Context without fully respecting the unique hash requirements: the only consequence is that search and self-search primitives will produce unspecified results when working on such values.
type Callable ¶
type Callable interface { BV // Apply calls the value with one or more arguments. The arguments are // provided in stack order, as in the right to left semantics used by // the language: the first argument is the last element. Apply(*Context, []V) V }
Callable represents user-defined boxed values that can be applied. To check if any kind of value (built-in or user-defined) is callable, use the IsCallable method for type V.
type Context ¶
type Context struct { // Log is the output writer for logging with \expr and rt.log (defaults // to io.Discard). Log io.Writer // Rand is the pseudo-random number generator. If unset, a PCG // generator is initialized on first use with a random seed obtained // from the hash/maphash package. Rand *rand.Rand // 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) AssignGlobal ¶
AssignGlobal assigns a value to a global variable name.
func (*Context) AssignedLast ¶
AssignedLast reports whether the last executed expression was an assignment. The intended use for this method is avoiding echo in REPL in such a case.
func (*Context) Compile ¶
Compile parses and compiles code from the given source string in the current context. The loc argument is the location used for error reporting and represents usually a filename. The pfx is the default dot prefix for globals (an empty pfx disables dot prefix). For non-empty locations, Compile handles and skips shebang line (if any). Also, if the same non-empty location was already compiled with the same global prefix, an error ErrPackageImported is returned.
You cannot call it within a variadic function, as some information of the current execution state would be lost. Use Eval or EvalPackage instead in such case, as they save and restore information related to the current execution state.
func (*Context) Eval ¶
Eval calls Compile with the given string and an empty location and prefix, and then Run.
The package's current execution state is saved and then restored, so unlike Compile and Run, the Eval method is safe to call within a variadic function.
func (*Context) EvalPackage ¶
EvalPackage calls Compile with the string s as source, loc as error location (usually a filename), pfx as prefix for global variables (usually a filename without the extension), and then Run. If a package with same location and prefix has already been evaluated, it returns ErrPackageImported. This means that even though Goal allows, for convenience, to evaluate or import with the same location and prefix several times, only the first one counts.
The package's current execution state is saved and then restored, so unlike Compile and Run, the EvalPackage method is safe to call within a variadic function.
func (*Context) Get ¶
Get returns the value corresponding to the given key, as previously set with the Set method.
func (*Context) GetGlobal ¶
GetGlobal returns the value attached to a global variable with the given name.
func (*Context) GetVariadic ¶
func (ctx *Context) GetVariadic(name string) (V, VariadicFunc)
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) GlobalNames ¶
GlobalNames returns a string slice with the names of all global variables. The provided argument is used as a buffer for the result if it's large enough.
Note that the list of names does not include the names of globals that have not been assigned yet.
func (*Context) Keywords ¶
Keywords returns a string slice of all keywords. The provided argument is used as a buffer for the result if it's large enough.
func (*Context) RegisterDyad ¶
func (ctx *Context) RegisterDyad(name string, vf VariadicFunc) V
RegisterDyad adds a variadic function to the context, and generates a new dyadic keyword or operator for that variadic (parsing will search for a left argument). The variadic is also returned as a value.
The given name may be an invalid identifier. This is useful is you want to register a new variadic function and store it in a global without introducing new syntax. The name is still used when printing the variadic function value. It it also used to identify the variadic function in methods like GetVariadic or Keywords.
You cannot call it within a variadic function.
func (*Context) RegisterExtension ¶
RegisterExtension informs rt.get"version" about an extension. The name should be a valid identifier, and the optional version should be of the form v1.2.3.
func (*Context) RegisterMonad ¶
func (ctx *Context) RegisterMonad(name string, vf VariadicFunc) V
RegisterMonad adds a variadic function to the context, and generates a new monadic keyword or operator for that variadic (parsing will not search for a left argument). The variadic is also returned as a value. Note that while 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. Also, this function cannot be used to change the dyadic nature of builtin symbols.
The given name may be an invalid identifier. This is useful is you want to register a new variadic function and store it in a global without introducing new syntax. The name is still used when printing the variadic function value. It it also used to identify the variadic function in methods like GetVariadic or Keywords.
You cannot call it within a variadic function.
func (*Context) Run ¶
Run runs the last compiled code in the current context, and returns the result value (if any).
Like Compile, you cannot call it within a variadic function.
func (*Context) Set ¶
Set associates an arbitrary value with a given string key. Use this method to carry values used in variadic functions. It is recommended to use key strings with a unique prefix to avoid naming conflicts.
func (*Context) String ¶
String returns a string representation of the compiled program and other relevant context data for debugging purposes.
func (*Context) Version ¶
Version returns the same version string as rt.get"v". It follows the pattern "version +extname [version] ...".
type D ¶
type D struct {
// contains filtered or unexported fields
}
D represents a dictionary.
func (*D) Append ¶
Append appends a unique program representation of the value to dst, and returns the extended buffer.
func (*D) Get ¶
Get is a convenience method that returns either the value associated to the first occurrence of the given key along a true boolean, or a zero value along a false boolean otherwise. This performs a simple generic linear search. If performance matters, you may prefer to use ApplyAt on the dict value, which works like indexing in Goal and allows to retrieve efficiently several values at once, but returns a zero value on outdexing and requires enlisting non-atom values if you want to search for them as a whole.
func (*D) GetS ¶
GetS is like Get but specialized to the case of a string key. It also performs a simple linear search, but may be faster than Get if the dict keys are all strings.
func (*D) KeyArray ¶
KeyArray returns the keys of the dictionary as an Array value.
func (*D) Keys ¶
Keys returns the keys of the dictionary as a generic value.
func (*D) Len ¶
Len returns the length of the dictionary, that is the common length to its key and value arrays.
func (*D) LessT ¶
LessT satisfies the specification of the BV interface.
func (*D) MarkImmutable ¶
func (d *D) MarkImmutable()
MarkImmutable marks the value as definitively non-reusable. You can use this method to tell the optimizer that it cannot reuse its memory anymore. In particular, you should call this method before storing a value within a user-defined boxed value type.
func (*D) MarshalJSON ¶
MarshalJSON implements json.Marshaler. Note that not all generic values can be marshalled, only unboxed integers and floats, as well as boxed values implementing json.Marshaler. Also, nested lists are not supported for keys. The json builtin in Goal is more tolerant and stringifies others kinds of values before encoding.
func (*D) Matches ¶
Matches reports whether the two values match like in x~y.
func (*D) Type ¶
Type returns the name of the value's type.
func (*D) ValueArray ¶
ValueArray returns the values of the dictionary as an Array value.
func (*D) Values ¶
Values returns the values of the dictionary as a generic value.
type ErrPackageImported ¶
type ErrPackageImported struct{}
ErrPackageImported is returned by EvalPackage and Compile for packages that have already been processed (same prefix and location).
func (ErrPackageImported) Error ¶
func (e ErrPackageImported) Error() string
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error represents a recoverable error. It may contain some goal value of any kind.
func (*Error) Append ¶
Append appends a unique program representation of the value to dst, and returns the extended buffer.
Note that the program representation is not the same as the error message: use the Msg method for that.
func (*Error) LessT ¶
LessT satisfies the specification of the BV interface.
func (*Error) Matches ¶
Matches reports whether the two values match like in x~y.
func (*Error) Msg ¶
Msg returns the error message as in e..msg.
func (*Error) Type ¶
Type returns the type of an error value ("e").
func (*Error) Value ¶
Value returns the underlying error value as in (.e).
type OrderedBV ¶
type OrderedBV interface { BV // LessT reports whether 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. LessT(BV) bool }
OrderedBV is the interface satisfied by ordered boxed values. It is used by sort and (self-)search primitives.
type Panic ¶
type Panic struct { Err error // error message (without location) // contains filtered or unexported fields }
Panic represents a fatal error returned by any Context method.
While Panic also satisfies the BV interface, you shouldn't create generic panic values from a Panic using NewV but NewPanicV instead. Panic is a special kind of boxed value that has to be marked specially so that checking for panics can efficiently be handled by the runtime.
Note that Panic values are not meant to be used explicitly from Goal code. In particular, they should not be assigned to variables or stored in generic arrays: use an Error value instead for such purposes.
func (*Panic) Append ¶
Append appends a unique program representation of the value to dst, and returns the extended buffer.
func (*Panic) Error ¶
Error returns the panic message without location information.
func (*Panic) ErrorStack ¶
ErrorStack returns the full panic message with locations and source information obtained from its running context.
func (*Panic) LessT ¶
LessT implements BV interface. It is not used in practice for panics.
func (*Panic) Matches ¶
Matches implements BV interface. It is not used in practice for panics.
func (*Panic) Type ¶
Type implements BV interface. It is not used in practice for panics.
func (*Panic) Unwrap ¶
Unwrap returns the underlying error.
func (*Panic) WithPrefix ¶
WithPrefix adds a prefix to the error message and returns the modified Panic value. It creates a wrapped error using the %w format verb.
type R ¶
type R struct {
// contains filtered or unexported fields
}
R represents a regular expression as created by the rx builtin.
func (*R) Append ¶
Append appends a unique program representation of the value to dst, and returns the extended buffer.
func (*R) LessT ¶
LessT satisfies the specification of the BV interface.
func (*R) Matches ¶
Matches reports whether the two values match like in x~y.
func (*R) Regexp ¶
Regexp returns the underlying Regexp object.
func (*R) Type ¶
Type returns "r" for regexp atoms.
type S ¶
type S string
S represents an immutable string of bytes.
func (S) Append ¶
Append appends a unique program representation of the value to dst, and returns the extended buffer.
func (S) LessT ¶
LessT satisfies the specification of the BV interface.
func (S) MarshalJSON ¶
MarshalJSON implements json.Marshaler.
func (S) Matches ¶
Matches reports whether the two values match like in x~y.
func (S) Type ¶
Type returns "s" for string atoms.
type V ¶
type V struct {
// contains filtered or unexported fields
}
V contains a generic boxed or unboxed value.
func Apply2FloatFunc ¶
Apply2FloatFunc dyadically applies float math function. It returns a panic on error using MATHDYAD as operator template name in prefix. Its purpose is the same as ApplyFloatFunc, but for math functions taking two floats as arguments.
Within a variadic function, taking math.Pow as example, code using Apply2FloatFunc will typically look as follows:
r := goal.Apply2FloatFunc(x, y, math.Pow) if r.IsPanic() { return goal.NewPanic(strings.ReplaceAll(r.Panic().Error(), "MATHDYAD", "pow")) } return r
func ApplyFloatFunc ¶
ApplyFloatFunc monadically applies float math function. It returns a panic on error without prefix. It can be used when more flexibility than provided by MonadicFloatFunc is needed.
Within a variadic function, taking math.Gamma as example, code using ApplyFloatFunc will typically look as follows:
r := goal.ApplyFloatFunc(x, math.Gamma) if r.IsPanic() { return goal.NewPanicV(r.Panic().WithPrefix("gamma :")) } return r
func Errorf ¶
Errorf formats an error message and returns it as a recoverable error value.
func NewAB ¶
NewAB returns a new byte array.
func NewAF ¶
NewAF returns a new array of float64 values.
func NewAI ¶
NewAI returns a new array of int64 values.
func NewAS ¶
NewAS returns a new array of strings.
func NewAV ¶
NewAV returns a new array from a slice of generic values. The result value will be an array in canonical form. If the values are all of the same specialized type, like integers, floats, or strings, the underlying array will use a specialized representation, instead of a generic one.
It is the same as NewArray, but returns a generic value instead of an Array value.
func NewD ¶
NewD returns a dictionary. Both keys and values should be arrays, and they should have the same length.
It is the same as NewDict, but with generic value inputs instead of Array values.
func NewDict ¶
NewDict returns a dictionary. Both keys and values should have the same length.
It is the same as NewD, but with Array inputs instead of generic values.
func NewError ¶
NewError returns a new recoverable error value. It marks x as immutable.
func NewF ¶
NewF returns a new unboxed float64 value.
func NewGap ¶
func NewGap() V
NewGap returns a new gap value. It is the same as the zero value for type V.
The NewGap function is mostly meant to be used when building projections with NewProjection.
Note that gap values are not meant to be used explicitly from Goal code. Gap values are values generated and used by the interpreter to signal missing data in a few contexts:
- They are used to represent implicit non-fixed arguments in projections.
- They are the default value of undefined globals.
- They are returned by Run and Eval when code did not end returning a value.
Assigning a gap value with AssignGlobal can be used as a means of un-assigning a global, to make any ulterior attempt of using it panic.
func NewI ¶
NewI returns a new unboxed int64 value.
func NewPanic ¶
NewPanic returns a new panic value with given message.
func NewPanicError ¶
NewPanicError returns a new panic value from given error. If the error is already of type *Panic any underlying location information is preserved.
func NewPanicV ¶
NewPanicV returns a new panic value.
func NewProjection ¶
NewProjection returns a projection of x (usually a function) with the given arguments (in stack order). Gap values in args represent non-fixed arguments.
func NewR ¶
NewR returns a new regular expression value.
func NewS ¶
NewS returns a new string value.
func NewV ¶
NewV returns a new boxed value. For Panic boxed values, use NewPanicV instead.
func Panicf ¶
Panicf formats an error message and returns it as a panic value.
func (V) Append ¶
Append appends a unique program representation of the value to dst, and returns the extended buffer.
func (V) Apply ¶
Apply 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 (V) ApplyAt ¶
ApplyAt calls a value with a single argument. It is a faster specialized version of Apply for the one-argument case.
func (V) BV ¶
BV retrieves the boxed value, or nil if the value is not boxed. You can check whether the value is boxed with x.IsBV().
func (V) Error ¶
Error retrieves the boxed error value. It assumes x.IsError().
func (V) F ¶
F retrieves the unboxed float64 value. It assumes x.IsF().
func (V) I ¶
I retrieves the unboxed int64 value. It assumes x.IsI().
func (V) IsBV ¶
IsBV reports whether the value is a boxed value satisfying the BV interface. You can retrieve the underlying boxed value with the BV method.
func (V) IsCallable ¶
IsCallable reports whether the value can be called with one or more arguments. This is true for most values, including non-function ones.
func (V) IsError ¶
IsError reports whether the value is a recoverable error.
func (V) IsF ¶
IsF reports whether the value is a float64 value.
func (V) IsFalse ¶
IsFalse returns true for 0, 0i, -0w, 0n, "", rx//, (:), zero-length values, and errors.
func (V) IsFunction ¶
IsFunction reports whether the value is some kind of built-in function type, like verbs, adverbs, lambdas and projections. Type returns "f" for such values.
func (V) IsGap ¶
IsGap reports whether x represents a gap value.
func (V) IsI ¶
IsI reports whether the value is an int64 value.
func (V) IsPanic ¶
IsPanic reports whether the value is a panic value.
func (V) IsProjection ¶
IsProjection reports whether x is a projection.
func (V) IsTrue ¶
IsTrue returns false for 0, 0i, -0w, 0n, "", rx//, (:), zero-length values, and errors.
func (V) Len ¶
Len returns the length of a value like in #x.
func (V) LessT ¶
LessT reports whether x is ordered before y. It represents a strict total order among ordered values. Values are ordered as follows: unboxed atoms first (numbers with NaNs first, 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.
Boxed values that do not satisfy the OrderedBV interface are compared to others lexicographically by type.
func (V) MarkImmutable ¶
MarkImmutable marks the value as definitively non-reusable. You can use this method to tell the optimizer that it cannot reuse its memory anymore. In particular, you should call this method before storing a value within a user-defined boxed value type.
MarkImmutable returns the marked value. Any underlying boxed value is marked as immutable as a side effect, but the returned value of type V is marked in an extra way for optimization purposes.
func (V) MarshalJSON ¶
MarshalJSON implements json.Marshaler. Note that not all generic values can be marshalled, only unboxed integers and floats, as well as boxed values implementing json.Marshaler. The json builtin in Goal is more tolerant and stringifies others kinds of values before encoding.
func (V) Matches ¶
Matches reports whether the two values match like in x~y.
func (V) Panic ¶
Panic retrieves the boxed panic value. It assumes x.IsPanic().
func (V) Rank ¶
Rank returns the value's rank (default arity). It returns 0 for non-function values. Rank is used in lambda partial application and in some adverbial expressions: see the FAQ's “How does function rank work?” question for details. Currently, ranks are as follows:
monad 1 dyad 2 lambda number of arguments projections number of gaps derived verb depends on the verb and adverb
func (V) Sprint ¶
Sprint returns a matching program string representation of the value. The compact boolean determines whether insignificant whitespace should be removed or not.
func (V) String ¶
String returns a string representation of the value corresponding to Sprint with nil Context and compacting on.
func (V) Type ¶
Type returns the name of the value's type.
type VariadicFunc ¶
VariadicFunc represents a variadic function. The slice of arguments is in stack order: the first argument is the last element.
Most Context methods are available for use in variadic functions, except the ones that could affect the current execution state (Compile, Run).
See RegisterMonad and RegisterDyad for defining new variadic builtins usable from Goal.
func MonadicFloatFunc ¶
func MonadicFloatFunc(name string, f func(float64) float64) VariadicFunc
MonadicFloatFunc returns a new variadic function for a given single-argument floating point math function. The name argument is used in error messages and is usually a monadic keyword. It can be used to easily register new monadic math functions, like for example:
ctx.RegisterMonad("gamma", goal.MonadicFloatFunc("gamma", math.Gamma))
Source Files ¶
adverbs.go adverbspecial.go amend.go amendspecial.go apply.go arithd.go arithm.go arrays.go ast.go cmpsearch.go compiler.go context.go csv.go dicts.go encdec.go errors.go fills.go flip.go fns.go functions.go group.go join.go json.go less.go math.go norm.go opcode.go parser.go parsev.go radix.go rand.go refcount.go regexp.go rt.go sfns.go sort.go stringer.go strings.go time.go unsafe.go utf8.go utils.go valstring.go value.go variadics.go vm.go
Directories ¶
Path | Synopsis |
---|---|
archive | |
archive/zip | Package zip provides zip archive support for Goal. |
cmd | Package cmd provides a quick way to create derived interpreters. |
cmd/goal | Default command-line interpreter for Goal. |
cmd/goal2html | HTML-coloration tool for Goal code based on scan/html. |
cmd/wasm | |
examples | |
examples/goalheap | |
help | Package help exports a function with default help for the core Goal language. |
internal | |
io | |
io/fs | Package fs provides functionality for working with read-only file system values and files in Goal. |
math | Package math provides a convenient way to register extra standard math functions. |
os | Package os provides Goal's default IO/OS builtins. |
scan | Package scan implements a scanner for Goal source text. |
scan/html | Package html provides a simple html highlighter for Goal code working at the token level. |
- Version
- v1.1.0 (latest)
- Published
- Feb 17, 2025
- Platform
- linux/amd64
- Imports
- 25 packages
- Last checked
- 19 hours ago –
Tools for package owners.