interpreter

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2026 License: MIT Imports: 11 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromGo

func FromGo(value interface{}) (objects.Object, error)

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

func ToArray(obj objects.Object) ([]objects.Object, error)

ToArray converts an object to a slice of objects if possible. Returns an error if the object is not an Array.

func ToBool

func ToBool(obj objects.Object) (bool, error)

ToBool converts an object to a bool if possible. Returns an error if the object is not a Bool.

func ToFloat

func ToFloat(obj objects.Object) (float64, error)

ToFloat converts an object to a float64 if possible. Returns an error if the object is not a Float.

func ToGo

func ToGo(obj objects.Object) interface{}

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

func ToInt(obj objects.Object) (int64, error)

ToInt converts an object to an int64 if possible. Returns an error if the object is not an Int.

func ToMap

func ToMap(obj objects.Object) (map[string]objects.Object, error)

ToMap converts an object to a map of objects if possible. Returns an error if the object is not a Map.

func ToString

func ToString(obj objects.Object) (string, error)

ToString converts an object to a string if possible. Returns an error if the object is not a String.

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

func WithGlobal(name string, value objects.Object) Option

WithGlobal sets a single global variable. This is a convenience option for setting a few globals without creating a full slice.

func WithGlobalGo

func WithGlobalGo(name string, value interface{}) Option

WithGlobalGo sets a single global variable from a Go value. The value is converted using FromGo.

func WithGlobals

func WithGlobals(globals []objects.Object) Option

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

func WithLoader(loader *module.Loader) Option

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

Jump to

Keyboard shortcuts

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