object

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2020 License: MIT Imports: 6 Imported by: 12

Documentation

Index

Constants

View Source
const (
	IntegerObj          = "INTEGER"
	BooleanObj          = "BOOLEAN"
	NullObj             = "NULL"
	ReturnValueObj      = "RETURN_VALUE"
	ErrorObj            = "ERROR"
	FunctionObj         = "FUNCTION"
	StringObj           = "STRING"
	BuiltinObj          = "BUILTIN"
	ArrayObj            = "ARRAY"
	HashObj             = "HASH"
	CompiledFunctionObj = "COMPILED_FUNCTION_OBJ"
	ClosureObj          = "CLOSURE"
)

Define object types

Variables

View Source
var Builtins = []struct {
	Name    string
	Builtin *Builtin
}{
	{
		"len",
		&Builtin{
			Fn: func(args ...Object) Object {
				if len(args) != 1 {
					return newError("Wrong number of arguments. Got: %d, Expected: 1", len(args))
				}
				switch arg := args[0].(type) {
				case *Array:
					return &Integer{Value: int64(len(arg.Elements))}
				case *String:
					return &Integer{Value: int64(len(arg.Value))}
				default:
					return newError("Argument to `len` not supported. Got: %s", args[0].Type())
				}
			},
		},
	},
	{
		"print",
		&Builtin{
			Fn: func(args ...Object) Object {
				for _, arg := range args {
					fmt.Println(arg.Inspect())
				}
				return nil
			},
		},
	},
	{
		"first",
		&Builtin{
			Fn: func(args ...Object) Object {
				if len(args) != 1 {
					return newError("Wrong number of arguments. Got: %d, Expected: 1", len(args))
				}
				if args[0].Type() != ArrayObj {
					return newError("Argument to `first` must be an Array. Got: %s", args[0].Type())
				}

				array := args[0].(*Array)
				if len(array.Elements) > 0 {
					return array.Elements[0]
				}

				return nil
			},
		},
	},
	{
		"last",
		&Builtin{
			Fn: func(args ...Object) Object {
				if len(args) != 1 {
					return newError("Wrong number of arguments. Got: %d, Expected: 1", len(args))
				}
				if args[0].Type() != ArrayObj {
					return newError("Argument to `last` must be an Array. Got: %s", args[0].Type())
				}

				array := args[0].(*Array)
				length := len(array.Elements)
				if length > 0 {
					return array.Elements[length-1]
				}

				return nil
			},
		},
	},
	{
		"rest",
		&Builtin{
			Fn: func(args ...Object) Object {
				if len(args) != 1 {
					return newError("Wrong number of arguments. Got: %d, Expected: 1", len(args))
				}
				if args[0].Type() != ArrayObj {
					return newError("Argument to `rest` must be an Array. Got: %s", args[0].Type())
				}

				array := args[0].(*Array)
				length := len(array.Elements)
				if length > 0 {
					newElements := make([]Object, length-1, length-1)
					copy(newElements, array.Elements[1:length])
					return &Array{Elements: newElements}
				}

				return nil
			},
		},
	},
	{
		"push",
		&Builtin{
			Fn: func(args ...Object) Object {
				if len(args) != 2 {
					return newError("Wrong number of arguments. Got: %d, Expected: 2", len(args))
				}
				if args[0].Type() != ArrayObj {
					return newError("Argument to `push` must be an Array. Got: %s", args[0].Type())
				}

				array := args[0].(*Array)
				length := len(array.Elements)

				newElements := make([]Object, length+1, length+1)
				copy(newElements, array.Elements)
				newElements[length] = args[1]

				return &Array{Elements: newElements}
			},
		},
	},
	{
		"pop",
		&Builtin{
			Fn: func(args ...Object) Object {
				if len(args) != 1 {
					return newError("Wrong number of arguments. Got: %d, Expected: 1", len(args))
				}
				if args[0].Type() != ArrayObj {
					return newError("Argument to `pop` must be an Array. Got: %s", args[0].Type())
				}

				array := args[0].(*Array)
				length := len(array.Elements)
				if length == 0 {
					return nil
				}

				newElements := make([]Object, length-1, length-1)
				copy(newElements, array.Elements[0:length-1])

				return &Array{Elements: newElements}
			},
		},
	},
}

Builtins defines all of Monkey's built in functions

Functions

This section is empty.

Types

type Array

type Array struct {
	Elements []Object
}

Array type wraps the array's elements in a slice of Objects

func (*Array) Inspect

func (a *Array) Inspect() string

Inspect returns a string representation of the Array's elements: [1, 2, 3]

func (*Array) Type

func (a *Array) Type() ObjectType

Type returns our Array's ObjectType (ArrayObj)

type Boolean

type Boolean struct {
	Value bool
}

Boolean type holds the value of the boolean as a bool

func (*Boolean) HashKey

func (b *Boolean) HashKey() HashKey

HashKey returns a HashKey with a Value of 1 or 0 (true or false) and a Type of BooleanObj

func (*Boolean) Inspect

func (b *Boolean) Inspect() string

Inspect returns a string representation of the Boolean's Value

func (*Boolean) Type

func (b *Boolean) Type() ObjectType

Type returns our Boolean's ObjectType (BooleanObj)

type Builtin

type Builtin struct {
	Fn BuiltinFunction
}

Builtin is our object wrapper holding a builtin function

func GetBuiltinByName

func GetBuiltinByName(name string) *Builtin

GetBuiltinByName takes a name, iterates over our builtins slice and returns the appropriate builtin

func (*Builtin) Inspect

func (n *Builtin) Inspect() string

Inspect simply returns "builtin function"

func (*Builtin) Type

func (n *Builtin) Type() ObjectType

Type returns our Builtin's ObjectType

type BuiltinFunction

type BuiltinFunction func(args ...Object) Object

BuiltinFunction is a type representing functions we write in Go and expose to our users inside monkey-lang

type Closure

type Closure struct {
	Fn   *CompiledFunction
	Free []Object
}

Closure holds a pointer to its compiled function and a slice of its free objects (variables it has access to that are not in either global or local scope)

func (*Closure) Inspect

func (c *Closure) Inspect() string

Inspect returns a string representation of the Closure with its address

func (*Closure) Type

func (c *Closure) Type() ObjectType

Type returns our Closure's ObjectType (ClosureObj)

type CompiledFunction

type CompiledFunction struct {
	Instructions  code.Instructions
	NumLocals     int
	NumParameters int
}

CompiledFunction holds the instructions we get from the compilation of a function literal and is an object.Object, which means we can add it as a constant to our compiler.Bytecode and load it in the VM. It also holds the NumLocals which we pass to the VM to allocate the correct amount of stack space ("hole") to save the local bindings

func (*CompiledFunction) Inspect

func (cf *CompiledFunction) Inspect() string

Inspect returns the string "CompiledFunction[address]" - Address of 0th element in base 16 notation, with leading 0x

func (*CompiledFunction) Type

func (cf *CompiledFunction) Type() ObjectType

Type returns our CompiledFunction's ObjectType (CompiledFunctionObj)

type Environment

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

Environment holds a store of key value pairs and a pointer to an "outer", enclosing environment

func NewEnclosedEnvironment

func NewEnclosedEnvironment(outer *Environment) *Environment

NewEnclosedEnvironment creates a new Environment and attaches the outer environment that's passed in, to the new environment, as it's enclosing environment

func NewEnvironment

func NewEnvironment() *Environment

NewEnvironment creates and returns a pointer to an Environment

func (*Environment) Get

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

Get retrieves a key from an Environment's store by name. If it does not find it, it recursively looks for the key in the enclosing environment(s)

func (*Environment) Set

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

Set sets a key to an Environment's store by name

type Error

type Error struct {
	Message string
}

Error type holds an error Message

func (*Error) Inspect

func (e *Error) Inspect() string

Inspect returns an error message string

func (*Error) Type

func (e *Error) Type() ObjectType

Type returns our Error's ObjectType (ErrorObj)

type Function

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

Function holds Parameters as a slice of *Identifier, a Body which is a *ast.BlockStatement and a pointer to it's environment

func (*Function) Inspect

func (f *Function) Inspect() string

Inspect returns a string representation of the function definition

func (*Function) Type

func (f *Function) Type() ObjectType

Type returns our Function's ObjectType (FunctionObj)

type Hash

type Hash struct {
	Pairs map[HashKey]HashPair
}

Hash hold Pairs which are a map of HashKey -> HashPair. We map the keys to HashPairs instead of Objects for a better ability to Inspect() and see the key and value

func (*Hash) Inspect

func (h *Hash) Inspect() string

Inspect returns a string representation of the Hash

func (*Hash) Type

func (h *Hash) Type() ObjectType

Type returns Hash's ObjectType (HashObj)

type HashKey

type HashKey struct {
	Type  ObjectType
	Value uint64
}

HashKey type wraps the key's type and holds its value

type HashPair

type HashPair struct {
	Key   Object
	Value Object
}

HashPair holds key value pairs

type Hashable

type Hashable interface {
	HashKey() HashKey
}

Hashable is one method called HashKey. Any object that that can be used as a HashKey must implement this interface (*object.String, *object.boolean, *object.integer)

type Integer

type Integer struct {
	Value int64
}

Integer type holds the value of the integer as an int64

func (*Integer) HashKey

func (i *Integer) HashKey() HashKey

HashKey returns a HashKey with a Value of the Integer and a Type of IntegerObj

func (*Integer) Inspect

func (i *Integer) Inspect() string

Inspect returns a string representation of the Integer's Value

func (*Integer) Type

func (i *Integer) Type() ObjectType

Type returns our Integer's ObjectType

type Null

type Null struct{}

Null type is an empty struct

func (*Null) Inspect

func (n *Null) Inspect() string

Inspect returns a string representation of Null ("null")

func (*Null) Type

func (n *Null) Type() ObjectType

Type returns Null's ObjectType (NullObj)

type Object

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

Object represents monkey's object system. Every value in monkey-lang must implement this interface

type ObjectType

type ObjectType string

ObjectType - Type alias for a string.

type ReturnValue

type ReturnValue struct {
	Value Object
}

ReturnValue type holds a return value

func (*ReturnValue) Inspect

func (rv *ReturnValue) Inspect() string

Inspect returns a string representation of the ReturnValue's Value

func (*ReturnValue) Type

func (rv *ReturnValue) Type() ObjectType

Type returns our ReturnValue's ObjectType (ReturnValueObj)

type String

type String struct {
	Value string
}

String type holds the value of the string

func (*String) HashKey

func (s *String) HashKey() HashKey

HashKey returns a HashKey with a Value of a 64-bit FNV-1a hash of the String and a Type of StringObj

func (*String) Inspect

func (s *String) Inspect() string

Inspect returns a string representation of the String's Value

func (*String) Type

func (s *String) Type() ObjectType

Type returns our String's ObjectType

Jump to

Keyboard shortcuts

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