object

package
v0.0.0-...-65d0a20 Latest Latest
Warning

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

Go to latest
Published: May 3, 2019 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// IntegerType represents a type of integers.
	IntegerType Type = "Integer"
	// FloatType represents a type of floating point numbers.
	FloatType = "Float"
	// BooleanType represents a type of booleans.
	BooleanType = "Boolean"
	// NilType represents a type of nil.
	NilType = "Nil"
	// ReturnValueType represents a type of return values.
	ReturnValueType = "ReturnValue"
	// ErrorType represents a type of errors.
	ErrorType = "Error"
	// FunctionType represents a type of functions.
	FunctionType = "Function"
	// StringType represents a type of strings.
	StringType = "String"
	// BuiltinType represents a type of builtin functions.
	BuiltinType = "Builtin"
	// ArrayType represents a type of arrays.
	ArrayType = "Array"
	// HashType represents a type of hashes.
	HashType = "Hash"
	// QuoteType represents a type of quotes used for macros.
	QuoteType = "Quote"
	// MacroType represents a type of macros.
	MacroType = "Macro"
	// CompiledFunctionType represents a type of compiled functions.
	CompiledFunctionType = "CompiledFunction"
	// ClosureType represents a type of closures.
	ClosureType = "Closure"
)

Variables

View Source
var Builtins = []struct {
	Name    string
	Builtin *Builtin
}{
	{
		Name: "len",
		Builtin: &Builtin{
			Fn: func(args ...Object) Object {
				if l := len(args); l != 1 {
					return newError("wrong number of arguments. want=1, got=%d", l)
				}

				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", arg.Type())
				}
			},
		},
	},
	{
		Name: "puts",
		Builtin: &Builtin{
			Fn: func(args ...Object) Object {
				for _, arg := range args {
					fmt.Println(arg.Inspect())
				}
				return nil
			},
		},
	},
	{
		Name: "first",
		Builtin: &Builtin{
			Fn: func(args ...Object) Object {
				if l := len(args); l != 1 {
					return newError("wrong number of arguments. want=1, got=%d", l)
				}

				if typ := args[0].Type(); typ != ArrayType {
					return newError("argument to `first` must be Array, got %s", typ)
				}

				arr := args[0].(*Array)
				if len(arr.Elements) > 0 {
					return arr.Elements[0]
				}
				return nil
			},
		},
	},
	{
		Name: "last",
		Builtin: &Builtin{
			Fn: func(args ...Object) Object {
				if l := len(args); l != 1 {
					return newError("wrong number of arguments. want=1, got=%d", l)
				}

				if typ := args[0].Type(); typ != ArrayType {
					return newError("argument to `last` must be Array, got %s", typ)
				}

				arr := args[0].(*Array)
				if l := len(arr.Elements); l > 0 {
					return arr.Elements[l-1]
				}
				return nil
			},
		},
	},
	{
		Name: "rest",
		Builtin: &Builtin{
			Fn: func(args ...Object) Object {
				if l := len(args); l != 1 {
					return newError("wrong number of arguments. want=1, got=%d", l)
				}

				if typ := args[0].Type(); typ != ArrayType {
					return newError("argument to `last` must be Array, got %s", typ)
				}

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

				newElems := make([]Object, l-1)
				copy(newElems, arr.Elements[1:l])
				return &Array{Elements: newElems}
			},
		},
	},
	{
		Name: "push",
		Builtin: &Builtin{
			Fn: func(args ...Object) Object {
				if l := len(args); l != 2 {
					return newError("wrong number of arguments. want=%d, got=%d", 2, l)
				}

				if typ := args[0].Type(); typ != ArrayType {
					return newError("first argument to `push` must be Array, got %s", typ)
				}

				arr := args[0].(*Array)
				l := len(arr.Elements)

				newElems := make([]Object, l+1)
				copy(newElems, arr.Elements)
				newElems[l] = args[1]
				return &Array{Elements: newElems}
			},
		},
	},
}

Builtins is a list of built-in functions.

Functions

This section is empty.

Types

type Array

type Array struct {
	Elements []Object
}

Array represents an array.

func (*Array) Inspect

func (a *Array) Inspect() string

Inspect returns a string representation of the Array.

func (*Array) Type

func (*Array) Type() Type

Type returns the type of the Array.

type Boolean

type Boolean struct {
	Value bool
}

Boolean represents a boolean.

func (*Boolean) HashKey

func (b *Boolean) HashKey() HashKey

HashKey returns a hash key object for b.

func (*Boolean) Inspect

func (b *Boolean) Inspect() string

Inspect returns a string representation of the Boolean.

func (*Boolean) Type

func (b *Boolean) Type() Type

Type returns the type of the Boolean.

type Builtin

type Builtin struct {
	Fn BuiltinFunction
}

Builtin represents a builtin function.

func GetBuiltinByName

func GetBuiltinByName(name string) *Builtin

GetBuiltinByName returns a built-in function matching a given name. If no function is found with the name, it returns nil.

func (*Builtin) Inspect

func (b *Builtin) Inspect() string

Inspect returns a string representation of the Builtin.

func (*Builtin) Type

func (b *Builtin) Type() Type

Type returns the type of the Builtin.

type BuiltinFunction

type BuiltinFunction func(args ...Object) Object

BuiltinFunction represents a function signature of builtin functions.

type Closure

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

Closure represents a closure. It has a pointer to the function it wraps, `Fn`, and a place to keep the free variables it carries around, `Free`.

func (*Closure) Inspect

func (c *Closure) Inspect() string

Inspect returns a string representation of `c`.

func (*Closure) Type

func (c *Closure) Type() Type

Type returns the type of `c`.

type CompiledFunction

type CompiledFunction struct {
	Instructions code.Instructions
	// NumLocals is used for reserving slots to store local bindings on the stack
	NumLocals     int
	NumParameters int
}

CompiledFunction represents a function compiled to bytecode instructions.

func (*CompiledFunction) Inspect

func (cf *CompiledFunction) Inspect() string

Inspect returns a string representation of `cf`.

func (*CompiledFunction) Type

func (cf *CompiledFunction) Type() Type

Type returns the type of `cf`.

type Environment

type Environment interface {
	// Get retrieves the value of a variable named by the `name`.
	// If the variable is present in the environment the value is returned and the boolean is true.
	// Otherwise the returned value will be nil and the boolean will be false.
	Get(name string) (Object, bool)

	// Set sets the `val` of a variable named by the `name` and returns the `val` itself.
	Set(name string, val Object) Object
}

Environment associates values with variable names.

func NewEnclosedEnvironment

func NewEnclosedEnvironment(outer Environment) Environment

NewEnclosedEnvironment creates a new Environment which holds the given outer Environment.

func NewEnvironment

func NewEnvironment() Environment

NewEnvironment returns a new Environment.

type Error

type Error struct {
	Message string
}

Error represents an error.

func (*Error) Inspect

func (e *Error) Inspect() string

Inspect returns a string representation of the Error.

func (*Error) Type

func (e *Error) Type() Type

Type returns the type of the Error.

type Float

type Float struct {
	Value float64
}

Float represents an integer.

func (*Float) HashKey

func (f *Float) HashKey() HashKey

HashKey returns a hash key object for f.

func (*Float) Inspect

func (f *Float) Inspect() string

Inspect returns a string representation of f.

func (*Float) Type

func (f *Float) Type() Type

Type returns the type of f.

type Function

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

Function represents a function.

func (*Function) Inspect

func (f *Function) Inspect() string

Inspect returns a string representation of the Function.

func (*Function) Type

func (f *Function) Type() Type

Type returns the type of the Function.

type Hash

type Hash struct {
	Pairs map[HashKey]HashPair
}

Hash represents a hash.

func (*Hash) Inspect

func (h *Hash) Inspect() string

Inspect returns a string representation of the Hash.

func (*Hash) Type

func (*Hash) Type() Type

Type returns the type of the Hash.

type HashKey

type HashKey struct {
	Type  Type
	Value uint64
}

HashKey represents a key of a hash.

type HashPair

type HashPair struct {
	Key   Object
	Value Object
}

HashPair represents a key-value pair in a hash.

type Hashable

type Hashable interface {
	HashKey() HashKey
}

Hashable is the interface that is able to become a hash key.

type Integer

type Integer struct {
	Value int64
}

Integer represents an integer.

func (*Integer) HashKey

func (i *Integer) HashKey() HashKey

HashKey returns a hash key object for i.

func (*Integer) Inspect

func (i *Integer) Inspect() string

Inspect returns a string representation of the Integer.

func (*Integer) Type

func (i *Integer) Type() Type

Type returns the type of the Integer.

type Macro

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

Macro represents a macro.

func (*Macro) Inspect

func (m *Macro) Inspect() string

Inspect returns a string representation of `m`.

func (*Macro) Type

func (m *Macro) Type() Type

Type returns the type of `m`.

type Nil

type Nil struct{}

Nil represents the absence of any value.

func (*Nil) HashKey

func (n *Nil) HashKey() HashKey

HashKey returns a hash key object for n.

func (*Nil) Inspect

func (n *Nil) Inspect() string

Inspect returns a string representation of the Nil.

func (*Nil) Type

func (n *Nil) Type() Type

Type returns the type of the Nil.

type Object

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

Object represents an object of Monkey language.

type Quote

type Quote struct {
	ast.Node
}

Quote represents a quote, i.e. an unevaluated expression.

func (*Quote) Inspect

func (q *Quote) Inspect() string

Inspect returns a string representation of `q`.

func (*Quote) Type

func (q *Quote) Type() Type

Type returns the type of `q`.

type ReturnValue

type ReturnValue struct {
	Value Object
}

ReturnValue represents a return value.

func (*ReturnValue) Inspect

func (rv *ReturnValue) Inspect() string

Inspect returns a string representation of the ReturnValue.

func (*ReturnValue) Type

func (rv *ReturnValue) Type() Type

Type returns the type of the ReturnValue.

type String

type String struct {
	Value string
}

String represents a string.

func (*String) HashKey

func (s *String) HashKey() HashKey

HashKey returns a hash key object for s.

func (*String) Inspect

func (s *String) Inspect() string

Inspect returns a string representation of the String.

func (*String) Type

func (s *String) Type() Type

Type returns the type of the String.

type Type

type Type string

Type is a type of objects.

Jump to

Keyboard shortcuts

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