Documentation
¶
Index ¶
- Constants
- Variables
- func AddIdentifier(name string, value Object)
- func BigIntValue(o Object) (*big.Int, bool)
- func Cmp(ei, ej Object) int
- func CompareKeys(a, b keyValuePair) int
- func Constant(name string) bool
- func CreateFunction(cmd Extension) error
- func Equals(left, right Object) bool
- func FreeMemory() int64
- func Hashable(o Object) bool
- func Init()
- func IsAnyIntType(t Type) bool
- func IsExtraFunction(name string) bool
- func IsIntType(t Type) bool
- func Len(a Object) int
- func MustBeOk(n int)
- func SetCacheKey(f *Function) string
- func SizeOk(n int) (bool, int64)
- func TypeEqual(a, b Type) bool
- func Unwrap(objs []Object, forceStringKeys bool) []any
- func UnwrapHashable(o Object) any
- func UnwrapStringKeys(m Map, forceStringKeys bool) (map[string]any, bool)
- func ValidIdentifier(name string) bool
- func WriteStrings(out *strings.Builder, list []Object, before, sep, after string)
- type Array
- type BigArray
- func (ao BigArray) Elements() []Object
- func (ao BigArray) First() Object
- func (ao BigArray) Inspect() string
- func (ao BigArray) JSON(w io.Writer) error
- func (ao BigArray) Len() int
- func (ao BigArray) Less(i, j int) bool
- func (ao BigArray) Rest() Object
- func (ao BigArray) Swap(i, j int)
- func (ao BigArray) Type() Type
- func (ao BigArray) Unwrap(forceStringKeys bool) any
- type BigInt
- type BigMap
- func (m *BigMap) Append(right Map) Map
- func (m *BigMap) Delete(key Object) (Map, bool)
- func (m *BigMap) First() Object
- func (m *BigMap) Get(key Object) (Object, bool)
- func (m *BigMap) Inspect() string
- func (m *BigMap) JSON(w io.Writer) error
- func (m *BigMap) Len() int
- func (m *BigMap) Range(l, r int64) Object
- func (m *BigMap) Rest() Object
- func (m *BigMap) Set(key, value Object) Map
- func (m *BigMap) Type() Type
- func (m *BigMap) Unwrap(forceStringKeys bool) any
- type Boolean
- type Environment
- func (e *Environment) BaseInfo() *BigMap
- func (e *Environment) CantCache() bool
- func (e *Environment) CreateOrSet(name string, val Object, create bool) Object
- func (e *Environment) Delete(name string) Object
- func (e *Environment) Get(name string) (Object, bool)
- func (e *Environment) GetMisses() int64
- func (e *Environment) HasRegisters() bool
- func (e *Environment) Info() Object
- func (e *Environment) IsRef(name string) (*Environment, string)
- func (e *Environment) Len() int
- func (e *Environment) MakeRegister(originalName string, v int64) Register
- func (e *Environment) Name() string
- func (e *Environment) NumSet() int64
- func (e *Environment) RegisterTrie(t *trie.Trie)
- func (e *Environment) ReleaseRegister(register Register)
- func (e *Environment) SaveGlobals(to io.Writer, maxValueLen int) (int, error)
- func (e *Environment) Set(name string, val Object) Object
- func (e *Environment) SetNoChecks(name string, val Object, create bool) Object
- func (e *Environment) StackParent() *Environment
- func (e *Environment) TriggerNoCache()
- type Error
- type ExtFunction
- type Extension
- type ExtensionMap
- type Float
- type Function
- type Integer
- type Macro
- type Map
- type Null
- type Object
- type Quote
- type Reference
- type Register
- func (r *Register) DebugString() string
- func (r *Register) Inspect() string
- func (r *Register) Int64() int64
- func (r *Register) JSON(w io.Writer) error
- func (r *Register) ObjValue() Object
- func (r *Register) PrettyPrint(out *ast.PrintState) *ast.PrintState
- func (r *Register) Ptr() *int64
- func (r *Register) Type() Type
- func (r *Register) Unwrap(str bool) any
- type ReturnValue
- type ShortExtFunction
- type SmallArray
- func (sa SmallArray) Elements() []Object
- func (sa SmallArray) First() Object
- func (sa SmallArray) Inspect() string
- func (sa SmallArray) JSON(w io.Writer) error
- func (sa SmallArray) Len() int
- func (sa SmallArray) Rest() Object
- func (sa SmallArray) Type() Type
- func (sa SmallArray) Unwrap(forceStringKeys bool) any
- type SmallMap
- func (m SmallMap) Append(right Map) Map
- func (m SmallMap) Delete(key Object) (Map, bool)
- func (m SmallMap) First() Object
- func (m SmallMap) Get(key Object) (Object, bool)
- func (m SmallMap) Inspect() string
- func (m SmallMap) JSON(w io.Writer) error
- func (m SmallMap) Len() int
- func (m SmallMap) Range(l, r int64) Object
- func (m SmallMap) Rest() Object
- func (m SmallMap) Set(key, value Object) Map
- func (m SmallMap) Type() Type
- func (m SmallMap) Unwrap(forceStringKeys bool) any
- type String
- type Type
Constants ¶
const ( CategoryMath = "math" CategoryIntrospection = "introspection" CategoryString = "string" CategoryTime = "time" CategoryIO = "io" CategoryImage = "image" )
Extension categories.
const MaxSmallArray = 8
const MaxSmallMap = 4 // so 2x, so same as MaxSmallArray really.
const NumRegisters = 8
const ObjectSize = 2 * bits.UintSize / 8 // also unsafe.Sizeof(interface) == 16 bytes (2 pointers == 2 ints)
ObjectSize represents the size of the Object interface in bytes.
Variables ¶
var ( KeyKey = String{Value: "key"} ValueKey = String{Value: "value"} )
var EmptyArray = SmallArray{}
Functions ¶
func AddIdentifier ¶ added in v0.34.0
AddIdentifier adds values to top level environment, e.g "pi" -> 3.14159... or "printf(){print(sprintf(%s, args...))}".
func BigIntValue ¶ added in v0.99.0
BigIntValue extracts a *big.Int from Integer, BigInt, or Register.
func CompareKeys ¶ added in v0.43.0
func CompareKeys(a, b keyValuePair) int
func Constant ¶ added in v0.34.0
Constant 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
CreateFunction adds a new function to the table of extended functions.
func FreeMemory ¶ added in v0.51.0
func FreeMemory() int64
FreeMemory returns the amount of free memory in bytes.
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 IsAnyIntType ¶ added in v0.99.0
IsAnyIntType checks if the type is an integer, bigint, or register.
func IsExtraFunction ¶ added in v0.62.0
func IsIntType ¶ added in v0.74.0
IsIntType checks if the type is an integer (or register, but not bigint).
func SetCacheKey ¶ added in v0.76.0
SetCacheKey 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 TypeEqual ¶ added in v0.74.0
TypeEqual checks if types are equal, considering registers as integers for comparison purposes.
func UnwrapHashable ¶ added in v0.56.0
func UnwrapStringKeys ¶ added in v0.56.0
func ValidIdentifier ¶ added in v0.67.0
Types ¶
type BigInt ¶ added in v0.99.0
BigInt represents an arbitrary precision integer.
func (BigInt) IsSmall ¶ added in v0.99.0
IsSmall returns true if the BigInt fits in an int64 and the int64 value.
type BigMap ¶ added in v0.52.0
type BigMap struct {
// contains filtered or unexported fields
}
BigMap represents a collection of sorted key-value pairs, providing O(n) insert and O(log n) access or mutation for the same key.
type Environment ¶
type Environment struct {
// Output buffering state
OutputBuffer *bytes.Buffer
PrevOut io.Writer
// 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)
NewFunctionEnvironment creates 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
NewMacroEnvironment creates a 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) CantCache ¶ added in v0.78.0
func (e *Environment) CantCache() bool
CantCache returns true if a non-cacheable extension (like rand()) was called.
func (*Environment) CreateOrSet ¶ added in v0.65.0
func (e *Environment) CreateOrSet(name string, val Object, create bool) Object
func (*Environment) Delete ¶ added in v0.80.0
func (e *Environment) Delete(name string) Object
Delete removes the first entry found under that name from the environment. TODO: check if references need special handling / if variable is in use/referenced somewhere. (had a SEGV in https://github.com/grol-io/grol/issues/335 ).
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
Name returns the name of the frame or stack.
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)
RegisterTrie 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
SaveGlobals saves and returns the number of ids written. maxValueLen <= 0 means no limit.
func (*Environment) SetNoChecks ¶ added in v0.34.0
func (e *Environment) SetNoChecks(name string, val Object, create bool) Object
SetNoChecks forces 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
StackParent 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 ¶
func Errorf ¶ added in v0.66.0
Errorf creates an error object with a formatted message. Use eval's Errorf() instead whenever possible to get the stack. This function should only be used by extensions that do not take the state as clientdata.
type ExtFunction ¶ added in v0.34.0
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
ShortCallback adapts functions that only need the arguments.
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.
Category string // Category of the function (math, string, io, etc.)
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).
}
Extension represents functions implemented in Go and exposed to grol.
type ExtensionMap ¶ added in v0.67.0
func ExtraFunctions ¶ added in v0.34.0
func ExtraFunctions() ExtensionMap
ExtraFunctions returns the table of extended functions to seed the state of an eval.
type Function ¶
type Function struct {
Parameters []ast.Node
Name *ast.Identifier
CacheKey string
Body *ast.Statements
Env *Environment
Variadic bool // i.e. has no name.
Lambda bool
}
type Macro ¶
type Macro struct {
Parameters []ast.Node
Body *ast.Statements
Env *Environment
}
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
Delete(key Object) (Map, bool)
// contains filtered or unexported methods
}
func MakeQuad ¶ added in v0.52.0
MakeQuad makes a map with 2 key value pairs. Make sure the keys are sorted before calling this. otherwise just use NewMap() + Set() twice.
func NewMapSize ¶ added in v0.52.0
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 MakeObjectSlice ¶ added in v0.51.0
MakeObjectSlice provides a memory checking version of make() to avoid OOM kills and fatal errors.
type Reference ¶ added in v0.65.0
type Reference struct {
Name string
RefEnv *Environment
}
Reference represents a pointer to the original object up the stack.
type Register ¶ added in v0.74.0
type Register struct {
ast.Base
RefEnv *Environment
Idx int
Count int
}
Register represents fast local integer variables skipping the environment map lookup.
func (*Register) DebugString ¶ added in v0.74.0
func (*Register) PrettyPrint ¶ added in v0.74.0
func (r *Register) PrettyPrint(out *ast.PrintState) *ast.PrintState
type ReturnValue ¶
func (ReturnValue) Inspect ¶
func (rv ReturnValue) Inspect() string
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
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) 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 Type ¶
type Type uint8
const ( UNKNOWN Type = iota INTEGER FLOAT // These 2 must stay in that order for areIntFloat to work. BIGINT // big.Int arbitrary precision integer. 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. )