object

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package object defines the object system for the Monke programming language.

This package implements the runtime object system that represents values during the execution of a Monke 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 (
	INTEGER_OBJ      = "INTEGER"
	BOOLEAN_OBJ      = "BOOLEAN"
	STRING_OBJ       = "STRING"
	NULL_OBJ         = "NULL"
	RETURN_VALUE_OBJ = "RETURN_VALUE"
	ERROR_OBJ        = "ERROR"
	FUNCTION_OBJ     = "FUNCTION"
	BUILTIN_OBJ      = "BUILTIN"
	ARRAY_OBJ        = "ARRAY"
	HASH_OBJ         = "HASH"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Array

type Array struct {
	Elements []Object
}

Array represents a Monke 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 Monke 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 Monke builtin.

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 Monke builtin function.

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 Monke 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        *Environment
}

Function represents a Monke 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 Monke 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 Monke 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 Monke 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() Type
	Inspect() string
}

Object is the interface that wraps the basic operations of all Monke objects. All Monke objects implement this interface.

type ReturnValue

type ReturnValue struct {
	Value Object
}

ReturnValue represents a Monke 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 Monke 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