goal

package module
v0.33.0 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2024 License: ISC Imports: 18 Imported by: 2

README

Goal

Goal is an embeddable array programming language with a bytecode interpreter, written in Go. The command line intepreter can execute scripts or run in interactive mode. Goal shines the most in common scripting tasks, like handling columnar data or text processing. It is also suitable for exploratory programming.

Install

To install the command line interpreter, you only need to have the go compiler installed. There are no extra dependencies.

You can then build the intepreter with:

go install ./cmd/goal

You may add $(go env GOPATH)/bin to your $PATH (for example export PATH="$PATH:$(go env GOPATH)/bin"), so that your shell can find the goal executable.

Alternatively, you may put the resulting binary in a custom location (usually in your $PATH) by using instead the following command:

go build -o /path/to/bin/goal ./cmd/goal

In both cases, you should now have a goal executable somewhere. Type goal --help for command-line usage.

Executing goal without arguments opens the REPL. For a better experience using the REPL (to get typical keyboard shortcuts), you can install the readline wrapper rlwrap program (available as a package in most systems), and then use instead rlwrap goal.

Examples

Aside from the examples found in the documentation, you'll find:

  • The examples directory contains AOC solutions and a few short scripts.
  • A few short scripts in the lib and testdata/scripts directories. Because the latter are used in regression testing, they come along an expected result after a /RESULT: comment.
  • Various code generation Goal scripts in the toplevel scripts directory.

Here's how a Goal script looks like:

/ Handle command-line arguments: script name + optional file.
(2<#ARGS)and:error"USAGE: goal wordstats.goal [file]"
/ Read STDIN or filename given by last argument; lowercase everything.
src:_ 'read?[1=#ARGS;STDIN;*|ARGS]
/ Get all words (Unicode letters + dashes).
words:rx/[\p{L}-]+/[src;-1]
/ Print number of words; number of distinct words; five most frequent words.
say(#words;#dw:?words;5@!>dw!=%words)

See the tutorial for detailed explanations on a similar example, and with syntax highlighting!

Community

Contribute

User testing and bug reports are welcome! Feel free to open an issue, send a pull request, or send a patch by email.

Tools

  • goal2html: html markup generation for Goal source.

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

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 {

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

type AB A[byte]

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

func (x *AB) Append(ctx *Context, dst []byte, compact bool) []byte

Append appends a unique program representation of the value to dst, and returns the extended buffer.

func (*AB) At

func (x *AB) At(i int) V

At returns the value at index i, assuming it's not out of bounds.

func (*AB) IsBoolean added in v0.16.0

func (x *AB) IsBoolean() bool

IsBoolean returns true when the array of bytes is known to contain only 1s and 0s.

func (*AB) Len

func (x *AB) Len() int

Len returns the length of the array.

func (*AB) Less added in v0.4.0

func (x *AB) Less(i, j int) bool

Less satisfies the specification of sort.Interface.

func (*AB) LessT added in v0.10.0

func (x *AB) LessT(y BV) bool

LessT satisfies the specification of the BV interface.

func (*AB) MarkImmutable added in v0.19.0

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 an user-defined boxed value type.

func (*AB) MarshalJSON added in v0.25.0

func (x *AB) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (*AB) Matches

func (x *AB) Matches(y BV) bool

Matches returns true if the two values match like in x~y.

func (*AB) Reusable added in v0.24.0

func (x *AB) Reusable() bool

Reusable returns true if 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 added in v0.9.0

func (x *AB) Swap(i, j int)

Swap satisfies the specification of sort.Interface.

func (*AB) Type

func (x *AB) Type() string

Type returns the name of the value's type ("I").

type AF

type AF A[float64]

AF represents an array of float64 values.

func (*AF) Append added in v0.6.0

func (x *AF) Append(ctx *Context, dst []byte, compact bool) []byte

Append appends a unique program representation of the value to dst, and returns the extended buffer.

func (*AF) At

func (x *AF) At(i int) V

At returns the value at index i, assuming it's not out of bounds.

func (*AF) Len

func (x *AF) Len() int

Len returns the length of the array.

func (*AF) Less added in v0.4.0

func (x *AF) Less(i, j int) bool

Less satisfies the specification of sort.Interface.

func (*AF) LessT added in v0.10.0

func (x *AF) LessT(y BV) bool

LessT satisfies the specification of the BV interface.

func (*AF) MarkImmutable added in v0.19.0

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 an user-defined boxed value type.

func (*AF) MarshalJSON added in v0.25.0

func (x *AF) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (*AF) Matches

func (x *AF) Matches(y BV) bool

Matches returns true if the two values match like in x~y.

func (*AF) Reusable added in v0.24.0

func (x *AF) Reusable() bool

Reusable returns true if 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 added in v0.9.0

func (x *AF) Swap(i, j int)

Swap satisfies the specification of sort.Interface.

func (*AF) Type

func (x *AF) Type() string

Type returns the name of the value's type ("N").

type AI

type AI A[int64]

AI represents an array of int64 values.

func (*AI) Append added in v0.6.0

func (x *AI) Append(ctx *Context, dst []byte, compact bool) []byte

Append appends a unique program representation of the value to dst, and returns the extended buffer.

func (*AI) At

func (x *AI) At(i int) V

At returns the value at index i, assuming it's not out of bounds.

func (*AI) Len

func (x *AI) Len() int

Len returns the length of the array.

func (*AI) Less added in v0.4.0

func (x *AI) Less(i, j int) bool

Less satisfies the specification of sort.Interface.

func (*AI) LessT added in v0.10.0

func (x *AI) LessT(y BV) bool

LessT satisfies the specification of the BV interface.

func (*AI) MarkImmutable added in v0.19.0

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 an user-defined boxed value type.

func (*AI) MarshalJSON added in v0.25.0

func (x *AI) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (*AI) Matches

func (x *AI) Matches(y BV) bool

Matches returns true if the two values match like in x~y.

func (*AI) Reusable added in v0.24.0

func (x *AI) Reusable() bool

Reusable returns true if 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 added in v0.9.0

func (x *AI) Swap(i, j int)

Swap satisfies the specification of sort.Interface.

func (*AI) Type

func (x *AI) Type() string

Type returns the name of the value's type ("I").

type AS

type AS A[string]

AS represents an array of strings.

func (*AS) Append added in v0.6.0

func (x *AS) Append(ctx *Context, dst []byte, compact bool) []byte

Append appends a unique program representation of the value to dst, and returns the extended buffer.

func (*AS) At

func (x *AS) At(i int) V

At returns the value at index i, assuming it's not out of bounds.

func (*AS) Len

func (x *AS) Len() int

Len returns the length of the array.

func (*AS) Less added in v0.4.0

func (x *AS) Less(i, j int) bool

Less satisfies the specification of sort.Interface.

func (*AS) LessT added in v0.10.0

func (x *AS) LessT(y BV) bool

LessT satisfies the specification of the BV interface.

func (*AS) MarkImmutable added in v0.19.0

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 an user-defined boxed value type.

func (*AS) MarshalJSON added in v0.25.0

func (x *AS) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (*AS) Matches

func (x *AS) Matches(y BV) bool

Matches returns true if the two values match like in x~y.

func (*AS) Reusable added in v0.24.0

func (x *AS) Reusable() bool

Reusable returns true if 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 added in v0.9.0

func (x *AS) Swap(i, j int)

Swap satisfies the specification of sort.Interface.

func (*AS) Type

func (x *AS) Type() string

Type returns the name of the value's type ("S").

type AV

type AV A[V]

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

func (x *AV) Append(ctx *Context, dst []byte, compact bool) []byte

Append appends a unique program representation of the value to dst, and returns the extended buffer.

func (*AV) At

func (x *AV) At(i int) V

At returns the value at index i, assuming it's not out of bounds.

func (*AV) Len

func (x *AV) Len() int

Len returns the length of the array.

func (*AV) Less added in v0.4.0

func (x *AV) Less(i, j int) bool

Less satisfies the specification of sort.Interface.

func (*AV) LessT added in v0.10.0

func (x *AV) LessT(y BV) bool

LessT satisfies the specification of the BV interface.

func (*AV) MarkImmutable added in v0.19.0

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 an user-defined boxed value type.

func (*AV) MarshalJSON added in v0.25.0

func (x *AV) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (*AV) Matches

func (x *AV) Matches(y BV) bool

Matches returns true if the two values match like in x~y.

func (*AV) Reusable added in v0.24.0

func (x *AV) Reusable() bool

Reusable returns true if 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 added in v0.9.0

func (x *AV) Swap(i, j int)

Swap satisfies the specification of sort.Interface.

func (*AV) Type

func (x *AV) Type() string

Type returns the name of the value's type ("A").

type Array added in v0.19.0

type Array interface {
	BV
	sort.Interface

	// Len returns the value's length.
	Len() int
	// Reusable returns true if 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
	// At returns the value at index i, assuming it's not out of bounds.
	At(i int) V
	// contains filtered or unexported methods
}

Array interface is satisfied by the different kinds of supported builtin array values.

func NewArray added in v0.24.0

func NewArray(x []V) Array

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 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. 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
	// 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. It can be satisfied by user-defined types.

type Callable added in v0.24.0

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 (builtin 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.
	Log io.Writer
	// OFS is the output field separator (default: " ").
	OFS string
	// 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

func (ctx *Context) AssignGlobal(name string, x V)

AssignGlobal assigns a value to a global variable name.

func (*Context) AssignedLast

func (ctx *Context) AssignedLast() bool

AssignedLast returns true if the last compiled expression was an assignment.

func (*Context) Compile

func (ctx *Context) Compile(s, loc string) error

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. For non-empty locations, Compile handles and skips shebang line (if any).

func (*Context) Eval

func (ctx *Context) Eval(s string) (V, error)

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

func (ctx *Context) EvalPackage(s, loc, pfx string) (V, error)

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 and prefix has already been evaluated, it returns ErrPackageImported. This means that even though Goal allows to evaluate (also via import) with the same location and prefix 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) Get added in v0.24.0

func (ctx *Context) Get(key string) any

Get returns the value corresponding to the given key, as previously set with the Set method.

func (*Context) GetGlobal

func (ctx *Context) GetGlobal(name string) (V, bool)

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 or operator 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 or operator 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. Also, this function cannot be used to change the dyadic nature of builtin symbols.

func (*Context) Run

func (ctx *Context) Run() (V, error)

Run runs compiled code in the current context, if not already done, and returns the result value.

func (*Context) Set added in v0.24.0

func (ctx *Context) Set(key string, value any)

Set associates an arbitrary value with a given string key. Use this method to carry values used in user-defined variadics. It is recommended to use key strings with a unique prefix to avoid naming conflicts.

func (*Context) String added in v0.24.0

func (ctx *Context) String() string

String returns a string representation of the compiled program and other relevant context data for debugging purposes.

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

func (d *D) Append(ctx *Context, dst []byte, compact bool) []byte

Append appends a unique program representation of the value to dst, and returns the extended buffer.

func (*D) KeyArray added in v0.24.0

func (d *D) KeyArray() Array

KeyArray returns the keys of the dictionary as an Array value.

func (*D) Keys added in v0.19.0

func (d *D) Keys() V

Keys returns the keys of the dictionary as a generic value.

func (*D) Len added in v0.19.0

func (d *D) Len() int

Len returns the length of the dictionary, that is the common length to its key and value arrays.

func (*D) LessT added in v0.19.0

func (d *D) LessT(y BV) bool

LessT satisfies the specification of the BV interface.

func (*D) MarkImmutable added in v0.19.0

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 an user-defined boxed value type.

func (*D) MarshalJSON added in v0.25.0

func (x *D) MarshalJSON() ([]byte, error)

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 added in v0.19.0

func (d *D) Matches(y BV) bool

Matches returns true if the two values match like in x~y.

func (*D) Type added in v0.19.0

func (d *D) Type() string

Type returns the name of the value's type.

func (*D) ValueArray added in v0.24.0

func (d *D) ValueArray() Array

ValueArray returns the values of the dictionary as an Array value.

func (*D) Values added in v0.19.0

func (d *D) Values() V

Values returns the values of the dictionary as a generic value.

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 Panic added in v0.26.0

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.

func (*Panic) Append added in v0.26.0

func (e *Panic) Append(ctx *Context, dst []byte, compact bool) []byte

Append appends a unique program representation of the value to dst, and returns the extended buffer.

func (*Panic) Error added in v0.26.0

func (e *Panic) Error() string

Error returns the panic message without location information.

func (*Panic) ErrorStack added in v0.26.0

func (e *Panic) ErrorStack() string

ErrorStack returns the full panic message with locations and source information obtained from its running context.

func (*Panic) LessT added in v0.26.0

func (e *Panic) LessT(y BV) bool

LessT implements BV interface. It is not used in practice for panics.

func (*Panic) Matches added in v0.26.0

func (e *Panic) Matches(y BV) bool

Matches implements BV interface. It is not used in practice for panics.

func (*Panic) Type added in v0.26.0

func (e *Panic) Type() string

Type implements BV interface. It is not used in practice for panics.

func (*Panic) Unwrap added in v0.26.0

func (e *Panic) Unwrap() error

Unwrap returns the underlying error.

func (*Panic) WithPrefix added in v0.26.0

func (e *Panic) WithPrefix(pfx string) *Panic

WithPrefix adds a prefix to the panic message and returns the modified Panic value.

type S

type S string

S represents (immutable) strings of bytes.

func (S) Append added in v0.6.0

func (s S) Append(ctx *Context, dst []byte, compact bool) []byte

Append appends a unique program representation of the value to dst, and returns the extended buffer.

func (S) LessT added in v0.10.0

func (s S) LessT(y BV) bool

LessT satisfies the specification of the BV interface.

func (S) MarshalJSON added in v0.25.0

func (x S) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (S) Matches

func (s S) Matches(y BV) bool

Matches returns true if the two values match like in x~y.

func (S) Type

func (s S) Type() string

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 Errorf

func Errorf(format string, a ...any) V

Errorf returns a formatted recoverable error value message.

func NewAB

func NewAB(x []byte) V

NewAB returns a new byte array.

func NewAF

func NewAF(x []float64) V

NewAF returns a new array of float64 values.

func NewAI

func NewAI(x []int64) V

NewAI returns a new int array.

func NewAS

func NewAS(x []string) V

NewAS returns a new array of strings.

func NewAV

func NewAV(x []V) V

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 added in v0.19.0

func NewD(keys, values V) V

NewD returns a dictionary. Both keys and values should be arrays, and they should have the same length.

func NewError

func NewError(x V) V

NewError returns a new recoverable error value.

func NewF

func NewF(f float64) V

NewF returns a new unboxed float64 value.

func NewI

func NewI(i int64) V

NewI returns a new unboxed int64 value.

func NewPanic

func NewPanic(msg string) V

NewPanic returns a new panic value with given message.

func NewPanicError added in v0.26.0

func NewPanicError(err error) V

NewPanicError returns a new panic value from given error. If the error is already of type Panic the underlying location information is preserved.

func NewPanicV added in v0.26.0

func NewPanicV(x *Panic) V

NewPanicV returns a new panic value.

func NewProjection added in v0.30.0

func NewProjection(x V, args []V) V

NewProjection returns a projection of x (usually a function) with the given arguments. Zero arguments are the non-fixed arguments.

func NewS

func NewS(s string) V

NewS returns a new string value.

func NewV

func NewV(x BV) V

NewV returns a new boxed value. For Panic boxed values, use NewPanicV instead.

func Panicf

func Panicf(format string, a ...any) V

Panicf returns a formatted fatal error value.

func (V) Append added in v0.6.0

func (x V) Append(ctx *Context, dst []byte, compact bool) []byte

Append appends a unique program representation of the value to dst, and returns the extended buffer.

func (V) Apply added in v0.24.0

func (x V) Apply(ctx *Context, args []V) V

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) Apply2 added in v0.24.0

func (x V) Apply2(ctx *Context, y, z V) V

Apply2 calls a value with two arguments.

func (V) ApplyAt added in v0.24.0

func (x V) ApplyAt(ctx *Context, y V) V

ApplyAt calls a value with a single argument.

func (V) BV added in v0.19.0

func (x V) BV() 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

func (x V) Error() V

Error retrieves the error value. It assumes x.IsError().

func (V) F

func (x V) F() float64

F retrieves the unboxed float64 value. It assumes x.IsF().

func (V) I

func (x V) I() int64

I retrieves the unboxed integer value from uv field. It assumes x.IsI().

func (V) IsBV added in v0.19.0

func (x V) IsBV() bool

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

func (x V) IsCallable() bool

IsCallable returns true if the value can be called with one or more arguments. This is true for most values, including non-function ones.

func (V) IsError

func (x V) IsError() bool

IsError returns true if the value is a recoverable error.

func (V) IsF

func (x V) IsF() bool

IsF returns true if the value is a float.

func (V) IsFalse added in v0.10.0

func (x V) IsFalse() bool

IsFalse returns true for false values, that is zero numbers, empty strings, zero-length values, and errors.

func (V) IsFunction

func (x V) IsFunction() bool

IsFunction returns true if the value is some kind of builtin function type, like verbs, adverbs, lambdas and projections. Type returns "f" for such values.

func (V) IsGap added in v0.30.0

func (x V) IsGap() bool

IsGap returns true if x represents a gap, that is an empty argument as used in projections.

func (V) IsI

func (x V) IsI() bool

IsI returns true if the value is an integer.

func (V) IsPanic

func (x V) IsPanic() bool

IsPanic returns true if the value is a fatal error.

func (V) IsProjection added in v0.30.0

func (x V) IsProjection() bool

IsProjection returns true if x is a projection.

func (V) IsTrue added in v0.10.0

func (x V) IsTrue() bool

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) Len added in v0.10.0

func (x V) Len() int

Len returns the length of a value like in #x.

func (V) LessT added in v0.10.0

func (x V) LessT(y V) bool

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. 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 an user-defined boxed value type.

func (V) MarshalJSON added in v0.25.0

func (x V) MarshalJSON() ([]byte, error)

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 added in v0.10.0

func (x V) Matches(y V) bool

Matches returns true if the two values match like in x~y.

func (V) Panic added in v0.6.0

func (x V) Panic() *Panic

Panic returns the panic value. It assumes x.IsPanic().

func (V) Rank

func (x V) Rank(ctx *Context) int

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 in adverbial expressions that have different semantics depending on the modified function arity. 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

func (x V) Sprint(ctx *Context, compact bool) string

Sprint returns a matching program string representation of the value. The compact boolean determines whether insignificant whitespace should be removed or not.

func (V) Type

func (x V) Type() string

Type returns the name of the value's type.

type VariadicFun

type VariadicFun func(*Context, []V) V

VariadicFun represents a variadic function. The array of arguments is in stack order: the first argument is its last element.

func MonadicFloatFunc added in v0.31.0

func MonadicFloatFunc(name string, f func(float64) float64) VariadicFun

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 register new math functions, like for example:

ctx.RegisterMonad("gamma", goal.MonadicFloatFunc("gamma", math.Gamma))

Directories

Path Synopsis
cmd
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.
Package scan implements a scanner for Goal source text.
Package scan implements a scanner for Goal source text.

Jump to

Keyboard shortcuts

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