object

package
v0.0.0-...-42894c0 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2020 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// INTEGER_OBJ is the Integer object type.
	INTEGER_OBJ = "INTEGER"

	// BOOLEAN_OBJ is the Boolean object type.
	BOOLEAN_OBJ = "BOOLEAN"

	// STRING_OBJ is the String object type.
	STRING_OBJ = "STRING"

	// NULL_OBJ is the Null object type.
	NULL_OBJ = "NULL"

	// RETURN_VALUE_OBJ is the Return value object type.
	RETURN_VALUE_OBJ = "RETURN_VALUE"

	// ERROR_OBJ is the Error object type.
	ERROR_OBJ = "ERROR"

	// FUNCTION_OBJ is the Function object type.
	FUNCTION_OBJ = "FUNCTION"

	// BUILTIN_OBJ is the Builtin object type.
	BUILTIN_OBJ = "BUILTIN"

	// ARRAY_OBJ is the Array object type.
	ARRAY_OBJ = "ARRAY"

	// HASH_OBJ is the Hash object type.
	HASH_OBJ = "HASH"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Array

type Array struct {
	Elements []Object
}

Array is the array literal type that holds a slice of Object(s).

func (*Array) Inspect

func (ao *Array) Inspect() string

Inspect returns a stringified version of the object for debugging.

func (*Array) Type

func (ao *Array) Type() ObjectType

Type returns the type of the object

type Boolean

type Boolean struct {
	Value bool
}

Boolean is the boolean type and used to represent boolean literals and holds an internal bool value.

func (*Boolean) HashKey

func (b *Boolean) HashKey() HashKey

HashKey returns a HashKey object.

func (*Boolean) Inspect

func (b *Boolean) Inspect() string

Inspect returns a stringified version of the object for debugging.

func (*Boolean) Type

func (b *Boolean) Type() ObjectType

Type returns the type of the object.

type Builtin

type Builtin struct {
	Fn BuiltinFunction
}

Builtin is the builtin object type that simply holds a reference to a BuiltinFunction type that takes zero or more objects as arguments and returns an object.

func (*Builtin) Inspect

func (b *Builtin) Inspect() string

Inspect returns a stringified version of the object for debugging.

func (*Builtin) Type

func (b *Builtin) Type() ObjectType

Type returns the type of the object.

type BuiltinFunction

type BuiltinFunction func(args ...Object) Object

BuiltinFunction represents the builtin function type. It's the type definition of a callable Go function.

type Environment

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

Environment is what we use to keep track of value by associating them with a name. Technically, it's an object that holds a mapping of names to bound objets.

func NewEnclosedEnvironment

func NewEnclosedEnvironment(outer *Environment) *Environment

NewEnclosedEnvironment returns a new Environment with the outer set to the current environment (enclosing environment).

func NewEnvironment

func NewEnvironment() *Environment

NewEnvironment constructs a new Environment object to hold bindings of identifiers to their names.

func (*Environment) Get

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

Get returns the object bound by name.

func (*Environment) Set

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

Set stores the object with the given name.

type Error

type Error struct {
	Message string
}

Error is the error type and used to hold a message denoting the details of error encountered. This object is tracked through the evaluator and when encountered stops evaulation of the program or body of a function. In a production-ready interpreter we'd want to attach a stack trace to such error objects, add the line and column numbers of its origin.

func (*Error) Inspect

func (e *Error) Inspect() string

Inspect returns a stringified version of the object for debugging.

func (*Error) Type

func (e *Error) Type() ObjectType

Type returns the type of the object.

type Function

type Function struct {
	Parameters []*ast.Identifier
	Body       *ast.BlockStatement
	Env        *Environment
}

Function is the function type that holds the function's formal parameters, body and an environment to support closures.

func (*Function) Inspect

func (f *Function) Inspect() string

Inspect returns a stringified version of the object for debugging.

func (*Function) Type

func (f *Function) Type() ObjectType

Type returns the type of the object.

type Hash

type Hash struct {
	Pairs map[HashKey]HashPair
}

Hash is a hash map and holds a map of HashKey to HashPair(s).

func (*Hash) Inspect

func (h *Hash) Inspect() string

Inspect returns a stringified version of the object for debugging.

func (*Hash) Type

func (h *Hash) Type() ObjectType

Type returns the type of the object.

type HashKey

type HashKey struct {
	Type  ObjectType
	Value uint64
}

HashKey represents a hash key object and holds the Type of Object hashed and its hash value in Value.

type HashPair

type HashPair struct {
	Key   Object
	Value Object
}

HashPair is an object that holds a key and value of type Object.

type Hashable

type Hashable interface {
	HashKey() HashKey
}

Hashable is the interface for all hashable objects which must implement the HashKey() method which returns a HashKey result.

type Integer

type Integer struct {
	Value int64
}

Integer is the integer type used to represent integer literals and holds an internal int64 value. Whenever we encounter an integer literal in the source code we first turn it into an ast.IntegerLiteral and then, when evaluating that AST node, we turn it into an object.Integer, saving the value inside our struct and passing around a reference to this struct.

func (*Integer) HashKey

func (i *Integer) HashKey() HashKey

HashKey returns a HashKey object.

func (*Integer) Inspect

func (i *Integer) Inspect() string

Inspect returns a stringified version of the object for debugging.

func (*Integer) Type

func (i *Integer) Type() ObjectType

Type returns the type of the object.

type Null

type Null struct{}

Null is the null type and used to represent the absence of a value.

func (*Null) Inspect

func (n *Null) Inspect() string

Inspect returns a stringified version of the object for debugging.

func (*Null) Type

func (n *Null) Type() ObjectType

Type returns the type of the object.

type Object

type Object interface {
	Type() ObjectType
	Inspect() string
}

Object represents a value and implementations are expected to implement `Type()` and `Inspect()` functions. The reason Object being an interface instead of struct is that every value needs a different internal representation and it’s easier to define two different struct types than trying to fit booleans and integers into the same struct field.

type ObjectType

type ObjectType string

ObjectType represents the type of an object.

type ReturnValue

type ReturnValue struct {
	Value Object
}

ReturnValue is the return value type and used to hold the value of another object. This is used for `return` statements and this object is tracked through the evaluator and when encountered stops evaluation of the program, or body of a function.

func (*ReturnValue) Inspect

func (rv *ReturnValue) Inspect() string

Inspect returns a stringified version of the object for debugging.

func (*ReturnValue) Type

func (rv *ReturnValue) Type() ObjectType

Type returns the type of the object.

type String

type String struct {
	Value string
}

String is the string type used to represent string literals and holds an internal string value.

func (*String) HashKey

func (s *String) HashKey() HashKey

HashKey returns a HashKey object.

func (*String) Inspect

func (s *String) Inspect() string

Inspect returns a stringified version of the object for debugging.

func (*String) Type

func (s *String) Type() ObjectType

Type returns the type of the object.

Jump to

Keyboard shortcuts

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