script

package
v3.0.12+incompatible Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2013 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ScalarFunction_t = reflect.TypeOf(dummy_f).In(0)
	VectorFunction_t = reflect.TypeOf(dummy_f3).In(0)
)

common type definitions

View Source
var Debug bool // print debug info?

Functions

func Const

func Const(e Expr) bool

Types

type Expr

type Expr interface {
	Eval() interface{}  // evaluate and return result (nil for void)
	Type() reflect.Type // return type, nil for void

}

an expression can be evaluated

type LValue

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

left-hand value in (single) assign statement

type ScalarFunction

type ScalarFunction interface {
	Float() float64
	Const() bool
}

type VectorFunction

type VectorFunction interface {
	Float3() [3]float64
	Const() bool
}

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 Expr, 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) PureFunc

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

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