goal

package module
v0.17.1 Latest Latest
Warning

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

Go to latest
Published: May 16, 2023 License: ISC Imports: 17 Imported by: 2

README

Goal

pkg.go.dev godocs.io

Goal is an embeddable array programming language with a bytecode interpreter, written in Go. It provides both a command line intepreter (that can be used in the REPL), and a library interface. The core features are mostly there and tested, so Goal is usable both for writing useful short scripts and playing with the REPL. User testing and bug reports are welcome!

Like in most array programming languages, Goal's builtins vectorize operations on immutable arrays, and encourage a functional style for control and data transformations, supported by a simple dynamic type system with little abstraction, and mutable variables (but no mutable values).

It's main distinctive features are as follows:

  • Syntax inspired mainly from the K language, but with quite a few deviations. For example, backquotes produce Go-like raw strings instead of symbols, rx/\s+/ is a compile-time regular expression literal, and there is Perl-style string interpolation. On the other side, there are no tacit compositions, and digraph operator verbs and adverbs are gone or done differently (except for global assignment with ::).
  • Primitive semantics are both inspired from the ngn/k variant of the K language and BQN. For example, group by, classify, shifts, windows, binary search and occurrence count take after BQN's semantics, but free-form immutable arrays, dictionaries and adverbs take after K.
  • Unlike in typical array languages, strings are atoms, and common string handling functions have been integrated into the primitives, including regular expression functions. Primitives acting on whole strings are Unicode-aware (like case-folding or Unicode properties in regexps).
  • Error handling makes a distinction between fatal errors (panics) and recoverable errors which are handled as values.
  • Integrated support for csv, json, time handling, and basic math.
  • Simple IO: read/write files, run commands/pipes, open filehandles.
  • Easily embeddable and extensible in Go.
  • Array performance is quite decent, with specialized algorithms depending on inputs (type, size, range), and variable liveness analysis that reduces cloning by reusing dead immutable arrays (in code with limited branching). However, it is not a goal to reach state-of-the-art (no SIMD, and no bit booleans, fitting integers in arrays using either uint8 or int64 elements).
  • Scalar performance is typical for a bytecode-compiled interpreter (without JIT), somewhat slower than a C bytecode interpreter: value representation is less compact than how it could be done in C, but Goal does have unboxed integers and floats.

If this list is not enough to satisfy your curiosity, have a look at the why section of the FAQ. You can also read the Credits.md to know about main inspiration sources for the language. Last, but not least, there are some implementation notes too.

Install

To install the command line interpreter, first do the following:

  • Install the go compiler.
  • Add $(go env GOPATH)/bin to your $PATH (for example export PATH="$PATH:$(go env GOPATH)/bin").

Then you can build the intepreter with:

go install ./cmd/goal

Alternatively, you may type go build -o /path/to/bin/goal ./cmd/goal to put the resulting binary in a custom location in your $PATH.

The goal command should now be available. Type goal --help for command-line usage.

Typing just goal 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.

Documentation

In addition to the work-in-progress documentation website, you might be interested in the Changelog changes between releases. The REPL help is also available in text form here at docs/help.txt.

Examples

A few short examples can be found in the examples and testdata/scripts directory. Because the latter are used for testing, they come along an expected result after a /RESULT: comment.

Also, various code generation scripts in the toplevel scripts directory are written in Goal.

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 AB

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

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) []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) byte

At returns array value at the given index.

func (*AB) CloneWithRC added in v0.2.0

func (x *AB) CloneWithRC(rc *int) Value

CloneWithRC satisfies the specification of the RefCounter interface.

func (*AB) DecrRC

func (x *AB) DecrRC()

DecrRC decrements the reference count by one, or zero if it is already non positive.

func (*AB) IncrRC

func (x *AB) IncrRC()

IncrRC increments the reference count by one. It can panic if the value's refcount pointer has not been properly initialized.

func (*AB) InitWithRC added in v0.2.0

func (x *AB) InitWithRC(rc *int)

InitWithRC satisfies the specification of the RefCounter interface.

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 Value) bool

LessT satisfies the specification of the Value interface.

func (*AB) Matches

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

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

func (*AB) RC added in v0.2.0

func (x *AB) RC() *int

RC returns the array's reference count pointer.

func (*AB) Slice

func (x *AB) Slice() []byte

Slice returns the underlying immutable slice of values. It should not be modified unless the value's refcount pointer is reusable, and even then, you should normally return a new array with the modified slice.

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 struct {
	// contains filtered or unexported fields
}

AF represents an array of reals.

func (*AF) Append added in v0.6.0

func (x *AF) Append(ctx *Context, dst []byte) []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) float64

At returns array value at the given index.

func (*AF) CloneWithRC added in v0.2.0

func (x *AF) CloneWithRC(rc *int) Value

CloneWithRC satisfies the specification of the RefCounter interface.

func (*AF) DecrRC

func (x *AF) DecrRC()

DecrRC decrements the reference count by one, or zero if it is already non positive.

func (*AF) IncrRC

func (x *AF) IncrRC()

IncrRC increments the reference count by one. It can panic if the value's refcount pointer has not been properly initialized.

func (*AF) InitWithRC added in v0.2.0

func (x *AF) InitWithRC(rc *int)

InitWithRC satisfies the specification of the RefCounter interface.

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 Value) bool

LessT satisfies the specification of the Value interface.

func (*AF) Matches

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

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

func (*AF) RC added in v0.2.0

func (x *AF) RC() *int

RC returns the array's reference count pointer.

func (*AF) Slice

func (x *AF) Slice() []float64

Slice returns the underlying immutable slice of values. It should not be modified unless the value's refcount pointer is reusable, and even then, you should normally return a new array with the modified slice.

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 struct {
	// contains filtered or unexported fields
}

AI represents an array of integers.

func (*AI) Append added in v0.6.0

func (x *AI) Append(ctx *Context, dst []byte) []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) int64

At returns array value at the given index.

func (*AI) CloneWithRC added in v0.2.0

func (x *AI) CloneWithRC(rc *int) Value

CloneWithRC satisfies the specification of the RefCounter interface.

func (*AI) DecrRC

func (x *AI) DecrRC()

DecrRC decrements the reference count by one, or zero if it is already non positive.

func (*AI) IncrRC

func (x *AI) IncrRC()

IncrRC increments the reference count by one. It can panic if the value's refcount pointer has not been properly initialized.

func (*AI) InitWithRC added in v0.2.0

func (x *AI) InitWithRC(rc *int)

InitWithRC satisfies the specification of the RefCounter interface.

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 Value) bool

LessT satisfies the specification of the Value interface.

func (*AI) Matches

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

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

func (*AI) RC added in v0.2.0

func (x *AI) RC() *int

RC returns the array's reference count pointer.

func (*AI) Slice

func (x *AI) Slice() []int64

Slice returns the underlying immutable slice of values. It should not be modified unless the value's refcount pointer is reusable, and even then, you should normally return a new array with the modified slice.

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 struct {
	// contains filtered or unexported fields
}

AS represents an array of strings.

func (*AS) Append added in v0.6.0

func (x *AS) Append(ctx *Context, dst []byte) []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) string

At returns array value at the given index.

func (*AS) CloneWithRC added in v0.2.0

func (x *AS) CloneWithRC(rc *int) Value

CloneWithRC satisfies the specification of the RefCounter interface.

func (*AS) DecrRC

func (x *AS) DecrRC()

DecrRC decrements the reference count by one, or zero if it is already non positive.

func (*AS) IncrRC

func (x *AS) IncrRC()

IncrRC increments the reference count by one. It can panic if the value's refcount pointer has not been properly initialized.

func (*AS) InitWithRC added in v0.2.0

func (x *AS) InitWithRC(rc *int)

InitWithRC satisfies the specification of the RefCounter interface.

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 Value) bool

LessT satisfies the specification of the Value interface.

func (*AS) Matches

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

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

func (*AS) RC added in v0.2.0

func (x *AS) RC() *int

RC returns the array's reference count pointer.

func (*AS) Slice

func (x *AS) Slice() []string

Slice returns the underlying immutable slice of values. It should not be modified unless the value's refcount pointer is reusable, and even then, you should normally return a new array with the modified slice.

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 struct {
	// contains filtered or unexported fields
}

AV represents a generic array.

func (*AV) Append added in v0.6.0

func (x *AV) Append(ctx *Context, dst []byte) []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 array value at the given index.

func (*AV) CloneWithRC added in v0.2.0

func (x *AV) CloneWithRC(rc *int) Value

CloneWithRC satisfies the specification of the RefCounter interface.

func (*AV) DecrRC

func (x *AV) DecrRC()

DecrRC decrements the reference count by one, or zero if it is already non positive.

func (*AV) IncrRC

func (x *AV) IncrRC()

IncrRC increments the reference count by one. It can panic if the value's refcount pointer has not been properly initialized.

func (*AV) InitWithRC added in v0.2.0

func (x *AV) InitWithRC(rc *int)

InitWithRC satisfies the specification of the RefCounter interface.

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 Value) bool

LessT satisfies the specification of the Value interface.

func (*AV) Matches

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

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

func (*AV) RC added in v0.2.0

func (x *AV) RC() *int

RC returns the array's reference count pointer.

func (*AV) Slice

func (x *AV) Slice() []V

Slice returns the underlying immutable slice of values. It should not be modified unless the value's refcount pointer is reusable, and even then, you should normally return a new array with the modified slice.

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 Context

type Context struct {
	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) Apply

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

Apply calls a value with a single argument.

func (*Context) Apply2

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

Apply2 calls a value with two arguments.

func (*Context) ApplyN

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

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

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(loc string, s string) error

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

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

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

func (*Context) Run

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

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

func (*Context) Show

func (ctx *Context) Show() string

Show returns a string representation with debug information about the context.

type Dict added in v0.5.0

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

Dict represents a dictionary.

func (*Dict) Append added in v0.6.0

func (d *Dict) Append(ctx *Context, dst []byte) []byte

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

func (*Dict) CloneWithRC added in v0.5.0

func (d *Dict) CloneWithRC(rc *int) Value

CloneWithRC satisfies the specification of the RefCounter interface.

func (*Dict) DecrRC added in v0.5.0

func (d *Dict) DecrRC()

DecrRC decrements the reference count of both the key and value arrays by one, or zero if they are already non positive.

func (*Dict) IncrRC added in v0.5.0

func (d *Dict) IncrRC()

IncrRC increments the reference count of both the key and value arrays by one.

func (*Dict) InitWithRC added in v0.5.0

func (d *Dict) InitWithRC(rc *int)

InitWithRC satisfies the specification of the RefCounter interface.

func (*Dict) Keys added in v0.5.0

func (d *Dict) Keys() V

Keys returns the keys of the dictionary.

func (*Dict) Len added in v0.5.0

func (d *Dict) Len() int

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

func (*Dict) Less added in v0.5.0

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

Less satisfies the specification of sort.Interface.

func (*Dict) LessT added in v0.10.0

func (d *Dict) LessT(y Value) bool

LessT satisfies the specification of the Value interface.

func (*Dict) Matches added in v0.5.0

func (d *Dict) Matches(y Value) bool

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

func (*Dict) Swap added in v0.9.0

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

Swap satisfies the specification of sort.Interface.

func (*Dict) Type added in v0.5.0

func (d *Dict) Type() string

Type returns the name of the value's type.

func (*Dict) Values added in v0.5.0

func (d *Dict) Values() V

Values returns the values of the dictionary.

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.

const (
	IdentVar   IdentType = iota // a normal identifier (default zero value)
	IdentMonad                  // a builtin monad (cannot have left argument)
	IdentDyad                   // a builtin dyad (can have left argument)
)

These constants represent the different kinds of special names.

type PanicError

type PanicError struct {
	Msg string // error message (without location)
	// contains filtered or unexported fields
}

PanicError represents a fatal error returned by any Context method.

func (*PanicError) Error

func (e *PanicError) Error() string

Error returns the default string representation. It makes uses of position information obtained from its running context.

type RefCountHolder added in v0.2.0

type RefCountHolder interface {
	RefCounter

	// RC returns the value's root reference count pointer.
	RC() *int
}

RefCountHolder is a RefCounter that has a root refcount pointer. When such values are returned from a variadic function, if the refcount pointer is still nil, InitWithRC is automatically called with a newly allocated refcount pointer to a zero count.

type RefCounter

type RefCounter interface {
	Value

	// IncrRC increments the reference count by one. It can panic if the
	// value's refcount pointer has not been properly initialized.
	IncrRC()

	// DecrRC decrements the reference count by one, or zero if it is
	// already non positive.
	DecrRC()

	// InitWithRC recursively sets the refcount pointer for reusable
	// values, and increments by 2 the refcount of non-reusable values, to
	// ensure immutability of non-reusable children without cloning them.
	InitWithRC(rc *int)

	// CloneWithRC returns a clone of the value, with rc as new refcount
	// pointer.  If the current value's refcount pointer is nil, reusable
	// or equal to the passed one, the same value is returned after
	// updating the refcount pointer as needed, instead of doing a full
	// clone.
	CloneWithRC(rc *int) Value
}

RefCounter is implemented by values that use a reference count. In goal the refcount is not used for memory management, but only for optimization of memory allocations. Refcount is increased by each assignement, and each push operation on the stack, except for pushes corresponding to the last use of a variable (as approximated conservatively). It is reduced after each drop. If refcount is equal or less than one, then the value is considered reusable.

When defining a new type implementing the Value interface, it is only necessary to also implement RefCounter if the type definition makes use of a type implementing it (for example an array type or a generic V).

type S

type S string

S represents (immutable) strings of bytes.

func (S) Append added in v0.6.0

func (s S) Append(ctx *Context, dst []byte) []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 Value) bool

LessT satisfies the specification of the Value interface.

func (S) Matches

func (s S) Matches(y Value) bool

func (S) Type

func (s S) Type() string

Type retuns "s" for string atoms.

type Scanner

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

Scanner represents the state of the scanner.

func NewScanner

func NewScanner(names map[string]IdentType, source string) *Scanner

NewScanner returns a scanner for the given source string.

func (*Scanner) Next

func (s *Scanner) Next() Token

Next produces the next token from the input reader.

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.

func (Token) String

func (t Token) String() string

type TokenType

type TokenType int

TokenType represents the different kinds of tokens.

const (
	NONE TokenType = iota
	EOF
	ERROR
	ADVERB
	DYAD
	DYADASSIGN
	IDENT
	LEFTBRACE
	LEFTBRACKET
	LEFTBRACKETS
	LEFTPAREN
	NEWLINE
	NUMBER
	MONAD
	REGEXP
	RIGHTBRACE
	RIGHTBRACKET
	RIGHTPAREN
	SEMICOLON
	SADVERB
	STRING
	QQSTART
	QQEND
)

These constants describe the possible kinds of tokens.

func (TokenType) String

func (i TokenType) String() string

type V

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

V contains a boxed or unboxed value.

func Canonical

func Canonical(x V) V

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

func CanonicalRec(x V) V

CanonicalRec returns the canonical form of a given value, that is the most specialized form. In practice, if the value is a generic array, but a more specialized version could represent the value, it returns the specialized value. All variadic functions have to return results in canonical form, so this function can be used to ensure that when defining new ones.

func Errorf

func Errorf(format string, a ...interface{}) V

Errorf returns a formatted recoverable error value.

func NewAB

func NewAB(x []byte) V

NewAB returns a new byte array. It does not initialize the reference counter.

func NewABWithRC added in v0.2.0

func NewABWithRC(x []byte, rc *int) V

NewABWithRC returns a new byte array.

func NewAF

func NewAF(x []float64) V

NewAF returns a new array of reals. It does not initialize the reference counter.

func NewAFWithRC added in v0.2.0

func NewAFWithRC(x []float64, rc *int) V

NewAFWithRC returns a new array of reals.

func NewAI

func NewAI(x []int64) V

NewAI returns a new int array. It does not initialize the reference counter.

func NewAIWithRC added in v0.2.0

func NewAIWithRC(x []int64, rc *int) V

NewAIWithRC returns a new int array.

func NewAS

func NewAS(x []string) V

NewAS returns a new array of strings. It does not initialize the reference counter.

func NewASWithRC added in v0.2.0

func NewASWithRC(x []string, rc *int) V

NewASWithRC returns a new array of strings.

func NewAV

func NewAV(x []V) V

NewAV returns a new generic array. It does not initialize the reference counter.

func NewAVWithRC added in v0.2.0

func NewAVWithRC(x []V, rc *int) V

NewAVWithRC returns a new generic array.

func NewDict added in v0.5.0

func NewDict(keys, values V) V

NewDict 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 float64 value.

func NewI

func NewI(i int64) V

NewI returns a new int64 value.

func NewPanic

func NewPanic(s string) V

NewPanic returns a fatal error value.

func NewS

func NewS(s string) V

NewS returns a new string value.

func NewV

func NewV(xv Value) V

NewV returns a new boxed value.

func Panicf

func Panicf(format string, a ...interface{}) V

Panicf returns a formatted fatal error value.

func (V) Append added in v0.6.0

func (v V) Append(ctx *Context, dst []byte) []byte

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

func (V) Clone added in v0.2.0

func (x V) Clone() V

Clone creates an identical deep copy of a value, or the value itself if it is reusable. It initializes refcount if necessary.

func (V) CloneWithRC added in v0.2.0

func (x V) CloneWithRC(rc *int) V

CloneWithRC clones the given value using its CloneWithRC method, if it is a RefCounter, or returns it as-is otherwise for immutable values that do not need cloning.

func (V) DecrRC

func (x V) DecrRC()

DecrRC decrements the value reference count (if it has any).

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) HasRC added in v0.2.0

func (x V) HasRC() bool

HasRC returns true if the value is boxed and implements RefCounter.

func (V) I

func (x V) I() int64

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

func (V) IncrRC

func (x V) IncrRC()

IncrRC increments the value reference count (if it has any).

func (V) InitRC added in v0.2.0

func (x V) InitRC()

InitRC initializes refcount if the value is a RefCountHolder with nil refcount.

func (V) InitWithRC added in v0.2.0

func (x V) InitWithRC(rc *int)

InitWithRC calls the method of the same name on boxed values.

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 functions, arrays, strings and regexps, for example.

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 function.

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

func (x V) IsValue() bool

IsValue returns true if the value is a boxed value satisfying the Value interface. You can then get the value with the Value method.

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

Panic returns the panic string. 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 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

func (V) Sprint

func (v V) Sprint(ctx *Context) string

Sprint returns a matching program string representation of the value.

func (V) Type

func (x V) Type() string

Type returns the name of the value's type.

func (V) Value

func (x V) Value() Value

Value retrieves the boxed value, or nil if the value is not boxed. You can check whether the value is boxed with IsValue(v).

type Value

type Value interface {
	// Matches returns true if the value matches another (in the sense of
	// the ~ operator).
	Matches(Value) 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(Value) bool
}

Value is the interface satisfied by all boxed values.

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.

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.

Jump to

Keyboard shortcuts

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