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
- Variables
- func IsTruthy(obj Object) bool
- func SetLoadPluginImpl(fn func(path string) (Object, error)) func(path string) (Object, error)
- func SetRunCodeImpl(fn func(code string, args *Map) (Object, error)) func(code string, args *Map) (Object, error)
- type Array
- type Bool
- type Builtin
- type BuiltinFunction
- type Class
- type CompiledFunction
- type Environment
- type Error
- type Float
- type Function
- type HashKey
- type Identifier
- type Instance
- type Int
- type Map
- type MapPair
- type Module
- type Null
- type Object
- type ObjectType
- type Return
- type String
- type StringBuilder
- func (sb *StringBuilder) Cap() int
- func (sb *StringBuilder) Clear()
- func (sb *StringBuilder) Grow(n int)
- func (sb *StringBuilder) HashKey() HashKey
- func (sb *StringBuilder) Inspect() string
- func (sb *StringBuilder) Len() int
- func (sb *StringBuilder) Reset()
- func (sb *StringBuilder) String() string
- func (sb *StringBuilder) ToBool() *Bool
- func (sb *StringBuilder) Type() ObjectType
- func (sb *StringBuilder) TypeTag() TypeTag
- func (sb *StringBuilder) Write(s string) int
- func (sb *StringBuilder) WriteLine(s string) int
- type TypeTag
Constants ¶
const IntCacheMax = 10000
IntCacheMax is the maximum cached integer value Extended to cover common loop indices and calculation results
const IntCacheMin = -100
IntCacheMin is the minimum cached integer value
Variables ¶
var ( TRUE = &Bool{Value: true} FALSE = &Bool{Value: false} )
TRUE and FALSE are singleton boolean values
var Builtins = map[string]*Builtin{}/* 105 elements not displayed */
Builtins contains all built-in functions
var NULL = &Null{}
NULL is the singleton null value
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 SetLoadPluginImpl ¶ added in v0.4.19
SetLoadPluginImpl registers the loadPlugin 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
type Bool ¶
type Bool struct {
Value bool
}
Bool represents a boolean value
func (*Bool) Type ¶
func (b *Bool) Type() ObjectType
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) Type ¶
func (b *Builtin) Type() ObjectType
type BuiltinFunction ¶
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) Type ¶
func (c *Class) Type() ObjectType
type CompiledFunction ¶
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
type Error ¶
type Error struct {
Message string
}
Error represents a runtime error
func (*Error) Type ¶
func (e *Error) Type() ObjectType
type Float ¶
type Float struct {
Value float64
}
Float represents a floating-point value
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) Type ¶
func (f *Function) Type() ObjectType
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 ¶
Instance represents an instance of a class
func (*Instance) Type ¶
func (i *Instance) Type() ObjectType
type Int ¶
type Int struct {
Value int64
}
Int represents an integer value
type Map ¶
Map represents a map value
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 ¶
HashKey returns a hash key for the module. Modules are not hashable in a meaningful way, so we return a constant.
type Null ¶
type Null struct{}
Null represents the null value
func (*Null) Type ¶
func (n *Null) Type() ObjectType
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) Type ¶
func (r *Return) Type() ObjectType
type String ¶
type String struct {
Value string
}
String represents a string value
func InternBatch ¶
InternBatch pre-interns a batch of strings
func InternString ¶
Intern returns a cached *String for the given value This reduces allocations for frequently used strings
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