object

package
v0.23.0 Latest Latest
Warning

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

Go to latest
Published: May 15, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrWrongNumberArguments    = errors.New("wrong number of argument(s)")
	ErrUnsupportedArgumentType = errors.New("unsupported argument type")
)
View Source
var (
	INTEGER_OBJ      = ObjectType("INTEGER")
	BOOLEAN_OBJ      = ObjectType("BOOLEAN")
	STRING_OBJ       = ObjectType("STRING")
	ARRAY_OBJ        = ObjectType("ARRAY")
	HASH_OBJ         = ObjectType("HASH")
	NIL_OBJ          = ObjectType("NIL")
	RETURN_VALUE_OBJ = ObjectType("RETURN_VALUE")
	ERROR_OBJ        = ObjectType("ERROR")
	FUNCTION_OBJ     = ObjectType("FUNCTION")
	BUILTINFUNC_OBJ  = ObjectType("BUILTINFUNC")
)
View Source
var (
	TRUE  = NewBoolean(true)
	FALSE = NewBoolean(false)
	NIL   = NewNil()
)

boolean literal objects

View Source
var BuiltinFuncs = map[string]BuiltinFunc{
	"len": func(args ...Object) (Object, error) {
		if len(args) != 1 {
			return NIL, ErrWrongNumberArguments
		}

		switch arg := args[0].(type) {
		case *String:
			return NewInteger(int64(len(arg.Value))), nil
		case *Array:
			return NewInteger(int64(len(arg.Elements))), nil
		default:
			return NIL, ErrUnsupportedArgumentType
		}
	},
	"print": func(args ...Object) (Object, error) {
		strToPrint := []string{}
		for _, arg := range args {
			strToPrint = append(strToPrint, arg.Inspect())
		}

		fmt.Println(strings.Join(strToPrint, " "))

		return NIL, nil
	},
}

all built-in functions

Functions

This section is empty.

Types

type Array added in v0.15.0

type Array struct {
	Elements []Object
}

the array object

func NewArray added in v0.15.0

func NewArray(elements ...Object) *Array

func (*Array) Inspect added in v0.15.0

func (a *Array) Inspect() string

func (*Array) IsTruthy added in v0.15.0

func (a *Array) IsTruthy() bool

func (*Array) Type added in v0.15.0

func (a *Array) Type() ObjectType

type Boolean

type Boolean struct {
	Value bool
}

the boolean object

func NewBoolean

func NewBoolean(value bool) *Boolean

func (*Boolean) HashKey added in v0.16.0

func (b *Boolean) HashKey() HashKey

func (*Boolean) Inspect

func (b *Boolean) Inspect() string

func (*Boolean) IsTruthy

func (b *Boolean) IsTruthy() bool

func (*Boolean) Type

func (b *Boolean) Type() ObjectType

type BuiltinFunc added in v0.13.0

type BuiltinFunc func(args ...Object) (Object, error)

BuiltinFunc represents a builtin function object

func (BuiltinFunc) Inspect added in v0.13.0

func (b BuiltinFunc) Inspect() string

func (BuiltinFunc) IsTruthy added in v0.13.0

func (b BuiltinFunc) IsTruthy() bool

FIXME: this behavior is undined, not sure whether a builtin function is truthy or not

func (BuiltinFunc) Type added in v0.13.0

func (b BuiltinFunc) Type() ObjectType

type Environment

type Environment interface {
	Keys() []string
	Get(name string) (Object, bool)
	Set(name string, val Object)
	// contains filtered or unexported methods
}

func NewClosureEnvironment

func NewClosureEnvironment(env Environment) Environment

func NewEnvironment

func NewEnvironment() Environment

type Error

type Error struct {
	Message string
}

Error represents an error Note: this is not necessary for the interpreter to work, we do error handling in Go's native way

func NewError

func NewError(msg string) *Error

func (*Error) Inspect

func (e *Error) Inspect() string

func (*Error) IsTruthy

func (e *Error) IsTruthy() bool

FIXME: this behavior is undined, not sure an error is truthy or not

func (*Error) Type

func (e *Error) Type() ObjectType

type Func

type Func struct {
	Parameters []*ast.IdentifierExpression
	Body       *ast.BlockStatement
}

Func represents a function object

func NewFunc

func NewFunc(params []*ast.IdentifierExpression, body *ast.BlockStatement, env Environment) *Func

func (*Func) Inspect

func (f *Func) Inspect() string

func (*Func) IsTruthy

func (f *Func) IsTruthy() bool

FIXME: this behavior is undined, not sure whether a function is truthy or not

func (*Func) Type

func (f *Func) Type() ObjectType

type Hash added in v0.16.0

type Hash struct {
	Items map[HashKey]Object
}

the hash object

func NewHash added in v0.16.0

func NewHash(items map[HashKey]Object) *Hash

func (*Hash) Inspect added in v0.16.0

func (h *Hash) Inspect() string

func (*Hash) IsTruthy added in v0.16.0

func (h *Hash) IsTruthy() bool

func (*Hash) Type added in v0.16.0

func (h *Hash) Type() ObjectType

type HashKey added in v0.16.0

type HashKey struct {
	Type          ObjectType
	ObjectLiteral string
	Value         uint64
}

type Hashable added in v0.16.0

type Hashable interface {
	HashKey() HashKey
}

type Integer

type Integer struct {
	Value int64
}

Integer

func NewInteger

func NewInteger(value int64) *Integer

func (*Integer) HashKey added in v0.16.0

func (i *Integer) HashKey() HashKey

func (*Integer) Inspect

func (i *Integer) Inspect() string

func (*Integer) IsTruthy

func (i *Integer) IsTruthy() bool

func (*Integer) Type

func (i *Integer) Type() ObjectType

type Nil

type Nil struct{}

Nil represents the absence of any value

func NewNil

func NewNil() *Nil

func (*Nil) Inspect

func (n *Nil) Inspect() string

func (*Nil) IsTruthy

func (n *Nil) IsTruthy() bool

func (*Nil) Type

func (n *Nil) Type() ObjectType

type Object

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

type ObjectType

type ObjectType string

type ReturnValue

type ReturnValue struct {
	Value Object
}

ReturnValue represents a return value of a function

func NewReturnValue

func NewReturnValue(value Object) *ReturnValue

func (*ReturnValue) Inspect

func (rv *ReturnValue) Inspect() string

func (*ReturnValue) IsTruthy

func (rv *ReturnValue) IsTruthy() bool

func (*ReturnValue) Type

func (rv *ReturnValue) Type() ObjectType

type String

type String struct {
	Value string
}

the string object

func NewString

func NewString(value string) *String

func (*String) HashKey added in v0.16.0

func (s *String) HashKey() HashKey

func (*String) Inspect

func (s *String) Inspect() string

func (*String) IsTruthy

func (s *String) IsTruthy() bool

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