Documentation
¶
Overview ¶
pkg/interpreter/convert.go Type conversion helpers between Go and xxlang objects.
pkg/interpreter/interpreter.go Clean embedding API for using xxlang as a Go library.
pkg/interpreter/options.go Functional options for configuring the interpreter.
Index ¶
- func FromGo(value interface{}) (objects.Object, error)
- func ToArray(obj objects.Object) ([]objects.Object, error)
- func ToBool(obj objects.Object) (bool, error)
- func ToFloat(obj objects.Object) (float64, error)
- func ToGo(obj objects.Object) interface{}
- func ToInt(obj objects.Object) (int64, error)
- func ToMap(obj objects.Object) (map[string]objects.Object, error)
- func ToString(obj objects.Object) (string, error)
- type Interpreter
- func (i *Interpreter) Eval(code string) (objects.Object, error)
- func (i *Interpreter) EvalFile(path string) (objects.Object, error)
- func (i *Interpreter) GetGlobal(name string) (objects.Object, bool)
- func (i *Interpreter) GetGlobalAs(name string) (interface{}, bool)
- func (i *Interpreter) Globals() []objects.Object
- func (i *Interpreter) Loader() *module.Loader
- func (i *Interpreter) Reset()
- func (i *Interpreter) SetGlobal(name string, value interface{}) error
- type Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FromGo ¶
FromGo converts a Go value to an xxlang object. Supported Go types:
- int, int8, int16, int32, int64 -> Int
- uint, uint8, uint16, uint32, uint64 -> Int
- float32, float64 -> Float
- string -> String
- bool -> Bool
- []T -> Array (elements converted recursively)
- map[string]T -> Map (values converted recursively)
- nil -> Null
- objects.Object -> returned as-is
func ToArray ¶
ToArray converts an object to a slice of objects if possible. Returns an error if the object is not an Array.
func ToBool ¶
ToBool converts an object to a bool if possible. Returns an error if the object is not a Bool.
func ToFloat ¶
ToFloat converts an object to a float64 if possible. Returns an error if the object is not a Float.
func ToGo ¶
ToGo converts an xxlang object to a Go value. Returns a Go native type that best represents the object:
- Int -> int64
- Float -> float64
- String -> string
- Bool -> bool
- Array -> []interface{} (elements converted recursively)
- Map -> map[string]interface{} (values converted recursively)
- Null -> nil
- Other -> objects.Object (returned as-is)
func ToInt ¶
ToInt converts an object to an int64 if possible. Returns an error if the object is not an Int.
Types ¶
type Interpreter ¶
type Interpreter struct {
// contains filtered or unexported fields
}
Interpreter wraps VM and Compiler for easy embedding. It provides a high-level API for evaluating xxlang code and passing values between Go and xxlang.
func New ¶
func New(opts ...Option) *Interpreter
New creates a new interpreter with the given options. Default interpreter has no stdlib and empty globals.
func (*Interpreter) Eval ¶
func (i *Interpreter) Eval(code string) (objects.Object, error)
Eval compiles and executes xxlang code, returning the result. This is the primary method for running xxlang code from Go.
func (*Interpreter) EvalFile ¶
func (i *Interpreter) EvalFile(path string) (objects.Object, error)
EvalFile loads and executes an xxlang source file. The file path is used for module resolution and error messages.
func (*Interpreter) GetGlobal ¶
func (i *Interpreter) GetGlobal(name string) (objects.Object, bool)
GetGlobal retrieves a global variable from the interpreter. Returns the xxlang object directly (not converted to Go).
func (*Interpreter) GetGlobalAs ¶
func (i *Interpreter) GetGlobalAs(name string) (interface{}, bool)
GetGlobalAs retrieves a global variable and converts it to a Go value. This is a convenience method that combines GetGlobal and ToGo.
func (*Interpreter) Globals ¶
func (i *Interpreter) Globals() []objects.Object
Globals returns the interpreter's global variables slice. This is useful for advanced use cases where direct access is needed.
func (*Interpreter) Loader ¶
func (i *Interpreter) Loader() *module.Loader
Loader returns the interpreter's module loader. This can be used to pre-load modules or check loaded modules.
func (*Interpreter) Reset ¶
func (i *Interpreter) Reset()
Reset clears all state and creates a fresh interpreter. This is useful when you want to start over without creating a new instance.
func (*Interpreter) SetGlobal ¶
func (i *Interpreter) SetGlobal(name string, value interface{}) error
SetGlobal sets a global variable in the interpreter. The value is converted from Go to xxlang using FromGo.
type Option ¶
type Option func(*Interpreter)
Option configures the interpreter.
func WithGlobal ¶
WithGlobal sets a single global variable. This is a convenience option for setting a few globals without creating a full slice.
func WithGlobalGo ¶
WithGlobalGo sets a single global variable from a Go value. The value is converted using FromGo.
func WithGlobals ¶
WithGlobals sets initial global variables. The globals slice is copied, so modifications to the original slice after calling this will not affect the interpreter.
func WithLoader ¶
WithLoader sets a custom module loader. This is useful for sharing modules between multiple interpreters or for pre-loading modules.
func WithStdlib ¶
func WithStdlib() Option
WithStdlib enables standard library modules. This registers all stdlib modules (std/math, std/string, etc.) so they can be imported with `import "std/math"`.