interp

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2025 License: Apache-2.0 Imports: 22 Imported by: 1

Documentation

Overview

Package interp evaluates GX code given an evaluator.

All values in the interpreter are represented in elements. The GX Context evaluates GX code represented as an intermediate representation (IR) tree (see github.com/gx-org/gx/build/ir), evaluates a function given a receiver and arguments passed as interpreter elements.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FuncDeclFromElement added in v0.3.0

func FuncDeclFromElement(el ir.Element) (*ir.FuncDecl, error)

FuncDeclFromElement extracts a function declaration from an element.

func FuncFromElement added in v0.6.0

func FuncFromElement(el ir.Element) (ir.Func, error)

FuncFromElement extracts a function declaration from an element.

func PkgFuncFromElement added in v0.6.0

func PkgFuncFromElement(el ir.Element) (ir.PkgFunc, error)

PkgFuncFromElement extracts a function declaration from an element.

func ShapeFromElement added in v0.3.0

func ShapeFromElement(node ir.Element) (*shape.Shape, error)

ShapeFromElement returns the shape of a numerical element.

func ToSingleElement

func ToSingleElement(ctx ir.Evaluator, node ir.SourceNode, els []ir.Element) (ir.Element, error)

ToSingleElement packs multiple elements into a tuple. If the slice els contains only one element, this element is returned.

func Underlying added in v0.3.0

func Underlying(val ir.Element) ir.Element

Underlying returns the underlying element.

Types

type ArraySlicer added in v0.3.0

type ArraySlicer interface {
	evaluator.NumericalElement
	SliceArray(fitp *FileScope, expr ir.AssignableExpr, index evaluator.NumericalElement) (evaluator.NumericalElement, error)
	Type() ir.Type
}

ArraySlicer is a state element with an array that can be sliced.

type Copier added in v0.3.0

type Copier interface {
	ir.Element
	Copy() Copier
}

Copier is an interface implemented by nodes that need to be copied when passed to a function.

type Evaluator added in v0.3.0

type Evaluator interface {
	evaluator.Evaluator

	// NewFunc creates a new function given its definition and a receiver.
	NewFunc(*Interpreter, ir.PkgFunc, *Receiver) Func

	// NewFuncLit calls a function literal.
	NewFuncLit(fitp *FileScope, ref *ir.FuncLit) (Func, error)
}

Evaluator provides core primitives for the interpreter.

type FileScope added in v0.3.0

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

FileScope returns an interpreter given the scope of a file from within a package.

func (*FileScope) Context added in v0.3.0

func (fitp *FileScope) Context() *context.Context

Context used by the interpreter.

func (*FileScope) EvalExpr added in v0.3.0

func (fitp *FileScope) EvalExpr(expr ir.Expr) (ir.Element, error)

EvalExpr evaluates an expression for a given context.

func (*FileScope) EvalFunc added in v0.3.0

func (fitp *FileScope) EvalFunc(f ir.PkgFunc, call *ir.CallExpr, args []ir.Element) ([]ir.Element, error)

EvalFunc evaluates a function.

func (*FileScope) Evaluator added in v0.3.0

func (fitp *FileScope) Evaluator() evaluator.Evaluator

Evaluator returns the evaluator used by the interpreter

func (*FileScope) File added in v0.3.0

func (fitp *FileScope) File() *ir.File

File returns the current file of the current execution.

func (*FileScope) InitScope added in v0.3.0

func (fitp *FileScope) InitScope() *ir.File

InitScope returns the initial file scope (as opposed to the file of the current context).

func (*FileScope) Materialiser added in v0.3.0

func (fitp *FileScope) Materialiser() materialise.Materialiser

Materialiser returns the materialiser to convert elements into graph nodes.

func (*FileScope) NewFunc added in v0.3.0

func (fitp *FileScope) NewFunc(fn ir.PkgFunc, recv *Receiver) Func

NewFunc creates function elements from function IRs.

func (*FileScope) NewFuncLitScope added in v0.3.0

func (fitp *FileScope) NewFuncLitScope(eval Evaluator) *FuncLitScope

NewFuncLitScope returns a new interpreter for a function literal.

func (*FileScope) Sub added in v0.3.0

func (fitp *FileScope) Sub(vals map[string]ir.Element) *FileScope

Sub returns a new interpreter with additional values defined in the context.

type FixedShape added in v0.3.0

type FixedShape interface {
	ir.Element
	Shape() *shape.Shape
}

FixedShape is an (array) element from which the shape has been fully determined.

type FixedSlice added in v0.3.0

type FixedSlice interface {
	ir.Element
	Elements() []ir.Element
	Len() int
}

FixedSlice is a slice

type Func added in v0.3.0

type Func interface {
	ir.Element
	Func() ir.Func
	Recv() *Receiver
	Call(fitp *FileScope, call *ir.CallExpr, args []ir.Element) ([]ir.Element, error)
}

Func is an element owning a callable function.

func NewRunFunc

func NewRunFunc(fn ir.PkgFunc, recv *Receiver) Func

NewRunFunc creates a function given an IR and a receiver. The function is run when being called.

type FuncBuiltin

type FuncBuiltin func(ctx evaluator.Context, call elements.CallAt, fn Func, irFunc *ir.FuncBuiltin, args []ir.Element) ([]ir.Element, error)

FuncBuiltin defines a builtin function provided by a backend.

type FuncLitScope added in v0.3.0

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

FuncLitScope is an interpreter scope to evaluate function literals.

func (*FuncLitScope) FileScope added in v0.3.0

func (litp *FuncLitScope) FileScope() *FileScope

FileScope returns the interpreter for a file scope.

func (*FuncLitScope) RunFuncLit added in v0.3.0

func (litp *FuncLitScope) RunFuncLit(eval Evaluator, fn *ir.FuncLit, args []ir.Element) ([]ir.Element, error)

RunFuncLit runs a function literal given the current context.

type Interpreter added in v0.3.0

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

Interpreter runs GX code given an evaluator and package options.

func New added in v0.3.0

func New(eval Evaluator, options []options.PackageOption) (*Interpreter, error)

New returns a new interpreter.

func (*Interpreter) Core added in v0.3.0

func (itp *Interpreter) Core() *context.Core

Core returns the core context.

func (*Interpreter) EvalFunc added in v0.3.0

func (itp *Interpreter) EvalFunc(fn *ir.FuncDecl, in *elements.InputElements) (outs []ir.Element, err error)

EvalFunc evaluates a function.

func (*Interpreter) ForFile added in v0.3.0

func (itp *Interpreter) ForFile(file *ir.File) (*FileScope, error)

ForFile returns an interpreter for a file context.

func (*Interpreter) NewFunc added in v0.3.0

func (itp *Interpreter) NewFunc(fn ir.PkgFunc, recv *Receiver) Func

NewFunc creates function elements from function IRs.

type NType added in v0.3.0

type NType interface {
	Under() ir.Element
}

NType is a named type.

type NamedType added in v0.3.0

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

NamedType references a type exported by an imported package.

func NewNamedType added in v0.3.0

func NewNamedType(newFunc NewFunc, typ *ir.NamedType, under Copier) *NamedType

NewNamedType returns a new node representing an exported type.

func (*NamedType) Copy added in v0.3.0

func (n *NamedType) Copy() Copier

Copy the element.

func (*NamedType) Flatten added in v0.3.0

func (n *NamedType) Flatten() ([]ir.Element, error)

Flatten returns the named type in a slice of elements.

func (*NamedType) NamedType added in v0.3.0

func (n *NamedType) NamedType() *ir.NamedType

NamedType returns the type of the element.

func (*NamedType) RecvCopy added in v0.3.0

func (n *NamedType) RecvCopy() *NamedType

RecvCopy copies the underlying element and returns the element encapsulated in this named type.

func (*NamedType) Select added in v0.3.0

func (n *NamedType) Select(expr *ir.SelectorExpr) (ir.Element, error)

Select returns the field given an index. Returns nil if the receiver type cannot select fields.

func (*NamedType) String added in v0.3.0

func (n *NamedType) String() string

String returns a string representation of the node.

func (*NamedType) Type added in v0.3.0

func (n *NamedType) Type() ir.Type

Type of the element.

func (*NamedType) Under added in v0.3.0

func (n *NamedType) Under() ir.Element

Under returns the underlying element of the named type.

func (*NamedType) Unflatten added in v0.3.0

func (n *NamedType) Unflatten(handles *flatten.Parser) (values.Value, error)

Unflatten consumes the next handles to return a GX value.

type NewFunc added in v0.3.0

type NewFunc func(ir.PkgFunc, *Receiver) Func

NewFunc creates function elements from function IRs.

type Package added in v0.3.0

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

Package groups elements exported by a package.

func NewPackage added in v0.3.0

func NewPackage(pkg *ir.Package, defs scope.Scope[ir.Element], newFunc NewFunc) *Package

NewPackage returns a package grouping everything that a package exports.

func (*Package) Select added in v0.3.0

func (pkg *Package) Select(expr *ir.SelectorExpr) (ir.Element, error)

Select a member of the package.

func (*Package) String added in v0.3.0

func (pkg *Package) String() string

String returns a string representation of the node.

func (*Package) Type added in v0.3.0

func (pkg *Package) Type() ir.Type

Type of the element.

type Receiver added in v0.3.0

type Receiver struct {
	Ident   *ast.Ident
	Element *NamedType
}

Receiver of a function.

func NewReceiver added in v0.3.0

func NewReceiver(el *NamedType, fn ir.Func) *Receiver

NewReceiver returns a new receiver given a function definition and the element representing the receiver.

type Selector added in v0.3.0

type Selector interface {
	ir.Element
	Select(expr *ir.SelectorExpr) (ir.Element, error)
}

Selector selects a field given its index.

type Slice added in v0.3.0

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

Slice element storing a slice of elements.

func NewSlice added in v0.3.0

func NewSlice(typ ir.Type, elements []ir.Element) *Slice

NewSlice returns a slice from a slice of elements.

func (*Slice) Compare added in v0.3.0

func (n *Slice) Compare(x canonical.Comparable) bool

Compare the slice to another canonical value.

func (*Slice) Elements added in v0.3.0

func (n *Slice) Elements() []ir.Element

Elements stored in the slice.

func (*Slice) Expr added in v0.3.0

func (n *Slice) Expr() (ir.AssignableExpr, error)

Expr returns the IR expression representing the slice.

func (*Slice) Flatten added in v0.3.0

func (n *Slice) Flatten() ([]ir.Element, error)

Flatten returns the elements of the slice.

func (*Slice) Kind added in v0.3.0

func (*Slice) Kind() ir.Kind

Kind of the element.

func (*Slice) Len added in v0.3.0

func (n *Slice) Len() int

Len returns the number of elements in the slice.

func (*Slice) Slice added in v0.3.0

func (n *Slice) Slice(fitp *FileScope, expr *ir.IndexExpr, index evaluator.NumericalElement) (ir.Element, error)

Slice of the tuple.

func (*Slice) String added in v0.3.0

func (n *Slice) String() string

String returns a string representation of the slice.

func (*Slice) Type added in v0.3.0

func (n *Slice) Type() ir.Type

Type of the slice.

func (*Slice) Unflatten added in v0.3.0

func (n *Slice) Unflatten(handles *flatten.Parser) (values.Value, error)

Unflatten consumes the next handles to return a GX value.

type Slicer added in v0.3.0

type Slicer interface {
	Slice(fitp *FileScope, expr *ir.IndexExpr, index evaluator.NumericalElement) (ir.Element, error)
}

Slicer is a state element that can be sliced.

type Struct added in v0.3.0

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

Struct is an instance of a structure.

func NewStruct added in v0.3.0

func NewStruct(typ *ir.StructType, fields map[string]ir.Element) *Struct

NewStruct returns a new node representing a structure instance.

func NewStructFromElements added in v0.3.0

func NewStructFromElements(typ *ir.StructType, vals []ir.Element) *Struct

NewStructFromElements returns a new node representing a structure instance given a slice of

func (*Struct) Copy added in v0.3.0

func (n *Struct) Copy() Copier

Copy the structure to a new node.

func (*Struct) Flatten added in v0.3.0

func (n *Struct) Flatten() ([]ir.Element, error)

Flatten returns a flat list of all the elements stored in the structure.

func (*Struct) Select added in v0.3.0

func (n *Struct) Select(expr *ir.SelectorExpr) (ir.Element, error)

Select returns the value of a field of a structure given its index.

func (*Struct) SetField added in v0.3.0

func (n *Struct) SetField(name string, value ir.Element)

SetField sets the field in the structure.

func (*Struct) String added in v0.3.0

func (n *Struct) String() string

func (*Struct) StructType added in v0.3.0

func (n *Struct) StructType() *ir.StructType

StructType returns the type of the structure.

func (*Struct) Type added in v0.3.0

func (n *Struct) Type() ir.Type

Type of the element.

func (*Struct) Unflatten added in v0.3.0

func (n *Struct) Unflatten(handles *flatten.Parser) (values.Value, error)

Unflatten consumes the next handles to return a GX value.

type Tuple added in v0.3.0

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

Tuple value grouping multiple values together.

func NewTuple added in v0.3.0

func NewTuple(values []ir.Element) *Tuple

NewTuple returns a tuple to store the result of a function returning more than one value.

func (*Tuple) Elements added in v0.3.0

func (n *Tuple) Elements() []ir.Element

Elements returns the elements stored in the tuple.

func (*Tuple) Flatten added in v0.3.0

func (n *Tuple) Flatten() ([]ir.Element, error)

Flatten the tuple and all its elements.

func (*Tuple) Slice added in v0.3.0

func (n *Tuple) Slice(fitp *FileScope, expr *ir.IndexExpr, index evaluator.NumericalElement) (ir.Element, error)

Slice of the tuple.

func (*Tuple) String added in v0.3.0

func (n *Tuple) String() string

func (*Tuple) Type added in v0.3.0

func (n *Tuple) Type() ir.Type

Type of the element.

type WithAxes added in v0.3.0

type WithAxes interface {
	Axes(ev ir.Evaluator) (*Slice, error)
}

WithAxes is an element able to return its axes as a slice of element.

Directories

Path Synopsis
Package context maintains the namespaces and stack for the interpreter.
Package context maintains the namespaces and stack for the interpreter.
Package elements provides generic elements, independent of the evaluator, for the interpreter.
Package elements provides generic elements, independent of the evaluator, for the interpreter.
Package evaluator defines interfaces for the interpreter to use to evaluate GX code.
Package evaluator defines interfaces for the interpreter to use to evaluate GX code.
Package grapheval implements the evaluation of core GX functions.
Package grapheval implements the evaluation of core GX functions.
Package materialise defines interfaces and helper functions to transform elements into graph nodes.
Package materialise defines interfaces and helper functions to transform elements into graph nodes.
Package numbers implement elements representing numbers for the interpreter.
Package numbers implement elements representing numbers for the interpreter.
Package proxies provides proxy for all GX values.
Package proxies provides proxy for all GX values.

Jump to

Keyboard shortcuts

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