Documentation
¶
Overview ¶
Package object defines the object system for the Monkey programming language.
This package implements the runtime object system that represents values during the execution of a Monkey program. It defines various types of objects such as integers, booleans, strings, arrays, hashes, functions, and built-ins.
Key components:
- Object interface: The base interface for all runtime values
- Various object types (Integer, Boolean, String, Array, Hash, Function, etc.)
- Environment: Stores variable bindings during execution
- Hashable interface: For objects that can be used as hash keys
- Optimized hash table implementation with key caching for better performance
The evaluator uses the object system to represent and manipulate values during program execution.
Index ¶
Constants ¶
const ( IntegerObj = "INTEGER" BooleanObj = "BOOLEAN" StringObj = "STRING" NullObj = "NULL" ReturnValueObj = "RETURN_VALUE" ErrorObj = "ERROR" FunctionObj = "FUNCTION" BuiltinObj = "BUILTIN" ArrayObj = "ARRAY" HashObj = "HASH" CompiledFunctionObj = "COMPILED_FUNCTION_OBJ" ClosureObj = "CLOSURE" )
Variables ¶
var Builtins = []struct { // The name of the built-in function. Name string // The definition (and implementation) of the built-in function. Builtin *Builtin }{ { "len", &Builtin{ Fn: func(args ...Object) Object { if len(args) != 1 { return newError("wrong number of arguments. got=%d, want=1", len(args)) } switch arg := args[0].(type) { case *String: return &Integer{Value: int64(len(arg.Value))} case *Array: return &Integer{Value: int64(len(arg.Elements))} default: return newError("argument to `len` not supported, got %s", args[0].Type()) } }, }, }, { "first", &Builtin{ Fn: func(args ...Object) Object { if len(args) != 1 { return newError("wrong number of arguments. got=%d, want=1", len(args)) } switch arg := args[0].(type) { case *Array: if len(arg.Elements) > 0 { return arg.Elements[0] } return nil default: return newError("argument to `first` not supported, got %s", args[0].Type()) } }, }, }, { "rest", &Builtin{ Fn: func(args ...Object) Object { if len(args) != 1 { return newError("wrong number of arguments. got=%d, want=1", len(args)) } switch arg := args[0].(type) { case *Array: length := len(arg.Elements) if length > 0 { newElements := make([]Object, length-1) copy(newElements, arg.Elements[1:length]) return &Array{Elements: newElements} } return nil default: return newError("argument to `rest` not supported, got %s", args[0].Type()) } }, }, }, { "last", &Builtin{ Fn: func(args ...Object) Object { if len(args) != 1 { return newError("wrong number of arguments. got=%d, want=1", len(args)) } switch arg := args[0].(type) { case *Array: length := len(arg.Elements) if length > 0 { return arg.Elements[length-1] } return nil default: return newError("argument to `last` not supported, got %s", args[0].Type()) } }, }, }, { "push", &Builtin{ Fn: func(args ...Object) Object { if len(args) != 2 { return newError("wrong number of arguments. got=%d, want=2", len(args)) } switch arg := args[0].(type) { case *Array: length := len(arg.Elements) newElements := make([]Object, length+1) copy(newElements, arg.Elements) newElements[length] = args[1] return &Array{Elements: newElements} default: return newError("argument to `push` not supported, got %s", args[0].Type()) } }, }, }, { "puts", &Builtin{ Fn: func(args ...Object) Object { for _, arg := range args { fmt.Print(arg.Inspect() + " ") } fmt.Println() return nil }, }, }, }
Builtins is a collection of predefined built-in functions available for use within the language.
Functions ¶
This section is empty.
Types ¶
type Array ¶
type Array struct {
Elements []Object
}
Array represents a Monkey array.
type Boolean ¶
type Boolean struct {
Value bool
}
Boolean represents a Monkey boolean value.
type Builtin ¶
type Builtin struct {
Fn BuiltinFunction
}
Builtin represents a Monkey builtin.
func GetBuiltinByName ¶
GetBuiltinByName retrieves a built-in function definition by its name from the predefined Builtins collection.
It returns a pointer to the corresponding Builtin or nil if the name is not found.
type BuiltinFunction ¶
BuiltinFunction represents a Monkey builtin function.
type Closure ¶
type Closure struct {
// Fn is a reference to the compiled function containing the bytecode and metadata for closure execution.
Fn *CompiledFunction
// Free holds the objects representing free variables captured by the closure for use during its execution.
Free []Object
}
Closure represents a function and its free variables in a virtual machine's execution context.
func (*Closure) Inspect ¶
Inspect returns a string representation of the Closure instance, including its memory address.
func (*Closure) Type ¶
Type returns the type of the object, specifically ClosureObj for instances of Closure.
type CompiledFunction ¶
type CompiledFunction struct {
// Represents the bytecode sequence of a compiled function.
Instructions code.Instructions
// NumLocals indicates the number of local variables used within the compiled function.
NumLocals int
// NumParameters specifies the number of parameters accepted by the compiled function.
NumParameters int
}
CompiledFunction represents a compiled piece of bytecode with its instructions, local variables, and parameters.
func (*CompiledFunction) Inspect ¶
func (c *CompiledFunction) Inspect() string
Inspect returns a formatted string representation of the CompiledFunction instance, including its memory address.
func (*CompiledFunction) Type ¶
func (c *CompiledFunction) Type() Type
Type returns the object type of the compiled function, which is CompiledFunctionObj.
type Environment ¶
type Environment struct {
// contains filtered or unexported fields
}
Environment represents a scope in a program.
func NewEnclosedEnvironment ¶
func NewEnclosedEnvironment(outer *Environment) *Environment
NewEnclosedEnvironment creates a new Environment with an empty store and the given outer environment. This is used to create a new scope (e.g., for function calls) that has access to variables in the outer scope through the outer environment.
func NewEnvironment ¶
func NewEnvironment() *Environment
NewEnvironment creates a new Environment with an empty store and no outer environment. This is typically used to create the global environment for a program.
type Error ¶
type Error struct {
Message string
}
Error represents a Monkey error.
type Function ¶
type Function struct {
Parameters []*ast.Identifier
Body *ast.BlockStatement
// Env is the environment in which the function is defined, used to resolve variables during function execution.
Env *Environment
}
Function represents a Monkey function.
type Hash ¶
Hash represents a Monkey hash.
type Hashable ¶
type Hashable interface {
HashKey() HashKey
}
Hashable represents an object that can be used as a hash key.
type Integer ¶
type Integer struct {
Value int64
}
Integer represents a Monkey integer value.
type Null ¶
type Null struct{}
Null represents a Monkey null value.
type Object ¶
type Object interface {
// Type returns the type of the object as a value of Type.
Type() Type
// Inspect returns a string representation of the object.
Inspect() string
}
Object is the interface that wraps the basic operations of all Monkey objects. All Monkey objects implement this interface.
type ReturnValue ¶
type ReturnValue struct {
Value Object
}
ReturnValue represents a Monkey return value.
func (*ReturnValue) Inspect ¶
func (rv *ReturnValue) Inspect() string
Inspect returns a string representation of the object.