object

package
v0.0.0-...-44b95cc Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2019 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

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

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

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

	// 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
	ERROR_OBJ = "ERROR"

	// FUNCTION_OBJ is the function object type
	FUNCTION_OBJ = "FUNCTION"

	// BUILTIN_OBJ is the builtin function object type
	BUILTIN_OBJ = "BUILTIN"

	// ARRAY_OBJ is the list object type (keeping it named array for continuity with book)
	ARRAY_OBJ = "ARRAY"

	// HASH_OBJ is the hash object type
	HASH_OBJ = "HASH"

	// QUOTE_OBJ is the quote object type
	QUOTE_OBJ = "QUOTE"

	// MACRO_OBJ is the macro literal object type
	MACRO_OBJ = "MACRO"

	// COMPILED_FUNCTION_OBJ is the compiler funciton object type
	COMPILED_FUNCTION_OBJ = "COMPILED_FUNCTION_OBJ"

	// CLOSURE_OBJ is the closure object type
	CLOSURE_OBJ = "CLOSURE_OBJ"
)

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, want=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())
			}
		},
		},
	},
	{
		"puts",
		&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, want=1",
					len(args))
			}
			if args[0].Type() != ARRAY_OBJ {
				return newError("argument to `first` must be ARRAY, got %s",
					args[0].Type())
			}
			arr := args[0].(*Array)
			if len(arr.Elements) > 0 {
				return arr.Elements[0]
			}
			return nil
		},
		},
	},
	{
		"last",
		&Builtin{Fn: func(args ...Object) Object {
			if len(args) != 1 {
				return newError("wrong number of arguments. got=%d, want=1",
					len(args))
			}
			if args[0].Type() != ARRAY_OBJ {
				return newError("argument to `last` must be ARRAY, got %s",
					args[0].Type())
			}
			arr := args[0].(*Array)
			length := len(arr.Elements)
			if length > 0 {
				return arr.Elements[length-1]
			}
			return nil
		},
		},
	},
	{
		"rest",
		&Builtin{Fn: func(args ...Object) Object {
			if len(args) != 1 {
				return newError("wrong number of arguments. got=%d, want=1",
					len(args))
			}
			if args[0].Type() != ARRAY_OBJ {
				return newError("argument to `rest` must be ARRAY, got %s",
					args[0].Type())
			}
			arr := args[0].(*Array)
			length := len(arr.Elements)
			if length > 0 {
				newElements := make([]Object, length-1, length-1)
				copy(newElements, arr.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, want=2",
					len(args))
			}
			if args[0].Type() != ARRAY_OBJ {
				return newError("argument to `push` must be ARRAY, got %s",
					args[0].Type())
			}
			arr := args[0].(*Array)
			length := len(arr.Elements)
			newElements := make([]Object, length+1, length+1)
			copy(newElements, arr.Elements)
			newElements[length] = args[1]
			return &Array{Elements: newElements}
		},
		},
	},
}

Functions

This section is empty.

Types

type Array

type Array struct {
	Elements []Object
}

func (*Array) Inspect

func (ao *Array) Inspect() string

func (*Array) Type

func (ao *Array) Type() ObjectType

type Boolean

type Boolean struct {
	Value bool
}

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

func (*Boolean) HashKey

func (b *Boolean) HashKey() HashKey

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
}

func GetBuiltinByName

func GetBuiltinByName(name string) *Builtin

func (*Builtin) Inspect

func (b *Builtin) Inspect() string

func (*Builtin) Type

func (b *Builtin) Type() ObjectType

type BuiltinFunction

type BuiltinFunction func(args ...Object) Object

type Closure

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

func (*Closure) Inspect

func (c *Closure) Inspect() string

func (*Closure) Type

func (c *Closure) Type() ObjectType

type CompiledFunction

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

func (*CompiledFunction) Inspect

func (cf *CompiledFunction) Inspect() string

func (*CompiledFunction) Type

func (cf *CompiledFunction) Type() ObjectType

type Environment

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

func NewEnclosedEnvironment

func NewEnclosedEnvironment(outer *Environment) *Environment

func NewEnvironment

func NewEnvironment() *Environment

func (*Environment) Get

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

func (*Environment) Set

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

type Error

type Error struct {
	Message string
}

Error is the base struct of our Error type

func (*Error) Inspect

func (e *Error) Inspect() string

func (*Error) Type

func (e *Error) Type() ObjectType

type Function

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

func (*Function) Inspect

func (f *Function) Inspect() string

func (*Function) Type

func (f *Function) Type() ObjectType

type Hash

type Hash struct {
	Pairs map[HashKey]HashPair
}

func (*Hash) Inspect

func (h *Hash) Inspect() string

func (*Hash) Type

func (h *Hash) Type() ObjectType

type HashKey

type HashKey struct {
	Type  ObjectType
	Value uint64
}

type HashPair

type HashPair struct {
	Key   Object
	Value Object
}

type Hashable

type Hashable interface {
	HashKey() HashKey
}

type Integer

type Integer struct {
	Value int64
}

Integer is the integer type used to represent integer literals and holds an internal int64 value

func (*Integer) HashKey

func (i *Integer) HashKey() HashKey

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 Macro

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

func (*Macro) Inspect

func (m *Macro) Inspect() string

func (*Macro) Type

func (m *Macro) Type() ObjectType

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

type ObjectType

type ObjectType string

ObjectType represents the type of an object

type Quote

type Quote struct {
	Node ast.Node
}

func (*Quote) Inspect

func (q *Quote) Inspect() string

func (*Quote) Type

func (q *Quote) Type() ObjectType

type ReturnValue

type ReturnValue struct {
	Value Object
}

ReturnValue is the return type and used to hold the value of another object. This is used for `return` statements and this object is tracked through the evalulator 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

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