object

package
v0.77.0 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2024 License: Apache-2.0 Imports: 18 Imported by: 1

Documentation

Index

Constants

View Source
const MaxSmallArray = 8
View Source
const MaxSmallMap = 4 // so 2x, so same as MaxSmallArray really.
View Source
const NumRegisters = 8
View Source
const ObjectSize = 2 * bits.UintSize / 8 // also unsafe.Sizeof(interface) == 16 bytes (2 pointers == 2 ints)

Size of the Object interface in bytes.

Variables

View Source
var (
	NULL  = Null{}
	TRUE  = Boolean{Value: true}
	FALSE = Boolean{Value: false}
)
View Source
var (
	KeyKey   = String{Value: "key"}
	ValueKey = String{Value: "value"}
)
View Source
var EmptyArray = SmallArray{}

Functions

func AddIdentifier added in v0.34.0

func AddIdentifier(name string, value Object)

Add values to top level environment, e.g "pi" -> 3.14159... or "printf(){print(sprintf(%s, args...))}".

func Cmp added in v0.43.0

func Cmp(ei, ej Object) int

func CompareKeys added in v0.43.0

func CompareKeys(a, b keyValuePair) int

func Constant added in v0.34.0

func Constant(name string) bool

Defines constant as all CAPS (with _ ok in the middle) identifiers. Note that we use []byte as all identifiers are ASCII.

func CreateFunction added in v0.34.0

func CreateFunction(cmd Extension) error

CreateFunction adds a new function to the table of extended functions.

func Equals

func Equals(left, right Object) bool

func FreeMemory added in v0.51.0

func FreeMemory() int64

Returns the amount of free memory in bytes.

func Hashable added in v0.31.0

func Hashable(o Object) bool

Hashable in tem of Go map for cache key.

func Init added in v0.34.0

func Init()

Init resets the table of extended functions to empty. Optional, will be called on demand the first time through CreateFunction.

func IsExtraFunction added in v0.62.0

func IsExtraFunction(name string) bool

func IsIntType added in v0.74.0

func IsIntType(t Type) bool

registers are equivalent to integers.

func Len added in v0.52.0

func Len(a Object) int

func MustBeOk added in v0.51.0

func MustBeOk(n int)

func SetCacheKey added in v0.76.0

func SetCacheKey(f *Function) string

Must be called after the function is fully initialized. Whether a function result should be cached doesn't depend on the Name, so it's not part of the cache key.

func SizeOk added in v0.51.0

func SizeOk(n int) (bool, int64)

func TypeEqual added in v0.74.0

func TypeEqual(a, b Type) bool

registers are considered integer for the purpose of comparison.

func Unwrap added in v0.34.0

func Unwrap(objs []Object, forceStringKeys bool) []any

func UnwrapHashable added in v0.56.0

func UnwrapHashable(o Object) any

func UnwrapStringKeys added in v0.56.0

func UnwrapStringKeys(m Map, forceStringKeys bool) (map[string]any, bool)

func ValidIdentifier added in v0.67.0

func ValidIdentifier(name string) bool

func WriteStrings

func WriteStrings(out *strings.Builder, list []Object, before, sep, after string)

Types

type Array

type Array interface {
	Object
	Len() int
	First() Object
	Rest() Object
	Elements() []Object
}

type BigArray added in v0.52.0

type BigArray struct {
	// contains filtered or unexported fields
}

func (BigArray) Elements added in v0.52.0

func (ao BigArray) Elements() []Object

func (BigArray) First added in v0.52.0

func (ao BigArray) First() Object

func (BigArray) Inspect added in v0.52.0

func (ao BigArray) Inspect() string

func (BigArray) JSON added in v0.52.0

func (ao BigArray) JSON(w io.Writer) error

func (BigArray) Len added in v0.52.0

func (ao BigArray) Len() int

func (BigArray) Less added in v0.52.0

func (ao BigArray) Less(i, j int) bool

func (BigArray) Rest added in v0.52.0

func (ao BigArray) Rest() Object

func (BigArray) Swap added in v0.52.0

func (ao BigArray) Swap(i, j int)

func (BigArray) Type added in v0.52.0

func (ao BigArray) Type() Type

func (BigArray) Unwrap added in v0.52.0

func (ao BigArray) Unwrap(forceStringKeys bool) any

type BigMap added in v0.52.0

type BigMap struct {
	// contains filtered or unexported fields
}

Sorted KV pairs, O(n) insert O(log n) access/same key mutations.

func (*BigMap) Append added in v0.52.0

func (m *BigMap) Append(right Map) Map

Creates a new Map appending the right map to the left map.

func (*BigMap) First added in v0.52.0

func (m *BigMap) First() Object

func (*BigMap) Get added in v0.52.0

func (m *BigMap) Get(key Object) (Object, bool)

func (*BigMap) Inspect added in v0.52.0

func (m *BigMap) Inspect() string

func (*BigMap) JSON added in v0.52.0

func (m *BigMap) JSON(w io.Writer) error

func (*BigMap) Len added in v0.52.0

func (m *BigMap) Len() int

func (*BigMap) Range added in v0.54.0

func (m *BigMap) Range(l, r int64) Object

func (*BigMap) Rest added in v0.52.0

func (m *BigMap) Rest() Object

func (*BigMap) Set added in v0.52.0

func (m *BigMap) Set(key, value Object) Map

func (*BigMap) Type added in v0.52.0

func (m *BigMap) Type() Type

func (*BigMap) Unwrap added in v0.52.0

func (m *BigMap) Unwrap(forceStringKeys bool) any

type Boolean

type Boolean struct {
	Value bool
}

func NativeBoolToBooleanObject

func NativeBoolToBooleanObject(input bool) Boolean

func (Boolean) Inspect

func (b Boolean) Inspect() string

func (Boolean) JSON added in v0.36.0

func (b Boolean) JSON(w io.Writer) error

func (Boolean) Type

func (b Boolean) Type() Type

func (Boolean) Unwrap added in v0.34.0

func (b Boolean) Unwrap(_ bool) any

type Environment

type Environment struct {
	// contains filtered or unexported fields
}

func NewEnclosedEnvironment

func NewEnclosedEnvironment(outer *Environment) *Environment

func NewFunctionEnvironment added in v0.34.0

func NewFunctionEnvironment(fn Function, current *Environment) (*Environment, bool)

Create a new environment either based on original function definitions' environment or the current one if the function is the same, that allows a function to set some values visible through recursion to itself.

func test(n) {if (n==2) {x=1}; if (n==1) {return x}; test(n-1)}; test(3)

will return 1 (and not "identifier not found: x"). Returns true if the function is the same as the current one and we should probably set the function's name in that environment to avoid deep search for it.

func NewMacroEnvironment added in v0.34.0

func NewMacroEnvironment() *Environment

Truly empty store suitable for macros storage.

func NewRootEnvironment added in v0.34.0

func NewRootEnvironment() *Environment

NewRootEnvironment contains the identifiers pre-seeded by extensions.

func (*Environment) BaseInfo added in v0.35.0

func (e *Environment) BaseInfo() *BigMap

func (*Environment) CreateOrSet added in v0.65.0

func (e *Environment) CreateOrSet(name string, val Object, create bool) Object

func (*Environment) Get

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

func (*Environment) GetMisses added in v0.51.2

func (e *Environment) GetMisses() int64

GetMisses returns the cumulative number of get misses (a function tried to access up stack, so can't be cached).

func (*Environment) HasRegisters added in v0.74.0

func (e *Environment) HasRegisters() bool

func (*Environment) Info added in v0.35.0

func (e *Environment) Info() Object

func (*Environment) IsRef added in v0.65.0

func (e *Environment) IsRef(name string) (*Environment, string)

func (*Environment) Len

func (e *Environment) Len() int

func (*Environment) MakeRegister added in v0.74.0

func (e *Environment) MakeRegister(originalName string, v int64) Register

func (*Environment) Name added in v0.59.0

func (e *Environment) Name() string

Frame/stack name.

func (*Environment) NumSet added in v0.41.1

func (e *Environment) NumSet() int64

NumSet returns the cumulative number of set operations done in the toplevel environment so far. It can be used to avoid calling SaveGlobals if nothing has changed since the last time.

func (*Environment) RegisterTrie added in v0.39.0

func (e *Environment) RegisterTrie(t *trie.Trie)

Records the current toplevel ids and functions as well as sets up the callback to for future ids additions.

func (*Environment) ReleaseRegister added in v0.74.0

func (e *Environment) ReleaseRegister(register Register)

func (*Environment) SaveGlobals added in v0.40.0

func (e *Environment) SaveGlobals(to io.Writer, maxValueLen int) (int, error)

Returns the number of ids written. maxValueLen <= 0 means no limit.

func (*Environment) Set

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

func (*Environment) SetNoChecks added in v0.34.0

func (e *Environment) SetNoChecks(name string, val Object, create bool) Object

create force the creation of a new entry, even if had a previous value or ref. (eg. function parameters are always new).

func (*Environment) StackParent added in v0.59.0

func (e *Environment) StackParent() *Environment

Allows eval and others to walk up the stack of envs themselves (using Name() to produce a stack trace for instance).

func (*Environment) TriggerNoCache added in v0.62.0

func (e *Environment) TriggerNoCache()

TriggerNoCache is used prevent this call stack from caching. Meant to be used by extensions that for instance return random numbers or change state.

type Error

type Error struct {
	Value string // message
	Stack []string
}

func Errorf added in v0.66.0

func Errorf(format string, args ...interface{}) Error

Use eval's Errorf() instead whenever possible, to get the stack. This one should only be used by extensions that do not take the state as clientdata.

func Errorfp added in v0.66.0

func Errorfp(format string, args ...interface{}) *Error

Pointer version of Errorf. used in code conditionally returning an error (oerr pointer).

func (Error) Error added in v0.34.0

func (e Error) Error() string

func (Error) Inspect

func (e Error) Inspect() string

func (Error) JSON added in v0.36.0

func (e Error) JSON(w io.Writer) error

func (Error) Type

func (e Error) Type() Type

func (Error) Unwrap added in v0.34.0

func (e Error) Unwrap(forceStringKeys bool) any

type ExtFunction added in v0.34.0

type ExtFunction func(eval any, name string, args []Object) Object

ExtFunction is the signature of what grol will call when the extension is invoked. Incoming arguments are validated for type and number of arguments based on Extension. eval is the opaque state passed from the interpreter, it can be used with eval.Eval etc name is the function name as registered under.

func ShortCallback added in v0.36.0

func ShortCallback(f ShortExtFunction) ExtFunction

Adapter for functions that only need the argumants.

type Extension added in v0.34.0

type Extension struct {
	Name       string      // Name to make the function available as in grol.
	MinArgs    int         // Minimum number of arguments required.
	MaxArgs    int         // Maximum number of arguments allowed. -1 for unlimited.
	ArgTypes   []Type      // Type of each argument, provided at least up to MinArgs.
	Help       string      // Help text for the function. Appended as a comment when printing the function.
	Callback   ExtFunction // The go function or lambda to call when the grol by Name(...) is invoked.
	ClientData any         // Opaque data that will be passed as first argument of Callback if set (state is, if nil).
	Variadic   bool        // MaxArgs > MinArgs (or MaxArg == -1)
	DontCache  bool        // If true, the result of this function should not be cached (has side effects).
}

Extensions are functions implemented in go and exposed to grol.

func (Extension) Inspect added in v0.34.0

func (e Extension) Inspect() string

func (Extension) JSON added in v0.36.0

func (e Extension) JSON(w io.Writer) error

func (Extension) Type added in v0.34.0

func (e Extension) Type() Type

func (Extension) Unwrap added in v0.34.0

func (e Extension) Unwrap(_ bool) any

func (Extension) Usage added in v0.34.0

func (e Extension) Usage(out *strings.Builder)

type ExtensionMap added in v0.67.0

type ExtensionMap map[string]Extension

func ExtraFunctions added in v0.34.0

func ExtraFunctions() ExtensionMap

Returns the table of extended functions to seed the state of an eval.

type Float

type Float struct {
	Value float64
}

func (Float) Inspect

func (f Float) Inspect() string

func (Float) JSON added in v0.36.0

func (f Float) JSON(w io.Writer) error

func (Float) Type

func (f Float) Type() Type

func (Float) Unwrap added in v0.34.0

func (f Float) Unwrap(_ bool) any

type Function

type Function struct {
	Parameters []ast.Node
	Name       *ast.Identifier
	CacheKey   string
	Body       *ast.Statements
	Env        *Environment
	Variadic   bool
	Lambda     bool // i.e. has no name.
}

func (Function) Format added in v0.69.0

func (f Function) Format() string

Format is like Inspect but using non compact print state.

func (Function) Inspect

func (f Function) Inspect() string

func (Function) JSON added in v0.36.0

func (f Function) JSON(w io.Writer) error

func (Function) Type

func (f Function) Type() Type

func (Function) Unwrap added in v0.34.0

func (f Function) Unwrap(forceStringKeys bool) any

type Integer

type Integer struct {
	Value int64
}

func (Integer) Inspect

func (i Integer) Inspect() string

func (Integer) JSON added in v0.36.0

func (i Integer) JSON(w io.Writer) error

func (Integer) Type

func (i Integer) Type() Type

func (Integer) Unwrap added in v0.34.0

func (i Integer) Unwrap(_ bool) any

type Macro

type Macro struct {
	Parameters []ast.Node
	Body       *ast.Statements
	Env        *Environment
}

func (Macro) Inspect

func (m Macro) Inspect() string

func (Macro) JSON added in v0.36.0

func (m Macro) JSON(w io.Writer) error

func (Macro) Type

func (m Macro) Type() Type

func (Macro) Unwrap added in v0.34.0

func (m Macro) Unwrap(_ bool) any

type Map

type Map interface {
	Object
	Get(key Object) (Object, bool)
	Set(key, value Object) Map
	Len() int
	First() Object
	Rest() Object

	Append(right Map) Map
	// contains filtered or unexported methods
}

func MakePair added in v0.52.0

func MakePair(key, value Object) Map

Make a (small) map with a single key value pair entry.

func MakeQuad added in v0.52.0

func MakeQuad(key1, value1, key2, value2 Object) Map

Makes a map with 2 key value pairs. Make sure the keys are sorted before calling this. otherwise just use NewMap() + Set() twice.

func NewMap

func NewMap() Map

func NewMapSize added in v0.52.0

func NewMapSize(size int) Map

type Null

type Null struct{}

func (Null) Inspect

func (n Null) Inspect() string

func (Null) JSON added in v0.36.0

func (n Null) JSON(w io.Writer) error

func (Null) Type

func (n Null) Type() Type

func (Null) Unwrap added in v0.34.0

func (n Null) Unwrap(_ bool) any

type Object

type Object interface {
	Type() Type
	Inspect() string
	// ForceStringMapKeys makes maps[string]any instead of map[any]any irrespective of the key type.
	// This is used for go marshaler based JSON for instance.
	Unwrap(forceStringMapKeys bool) any
	JSON(out io.Writer) error
}

func CopyRegister added in v0.74.0

func CopyRegister(o Object) Object

func Elements added in v0.52.0

func Elements(val Object) []Object

Elements returns the keys of a map or the elements of an array.

func First added in v0.52.0

func First(a Object) Object

func MakeObjectSlice added in v0.51.0

func MakeObjectSlice(n int) []Object

Memory checking version of make(). To avoid OOM kills / fatal errors.

func NewArray added in v0.52.0

func NewArray(elements []Object) Object

func Range added in v0.54.0

func Range(val Object, l, r int64) Object

func Rest added in v0.52.0

func Rest(val Object) Object

func Value added in v0.65.0

func Value(o Object) Object

Deal with references and registers and return the actual value.

type Quote

type Quote struct {
	Node ast.Node
}

func (Quote) Inspect

func (q Quote) Inspect() string

func (Quote) JSON added in v0.36.0

func (q Quote) JSON(w io.Writer) error

func (Quote) Type

func (q Quote) Type() Type

func (Quote) Unwrap added in v0.34.0

func (q Quote) Unwrap(_ bool) any

type Reference added in v0.65.0

type Reference struct {
	Name   string
	RefEnv *Environment
}

References are pointer to original object up the stack.

func (Reference) Inspect added in v0.65.0

func (r Reference) Inspect() string

func (Reference) JSON added in v0.65.0

func (r Reference) JSON(w io.Writer) error

func (Reference) ObjValue added in v0.74.0

func (r Reference) ObjValue() Object

func (Reference) Type added in v0.65.0

func (r Reference) Type() Type

func (Reference) Unwrap added in v0.65.0

func (r Reference) Unwrap(str bool) any

type Register added in v0.74.0

type Register struct {
	ast.Base
	RefEnv *Environment
	Idx    int
	Count  int
}

Registers are fast local integer variables skipping the environment map lookup.

func (*Register) DebugString added in v0.74.0

func (r *Register) DebugString() string

func (*Register) Inspect added in v0.74.0

func (r *Register) Inspect() string

func (*Register) Int64 added in v0.74.0

func (r *Register) Int64() int64

func (*Register) JSON added in v0.74.0

func (r *Register) JSON(w io.Writer) error

func (*Register) ObjValue added in v0.74.0

func (r *Register) ObjValue() Object

func (*Register) PrettyPrint added in v0.74.0

func (r *Register) PrettyPrint(out *ast.PrintState) *ast.PrintState

func (*Register) Ptr added in v0.74.0

func (r *Register) Ptr() *int64

func (*Register) Type added in v0.74.0

func (r *Register) Type() Type

func (*Register) Unwrap added in v0.74.0

func (r *Register) Unwrap(str bool) any

type ReturnValue

type ReturnValue struct {
	Value       Object
	ControlType token.Type
}

func (ReturnValue) Inspect

func (rv ReturnValue) Inspect() string

func (ReturnValue) JSON added in v0.36.0

func (rv ReturnValue) JSON(w io.Writer) error

func (ReturnValue) Type

func (rv ReturnValue) Type() Type

func (ReturnValue) Unwrap added in v0.34.0

func (rv ReturnValue) Unwrap(_ bool) any

type ShortExtFunction added in v0.36.0

type ShortExtFunction func(args []Object) Object

ShortExtFunction is the signature for callbacks that do not need more than the arguments (like math functions).

type SmallArray added in v0.52.0

type SmallArray struct {
	// contains filtered or unexported fields
}

func (SmallArray) Elements added in v0.52.0

func (sa SmallArray) Elements() []Object

func (SmallArray) First added in v0.52.0

func (sa SmallArray) First() Object

func (SmallArray) Inspect added in v0.52.0

func (sa SmallArray) Inspect() string

func (SmallArray) JSON added in v0.52.0

func (sa SmallArray) JSON(w io.Writer) error

func (SmallArray) Len added in v0.52.0

func (sa SmallArray) Len() int

func (SmallArray) Rest added in v0.52.0

func (sa SmallArray) Rest() Object

func (SmallArray) Type added in v0.52.0

func (sa SmallArray) Type() Type

func (SmallArray) Unwrap added in v0.52.0

func (sa SmallArray) Unwrap(forceStringKeys bool) any

type SmallMap added in v0.52.0

type SmallMap struct {
	// contains filtered or unexported fields
}

func (SmallMap) Append added in v0.52.0

func (m SmallMap) Append(right Map) Map

func (SmallMap) First added in v0.52.0

func (m SmallMap) First() Object

func (SmallMap) Get added in v0.52.0

func (m SmallMap) Get(key Object) (Object, bool)

func (SmallMap) Inspect added in v0.52.0

func (m SmallMap) Inspect() string

func (SmallMap) JSON added in v0.52.0

func (m SmallMap) JSON(w io.Writer) error

func (SmallMap) Len added in v0.52.0

func (m SmallMap) Len() int

func (SmallMap) Range added in v0.54.0

func (m SmallMap) Range(l, r int64) Object

func (SmallMap) Rest added in v0.52.0

func (m SmallMap) Rest() Object

func (SmallMap) Set added in v0.52.0

func (m SmallMap) Set(key, value Object) Map

func (SmallMap) Type added in v0.52.0

func (m SmallMap) Type() Type

func (SmallMap) Unwrap added in v0.52.0

func (m SmallMap) Unwrap(forceStringKeys bool) any

type String

type String struct {
	Value string
}

func (String) Inspect

func (s String) Inspect() string

func (String) JSON added in v0.36.0

func (s String) JSON(w io.Writer) error

func (String) Type

func (s String) Type() Type

func (String) Unwrap added in v0.34.0

func (s String) Unwrap(_ bool) any

type Type

type Type uint8
const (
	UNKNOWN Type = iota
	INTEGER
	FLOAT // These 2 must stay in that order for areIntFloat to work.
	BOOLEAN
	NIL
	ERROR
	RETURN
	FUNC
	STRING
	ARRAY
	MAP // Ordered map and allows any key type including more maps/arrays/functions/...
	QUOTE
	MACRO
	EXTENSION
	REFERENCE
	REGISTER
	ANY // A marker, for extensions, not a real type.
)

func (Type) String

func (i Type) String() string

Jump to

Keyboard shortcuts

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