objects

package
v0.4.20 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

pkg/objects/array.go

pkg/objects/builtin.go

pkg/objects/class.go

pkg/objects/float.go

pkg/objects/function.go

pkg/objects/int.go

pkg/objects/map.go

pkg/objects/methods.go

pkg/objects/module.go Module object type for runtime module representation.

pkg/objects/object.go

pkg/objects/string.go

pkg/objects/stringbuilder.go

Index

Constants

View Source
const IntCacheMax = 10000

IntCacheMax is the maximum cached integer value Extended to cover common loop indices and calculation results

View Source
const IntCacheMin = -100

IntCacheMin is the minimum cached integer value

Variables

View Source
var (
	TRUE  = &Bool{Value: true}
	FALSE = &Bool{Value: false}
)

TRUE and FALSE are singleton boolean values

View Source
var Builtins = map[string]*Builtin{}/* 105 elements not displayed */

Builtins contains all built-in functions

View Source
var NULL = &Null{}

NULL is the singleton null value

View Source
var TypeMethods = map[ObjectType]map[string]*Builtin{
	IntType:           intMethods,
	FloatType:         floatMethods,
	StringType:        stringMethods,
	ArrayType:         arrayMethods,
	MapType:           mapMethods,
	BoolType:          boolMethods,
	NullType:          nullMethods,
	StringBuilderType: stringBuilderMethods,
}

TypeMethods maps ObjectType -> methodName -> *Builtin

Functions

func IsTruthy

func IsTruthy(obj Object) bool

IsTruthy checks if an object is truthy

func SetLoadPluginImpl added in v0.4.19

func SetLoadPluginImpl(fn func(path string) (Object, error)) func(path string) (Object, error)

SetLoadPluginImpl registers the loadPlugin implementation and returns the previous value

func SetRunCodeImpl added in v0.4.19

func SetRunCodeImpl(fn func(code string, args *Map) (Object, error)) func(code string, args *Map) (Object, error)

SetRunCodeImpl registers the runCode implementation and returns the previous value

Types

type Array

type Array struct {
	Elements   []Object
	LastPopped Object // Used by pop() to return the popped value
}

Array represents an array value

func (*Array) HashKey

func (a *Array) HashKey() HashKey

HashKey returns the hash key for map operations

func (*Array) Inspect

func (a *Array) Inspect() string

Inspect returns the string representation

func (*Array) ToBool

func (a *Array) ToBool() *Bool

ToBool converts the array to a boolean

func (*Array) Type

func (a *Array) Type() ObjectType

Type returns the object type

func (*Array) TypeTag

func (a *Array) TypeTag() TypeTag

TypeTag returns the type tag for fast type checking

type Bool

type Bool struct {
	Value bool
}

Bool represents a boolean value

func (*Bool) HashKey

func (b *Bool) HashKey() HashKey

func (*Bool) Inspect

func (b *Bool) Inspect() string

func (*Bool) ToBool

func (b *Bool) ToBool() *Bool

func (*Bool) Type

func (b *Bool) Type() ObjectType

func (*Bool) TypeTag

func (b *Bool) TypeTag() TypeTag

type Builtin

type Builtin struct {
	Fn BuiltinFunction
}

Builtin represents a built-in function

func GetMethod

func GetMethod(objType ObjectType, name string) (*Builtin, bool)

GetMethod returns the builtin method for the given object type and method name

func (*Builtin) HashKey

func (b *Builtin) HashKey() HashKey

func (*Builtin) Inspect

func (b *Builtin) Inspect() string

func (*Builtin) ToBool

func (b *Builtin) ToBool() *Bool

func (*Builtin) Type

func (b *Builtin) Type() ObjectType

func (*Builtin) TypeTag

func (b *Builtin) TypeTag() TypeTag

type BuiltinFunction

type BuiltinFunction func(args ...Object) Object

BuiltinFunction is the type for built-in functions

type Class

type Class struct {
	Name       string
	SuperClass *Class
	Methods    map[string]Object // methods are CompiledFunction objects
	InitMethod Object            // constructor method
	Fields     map[string]Object // default field values
}

Class represents a class definition

func (*Class) HashKey

func (c *Class) HashKey() HashKey

func (*Class) Inspect

func (c *Class) Inspect() string

func (*Class) ToBool

func (c *Class) ToBool() *Bool

func (*Class) Type

func (c *Class) Type() ObjectType

func (*Class) TypeTag

func (c *Class) TypeTag() TypeTag

type CompiledFunction

type CompiledFunction struct {
	Instructions  []byte
	NumLocals     int
	NumParameters int
	Name          string
}

CompiledFunction represents a compiled function for the VM

func (*CompiledFunction) HashKey

func (cf *CompiledFunction) HashKey() HashKey

func (*CompiledFunction) Inspect

func (cf *CompiledFunction) Inspect() string

func (*CompiledFunction) ToBool

func (cf *CompiledFunction) ToBool() *Bool

func (*CompiledFunction) Type

func (cf *CompiledFunction) Type() ObjectType

func (*CompiledFunction) TypeTag

func (cf *CompiledFunction) TypeTag() TypeTag

type Environment

type Environment struct {
	Store map[string]Object
	Outer *Environment
}

Environment represents a variable scope

func NewEnclosedEnvironment

func NewEnclosedEnvironment(outer *Environment) *Environment

NewEnclosedEnvironment creates a new environment with an outer scope

func NewEnvironment

func NewEnvironment() *Environment

NewEnvironment creates a new environment

func (*Environment) Get

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

Get retrieves a variable from the environment

func (*Environment) Set

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

Set sets a variable in the environment

type Error

type Error struct {
	Message string
}

Error represents a runtime error

func (*Error) HashKey

func (e *Error) HashKey() HashKey

func (*Error) Inspect

func (e *Error) Inspect() string

func (*Error) ToBool

func (e *Error) ToBool() *Bool

func (*Error) Type

func (e *Error) Type() ObjectType

func (*Error) TypeTag

func (e *Error) TypeTag() TypeTag

type Float

type Float struct {
	Value float64
}

Float represents a floating-point value

func (*Float) HashKey

func (f *Float) HashKey() HashKey

HashKey returns the hash key for map operations

func (*Float) Inspect

func (f *Float) Inspect() string

Inspect returns the string representation

func (*Float) ToBool

func (f *Float) ToBool() *Bool

ToBool converts the float to a boolean

func (*Float) Type

func (f *Float) Type() ObjectType

Type returns the object type

func (*Float) TypeTag

func (f *Float) TypeTag() TypeTag

TypeTag returns the type tag for fast type checking

type Function

type Function struct {
	Parameters []*Identifier
	Body       interface{} // Will be *ast.BlockStatement, using interface{} to avoid import cycle
	Env        *Environment
	Name       string // Optional: for named functions
}

Function represents a user-defined function

func (*Function) HashKey

func (f *Function) HashKey() HashKey

func (*Function) Inspect

func (f *Function) Inspect() string

func (*Function) ToBool

func (f *Function) ToBool() *Bool

func (*Function) Type

func (f *Function) Type() ObjectType

func (*Function) TypeTag

func (f *Function) TypeTag() TypeTag

type HashKey

type HashKey struct {
	Type  ObjectType
	Value uint64
}

HashKey is used for map keys

type Identifier

type Identifier struct {
	Value string
}

Identifier represents an identifier (used in function parameters)

func (*Identifier) String

func (i *Identifier) String() string

type Instance

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

Instance represents an instance of a class

func (*Instance) HashKey

func (i *Instance) HashKey() HashKey

func (*Instance) Inspect

func (i *Instance) Inspect() string

func (*Instance) ToBool

func (i *Instance) ToBool() *Bool

func (*Instance) Type

func (i *Instance) Type() ObjectType

func (*Instance) TypeTag

func (i *Instance) TypeTag() TypeTag

type Int

type Int struct {
	Value int64
}

Int represents an integer value

func NewInt

func NewInt(val int64) *Int

NewInt creates a new Int object, using cache for small values

func (*Int) HashKey

func (i *Int) HashKey() HashKey

HashKey returns the hash key for map operations

func (*Int) Inspect

func (i *Int) Inspect() string

Inspect returns the string representation

func (*Int) ToBool

func (i *Int) ToBool() *Bool

ToBool converts the integer to a boolean

func (*Int) Type

func (i *Int) Type() ObjectType

Type returns the object type

func (*Int) TypeTag

func (i *Int) TypeTag() TypeTag

TypeTag returns the type tag for fast type checking

type Map

type Map struct {
	Pairs map[HashKey]MapPair
}

Map represents a map value

func (*Map) HashKey

func (m *Map) HashKey() HashKey

HashKey returns the hash key for map operations

func (*Map) Inspect

func (m *Map) Inspect() string

Inspect returns the string representation

func (*Map) ToBool

func (m *Map) ToBool() *Bool

ToBool converts the map to a boolean

func (*Map) Type

func (m *Map) Type() ObjectType

Type returns the object type

func (*Map) TypeTag

func (m *Map) TypeTag() TypeTag

TypeTag returns the type tag for fast type checking

type MapPair

type MapPair struct {
	Key   Object
	Value Object
}

MapPair represents a key-value pair in a map

type Module

type Module struct {
	// Name is the module's identifier (typically the file path)
	Name string

	// Exports maps exported symbol names to their values
	Exports map[string]Object

	// Globals holds the module's global variables state.
	// This is needed so exported functions can access module-level variables.
	Globals []Object
}

Module represents a loaded module with its exported symbols. Modules are created when a source file is imported and compiled, and they hold all exported values accessible to importers.

func (*Module) HashKey

func (m *Module) HashKey() HashKey

HashKey returns a hash key for the module. Modules are not hashable in a meaningful way, so we return a constant.

func (*Module) Inspect

func (m *Module) Inspect() string

Inspect returns a string representation of the module.

func (*Module) ToBool

func (m *Module) ToBool() *Bool

ToBool converts the module to a boolean (always true).

func (*Module) Type

func (m *Module) Type() ObjectType

Type returns the object type.

func (*Module) TypeTag

func (m *Module) TypeTag() TypeTag

TypeTag returns the type tag for fast type checking.

type Null

type Null struct{}

Null represents the null value

func (*Null) HashKey

func (n *Null) HashKey() HashKey

func (*Null) Inspect

func (n *Null) Inspect() string

func (*Null) ToBool

func (n *Null) ToBool() *Bool

func (*Null) Type

func (n *Null) Type() ObjectType

func (*Null) TypeTag

func (n *Null) TypeTag() TypeTag

type Object

type Object interface {
	Type() ObjectType
	TypeTag() TypeTag // Fast type check without string comparison
	Inspect() string
	ToBool() *Bool
	HashKey() HashKey
}

Object is the base interface for all values in Xxlang

type ObjectType

type ObjectType string

ObjectType represents the type of an object

const (
	NullType          ObjectType = "NULL"
	IntType           ObjectType = "INT"
	FloatType         ObjectType = "FLOAT"
	StringType        ObjectType = "STRING"
	BoolType          ObjectType = "BOOL"
	ArrayType         ObjectType = "ARRAY"
	MapType           ObjectType = "MAP"
	FunctionType      ObjectType = "FUNCTION"
	BuiltinType       ObjectType = "BUILTIN"
	BytesType         ObjectType = "BYTES"
	ClassType         ObjectType = "CLASS"
	InstanceType      ObjectType = "INSTANCE"
	ErrorType         ObjectType = "ERROR"
	ReturnType        ObjectType = "RETURN"
	ClosureType       ObjectType = "CLOSURE"
	ModuleType        ObjectType = "MODULE"
	StringBuilderType ObjectType = "STRING_BUILDER"
)

Object types

const CompiledFunctionType ObjectType = "COMPILED_FUNCTION"

CompiledFunctionType is the type for compiled functions

type Return

type Return struct {
	Value Object
}

Return represents a return value (used internally)

func (*Return) HashKey

func (r *Return) HashKey() HashKey

func (*Return) Inspect

func (r *Return) Inspect() string

func (*Return) ToBool

func (r *Return) ToBool() *Bool

func (*Return) Type

func (r *Return) Type() ObjectType

func (*Return) TypeTag

func (r *Return) TypeTag() TypeTag

type String

type String struct {
	Value string
}

String represents a string value

func InternBatch

func InternBatch(strings []string) []*String

InternBatch pre-interns a batch of strings

func InternString

func InternString(val string) *String

Intern returns a cached *String for the given value This reduces allocations for frequently used strings

func (*String) HashKey

func (s *String) HashKey() HashKey

HashKey returns the hash key for map operations

func (*String) Inspect

func (s *String) Inspect() string

Inspect returns the string representation

func (*String) ToBool

func (s *String) ToBool() *Bool

ToBool converts the string to a boolean

func (*String) Type

func (s *String) Type() ObjectType

Type returns the object type

func (*String) TypeTag

func (s *String) TypeTag() TypeTag

TypeTag returns the type tag for fast type checking

type StringBuilder added in v0.4.19

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

StringBuilder is a mutable string builder for efficient string concatenation. Unlike regular string concatenation which creates a new string each time, StringBuilder uses an internal buffer to accumulate strings efficiently.

func NewStringBuilder added in v0.4.19

func NewStringBuilder() *StringBuilder

NewStringBuilder creates a new StringBuilder instance.

func (*StringBuilder) Cap added in v0.4.19

func (sb *StringBuilder) Cap() int

Cap returns the current capacity of the builder's internal buffer.

func (*StringBuilder) Clear added in v0.4.19

func (sb *StringBuilder) Clear()

Clear resets the builder, removing all content.

func (*StringBuilder) Grow added in v0.4.19

func (sb *StringBuilder) Grow(n int)

Grow grows the builder's capacity to hold at least n more bytes.

func (*StringBuilder) HashKey added in v0.4.19

func (sb *StringBuilder) HashKey() HashKey

HashKey returns a hash key for the StringBuilder.

func (*StringBuilder) Inspect added in v0.4.19

func (sb *StringBuilder) Inspect() string

Inspect returns a string representation.

func (*StringBuilder) Len added in v0.4.19

func (sb *StringBuilder) Len() int

Len returns the current length of the accumulated string.

func (*StringBuilder) Reset added in v0.4.19

func (sb *StringBuilder) Reset()

Reset is an alias for Clear.

func (*StringBuilder) String added in v0.4.19

func (sb *StringBuilder) String() string

String returns the accumulated string.

func (*StringBuilder) ToBool added in v0.4.19

func (sb *StringBuilder) ToBool() *Bool

ToBool returns true (StringBuilder is always truthy).

func (*StringBuilder) Type added in v0.4.19

func (sb *StringBuilder) Type() ObjectType

Type returns the object type.

func (*StringBuilder) TypeTag added in v0.4.19

func (sb *StringBuilder) TypeTag() TypeTag

TypeTag returns the fast type tag.

func (*StringBuilder) Write added in v0.4.19

func (sb *StringBuilder) Write(s string) int

Write appends a string to the builder.

func (*StringBuilder) WriteLine added in v0.4.19

func (sb *StringBuilder) WriteLine(s string) int

WriteLine appends a string followed by a newline to the builder.

type TypeTag

type TypeTag uint8

TypeTag is a fast integer type identifier for hot path checks

const (
	TagNull TypeTag = iota
	TagInt
	TagFloat
	TagString
	TagBool
	TagArray
	TagMap
	TagFunction
	TagBuiltin
	TagBytes
	TagClass
	TagInstance
	TagError
	TagReturn
	TagClosure
	TagModule
	TagStringBuilder
	TagUnknown
)

Type tags for fast type checking (must match ObjectType order)

const TagCompiledFunction TypeTag = TagClosure // Use same tag as Closure

TagCompiledFunction is the type tag for compiled functions

Jump to

Keyboard shortcuts

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