script

package
v0.0.0-...-f85407f Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2023 License: CC-BY-3.0, Freetype, GPL-3.0-or-later Imports: 15 Imported by: 0

Documentation

Overview

package script provides a script interpreter for input files and GUI commands.

Index

Constants

View Source
const GoExclusiveMethodSuffix = "Go"

Variables

View Source
var (
	ScalarFunction_t = reflect.TypeOf(dummy_f).In(0)
	VectorFunction_t = reflect.TypeOf(dummy_f3).In(0)
	ScalarIf_t       = reflect.TypeOf(dummy_scalarif).In(0)
	VectorIf_t       = reflect.TypeOf(dummy_vectorif).In(0)
)

common type definitions

View Source
var Debug = false // print debug info?

Functions

func Contains

func Contains(tree, search Expr) bool

func Format

func Format(n ast.Node) string

Types

type BlockStmt

type BlockStmt struct {
	Children []Expr
	Node     []ast.Node
}

block statement is a list of statements.

func (*BlockStmt) Child

func (b *BlockStmt) Child() []Expr

func (*BlockStmt) Eval

func (b *BlockStmt) Eval() interface{}

func (*BlockStmt) Fix

func (b *BlockStmt) Fix() Expr

func (*BlockStmt) Format

func (b *BlockStmt) Format() string

func (*BlockStmt) Type

func (b *BlockStmt) Type() reflect.Type

type Const

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

func NewConst

func NewConst(e Expr) *Const

func (*Const) Child

func (c *Const) Child() []Expr

func (*Const) Eval

func (c *Const) Eval() interface{}

func (*Const) Fix

func (c *Const) Fix() Expr

func (*Const) Type

func (c *Const) Type() reflect.Type

type Expr

type Expr interface {
	Eval() interface{}  // evaluate and return result (nil for void)
	Type() reflect.Type // return type, nil for void
	Child() []Expr
	Fix() Expr // replace all variables by their current value, except for the time "t".
}

an expression can be evaluated

type LValue

type LValue interface {
	Expr
	SetValue(interface{}) // assigns a new value
}

left-hand value in (single) assign statement

type ScalarFunction

type ScalarFunction interface {
	Expr
	Float() float64
}

type ScalarIf

type ScalarIf interface {
	Get() float64

} // TODO: Scalar

type TVar

type TVar struct {
	LValue
}

func (*TVar) Fix

func (t *TVar) Fix() Expr

type VectorFunction

type VectorFunction interface {
	Expr
	Float3() data.Vector
}

type VectorIf

type VectorIf interface {
	Get() data.Vector

} // TODO: Vector

type World

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

World stores an interpreted program's state like declared variables and functions.

func NewWorld

func NewWorld() *World

func (*World) Compile

func (w *World) Compile(src string) (code *BlockStmt, e error)

compiles source consisting of a number of statements. E.g.:

src = "a = 1; b = sin(x)"
code, err := world.Compile(src)
code.Eval()

func (*World) CompileExpr

func (w *World) CompileExpr(src string) (code Expr, e error)

Compiles an expression, which can then be evaluated. E.g.:

expr, err := world.CompileExpr("1+1")
expr.Eval()   // returns 2

func (World) Const

func (w World) Const(name string, val interface{}, doc ...string)

adds a constant. Cannot be changed in any way.

func (*World) EnterScope

func (w *World) EnterScope()

func (*World) Eval

func (w *World) Eval(src string) (ret interface{}, err error)

Eval compiles and evaluates src, which must be an expression, and returns the result(s). E.g.:

world.Eval("1+1")      // returns 2, nil

func (*World) Exec

func (w *World) Exec(src string) error

Exec compiles and executes the source statements.

func (*World) ExitScope

func (w *World) ExitScope()

func (World) Func

func (w World) Func(name string, f interface{}, doc ...string)

adds a native function to the world. E.g.:

world.Func("sin", math.Sin)
world.MustEval("sin(0)") // returns 0

func (World) LValue

func (w World) LValue(name string, v LValue, doc ...string)

adds a special variable to the world. Upon assignment, v's Set() will be called.

func (*World) LoadStdlib

func (w *World) LoadStdlib()

Loads standard functions into the world.

func (*World) MustCompile

func (w *World) MustCompile(src string) Expr

Like Compile but panics on error

func (*World) MustCompileExpr

func (w *World) MustCompileExpr(src string) Expr

CompileExpr with panic on error.

func (*World) MustEval

func (w *World) MustEval(src string) interface{}

Eval with panic on error.

func (*World) MustExec

func (w *World) MustExec(src string)

Exec with panic on error.

func (World) ROnly

func (w World) ROnly(name string, addr interface{}, doc ...string)

adds a native variable to the world. It cannot be changed from script.

var x = 3.14
world.ROnly("x", &x)
world.MustEval("x")   // returns 3.14
world.MustExec("x=2") // fails: cannot assign to x

func (*World) Resolve

func (w *World) Resolve(identifier string) (e Expr)

func (World) TVar

func (w World) TVar(name string, addr interface{}, doc ...string)

Hack for fixing the closure caveat: Decleare the time variable, the only variable closures close over.

func (World) Var

func (w World) Var(name string, addr interface{}, doc ...string)

adds a native variable to the world. E.g.:

var x = 3.14
world.Var("x", &x)
world.MustEval("x") // returns 3.14

Jump to

Keyboard shortcuts

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