object

package
v0.0.0-...-f3c609f Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2019 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	INTEGER_OBJ      = "INTEGER"
	BOOLEAN_OBJ      = "BOOLEAN"
	NULL_OBJ         = "NULL"
	RETURN_VALUE_OBJ = "RETURN_VALUE"
	ERROR_OBJ        = "ERROR"
	FUNCTION_OBJ     = "FUNCTION"
	STRING_OBJ       = "STRING"
	BUILDIN_OBJ      = "BUILTIN"
	ARRAY_OBJ        = "ARRAY"
	HASH_OBJ         = "HASH"
	NAMESPACE_OBJ    = "NAMESPACE"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Array

type Array struct {
	Elements []Object
}

func (*Array) Inspect

func (a *Array) Inspect() string

func (*Array) Type

func (a *Array) Type() ObjectType

type Boolean

type Boolean struct {
	Value bool
}

func (*Boolean) HashKey

func (b *Boolean) HashKey() HashKey

func (*Boolean) Inspect

func (b *Boolean) Inspect() string

func (*Boolean) Type

func (b *Boolean) Type() ObjectType

type Builtin

type Builtin struct {
	Fn BuiltinFunction
}

func (*Builtin) Inspect

func (b *Builtin) Inspect() string

func (*Builtin) Type

func (b *Builtin) Type() ObjectType

type BuiltinFunction

type BuiltinFunction func(env *Environment, args ...Object) Object

NOTE: envを引数に追加 (import, self, outer等は、評価の際今のスコープを知る必要があるため)

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) Inspect

func (e *Environment) Inspect() string

func (*Environment) Outer

func (e *Environment) Outer() *Environment

組み込み関数outer()の評価に必要なためgetterとして追加

func (*Environment) Set

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

type Error

type Error struct {
	Message string
}

NOTE: ErrorはReturnValueのように使う (エラーもreturnも「その先の評価を中断し脱出」という点で同じ)

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
}

EnvをFunctionのフィールドにしたのは、クロージャを実現するため (Envに入るのは関数が作られたときの、この関数のすぐ外側の名前空間。 そのため、関数outer内で関数innerを生成すると、inner内ではouterの束縛は 「outerの外側で呼び出されたときも」参照可能!)

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
}

NOTE: map[HashKey]Objectとしてもハッシュにはなるが、keyを参照しづらくなる イテレートするときやkeyとのペアを取得するには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が偶然同じでも、型が違えばハッシュキーは等しくない
	Value uint64
}

NOTE: HashKey()によるハッシュの要素探索 {"name": "Taro"}["name"] 直接Objectどうしを比較しても、.Valueが同じでも別のポインタであるため参照不可能 &String{Value: "name"} != &String{Value: "name"} 一方、全てのkeyの.Valueを使って照合する場合O(n)かかる… => ハッシュキーによりObjectの同値性を確かめる

type HashPair

type HashPair struct {
	Key   Object
	Value Object
}

type Hashable

type Hashable interface {
	HashKey() HashKey
}

type Integer

type Integer struct {
	Value int64
}

func (*Integer) HashKey

func (i *Integer) HashKey() HashKey

func (*Integer) Inspect

func (i *Integer) Inspect() string

func (*Integer) Type

func (i *Integer) Type() ObjectType

type NameSpace

type NameSpace struct {
	Env *Environment
}

func (*NameSpace) Inspect

func (ns *NameSpace) Inspect() string

func (*NameSpace) Type

func (ns *NameSpace) Type() ObjectType

type Null

type Null struct{}

func (*Null) Inspect

func (n *Null) Inspect() string

func (*Null) Type

func (n *Null) Type() ObjectType

type Object

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

NOTE: 内部表現によってフィールドが違う(boolとint等)のでstructではなくinterface

type ObjectType

type ObjectType string

monkeyでは、評価された値の内部表現は全てObjectで表される

type ReturnValue

type ReturnValue struct {
	Value Object
}

func (*ReturnValue) Inspect

func (rv *ReturnValue) Inspect() string

func (*ReturnValue) Type

func (rv *ReturnValue) Type() ObjectType

type String

type String struct {
	Value string
}

func (*String) HashKey

func (s *String) HashKey() HashKey

func (*String) Inspect

func (s *String) Inspect() string

func (*String) Type

func (s *String) Type() ObjectType

Jump to

Keyboard shortcuts

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