object

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2022 License: MIT Imports: 8 Imported by: 3

Documentation

Index

Constants

View Source
const (
	BuiltinFuncNameLen   = "len"
	BuiltinFuncNamePrint = "print"

	// Type conversions
	BuiltinFuncNameInt    = "int"
	BuiltinFuncNameFloat  = "float"
	BuiltinFuncNameString = "string"
	BuiltinFuncNameBytes  = "bytes"
)
View Source
const (
	IntegerObj          = "Integer"
	FloatObj            = "Float"
	BooleanObj          = "Boolean"
	NullObj             = "Null"
	ReturnValueObj      = "ReturnValue"
	ErrorObj            = "Error"
	FunctionObj         = "Function"
	BytesObj            = "Bytes"
	StringObj           = "String"
	BuiltinObj          = "Builtin"
	ArrayObj            = "Array"
	HashObj             = "Hash"
	CompiledFunctionObj = "CompiledFunction"
	ClosureObj          = "Closure"
	ScopeObj            = "Scope"
	ClassObj            = "Class"
)

Variables

View Source
var (
	NullValue = NullObject
	True      = &Boolean{Value: true}
	False     = &Boolean{Value: false}
)
View Source
var Builtins = []struct {
	Name    string
	Builtin *Builtin
}{
	{
		BuiltinFuncNameLen,
		&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 %q not supported, got %s",
					BuiltinFuncNameLen, args[0].Type())
			}
		},
		},
	},
	{
		BuiltinFuncNamePrint,
		&Builtin{Fn: func(args ...Object) Object {
			for _, arg := range args {
				fmt.Print(arg.Inspect())
			}
			fmt.Println()

			return nil
		},
		},
	},
	{
		BuiltinFuncNameInt,
		&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 *Integer:
				return arg
			case *Float:
				return &Integer{Value: int64(arg.Value)}
			case *String:
				val, err := strconv.ParseInt(arg.Value, 10, 64)
				if err != nil {
					return newError("Conversion to int failed!")
				}
				return &Integer{Value: val}
			default:
				return newError("argument to %q not supported, got %s",
					BuiltinFuncNameLen, args[0].Type())
			}
		},
		},
	},
	{
		BuiltinFuncNameFloat,
		&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 *Integer:
				return &Float{Value: float64(arg.Value)}
			case *Float:
				return arg
			case *String:
				val, err := strconv.ParseFloat(arg.Value, 64)
				if err != nil {
					return newError("Conversion to float failed!")
				}
				return &Float{Value: val}
			default:
				return newError("argument to %q not supported, got %s",
					BuiltinFuncNameLen, args[0].Type())
			}
		},
		},
	},
	{
		BuiltinFuncNameString,
		&Builtin{Fn: func(args ...Object) Object {
			if len(args) != 1 {
				return newError("wrong number of arguments. got=%d, want=1",
					len(args))
			}
			return &String{Value: args[0].String()}
		},
		},
	},
	{
		BuiltinFuncNameBytes,
		&Builtin{Fn: func(args ...Object) Object {
			if len(args) != 1 {
				return newError("wrong number of arguments. got=%d, want=1",
					len(args))
			}
			switch val := args[0].(type) {
			case *String:
				return &Bytes{Value: []byte(val.Value)}
			}
			return newError("couldn't convert to bytes!")
		},
		},
	},
}
View Source
var NullObject = &Null{}

Functions

This section is empty.

Types

type Array

type Array struct {
	Elements []Object
}

func (*Array) Equals added in v0.0.2

func (obj *Array) Equals(other Object) bool

func (*Array) GetMember

func (obj *Array) GetMember(name string) Object

func (*Array) InfixOperation

func (obj *Array) InfixOperation(operator string, other Object) Object

func (*Array) Inspect

func (ao *Array) Inspect() string

func (*Array) MarshalJSON added in v0.0.4

func (ao *Array) MarshalJSON() (text []byte, err error)

func (*Array) Native added in v0.0.2

func (obj *Array) Native() interface{}

func (*Array) SetMember added in v0.0.2

func (obj *Array) SetMember(name string, value Object) Object

func (*Array) String

func (ao *Array) String() string

func (*Array) Type

func (ao *Array) Type() ObjectType

type Boolean

type Boolean struct {
	Value bool
}

func NativeBoolToBooleanObject

func NativeBoolToBooleanObject(input bool) *Boolean

func (*Boolean) Equals added in v0.0.2

func (obj *Boolean) Equals(other Object) bool

func (*Boolean) GetMember

func (obj *Boolean) GetMember(name string) Object

func (*Boolean) HashKey

func (b *Boolean) HashKey() HashKey

func (*Boolean) InfixOperation

func (obj *Boolean) InfixOperation(operator string, other Object) Object

func (*Boolean) Inspect

func (b *Boolean) Inspect() string

func (*Boolean) MarshalJSON added in v0.0.4

func (b *Boolean) MarshalJSON() (text []byte, err error)

func (*Boolean) Native added in v0.0.2

func (obj *Boolean) Native() interface{}

func (*Boolean) SetMember added in v0.0.2

func (obj *Boolean) SetMember(name string, value Object) Object

func (*Boolean) String

func (b *Boolean) String() string

func (*Boolean) Type

func (b *Boolean) Type() ObjectType

type Builtin

type Builtin struct {
	Fn BuiltinFunction
}

func (*Builtin) Equals added in v0.0.2

func (obj *Builtin) Equals(other Object) bool

func (*Builtin) GetMember

func (obj *Builtin) GetMember(name string) Object

func (*Builtin) Inspect

func (b *Builtin) Inspect() string

func (*Builtin) SetMember added in v0.0.2

func (obj *Builtin) SetMember(name string, value Object) Object

func (*Builtin) String

func (b *Builtin) String() string

func (*Builtin) Type

func (b *Builtin) Type() ObjectType

type BuiltinFunction

type BuiltinFunction func(args ...Object) Object

type Bytes added in v0.0.2

type Bytes struct {
	Value []byte
}

func (*Bytes) Equals added in v0.0.2

func (obj *Bytes) Equals(other Object) bool

func (*Bytes) GetMember added in v0.0.2

func (obj *Bytes) GetMember(name string) Object

func (*Bytes) InfixOperation added in v0.0.2

func (obj *Bytes) InfixOperation(operator string, other Object) Object

func (*Bytes) Inspect added in v0.0.2

func (s *Bytes) Inspect() string

func (*Bytes) Native added in v0.0.2

func (obj *Bytes) Native() interface{}

func (*Bytes) SetMember added in v0.0.2

func (obj *Bytes) SetMember(name string, value Object) Object

func (*Bytes) String added in v0.0.2

func (s *Bytes) String() string

func (*Bytes) Type added in v0.0.2

func (s *Bytes) Type() ObjectType

type Closure

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

func (*Closure) Equals added in v0.0.2

func (obj *Closure) Equals(other Object) bool

func (*Closure) GetMember

func (obj *Closure) GetMember(name string) Object

func (*Closure) Inspect

func (c *Closure) Inspect() string

func (*Closure) SetMember added in v0.0.2

func (obj *Closure) SetMember(name string, value Object) Object

func (*Closure) String

func (c *Closure) String() string

func (*Closure) Type

func (c *Closure) Type() ObjectType

type CompiledFunction

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

func (*CompiledFunction) Equals added in v0.0.2

func (obj *CompiledFunction) Equals(other Object) bool

func (*CompiledFunction) GetMember

func (obj *CompiledFunction) GetMember(name string) Object

func (*CompiledFunction) Inspect

func (cf *CompiledFunction) Inspect() string

func (*CompiledFunction) SetMember added in v0.0.2

func (obj *CompiledFunction) SetMember(name string, value Object) Object

func (*CompiledFunction) String

func (cf *CompiledFunction) String() string

func (*CompiledFunction) Type

func (cf *CompiledFunction) Type() ObjectType

type Error

type Error struct {
	Message string
}

func NewError added in v0.0.2

func NewError(format string, a ...interface{}) *Error

func (*Error) Equals added in v0.0.2

func (obj *Error) Equals(other Object) bool

func (*Error) GetMember

func (obj *Error) GetMember(name string) Object

func (*Error) Inspect

func (e *Error) Inspect() string

func (*Error) Native added in v0.0.2

func (obj *Error) Native() interface{}

func (*Error) SetMember added in v0.0.2

func (obj *Error) SetMember(name string, value Object) Object

func (*Error) String

func (e *Error) String() string

func (*Error) Type

func (e *Error) Type() ObjectType

type Float

type Float struct {
	Value float64
}

func (*Float) Equals added in v0.0.2

func (obj *Float) Equals(other Object) bool

func (*Float) GetMember

func (obj *Float) GetMember(name string) Object

func (*Float) HashKey

func (i *Float) HashKey() HashKey

func (*Float) InfixOperation

func (obj *Float) InfixOperation(operator string, other Object) Object

func (*Float) Inspect

func (i *Float) Inspect() string

func (*Float) MarshalJSON added in v0.0.4

func (i *Float) MarshalJSON() (text []byte, err error)

func (*Float) Native added in v0.0.2

func (obj *Float) Native() interface{}

func (*Float) SetMember added in v0.0.2

func (obj *Float) SetMember(name string, value Object) Object

func (*Float) String

func (i *Float) String() string

func (*Float) Type

func (i *Float) Type() ObjectType

type Hash

type Hash struct {
	Pairs map[HashKey]HashPair
}

func (*Hash) Equals added in v0.0.2

func (obj *Hash) Equals(other Object) bool

func (*Hash) GetMember

func (obj *Hash) GetMember(name string) Object

func (*Hash) Inspect

func (h *Hash) Inspect() string

func (*Hash) MarshalJSON added in v0.0.4

func (h *Hash) MarshalJSON() (text []byte, err error)

func (*Hash) Native added in v0.0.2

func (obj *Hash) Native() interface{}

func (*Hash) SetMember added in v0.0.2

func (obj *Hash) SetMember(name string, value Object) Object

func (*Hash) String

func (h *Hash) String() string

func (*Hash) Type

func (h *Hash) Type() ObjectType

type HashKey

type HashKey struct {
	Type  ObjectType
	Value string
}

type HashPair

type HashPair struct {
	Key   Object
	Value Object
}

func (HashPair) MarshalJSON added in v0.0.4

func (p HashPair) MarshalJSON() (text []byte, err error)

type Hashable

type Hashable interface {
	HashKey() HashKey
}

type InfixOperatorObject added in v0.0.2

type InfixOperatorObject interface {
	Object
	InfixOperation(operator string, other Object) Object
}

type Integer

type Integer struct {
	Value int64
}

func (*Integer) Equals added in v0.0.2

func (obj *Integer) Equals(other Object) bool

func (*Integer) GetMember

func (obj *Integer) GetMember(name string) Object

func (*Integer) HashKey

func (i *Integer) HashKey() HashKey

func (*Integer) InfixOperation

func (obj *Integer) InfixOperation(operator string, other Object) Object

func (*Integer) Inspect

func (i *Integer) Inspect() string

func (*Integer) MarshalJSON added in v0.0.4

func (i *Integer) MarshalJSON() (text []byte, err error)

func (*Integer) Native added in v0.0.2

func (obj *Integer) Native() interface{}

func (*Integer) SetMember added in v0.0.2

func (obj *Integer) SetMember(name string, value Object) Object

func (*Integer) String

func (i *Integer) String() string

func (*Integer) Type

func (i *Integer) Type() ObjectType

type MemberFn

type MemberFn struct {
	Obj Object
	Fn  MemberFunction
}

func (*MemberFn) Equals added in v0.0.2

func (obj *MemberFn) Equals(other Object) bool

func (*MemberFn) GetMember

func (obj *MemberFn) GetMember(name string) Object

func (*MemberFn) Inspect

func (b *MemberFn) Inspect() string

func (*MemberFn) Native added in v0.0.2

func (obj *MemberFn) Native() interface{}

func (*MemberFn) SetMember added in v0.0.2

func (obj *MemberFn) SetMember(name string, value Object) Object

func (*MemberFn) String

func (b *MemberFn) String() string

func (*MemberFn) Type

func (b *MemberFn) Type() ObjectType

type MemberFunction

type MemberFunction func(this Object, args ...Object) Object

type NativeObject added in v0.0.2

type NativeObject interface {
	Object
	Native() interface{}
}

type Null

type Null struct{}

func (*Null) Equals added in v0.0.2

func (obj *Null) Equals(other Object) bool

func (*Null) GetMember

func (obj *Null) GetMember(name string) Object

func (*Null) InfixOperation

func (obj *Null) InfixOperation(operator string, other Object) Object

func (*Null) Inspect

func (n *Null) Inspect() string

func (*Null) MarshalJSON added in v0.0.4

func (n *Null) MarshalJSON() (text []byte, err error)

func (*Null) Native added in v0.0.2

func (obj *Null) Native() interface{}

func (*Null) SetMember added in v0.0.2

func (obj *Null) SetMember(name string, value Object) Object

func (*Null) String

func (n *Null) String() string

func (*Null) Type

func (n *Null) Type() ObjectType

type Object

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

	GetMember(name string) Object
	SetMember(name string, value Object) Object

	Equals(other Object) bool
}

type ObjectType

type ObjectType string

type ReturnValue

type ReturnValue struct {
	Value Object
}

func (*ReturnValue) Equals added in v0.0.2

func (obj *ReturnValue) Equals(other Object) bool

func (*ReturnValue) GetMember

func (obj *ReturnValue) GetMember(name string) Object

func (*ReturnValue) InfixOperation

func (obj *ReturnValue) InfixOperation(operator string, other Object) Object

func (*ReturnValue) Inspect

func (rv *ReturnValue) Inspect() string

func (*ReturnValue) SetMember added in v0.0.2

func (obj *ReturnValue) SetMember(name string, value Object) Object

func (*ReturnValue) String

func (rv *ReturnValue) String() string

func (*ReturnValue) Type

func (rv *ReturnValue) Type() ObjectType

type Scope

type Scope struct {
	Name         string
	Constants    []Object
	Instructions code.Instructions
	NumLocals    int
	Exports      map[string]Object
}

func (*Scope) Equals added in v0.0.2

func (obj *Scope) Equals(other Object) bool

func (*Scope) GetMember

func (obj *Scope) GetMember(name string) Object

func (*Scope) InfixOperation

func (obj *Scope) InfixOperation(operator string, other Object) Object

func (*Scope) Inspect

func (cf *Scope) Inspect() string

func (*Scope) Native added in v0.0.2

func (obj *Scope) Native() interface{}

func (*Scope) SetMember added in v0.0.2

func (obj *Scope) SetMember(name string, value Object) Object

func (*Scope) String

func (cf *Scope) String() string

func (*Scope) Type

func (cf *Scope) Type() ObjectType

type String

type String struct {
	Value string
}

func (*String) Equals added in v0.0.2

func (obj *String) Equals(other Object) bool

func (*String) GetMember

func (obj *String) GetMember(name string) Object

func (*String) HashKey

func (s *String) HashKey() HashKey

func (*String) InfixOperation

func (obj *String) InfixOperation(operator string, other Object) Object

func (*String) Inspect

func (s *String) Inspect() string

func (*String) MarshalJSON added in v0.0.4

func (s *String) MarshalJSON() (text []byte, err error)

func (*String) Native added in v0.0.2

func (obj *String) Native() interface{}

func (*String) SetMember added in v0.0.2

func (obj *String) SetMember(name string, value Object) Object

func (*String) String

func (s *String) String() 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