object

package
v0.0.0-...-ec5cb73 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	INTEGER_OBJ           = "INTEGER"
	BOOLEAN_OBJ           = "BOOLEAN"
	STRING_OBJ            = "STRING"
	NULL_OBJ              = "NULL"
	RETURN_VALUE_OBJ      = "RETURN_VALUE"
	ERROR_VALUE_OBJ       = "ERROR_VALUE"
	FUNCTION_OBJ          = "FUNCTION"
	COMPILED_FUNCTION_OBJ = "COMPILED_FUNCTION"
	CLOSURE_OBJ           = "CLOSURE"
	BUILTIN_OBJ           = "BUILTIN"
	ARRAY_OBJ             = "ARRAY"
	VAR_ARGS_OBJ          = "VAR_ARGS"
	MAP_OBJ               = "MAP"
)
View Source
const RED = "\x1b[31m"
View Source
const RESET_COLOR = "\x1b[0m"
View Source
const UNDERLINE = "\x1b[4m"
View Source
const UNDERLINE_RESET = "\x1b[24m"

Variables

View Source
var Builtins = []struct {
	Name    string
	Builtin *Builtin
}{
	{
		Name: "len",
		Builtin: &Builtin{
			Function: func(span token.Span, objects ...Object) Object {
				if len(objects) != 1 {
					return mkError(span, "\"len\" builtin takes a single string or array argument")
				}

				strObj, ok := objects[0].(*String)
				if ok {
					return &Integer{Value: int64(len(strObj.Value))}
				}

				arrObj, ok := objects[0].(*Array)
				if ok {
					return &Integer{Value: int64(len(arrObj.Elems))}
				}

				return mkError(span, "\"len\" builtin takes a single string or array argument")
			},
		},
	},
	{
		Name: "first",
		Builtin: &Builtin{
			Function: func(span token.Span, objects ...Object) Object {
				if len(objects) != 1 {
					return mkError(span, "\"first\" builtin takes a single array argument")
				}

				arrObj, ok := objects[0].(*Array)
				if ok {
					if len(arrObj.Elems) == 0 {
						return mkError(span, "Array is empty")
					}
					return arrObj.Elems[0]
				}

				return mkError(span, "\"first\" builtin takes a single array argument")
			},
		},
	},
	{
		Name: "last",
		Builtin: &Builtin{
			Function: func(span token.Span, objects ...Object) Object {
				if len(objects) != 1 {
					return mkError(span, "\"last\" builtin takes a single array argument")
				}

				arrObj, ok := objects[0].(*Array)
				if ok {
					if len(arrObj.Elems) == 0 {
						return mkError(span, "Array is empty")
					}
					return arrObj.Elems[len(arrObj.Elems)-1]
				}

				return mkError(span, "\"last\" builtin takes a single array argument")
			},
		},
	},
	{
		Name: "rest",
		Builtin: &Builtin{
			Function: func(span token.Span, objects ...Object) Object {
				if len(objects) != 1 {
					return mkError(span, "\"rest\" builtin takes a single array argument")
				}

				arrObj, ok := objects[0].(*Array)
				if ok {
					if len(arrObj.Elems) == 0 {
						return mkError(span, "Array is empty")
					}
					newArr := &Array{Elems: make([]Object, len(arrObj.Elems)-1)}
					copy(newArr.Elems[:], arrObj.Elems[1:])
					return newArr
				}

				return mkError(span, "\"rest\" builtin takes a single array argument")
			},
		},
	},
	{
		Name: "push",
		Builtin: &Builtin{
			Function: func(span token.Span, objects ...Object) Object {
				if len(objects) != 2 {
					return mkError(span, "\"push\" builtin takes an array argument and a new object to push")
				}

				arrObj, ok := objects[0].(*Array)
				if !ok {
					return mkError(span, "\"push\" builtin takes an array argument and a new object to push")
				}

				oldLen := len(arrObj.Elems)
				newArr := &Array{Elems: make([]Object, oldLen+1)}
				copy(newArr.Elems[:oldLen], arrObj.Elems[:])
				newArr.Elems[oldLen] = objects[1]

				return newArr
			},
		},
	},
	{
		Name: "puts",
		Builtin: &Builtin{
			Function: func(span token.Span, objects ...Object) Object {
				for _, object := range objects {
					fmt.Print(object.Inspect())
				}
				fmt.Println()
				return nil
			},
		},
	},
	{
		Name: "toArray",
		Builtin: &Builtin{
			Function: func(span token.Span, objects ...Object) Object {
				if len(objects) != 1 {
					return mkError(span, "\"toArray\" builtin takes a VarArg argument")
				}

				varArgObj, ok := objects[0].(*VarArgs)
				if !ok {
					return mkError(span, "\"toArray\" builtin takes a VarArg argument")
				}

				return &Array{Elems: varArgObj.Elems}
			},
		},
	},
	{
		Name: "contains",
		Builtin: &Builtin{
			Function: func(span token.Span, objects ...Object) Object {
				if len(objects) != 2 {
					return mkError(span, "\"contains\" builtin takes a HashMap argument and a key")
				}

				hashMapObj, ok := objects[0].(*HashMap)
				if !ok {
					return mkError(span, "First argument is not a hash map")
				}

				keyObj, ok := objects[1].(Hashable)
				if !ok {
					return mkError(span, "Second argument is not a hashable object")
				}

				elem, ok := hashMapObj.Elems[keyObj.HashKey()]
				if ok {
					if elem.Key.Inspect() == keyObj.Inspect() {
						return &Boolean{Value: true}
					}
				}

				return &Boolean{Value: false}
			},
		},
	},
}

Functions

This section is empty.

Types

type Array

type Array struct {
	Elems []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 (i *Boolean) Inspect() string

func (*Boolean) Type

func (i *Boolean) Type() ObjectType

type Builtin

type Builtin struct {
	Function BuiltinFunction
}

func GetBuiltinByName

func GetBuiltinByName(name string) *Builtin

func (*Builtin) Inspect

func (f *Builtin) Inspect() string

func (*Builtin) Type

func (f *Builtin) Type() ObjectType

type BuiltinFunction

type BuiltinFunction func(span token.Span, objects ...Object) Object

type Closure

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

func (*Closure) Inspect

func (f *Closure) Inspect() string

func (*Closure) Type

func (f *Closure) Type() ObjectType

type CompiledFunction

type CompiledFunction struct {
	Instructions code.Instructions
	NumLocals    int
	NumArgs      int
	VarArgs      bool
}

func (*CompiledFunction) Inspect

func (f *CompiledFunction) Inspect() string

func (*CompiledFunction) Type

func (f *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) Copy

func (e *Environment) Copy() *Environment

func (*Environment) Get

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

func (*Environment) GetVarArgs

func (e *Environment) GetVarArgs() ([]Object, bool)

func (*Environment) Set

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

func (*Environment) SetVarArgs

func (e *Environment) SetVarArgs(varArgs []Object)

type Error

type Error struct {
	Message string
	Span    token.Span
}

func (*Error) ContextualError

func (e *Error) ContextualError() string

func (*Error) Inspect

func (e *Error) Inspect() string

func (*Error) Type

func (e *Error) Type() ObjectType

type Function

type Function struct {
	Args    []*ast.IdentifierExpr
	VarArgs bool
	Body    *ast.BlockStatement
	Env     *Environment
}

func (*Function) Inspect

func (f *Function) Inspect() string

func (*Function) Type

func (f *Function) Type() ObjectType

type HashEntry

type HashEntry struct {
	Key   Object
	Value Object
}

type HashKey

type HashKey struct {
	Type string
	Hash uint64
}

type HashMap

type HashMap struct {
	Elems map[HashKey]HashEntry
}

func (*HashMap) Inspect

func (a *HashMap) Inspect() string

func (*HashMap) Type

func (a *HashMap) Type() ObjectType

type Hashable

type Hashable interface {
	Object
	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 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
}

type ObjectType

type ObjectType string

type Return

type Return struct {
	Value Object
}

func (*Return) Inspect

func (r *Return) Inspect() string

func (*Return) Type

func (r *Return) 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

type VarArgs

type VarArgs struct {
	Elems []Object
}

func (*VarArgs) Inspect

func (a *VarArgs) Inspect() string

func (*VarArgs) Type

func (a *VarArgs) Type() ObjectType

Jump to

Keyboard shortcuts

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