object

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2025 License: MIT Imports: 6 Imported by: 0

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

View Source
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

View Source
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.

func (*Array) Inspect

func (a *Array) Inspect() string

Inspect returns a string representation of the object.

func (*Array) Type

func (a *Array) Type() Type

Type returns the type of the object.

type Boolean

type Boolean struct {
	Value bool
}

Boolean represents a Monkey boolean value.

func (*Boolean) HashKey

func (b *Boolean) HashKey() HashKey

HashKey returns the hash key for the object.

func (*Boolean) Inspect

func (b *Boolean) Inspect() string

Inspect returns a string representation of the object.

func (*Boolean) Type

func (b *Boolean) Type() Type

Type returns the type of the object.

type Builtin

type Builtin struct {
	Fn BuiltinFunction
}

Builtin represents a Monkey builtin.

func GetBuiltinByName

func GetBuiltinByName(name string) *Builtin

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.

func (*Builtin) Inspect

func (b *Builtin) Inspect() string

Inspect returns a string representation of the object.

func (*Builtin) Type

func (b *Builtin) Type() Type

Type returns the type of the object.

type BuiltinFunction

type BuiltinFunction func(args ...Object) Object

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

func (c *Closure) Inspect() string

Inspect returns a string representation of the Closure instance, including its memory address.

func (*Closure) Type

func (c *Closure) Type() 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.

func (*Environment) Get

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

Get returns the value of the given variable name in the environment. If the variable is not found, it looks in the outer environment, if any.

func (*Environment) Set

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

Set sets the value of the given variable name in the environment.

type Error

type Error struct {
	Message string
}

Error represents a Monkey error.

func (*Error) Inspect

func (e *Error) Inspect() string

Inspect returns a string representation of the object.

func (*Error) Type

func (e *Error) Type() Type

Type returns the type of the object.

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.

func (*Function) Inspect

func (f *Function) Inspect() string

Inspect returns a string representation of the object.

func (*Function) Type

func (f *Function) Type() Type

Type returns the type of the object.

type Hash

type Hash struct {
	Pairs map[HashKey]HashPair
}

Hash represents a Monkey hash.

func (*Hash) Inspect

func (h *Hash) Inspect() string

Inspect returns a string representation of the object.

func (*Hash) Type

func (h *Hash) Type() Type

Type returns the type of the object.

type HashKey

type HashKey struct {
	Type  Type
	Value uint64
}

HashKey represents a hash key.

type HashPair

type HashPair struct {
	Key   Object
	Value Object
}

HashPair represents a hash pair.

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.

func (*Integer) HashKey

func (i *Integer) HashKey() HashKey

HashKey returns the hash key for the object.

func (*Integer) Inspect

func (i *Integer) Inspect() string

Inspect returns a string representation of the object.

func (*Integer) Type

func (i *Integer) Type() Type

Type returns the type of the object.

type Null

type Null struct{}

Null represents a Monkey null value.

func (*Null) Inspect

func (n *Null) Inspect() string

Inspect returns a string representation of the object.

func (*Null) Type

func (n *Null) Type() Type

Type returns the type of the object.

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.

func (*ReturnValue) Type

func (rv *ReturnValue) Type() Type

Type returns the type of the object.

type String

type String struct {
	Value string
	// contains filtered or unexported fields
}

String represents a Monkey string value.

func (*String) HashKey

func (s *String) HashKey() HashKey

HashKey returns the hash key for the object.

func (*String) Inspect

func (s *String) Inspect() string

Inspect returns a string representation of the object.

func (*String) Type

func (s *String) Type() Type

Type returns the type of the object.

type Type

type Type string

Type represents the type of object.

Jump to

Keyboard shortcuts

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