object

package
v0.2.8 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// Type conversion error messages (exported for use by external packages)
	ErrMustBeString   = "must be a string"
	ErrMustBeInteger  = "must be an integer"
	ErrMustBeNumber   = "must be a number"
	ErrMustBeBoolean  = "must be a boolean"
	ErrMustBeList     = "must be a list"
	ErrMustBeDict     = "must be a dict"
	ErrMustBeIterable = "must be iterable"
)

Small integer cache for common values (-5 to 10000) This follows Python's approach and eliminates allocations for loop counters Extended range to 10000 for better loop performance

View Source
const (
	ExceptionTypeSystemExit    = "SystemExit"
	ExceptionTypeException     = "Exception"
	ExceptionTypeValueError    = "ValueError"
	ExceptionTypeTypeError     = "TypeError"
	ExceptionTypeNameError     = "NameError"
	ExceptionTypeStopIteration = "StopIteration"
	ExceptionTypeGeneric       = "" // Default for legacy compatibility
)

Exception type constants

Variables

View Source
var (
	BREAK    = &Break{}
	CONTINUE = &Continue{}
)

Break and Continue singletons (like NULL, TRUE, FALSE)

Functions

func AsError

func AsError(obj Object) string

AsError returns the error message from an Object, or empty string if not an error. This is a shared helper for extracting error messages from Objects.

func DictKey

func DictKey(obj Object) string

DictKey returns a canonical string key for use in Dict and Set maps. Matches Python 3 semantics where:

  • int(1), float(1.0), and True all map to the same key
  • str("1") maps to a different key
  • None maps to its own unique key

func InstanceDataFromContext

func InstanceDataFromContext(ctx context.Context) any

InstanceDataFromContext retrieves instance data from the context Returns nil if no instance data is present

func IsError

func IsError(obj Object) bool

IsError returns true if the object is an Error type.

Types

type Boolean

type Boolean struct {
	Value bool
}

func (*Boolean) AsBool

func (b *Boolean) AsBool() (bool, Object)

func (*Boolean) AsDict

func (b *Boolean) AsDict() (map[string]Object, Object)

func (*Boolean) AsFloat

func (b *Boolean) AsFloat() (float64, Object)

func (*Boolean) AsInt

func (b *Boolean) AsInt() (int64, Object)

func (*Boolean) AsList

func (b *Boolean) AsList() ([]Object, Object)

func (*Boolean) AsString

func (b *Boolean) AsString() (string, Object)

func (*Boolean) CoerceFloat

func (b *Boolean) CoerceFloat() (float64, Object)

func (*Boolean) CoerceInt

func (b *Boolean) CoerceInt() (int64, Object)

func (*Boolean) CoerceString

func (b *Boolean) CoerceString() (string, Object)

func (*Boolean) Inspect

func (b *Boolean) Inspect() string

func (*Boolean) Type

func (b *Boolean) Type() ObjectType

type BoundMethod

type BoundMethod struct {
	Instance Object // The instance (self)
	Method   Object // The method function
}

BoundMethod represents a method bound to an instance When called, it automatically prepends self to the arguments

func (*BoundMethod) AsBool

func (bm *BoundMethod) AsBool() (bool, Object)

func (*BoundMethod) AsDict

func (bm *BoundMethod) AsDict() (map[string]Object, Object)

func (*BoundMethod) AsFloat

func (bm *BoundMethod) AsFloat() (float64, Object)

func (*BoundMethod) AsInt

func (bm *BoundMethod) AsInt() (int64, Object)

func (*BoundMethod) AsList

func (bm *BoundMethod) AsList() ([]Object, Object)

func (*BoundMethod) AsString

func (bm *BoundMethod) AsString() (string, Object)

func (*BoundMethod) CoerceFloat

func (bm *BoundMethod) CoerceFloat() (float64, Object)

func (*BoundMethod) CoerceInt

func (bm *BoundMethod) CoerceInt() (int64, Object)

func (*BoundMethod) CoerceString

func (bm *BoundMethod) CoerceString() (string, Object)

func (*BoundMethod) Inspect

func (bm *BoundMethod) Inspect() string

func (*BoundMethod) Type

func (bm *BoundMethod) Type() ObjectType

type Break

type Break struct{}

func (*Break) AsBool

func (b *Break) AsBool() (bool, Object)

func (*Break) AsDict

func (b *Break) AsDict() (map[string]Object, Object)

func (*Break) AsFloat

func (b *Break) AsFloat() (float64, Object)

func (*Break) AsInt

func (b *Break) AsInt() (int64, Object)

func (*Break) AsList

func (b *Break) AsList() ([]Object, Object)

func (*Break) AsString

func (b *Break) AsString() (string, Object)

func (*Break) CoerceFloat

func (b *Break) CoerceFloat() (float64, Object)

func (*Break) CoerceInt

func (b *Break) CoerceInt() (int64, Object)

func (*Break) CoerceString

func (b *Break) CoerceString() (string, Object)

func (*Break) Inspect

func (b *Break) Inspect() string

func (*Break) Type

func (b *Break) Type() ObjectType

type Builtin

type Builtin struct {
	Fn         BuiltinFunction
	HelpText   string            // Optional help documentation for this builtin
	Attributes map[string]Object // Optional attributes for this builtin
}

func (*Builtin) AsBool

func (b *Builtin) AsBool() (bool, Object)

func (*Builtin) AsDict

func (b *Builtin) AsDict() (map[string]Object, Object)

func (*Builtin) AsFloat

func (b *Builtin) AsFloat() (float64, Object)

func (*Builtin) AsInt

func (b *Builtin) AsInt() (int64, Object)

func (*Builtin) AsList

func (b *Builtin) AsList() ([]Object, Object)

func (*Builtin) AsString

func (b *Builtin) AsString() (string, Object)

func (*Builtin) CoerceFloat

func (b *Builtin) CoerceFloat() (float64, Object)

func (*Builtin) CoerceInt

func (b *Builtin) CoerceInt() (int64, Object)

func (*Builtin) CoerceString

func (b *Builtin) CoerceString() (string, Object)

func (*Builtin) Inspect

func (b *Builtin) Inspect() string

func (*Builtin) Type

func (b *Builtin) Type() ObjectType

type BuiltinFunction

type BuiltinFunction func(ctx context.Context, kwargs Kwargs, args ...Object) Object

BuiltinFunction is the signature for all builtin functions - ctx: Context with environment and runtime information - kwargs: Keyword arguments passed to the function (wrapped with helper methods) - args: Positional arguments passed to the function

type Class

type Class struct {
	Name      string
	BaseClass *Class // optional parent class for inheritance
	Methods   map[string]Object
	Env       *Environment
}

func (*Class) AsBool

func (c *Class) AsBool() (bool, Object)

func (*Class) AsDict

func (c *Class) AsDict() (map[string]Object, Object)

func (*Class) AsFloat

func (c *Class) AsFloat() (float64, Object)

func (*Class) AsInt

func (c *Class) AsInt() (int64, Object)

func (*Class) AsList

func (c *Class) AsList() ([]Object, Object)

func (*Class) AsString

func (c *Class) AsString() (string, Object)

func (*Class) CoerceFloat

func (c *Class) CoerceFloat() (float64, Object)

func (*Class) CoerceInt

func (c *Class) CoerceInt() (int64, Object)

func (*Class) CoerceString

func (c *Class) CoerceString() (string, Object)

func (*Class) Inspect

func (c *Class) Inspect() string

func (*Class) Type

func (c *Class) Type() ObjectType

type ClassBuilder

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

ClassBuilder provides a fluent API for creating scriptling classes. It allows registering typed Go methods that are automatically wrapped to handle conversion between Go types and scriptling Objects.

Example usage:

cb := NewClassBuilder("Person")
cb.Method("greet", func(self *Instance, name string) string {
    return "Hello, " + name
})
class := cb.Build()

func NewClassBuilder

func NewClassBuilder(name string) *ClassBuilder

NewClassBuilder creates a new ClassBuilder with the given class name.

func (*ClassBuilder) BaseClass

func (cb *ClassBuilder) BaseClass(base *Class) *ClassBuilder

BaseClass sets the base class for inheritance.

func (*ClassBuilder) Build

func (cb *ClassBuilder) Build() *Class

Build creates and returns the Class from this builder.

func (*ClassBuilder) Environment

func (cb *ClassBuilder) Environment(env *Environment) *ClassBuilder

Environment sets the environment for the class. This is optional and usually not needed.

func (*ClassBuilder) Method

func (cb *ClassBuilder) Method(name string, fn interface{}) *ClassBuilder

Method registers a typed Go method with the class. The method must be a Go function with typed parameters. The first parameter should be *Instance (the 'self' parameter). Supported signatures are the same as LibraryBuilder.Function().

Example:

cb.Method("greet", func(self *Instance, name string) string {
    return "Hello, " + name
})

func (*ClassBuilder) MethodWithHelp

func (cb *ClassBuilder) MethodWithHelp(name string, fn interface{}, helpText string) *ClassBuilder

MethodWithHelp registers a method with help text. Help text is displayed when users call help() on the method.

Example:

cb.MethodWithHelp("sqrt", func(self *Instance, x float64) float64 {
    return math.Sqrt(x)
}, "sqrt(x) - Return the square root of x")

func (*ClassBuilder) Property added in v0.2.0

func (cb *ClassBuilder) Property(name string, fn interface{}) *ClassBuilder

Property registers a getter function as a @property on the class. The getter receives self as its only argument.

Example:

cb.Property("area", func(self *Instance) float64 {
    r, _ := self.Fields["radius"].AsFloat()
    return math.Pi * r * r
})

func (*ClassBuilder) PropertyWithSetter added in v0.2.0

func (cb *ClassBuilder) PropertyWithSetter(name string, getter interface{}, setter interface{}) *ClassBuilder

PropertyWithSetter registers a getter and setter as a @property on the class. The getter receives self only; the setter receives self and the new value.

Example:

cb.PropertyWithSetter("radius",
    func(self *Instance) float64 { r, _ := self.Fields["r"].AsFloat(); return r },
    func(self *Instance, v float64) { self.Fields["r"] = &Float{Value: v} },
)

func (*ClassBuilder) StaticMethod added in v0.2.0

func (cb *ClassBuilder) StaticMethod(name string, fn interface{}) *ClassBuilder

StaticMethod registers a function as a @staticmethod on the class. The function does NOT receive self — do not include *Instance as the first parameter.

Example:

cb.StaticMethod("from_degrees", func(deg float64) float64 {
    return deg * math.Pi / 180
})

type ClassMethod added in v0.2.0

type ClassMethod struct {
	Fn Object
}

ClassMethod wraps a function for use with @classmethod. When called, the class is passed as the first argument instead of self.

func (*ClassMethod) AsBool added in v0.2.0

func (c *ClassMethod) AsBool() (bool, Object)

func (*ClassMethod) AsDict added in v0.2.0

func (c *ClassMethod) AsDict() (map[string]Object, Object)

func (*ClassMethod) AsFloat added in v0.2.0

func (c *ClassMethod) AsFloat() (float64, Object)

func (*ClassMethod) AsInt added in v0.2.0

func (c *ClassMethod) AsInt() (int64, Object)

func (*ClassMethod) AsList added in v0.2.0

func (c *ClassMethod) AsList() ([]Object, Object)

func (*ClassMethod) AsString added in v0.2.0

func (c *ClassMethod) AsString() (string, Object)

func (*ClassMethod) CoerceFloat added in v0.2.0

func (c *ClassMethod) CoerceFloat() (float64, Object)

func (*ClassMethod) CoerceInt added in v0.2.0

func (c *ClassMethod) CoerceInt() (int64, Object)

func (*ClassMethod) CoerceString added in v0.2.0

func (c *ClassMethod) CoerceString() (string, Object)

func (*ClassMethod) Inspect added in v0.2.0

func (c *ClassMethod) Inspect() string

func (*ClassMethod) Type added in v0.2.0

func (c *ClassMethod) Type() ObjectType

type ClientWrapper

type ClientWrapper struct {
	// TypeName is the display name used in Inspect() (e.g., "OpenAIClient", "MCPClient")
	TypeName string
	// Client is the underlying Go client pointer (opaque to scriptling)
	Client any
}

ClientWrapper is a generic wrapper for storing Go client pointers in object.Instance fields. The underlying client is stored as an opaque pointer and accessed via type assertion.

Example usage:

type MyClientWrapper struct {
    instance *MyClientInstance
}

func (w *MyClientWrapper) Type() ObjectType { return INSTANCE_OBJ }
func (w *MyClientWrapper) Inspect() string { return "<MyClient>" }
// ... implement other Object methods ...

// Store in instance:
instance.Fields["_client"] = &MyClientWrapper{instance: &MyClientInstance{...}}

// Extract from instance:
wrapper, _ := instance.Fields["_client"].(*MyClientWrapper)
client := wrapper.instance

For convenience, use NewClientWrapper to create a wrapper with a custom type name.

func GetClientField

func GetClientField(instance *Instance, fieldName string) (*ClientWrapper, bool)

GetClientField extracts a ClientWrapper from an object.Instance field. Returns the wrapper and true if found, nil and false otherwise.

This is a convenience function for the common pattern of extracting a client wrapper from the "_client" field of an instance.

func (*ClientWrapper) AsBool

func (w *ClientWrapper) AsBool() (bool, Object)

AsBool returns true - clients are truthy

func (*ClientWrapper) AsDict

func (w *ClientWrapper) AsDict() (map[string]Object, Object)

AsDict returns an error - clients cannot be converted to dict

func (*ClientWrapper) AsFloat

func (w *ClientWrapper) AsFloat() (float64, Object)

AsFloat returns an error - clients cannot be converted to float

func (*ClientWrapper) AsInt

func (w *ClientWrapper) AsInt() (int64, Object)

AsInt returns an error - clients cannot be converted to int

func (*ClientWrapper) AsList

func (w *ClientWrapper) AsList() ([]Object, Object)

AsList returns an error - clients cannot be converted to list

func (*ClientWrapper) AsString

func (w *ClientWrapper) AsString() (string, Object)

AsString returns the inspect representation

func (*ClientWrapper) CoerceFloat

func (w *ClientWrapper) CoerceFloat() (float64, Object)

func (*ClientWrapper) CoerceInt

func (w *ClientWrapper) CoerceInt() (int64, Object)

func (*ClientWrapper) CoerceString

func (w *ClientWrapper) CoerceString() (string, Object)

func (*ClientWrapper) Inspect

func (w *ClientWrapper) Inspect() string

Inspect returns a string representation of the client

func (*ClientWrapper) Type

func (w *ClientWrapper) Type() ObjectType

Type returns INSTANCE_OBJ as this wrapper represents an instance

type Continue

type Continue struct{}

func (*Continue) AsBool

func (c *Continue) AsBool() (bool, Object)

func (*Continue) AsDict

func (c *Continue) AsDict() (map[string]Object, Object)

func (*Continue) AsFloat

func (c *Continue) AsFloat() (float64, Object)

func (*Continue) AsInt

func (c *Continue) AsInt() (int64, Object)

func (*Continue) AsList

func (c *Continue) AsList() ([]Object, Object)

func (*Continue) AsString

func (c *Continue) AsString() (string, Object)

func (*Continue) CoerceFloat

func (c *Continue) CoerceFloat() (float64, Object)

func (*Continue) CoerceInt

func (c *Continue) CoerceInt() (int64, Object)

func (*Continue) CoerceString

func (c *Continue) CoerceString() (string, Object)

func (*Continue) Inspect

func (c *Continue) Inspect() string

func (*Continue) Type

func (c *Continue) Type() ObjectType

type Dict

type Dict struct {
	Pairs map[string]DictPair
}

func NewStringDict

func NewStringDict(entries map[string]Object) *Dict

NewStringDict creates a Dict from string key-value pairs. Usage: NewStringDict(map[string]Object{"key": value, ...})

func (*Dict) AsBool

func (d *Dict) AsBool() (bool, Object)

func (*Dict) AsDict

func (d *Dict) AsDict() (map[string]Object, Object)

func (*Dict) AsFloat

func (d *Dict) AsFloat() (float64, Object)

func (*Dict) AsInt

func (d *Dict) AsInt() (int64, Object)

func (*Dict) AsList

func (d *Dict) AsList() ([]Object, Object)

func (*Dict) AsString

func (d *Dict) AsString() (string, Object)

func (*Dict) CoerceFloat

func (d *Dict) CoerceFloat() (float64, Object)

func (*Dict) CoerceInt

func (d *Dict) CoerceInt() (int64, Object)

func (*Dict) CoerceString

func (d *Dict) CoerceString() (string, Object)

func (*Dict) DeleteByString

func (d *Dict) DeleteByString(name string) bool

DeleteByString deletes a string key from the dict. Returns true if key existed.

func (*Dict) DeleteKey

func (d *Dict) DeleteKey(key Object) bool

DeleteKey removes a key from the dict. Returns true if the key existed.

func (*Dict) GetByString

func (d *Dict) GetByString(name string) (DictPair, bool)

GetByString retrieves a pair using a string key (convenience for attribute-style access).

func (*Dict) GetPair

func (d *Dict) GetPair(key Object) (DictPair, bool)

GetPair retrieves a pair from the dict using proper DictKey lookup.

func (*Dict) HasByString

func (d *Dict) HasByString(name string) bool

HasByString checks if a string key exists in the dict.

func (*Dict) HasKey

func (d *Dict) HasKey(key Object) bool

HasKey checks if a key exists in the dict.

func (*Dict) Inspect

func (d *Dict) Inspect() string

func (*Dict) SetByString

func (d *Dict) SetByString(name string, value Object)

SetByString sets a pair using a string key (convenience for attribute-style access).

func (*Dict) SetPair

func (d *Dict) SetPair(key, value Object)

SetPair sets a key-value pair in the dict using proper DictKey.

func (*Dict) Type

func (d *Dict) Type() ObjectType

type DictItems

type DictItems struct {
	Dict *Dict
}

DictItems represents a view of a dictionary's items

func (*DictItems) AsBool

func (di *DictItems) AsBool() (bool, Object)

func (*DictItems) AsDict

func (di *DictItems) AsDict() (map[string]Object, Object)

func (*DictItems) AsFloat

func (di *DictItems) AsFloat() (float64, Object)

func (*DictItems) AsInt

func (di *DictItems) AsInt() (int64, Object)

func (*DictItems) AsList

func (di *DictItems) AsList() ([]Object, Object)

func (*DictItems) AsString

func (di *DictItems) AsString() (string, Object)

func (*DictItems) CoerceFloat

func (di *DictItems) CoerceFloat() (float64, Object)

func (*DictItems) CoerceInt

func (di *DictItems) CoerceInt() (int64, Object)

func (*DictItems) CoerceString

func (di *DictItems) CoerceString() (string, Object)

func (*DictItems) CreateIterator

func (di *DictItems) CreateIterator() *Iterator

func (*DictItems) Inspect

func (di *DictItems) Inspect() string

func (*DictItems) Type

func (di *DictItems) Type() ObjectType

type DictKeys

type DictKeys struct {
	Dict *Dict
}

DictKeys represents a view of a dictionary's keys

func (*DictKeys) AsBool

func (dk *DictKeys) AsBool() (bool, Object)

func (*DictKeys) AsDict

func (dk *DictKeys) AsDict() (map[string]Object, Object)

func (*DictKeys) AsFloat

func (dk *DictKeys) AsFloat() (float64, Object)

func (*DictKeys) AsInt

func (dk *DictKeys) AsInt() (int64, Object)

func (*DictKeys) AsList

func (dk *DictKeys) AsList() ([]Object, Object)

func (*DictKeys) AsString

func (dk *DictKeys) AsString() (string, Object)

func (*DictKeys) CoerceFloat

func (dk *DictKeys) CoerceFloat() (float64, Object)

func (*DictKeys) CoerceInt

func (dk *DictKeys) CoerceInt() (int64, Object)

func (*DictKeys) CoerceString

func (dk *DictKeys) CoerceString() (string, Object)

func (*DictKeys) CreateIterator

func (dk *DictKeys) CreateIterator() *Iterator

CreateIterator returns an iterator for the keys

func (*DictKeys) Inspect

func (dk *DictKeys) Inspect() string

func (*DictKeys) Type

func (dk *DictKeys) Type() ObjectType

type DictPair

type DictPair struct {
	Key   Object
	Value Object
}

func (DictPair) StringKey

func (p DictPair) StringKey() string

StringKey returns the string representation of the key. For String keys, returns the actual string value; for other types, returns Inspect(). This is the canonical way to extract a human-readable key from a DictPair.

type DictValues

type DictValues struct {
	Dict *Dict
}

DictValues represents a view of a dictionary's values

func (*DictValues) AsBool

func (dv *DictValues) AsBool() (bool, Object)

func (*DictValues) AsDict

func (dv *DictValues) AsDict() (map[string]Object, Object)

func (*DictValues) AsFloat

func (dv *DictValues) AsFloat() (float64, Object)

func (*DictValues) AsInt

func (dv *DictValues) AsInt() (int64, Object)

func (*DictValues) AsList

func (dv *DictValues) AsList() ([]Object, Object)

func (*DictValues) AsString

func (dv *DictValues) AsString() (string, Object)

func (*DictValues) CoerceFloat

func (dv *DictValues) CoerceFloat() (float64, Object)

func (*DictValues) CoerceInt

func (dv *DictValues) CoerceInt() (int64, Object)

func (*DictValues) CoerceString

func (dv *DictValues) CoerceString() (string, Object)

func (*DictValues) CreateIterator

func (dv *DictValues) CreateIterator() *Iterator

func (*DictValues) Inspect

func (dv *DictValues) Inspect() string

func (*DictValues) Type

func (dv *DictValues) Type() ObjectType

type Environment

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

func NewEnclosedEnvironment

func NewEnclosedEnvironment(outer *Environment) *Environment

func NewEnvironment

func NewEnvironment() *Environment

func (*Environment) Delete

func (e *Environment) Delete(name string)

Delete removes a variable from this environment (not parent scopes)

func (*Environment) EnableOutputCapture

func (e *Environment) EnableOutputCapture()

EnableOutputCapture enables output capture for this environment

func (*Environment) Get

func (e *Environment) Get(name string) (Object, bool)

func (*Environment) GetAvailableLibrariesCallback

func (e *Environment) GetAvailableLibrariesCallback() func() []LibraryInfo

GetAvailableLibrariesCallback gets the available libraries callback from this environment or outer

func (*Environment) GetGlobal

func (e *Environment) GetGlobal() *Environment

GetGlobal gets the global (outermost) environment

func (*Environment) GetImportCallback

func (e *Environment) GetImportCallback() func(string) error

GetImportCallback gets the import callback from this environment or outer

func (*Environment) GetOutput

func (e *Environment) GetOutput() string

GetOutput returns captured output and clears the buffer

func (*Environment) GetReader

func (e *Environment) GetReader() io.Reader

GetReader returns the appropriate reader for input

func (*Environment) GetStore

func (e *Environment) GetStore() map[string]Object

GetStore returns a copy of the environment's store (only local scope, not outer)

func (*Environment) GetWriter

func (e *Environment) GetWriter() io.Writer

GetWriter returns the appropriate writer for output

func (*Environment) IsGlobal

func (e *Environment) IsGlobal(name string) bool

IsGlobal checks if a variable is marked as global

func (*Environment) IsNonlocal

func (e *Environment) IsNonlocal(name string) bool

IsNonlocal checks if a variable is marked as nonlocal

func (*Environment) MarkGlobal

func (e *Environment) MarkGlobal(name string)

MarkGlobal marks a variable name as global in this scope

func (*Environment) MarkNonlocal

func (e *Environment) MarkNonlocal(name string)

MarkNonlocal marks a variable name as nonlocal in this scope

func (*Environment) ResetStore added in v0.2.7

func (e *Environment) ResetStore(keep map[string]bool)

ResetStore removes all keys from the environment store except those in keep.

func (*Environment) Set

func (e *Environment) Set(name string, val Object) Object

func (*Environment) SetAvailableLibrariesCallback

func (e *Environment) SetAvailableLibrariesCallback(fn func() []LibraryInfo)

SetAvailableLibrariesCallback sets the available libraries callback for this environment. GetAvailableLibrariesCallback walks up the scope chain, so setting on any env makes it available to that env and all enclosed children.

func (*Environment) SetGlobal

func (e *Environment) SetGlobal(name string, val Object) Object

SetGlobal sets a variable in the global (outermost) environment

func (*Environment) SetImportCallback

func (e *Environment) SetImportCallback(fn func(string) error)

SetImportCallback sets the import callback for this environment. GetImportCallback walks up the scope chain, so setting on any env makes it available to that env and all enclosed children.

func (*Environment) SetInParent

func (e *Environment) SetInParent(name string, val Object) bool

SetInParent sets a variable in the parent environment (for nonlocal)

func (*Environment) SetInputReader

func (e *Environment) SetInputReader(r io.Reader)

SetInputReader sets a custom reader for input

func (*Environment) SetOutputWriter

func (e *Environment) SetOutputWriter(w io.Writer)

SetOutputWriter sets a custom writer for output

type Error

type Error struct {
	Message  string
	Line     int
	File     string
	Function string
}

func AsErrorObj

func AsErrorObj(obj Object) (*Error, bool)

AsErrorObj returns the object as an Error, or nil/false if not

func (*Error) AsBool

func (e *Error) AsBool() (bool, Object)

func (*Error) AsDict

func (e *Error) AsDict() (map[string]Object, Object)

func (*Error) AsFloat

func (e *Error) AsFloat() (float64, Object)

func (*Error) AsInt

func (e *Error) AsInt() (int64, Object)

func (*Error) AsList

func (e *Error) AsList() ([]Object, Object)

func (*Error) AsString

func (e *Error) AsString() (string, Object)

func (*Error) CoerceFloat

func (e *Error) CoerceFloat() (float64, Object)

func (*Error) CoerceInt

func (e *Error) CoerceInt() (int64, Object)

func (*Error) CoerceString

func (e *Error) CoerceString() (string, Object)

func (*Error) Inspect

func (e *Error) Inspect() string

func (*Error) Type

func (e *Error) Type() ObjectType

type Exception

type Exception struct {
	Message       string
	ExceptionType string // Exception type for identification (e.g., "SystemExit", "ValueError", etc.)
	Code          int    // Exit code for SystemExit; ignored for other exception types
}

func AsException

func AsException(obj Object) (*Exception, bool)

AsException returns the object as an Exception, or nil/false if not

func NewSystemExit

func NewSystemExit(code int, message string) *Exception

NewSystemExit creates a new SystemExit exception with the given code and message

func (*Exception) AsBool

func (ex *Exception) AsBool() (bool, Object)

func (*Exception) AsDict

func (ex *Exception) AsDict() (map[string]Object, Object)

func (*Exception) AsFloat

func (ex *Exception) AsFloat() (float64, Object)

func (*Exception) AsInt

func (ex *Exception) AsInt() (int64, Object)

func (*Exception) AsList

func (ex *Exception) AsList() ([]Object, Object)

func (*Exception) AsString

func (ex *Exception) AsString() (string, Object)

func (*Exception) CoerceFloat

func (ex *Exception) CoerceFloat() (float64, Object)

func (*Exception) CoerceInt

func (ex *Exception) CoerceInt() (int64, Object)

func (*Exception) CoerceString

func (ex *Exception) CoerceString() (string, Object)

func (*Exception) GetExitCode

func (ex *Exception) GetExitCode() int

GetExitCode returns the exit code for SystemExit exceptions For non-SystemExit exceptions, returns 0 (the Code field is ignored)

func (*Exception) Inspect

func (ex *Exception) Inspect() string

func (*Exception) IsSystemExit

func (ex *Exception) IsSystemExit() bool

IsSystemExit returns true if this is a SystemExit exception

func (*Exception) Type

func (ex *Exception) Type() ObjectType

type Float

type Float struct {
	Value float64
}

func (*Float) AsBool

func (f *Float) AsBool() (bool, Object)

func (*Float) AsDict

func (f *Float) AsDict() (map[string]Object, Object)

func (*Float) AsFloat

func (f *Float) AsFloat() (float64, Object)

func (*Float) AsInt

func (f *Float) AsInt() (int64, Object)

func (*Float) AsList

func (f *Float) AsList() ([]Object, Object)

func (*Float) AsString

func (f *Float) AsString() (string, Object)

func (*Float) CoerceFloat

func (f *Float) CoerceFloat() (float64, Object)

func (*Float) CoerceInt

func (f *Float) CoerceInt() (int64, Object)

func (*Float) CoerceString

func (f *Float) CoerceString() (string, Object)

func (*Float) Inspect

func (f *Float) Inspect() string

func (*Float) Type

func (f *Float) Type() ObjectType

type Function

type Function struct {
	Name          string
	Parameters    []*ast.Identifier
	DefaultValues map[string]ast.Expression
	Variadic      *ast.Identifier // *args parameter
	Kwargs        *ast.Identifier // **kwargs parameter
	Body          *ast.BlockStatement
	Env           *Environment
}

func (*Function) AsBool

func (f *Function) AsBool() (bool, Object)

func (*Function) AsDict

func (f *Function) AsDict() (map[string]Object, Object)

func (*Function) AsFloat

func (f *Function) AsFloat() (float64, Object)

func (*Function) AsInt

func (f *Function) AsInt() (int64, Object)

func (*Function) AsList

func (f *Function) AsList() ([]Object, Object)

func (*Function) AsString

func (f *Function) AsString() (string, Object)

func (*Function) CoerceFloat

func (f *Function) CoerceFloat() (float64, Object)

func (*Function) CoerceInt

func (f *Function) CoerceInt() (int64, Object)

func (*Function) CoerceString

func (f *Function) CoerceString() (string, Object)

func (*Function) Inspect

func (f *Function) Inspect() string

func (*Function) Type

func (f *Function) Type() ObjectType

type FunctionBuilder

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

FunctionBuilder provides a fluent API for creating individual scriptling functions. It allows registering a single typed Go function that is automatically wrapped to handle conversion between Go types and scriptling Objects.

Example usage:

fb := NewFunctionBuilder()
fb.Function(func(a, b int) int { return a + b })
fn := fb.Build()
p.RegisterFunc("add", fn)

func NewFunctionBuilder

func NewFunctionBuilder() *FunctionBuilder

NewFunctionBuilder creates a new FunctionBuilder for building individual functions.

func (*FunctionBuilder) Build

func (fb *FunctionBuilder) Build() func(ctx context.Context, kwargs Kwargs, args ...Object) Object

Build creates and returns the BuiltinFunction from this builder. The returned function can be passed directly to RegisterFunc().

func (*FunctionBuilder) Function

func (fb *FunctionBuilder) Function(fn interface{}) *FunctionBuilder

Function registers a typed Go function with the builder. The function must be a Go function with typed parameters. Supported signatures are the same as LibraryBuilder.Function().

Example:

fb.Function(func(a, b int) int { return a + b })

func (*FunctionBuilder) FunctionWithHelp

func (fb *FunctionBuilder) FunctionWithHelp(fn interface{}, helpText string) *FunctionBuilder

FunctionWithHelp registers a function with help text. Help text is displayed when users call help() on the function.

Example:

fb.FunctionWithHelp(func(x float64) float64 {
    return math.Sqrt(x)
}, "sqrt(x) - Return the square root of x")

type FunctionSignature

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

FunctionSignature holds pre-computed function analysis

type Instance

type Instance struct {
	Class  *Class
	Fields map[string]Object
}

func (*Instance) AsBool

func (i *Instance) AsBool() (bool, Object)

func (*Instance) AsDict

func (i *Instance) AsDict() (map[string]Object, Object)

func (*Instance) AsFloat

func (i *Instance) AsFloat() (float64, Object)

func (*Instance) AsInt

func (i *Instance) AsInt() (int64, Object)

func (*Instance) AsList

func (i *Instance) AsList() ([]Object, Object)

func (*Instance) AsString

func (i *Instance) AsString() (string, Object)

func (*Instance) CoerceFloat

func (i *Instance) CoerceFloat() (float64, Object)

func (*Instance) CoerceInt

func (i *Instance) CoerceInt() (int64, Object)

func (*Instance) CoerceString

func (i *Instance) CoerceString() (string, Object)

func (*Instance) Inspect

func (i *Instance) Inspect() string

func (*Instance) Type

func (i *Instance) Type() ObjectType

type Integer

type Integer struct {
	Value int64
}

func NewInteger

func NewInteger(val int64) *Integer

NewInteger returns a cached integer for small values, or a new Integer for larger values

func (*Integer) AsBool

func (i *Integer) AsBool() (bool, Object)

func (*Integer) AsDict

func (i *Integer) AsDict() (map[string]Object, Object)

func (*Integer) AsFloat

func (i *Integer) AsFloat() (float64, Object)

func (*Integer) AsInt

func (i *Integer) AsInt() (int64, Object)

func (*Integer) AsList

func (i *Integer) AsList() ([]Object, Object)

func (*Integer) AsString

func (i *Integer) AsString() (string, Object)

func (*Integer) CoerceFloat

func (i *Integer) CoerceFloat() (float64, Object)

func (*Integer) CoerceInt

func (i *Integer) CoerceInt() (int64, Object)

func (*Integer) CoerceString

func (i *Integer) CoerceString() (string, Object)

func (*Integer) Inspect

func (i *Integer) Inspect() string

func (*Integer) Type

func (i *Integer) Type() ObjectType

type Iterator

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

Iterator represents a Python-style iterator

func NewEnumerateIterator

func NewEnumerateIterator(iterable Object, start int64) *Iterator

EnumerateIterator creates an iterator that returns (index, value) tuples

func NewFilterIterator

func NewFilterIterator(ctx context.Context, fn Object, iterable Object) *Iterator

FilterIterator creates an iterator that filters elements based on a predicate

func NewIterator

func NewIterator(nextFn func() (Object, bool)) *Iterator

NewIterator creates an iterator with a custom next function This allows creating iterators that can call functions with proper context

func NewMapIterator

func NewMapIterator(ctx context.Context, fn Object, iterable Object) *Iterator

MapIterator creates an iterator that applies a function to each element

func NewRangeIterator

func NewRangeIterator(start, stop, step int64) *Iterator

RangeIterator creates an iterator for range(start, stop, step)

func NewReversedIterator

func NewReversedIterator(iterable Object) *Iterator

ReversedIterator creates an iterator that returns elements in reverse order

func NewZipIterator

func NewZipIterator(iterables []Object) *Iterator

ZipIterator creates an iterator that zips multiple iterables together

func (*Iterator) AsBool

func (it *Iterator) AsBool() (bool, Object)

func (*Iterator) AsDict

func (it *Iterator) AsDict() (map[string]Object, Object)

func (*Iterator) AsFloat

func (it *Iterator) AsFloat() (float64, Object)

func (*Iterator) AsInt

func (it *Iterator) AsInt() (int64, Object)

func (*Iterator) AsList

func (it *Iterator) AsList() ([]Object, Object)

func (*Iterator) AsString

func (it *Iterator) AsString() (string, Object)

func (*Iterator) CoerceFloat

func (it *Iterator) CoerceFloat() (float64, Object)

func (*Iterator) CoerceInt

func (it *Iterator) CoerceInt() (int64, Object)

func (*Iterator) CoerceString

func (it *Iterator) CoerceString() (string, Object)

func (*Iterator) Inspect

func (it *Iterator) Inspect() string

func (*Iterator) Next

func (it *Iterator) Next() (Object, bool)

Next returns the next value from the iterator

func (*Iterator) Type

func (it *Iterator) Type() ObjectType

type Kwargs

type Kwargs struct {
	Kwargs map[string]Object
}

Kwargs is a special type for functions that accept keyword arguments. It wraps the raw kwargs map and provides helper methods for extracting values.

func NewKwargs

func NewKwargs(kwargs map[string]Object) Kwargs

NewKwargs creates a new Kwargs wrapper.

func (Kwargs) Get

func (k Kwargs) Get(key string) Object

Get returns the raw Object for the key, or nil if not found.

func (Kwargs) GetBool

func (k Kwargs) GetBool(name string, defaultValue bool) (bool, Object)

GetBool extracts a boolean keyword argument with a default value.

func (Kwargs) GetFloat

func (k Kwargs) GetFloat(name string, defaultValue float64) (float64, Object)

GetFloat extracts a float keyword argument with a default value.

func (Kwargs) GetInt

func (k Kwargs) GetInt(name string, defaultValue int64) (int64, Object)

GetInt extracts an integer keyword argument with a default value.

func (Kwargs) GetList

func (k Kwargs) GetList(name string, defaultValue []Object) ([]Object, Object)

GetList extracts a list keyword argument with a default value.

func (Kwargs) GetString

func (k Kwargs) GetString(name string, defaultValue string) (string, Object)

GetString extracts a string keyword argument with a default value.

func (Kwargs) Has

func (k Kwargs) Has(key string) bool

Has returns true if the key exists in kwargs.

func (Kwargs) Keys

func (k Kwargs) Keys() []string

Keys returns all kwargs keys.

func (Kwargs) Len

func (k Kwargs) Len() int

Len returns the number of kwargs.

func (Kwargs) MustGetBool

func (k Kwargs) MustGetBool(name string, defaultValue bool) bool

MustGetBool extracts a boolean keyword argument, ignoring errors.

func (Kwargs) MustGetFloat

func (k Kwargs) MustGetFloat(name string, defaultValue float64) float64

MustGetFloat extracts a float keyword argument, ignoring errors.

func (Kwargs) MustGetInt

func (k Kwargs) MustGetInt(name string, defaultValue int64) int64

MustGetInt extracts an integer keyword argument, ignoring errors.

func (Kwargs) MustGetList

func (k Kwargs) MustGetList(name string, defaultValue []Object) []Object

MustGetList extracts a list keyword argument, ignoring errors.

func (Kwargs) MustGetString

func (k Kwargs) MustGetString(name string, defaultValue string) string

MustGetString extracts a string keyword argument, ignoring errors.

type LambdaFunction

type LambdaFunction struct {
	Parameters    []*ast.Identifier
	DefaultValues map[string]ast.Expression
	Variadic      *ast.Identifier // *args parameter
	Kwargs        *ast.Identifier // **kwargs parameter
	Body          ast.Expression
	Env           *Environment
}

func (*LambdaFunction) AsBool

func (lf *LambdaFunction) AsBool() (bool, Object)

func (*LambdaFunction) AsDict

func (lf *LambdaFunction) AsDict() (map[string]Object, Object)

func (*LambdaFunction) AsFloat

func (lf *LambdaFunction) AsFloat() (float64, Object)

func (*LambdaFunction) AsInt

func (lf *LambdaFunction) AsInt() (int64, Object)

func (*LambdaFunction) AsList

func (lf *LambdaFunction) AsList() ([]Object, Object)

func (*LambdaFunction) AsString

func (lf *LambdaFunction) AsString() (string, Object)

func (*LambdaFunction) CoerceFloat

func (lf *LambdaFunction) CoerceFloat() (float64, Object)

func (*LambdaFunction) CoerceInt

func (lf *LambdaFunction) CoerceInt() (int64, Object)

func (*LambdaFunction) CoerceString

func (lf *LambdaFunction) CoerceString() (string, Object)

func (*LambdaFunction) Inspect

func (lf *LambdaFunction) Inspect() string

func (*LambdaFunction) Type

func (lf *LambdaFunction) Type() ObjectType

type Library

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

Library represents a pre-built collection of builtin functions and constants This eliminates the need for function wrappers and provides direct access Libraries can contain sub-libraries for nested module support (e.g., urllib.parse)

func NewLibrary

func NewLibrary(name string, functions map[string]*Builtin, constants map[string]Object, description string) *Library

NewLibrary creates a new library with functions, optional constants, and optional description Pass nil for constants if there are none, and "" for description if not needed

func NewLibraryWithSubs

func NewLibraryWithSubs(name string, functions map[string]*Builtin, constants map[string]Object, subLibraries map[string]*Library, description string) *Library

NewLibraryWithSubs creates a new library with functions, constants, sub-libraries, and description

func (*Library) AsBool

func (l *Library) AsBool() (bool, Object)

func (*Library) AsDict

func (l *Library) AsDict() (map[string]Object, Object)

func (*Library) AsFloat

func (l *Library) AsFloat() (float64, Object)

func (*Library) AsInt

func (l *Library) AsInt() (int64, Object)

func (*Library) AsList

func (l *Library) AsList() ([]Object, Object)

func (*Library) AsString

func (l *Library) AsString() (string, Object)

func (*Library) CachedDict

func (l *Library) CachedDict() *Dict

CachedDict returns the cached dict for testing purposes This is exported only for library_instantiate_test.go to verify caching behavior

func (*Library) CoerceFloat

func (l *Library) CoerceFloat() (float64, Object)

func (*Library) CoerceInt

func (l *Library) CoerceInt() (int64, Object)

func (*Library) CoerceString

func (l *Library) CoerceString() (string, Object)

func (*Library) Constants

func (l *Library) Constants() map[string]Object

Constants returns the library's constants map

func (*Library) Description

func (l *Library) Description() string

Description returns the library's description

func (*Library) Functions

func (l *Library) Functions() map[string]*Builtin

Functions returns the library's function map

func (*Library) GetDict

func (l *Library) GetDict() *Dict

GetDict returns the cached Dict representation of this library, building it if necessary This caching avoids rebuilding the dict every time a library is imported

func (*Library) Inspect

func (l *Library) Inspect() string

func (*Library) InstanceData

func (l *Library) InstanceData() any

InstanceData returns the instance-specific data for this library

func (*Library) Instantiate

func (l *Library) Instantiate(instanceData any) *Library

Instantiate creates a new library instance with instance-specific data Functions are wrapped to inject the instance data into the context

func (*Library) Name

func (l *Library) Name() string

Name returns the library's name

func (*Library) SubLibraries

func (l *Library) SubLibraries() map[string]*Library

SubLibraries returns the library's sub-libraries map

func (*Library) Type

func (l *Library) Type() ObjectType

type LibraryBuilder

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

LibraryBuilder provides a fluent API for creating scriptling libraries. It allows registering typed Go functions that are automatically wrapped to handle conversion between Go types and scriptling Objects.

Example usage:

lib := NewLibraryBuilder("mylib", "My custom library")
lib.Function("connect", func(host string, port int) error {
    // Connect to host:port
    return nil
})
lib.Function("disconnect", func() error {
    // Disconnect
    return nil
})
lib.Constant("VERSION", "1.0.0")
library := lib.Build()

func NewLibraryBuilder

func NewLibraryBuilder(name, description string) *LibraryBuilder

NewLibraryBuilder creates a new LibraryBuilder with the given name and description.

func (*LibraryBuilder) Alias

func (b *LibraryBuilder) Alias(alias, originalName string) *LibraryBuilder

Alias creates an alias for an existing function.

Example:

builder.Function("add", func(a, b int) int { return a + b })
builder.Alias("sum", "add")  // "sum" is now an alias for "add"

func (*LibraryBuilder) Build

func (b *LibraryBuilder) Build() *Library

Build creates and returns the Library from this builder. After calling Build(), the builder should not be used further.

func (*LibraryBuilder) Clear

func (b *LibraryBuilder) Clear() *LibraryBuilder

Clear removes all registered functions and constants.

func (*LibraryBuilder) Constant

func (b *LibraryBuilder) Constant(name string, value interface{}) *LibraryBuilder

Constant registers a constant value with the given name. The value is automatically converted to a scriptling Object. Supported types: string, int, int64, float64, bool, nil

Example:

builder.Constant("VERSION", "1.0.0")
builder.Constant("MAX_CONNECTIONS", 100)
builder.Constant("DEBUG", true)

func (*LibraryBuilder) ConstantCount

func (b *LibraryBuilder) ConstantCount() int

ConstantCount returns the number of registered constants.

func (*LibraryBuilder) Description

func (b *LibraryBuilder) Description(desc string) *LibraryBuilder

Description sets or updates the library description.

func (*LibraryBuilder) Function

func (b *LibraryBuilder) Function(name string, fn interface{}) *LibraryBuilder

Function registers a function with the given name. The function must be a Go function with typed parameters. Parameters can be: string, int, int64, float64, bool, []any, map[string]any Return values can be: any of the above types, or error

Example:

builder.Function("add", func(a, b int) int { return a + b })
builder.Function("greet", func(name string) string { return "Hello, " + name })
builder.Function("connect", func(host string, port int) error { ... })

func (*LibraryBuilder) FunctionCount

func (b *LibraryBuilder) FunctionCount() int

FunctionCount returns the number of registered functions.

func (*LibraryBuilder) FunctionFromVariadic

func (b *LibraryBuilder) FunctionFromVariadic(name string, fn interface{}) *LibraryBuilder

FunctionFromVariadic registers a variadic function that accepts a variable number of arguments. This is useful for functions like print() that can take any number of arguments.

Example:

builder.FunctionFromVariadic("print_all", func(args ...any) {
    for _, arg := range args {
        fmt.Println(arg)
    }
})

func (*LibraryBuilder) FunctionFromVariadicWithHelp

func (b *LibraryBuilder) FunctionFromVariadicWithHelp(name string, fn interface{}, helpText string) *LibraryBuilder

FunctionFromVariadicWithHelp registers a variadic function with help text.

func (*LibraryBuilder) FunctionWithHelp

func (b *LibraryBuilder) FunctionWithHelp(name string, fn interface{}, helpText string) *LibraryBuilder

FunctionWithHelp registers a function with the given name and help text. The function must be a Go function with typed parameters. Help text is displayed when users call help() on the function.

Example:

builder.FunctionWithHelp("sqrt", func(x float64) float64 {
    return math.Sqrt(x)
}, "sqrt(x) - Return the square root of x")

func (*LibraryBuilder) GetConstantNames

func (b *LibraryBuilder) GetConstantNames() []string

GetConstantNames returns a sorted list of registered constant names.

func (*LibraryBuilder) GetDescription

func (b *LibraryBuilder) GetDescription() string

GetDescription returns the current library description.

func (*LibraryBuilder) GetFunctionNames

func (b *LibraryBuilder) GetFunctionNames() []string

GetFunctionNames returns a sorted list of registered function names.

func (*LibraryBuilder) HasConstant

func (b *LibraryBuilder) HasConstant(name string) bool

HasConstant checks if a constant with the given name has been registered.

func (*LibraryBuilder) HasFunction

func (b *LibraryBuilder) HasFunction(name string) bool

HasFunction checks if a function with the given name has been registered.

func (*LibraryBuilder) Merge

func (b *LibraryBuilder) Merge(other *LibraryBuilder) *LibraryBuilder

Merge merges another builder's functions and constants into this one. If there are conflicts, the other builder's values take precedence.

func (*LibraryBuilder) RemoveConstant

func (b *LibraryBuilder) RemoveConstant(name string) *LibraryBuilder

RemoveConstant removes a constant by name.

func (*LibraryBuilder) RemoveFunction

func (b *LibraryBuilder) RemoveFunction(name string) *LibraryBuilder

RemoveFunction removes a function by name.

func (*LibraryBuilder) String

func (b *LibraryBuilder) String() string

String returns a string representation of the builder's state.

func (*LibraryBuilder) SubLibrary

func (b *LibraryBuilder) SubLibrary(name string, lib *Library) *LibraryBuilder

SubLibrary creates a new sub-library with the given name. Sub-libraries are accessed as `parent.sub` in scriptling code.

Example:

subLib := NewLibraryBuilder("parse", "URL parsing utilities")
subLib.Function("quote", func(s string) string { ... })
builder.SubLibrary("parse", subLib.Build())

type LibraryInfo

type LibraryInfo struct {
	Name       string
	IsImported bool
}

LibraryInfo contains information about available libraries

type LibraryRegistrar

type LibraryRegistrar interface {
	RegisterLibrary(lib *Library)
}

LibraryRegistrar is an interface for registering libraries. This allows external libraries to register themselves without circular imports.

type List

type List struct {
	Elements []Object
}

func (*List) AsBool

func (l *List) AsBool() (bool, Object)

func (*List) AsDict

func (l *List) AsDict() (map[string]Object, Object)

func (*List) AsFloat

func (l *List) AsFloat() (float64, Object)

func (*List) AsInt

func (l *List) AsInt() (int64, Object)

func (*List) AsList

func (l *List) AsList() ([]Object, Object)

func (*List) AsString

func (l *List) AsString() (string, Object)

func (*List) CoerceFloat

func (l *List) CoerceFloat() (float64, Object)

func (*List) CoerceInt

func (l *List) CoerceInt() (int64, Object)

func (*List) CoerceString

func (l *List) CoerceString() (string, Object)

func (*List) Inspect

func (l *List) Inspect() string

func (*List) Type

func (l *List) Type() ObjectType

type Null

type Null struct{}

func (*Null) AsBool

func (n *Null) AsBool() (bool, Object)

func (*Null) AsDict

func (n *Null) AsDict() (map[string]Object, Object)

func (*Null) AsFloat

func (n *Null) AsFloat() (float64, Object)

func (*Null) AsInt

func (n *Null) AsInt() (int64, Object)

func (*Null) AsList

func (n *Null) AsList() ([]Object, Object)

func (*Null) AsString

func (n *Null) AsString() (string, Object)

func (*Null) CoerceFloat

func (n *Null) CoerceFloat() (float64, Object)

func (*Null) CoerceInt

func (n *Null) CoerceInt() (int64, Object)

func (*Null) CoerceString

func (n *Null) CoerceString() (string, Object)

func (*Null) Inspect

func (n *Null) Inspect() string

func (*Null) Type

func (n *Null) Type() ObjectType

type Object

type Object interface {
	Type() ObjectType
	Inspect() string

	// Type-safe accessor methods (strict type checking)
	AsString() (string, Object)
	AsInt() (int64, Object)
	AsFloat() (float64, Object)
	AsBool() (bool, Object)
	AsList() ([]Object, Object)
	AsDict() (map[string]Object, Object)

	// Coercion methods (loose type conversion with best effort)
	CoerceString() (string, Object)
	CoerceInt() (int64, Object)
	CoerceFloat() (float64, Object)
}

func IterableToSlice

func IterableToSlice(obj Object) ([]Object, bool)

IterableToSlice converts any iterable object (List, Tuple, String, Iterator, Set) to a slice of Objects. Returns (elements, ok) where ok is true if the conversion succeeded. For strings, each character becomes a String object. For iterators, this consumes the iterator. For dicts, returns the keys (like Python's list(dict)).

type ObjectType

type ObjectType int
const (
	INTEGER_OBJ ObjectType = iota
	FLOAT_OBJ
	BOOLEAN_OBJ
	STRING_OBJ
	NULL_OBJ
	RETURN_OBJ
	BREAK_OBJ
	CONTINUE_OBJ
	FUNCTION_OBJ
	LAMBDA_OBJ
	BUILTIN_OBJ
	LIST_OBJ
	TUPLE_OBJ
	DICT_OBJ
	ERROR_OBJ
	EXCEPTION_OBJ
	CLASS_OBJ
	INSTANCE_OBJ
	SUPER_OBJ
	ITERATOR_OBJ
	DICT_KEYS_OBJ
	DICT_VALUES_OBJ
	DICT_ITEMS_OBJ
	SET_OBJ
	SLICE_OBJ
	PROPERTY_OBJ
	STATICMETHOD_OBJ
	CLASSMETHOD_OBJ
)

func (ObjectType) String

func (ot ObjectType) String() string

String returns the string representation of the ObjectType

type Property added in v0.2.0

type Property struct {
	Getter Object // Function to call when the attribute is accessed
	Setter Object // Function to call when the attribute is assigned (nil = read-only)
}

Property wraps a getter (and optional setter) for use with @property.

func (*Property) AsBool added in v0.2.0

func (p *Property) AsBool() (bool, Object)

func (*Property) AsDict added in v0.2.0

func (p *Property) AsDict() (map[string]Object, Object)

func (*Property) AsFloat added in v0.2.0

func (p *Property) AsFloat() (float64, Object)

func (*Property) AsInt added in v0.2.0

func (p *Property) AsInt() (int64, Object)

func (*Property) AsList added in v0.2.0

func (p *Property) AsList() ([]Object, Object)

func (*Property) AsString added in v0.2.0

func (p *Property) AsString() (string, Object)

func (*Property) CoerceFloat added in v0.2.0

func (p *Property) CoerceFloat() (float64, Object)

func (*Property) CoerceInt added in v0.2.0

func (p *Property) CoerceInt() (int64, Object)

func (*Property) CoerceString added in v0.2.0

func (p *Property) CoerceString() (string, Object)

func (*Property) Inspect added in v0.2.0

func (p *Property) Inspect() string

func (*Property) Type added in v0.2.0

func (p *Property) Type() ObjectType

type ReturnValue

type ReturnValue struct {
	Value Object
}

func (*ReturnValue) AsBool

func (rv *ReturnValue) AsBool() (bool, Object)

func (*ReturnValue) AsDict

func (rv *ReturnValue) AsDict() (map[string]Object, Object)

func (*ReturnValue) AsFloat

func (rv *ReturnValue) AsFloat() (float64, Object)

func (*ReturnValue) AsInt

func (rv *ReturnValue) AsInt() (int64, Object)

func (*ReturnValue) AsList

func (rv *ReturnValue) AsList() ([]Object, Object)

func (*ReturnValue) AsString

func (rv *ReturnValue) AsString() (string, Object)

func (*ReturnValue) CoerceFloat

func (rv *ReturnValue) CoerceFloat() (float64, Object)

func (*ReturnValue) CoerceInt

func (rv *ReturnValue) CoerceInt() (int64, Object)

func (*ReturnValue) CoerceString

func (rv *ReturnValue) CoerceString() (string, Object)

func (*ReturnValue) Inspect

func (rv *ReturnValue) Inspect() string

func (*ReturnValue) Type

func (rv *ReturnValue) Type() ObjectType

type Set

type Set struct {
	Elements map[string]Object
}

Set represents a set of unique objects

func NewSet

func NewSet() *Set

NewSet creates a new empty Set

func NewSetFromElements

func NewSetFromElements(elements []Object) *Set

NewSetFromElements creates a new Set from a slice of objects

func (*Set) Add

func (s *Set) Add(obj Object)

Add adds an element to the set

func (*Set) AsBool

func (s *Set) AsBool() (bool, Object)

func (*Set) AsDict

func (s *Set) AsDict() (map[string]Object, Object)

func (*Set) AsFloat

func (s *Set) AsFloat() (float64, Object)

func (*Set) AsInt

func (s *Set) AsInt() (int64, Object)

func (*Set) AsList

func (s *Set) AsList() ([]Object, Object)

func (*Set) AsString

func (s *Set) AsString() (string, Object)

func (*Set) CoerceFloat

func (s *Set) CoerceFloat() (float64, Object)

func (*Set) CoerceInt

func (s *Set) CoerceInt() (int64, Object)

func (*Set) CoerceString

func (s *Set) CoerceString() (string, Object)

func (*Set) Contains

func (s *Set) Contains(obj Object) bool

Contains checks if an element is in the set

func (*Set) Copy

func (s *Set) Copy() *Set

Copy returns a shallow copy of the set

func (*Set) CreateIterator

func (s *Set) CreateIterator() *Iterator

CreateIterator returns an iterator for the set

func (*Set) Difference

func (s *Set) Difference(other *Set) *Set

Difference returns a new set with elements in s but not in other

func (*Set) Inspect

func (s *Set) Inspect() string

func (*Set) Intersection

func (s *Set) Intersection(other *Set) *Set

Intersection returns a new set with elements common to both sets

func (*Set) IsSubset

func (s *Set) IsSubset(other *Set) bool

IsSubset checks if s is a subset of other

func (*Set) IsSuperset

func (s *Set) IsSuperset(other *Set) bool

IsSuperset checks if s is a superset of other

func (*Set) Remove

func (s *Set) Remove(obj Object) bool

Remove removes an element from the set

func (*Set) SymmetricDifference

func (s *Set) SymmetricDifference(other *Set) *Set

SymmetricDifference returns a new set with elements in either s or other but not both

func (*Set) Type

func (s *Set) Type() ObjectType

func (*Set) Union

func (s *Set) Union(other *Set) *Set

Union returns a new set with elements from both sets

type Slice

type Slice struct {
	Start *Integer // nil means None (default start)
	End   *Integer // nil means None (default end)
	Step  *Integer // nil means None (default step = 1)
}

func (*Slice) AsBool

func (s *Slice) AsBool() (bool, Object)

func (*Slice) AsDict

func (s *Slice) AsDict() (map[string]Object, Object)

func (*Slice) AsFloat

func (s *Slice) AsFloat() (float64, Object)

func (*Slice) AsInt

func (s *Slice) AsInt() (int64, Object)

func (*Slice) AsList

func (s *Slice) AsList() ([]Object, Object)

func (*Slice) AsString

func (s *Slice) AsString() (string, Object)

func (*Slice) CoerceFloat

func (s *Slice) CoerceFloat() (float64, Object)

func (*Slice) CoerceInt

func (s *Slice) CoerceInt() (int64, Object)

func (*Slice) CoerceString

func (s *Slice) CoerceString() (string, Object)

func (*Slice) Inspect

func (s *Slice) Inspect() string

func (*Slice) Type

func (s *Slice) Type() ObjectType

type StaticMethod added in v0.2.0

type StaticMethod struct {
	Fn Object
}

StaticMethod wraps a function for use with @staticmethod. When called on an instance, self is not prepended.

func (*StaticMethod) AsBool added in v0.2.0

func (s *StaticMethod) AsBool() (bool, Object)

func (*StaticMethod) AsDict added in v0.2.0

func (s *StaticMethod) AsDict() (map[string]Object, Object)

func (*StaticMethod) AsFloat added in v0.2.0

func (s *StaticMethod) AsFloat() (float64, Object)

func (*StaticMethod) AsInt added in v0.2.0

func (s *StaticMethod) AsInt() (int64, Object)

func (*StaticMethod) AsList added in v0.2.0

func (s *StaticMethod) AsList() ([]Object, Object)

func (*StaticMethod) AsString added in v0.2.0

func (s *StaticMethod) AsString() (string, Object)

func (*StaticMethod) CoerceFloat added in v0.2.0

func (s *StaticMethod) CoerceFloat() (float64, Object)

func (*StaticMethod) CoerceInt added in v0.2.0

func (s *StaticMethod) CoerceInt() (int64, Object)

func (*StaticMethod) CoerceString added in v0.2.0

func (s *StaticMethod) CoerceString() (string, Object)

func (*StaticMethod) Inspect added in v0.2.0

func (s *StaticMethod) Inspect() string

func (*StaticMethod) Type added in v0.2.0

func (s *StaticMethod) Type() ObjectType

type String

type String struct {
	Value string
}

func (*String) AsBool

func (s *String) AsBool() (bool, Object)

func (*String) AsDict

func (s *String) AsDict() (map[string]Object, Object)

func (*String) AsFloat

func (s *String) AsFloat() (float64, Object)

func (*String) AsInt

func (s *String) AsInt() (int64, Object)

func (*String) AsList

func (s *String) AsList() ([]Object, Object)

func (*String) AsString

func (s *String) AsString() (string, Object)

func (*String) CoerceFloat

func (s *String) CoerceFloat() (float64, Object)

func (*String) CoerceInt

func (s *String) CoerceInt() (int64, Object)

func (*String) CoerceString

func (s *String) CoerceString() (string, Object)

func (*String) Inspect

func (s *String) Inspect() string

func (*String) Type

func (s *String) Type() ObjectType

type Super

type Super struct {
	Class    *Class
	Instance *Instance
}

func (*Super) AsBool

func (s *Super) AsBool() (bool, Object)

func (*Super) AsDict

func (s *Super) AsDict() (map[string]Object, Object)

func (*Super) AsFloat

func (s *Super) AsFloat() (float64, Object)

func (*Super) AsInt

func (s *Super) AsInt() (int64, Object)

func (*Super) AsList

func (s *Super) AsList() ([]Object, Object)

func (*Super) AsString

func (s *Super) AsString() (string, Object)

func (*Super) CoerceFloat

func (s *Super) CoerceFloat() (float64, Object)

func (*Super) CoerceInt

func (s *Super) CoerceInt() (int64, Object)

func (*Super) CoerceString

func (s *Super) CoerceString() (string, Object)

func (*Super) Inspect

func (s *Super) Inspect() string

func (*Super) Type

func (s *Super) Type() ObjectType

type Tuple

type Tuple struct {
	Elements []Object
}

func (*Tuple) AsBool

func (t *Tuple) AsBool() (bool, Object)

func (*Tuple) AsDict

func (t *Tuple) AsDict() (map[string]Object, Object)

func (*Tuple) AsFloat

func (t *Tuple) AsFloat() (float64, Object)

func (*Tuple) AsInt

func (t *Tuple) AsInt() (int64, Object)

func (*Tuple) AsList

func (t *Tuple) AsList() ([]Object, Object)

func (*Tuple) AsString

func (t *Tuple) AsString() (string, Object)

func (*Tuple) CoerceFloat

func (t *Tuple) CoerceFloat() (float64, Object)

func (*Tuple) CoerceInt

func (t *Tuple) CoerceInt() (int64, Object)

func (*Tuple) CoerceString

func (t *Tuple) CoerceString() (string, Object)

func (*Tuple) Inspect

func (t *Tuple) Inspect() string

func (*Tuple) Type

func (t *Tuple) Type() ObjectType

Jump to

Keyboard shortcuts

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