Documentation ¶
Overview ¶
Package sxeval allows to evaluate s-expressions. Evaluation is splitted into parsing that s-expression and executing the result of the parsed expression. This is done to reduce syntax checks.
Index ¶
- Variables
- func AssertPure(sx.Vector) bool
- func PrintExprs(w io.Writer, exprs []Expr) (int, error)
- type Binding
- func (b *Binding) Bind(sym *sx.Symbol, obj sx.Object) error
- func (b *Binding) BindBuiltin(bi *Builtin) error
- func (b *Binding) BindSpecial(syn *Special) error
- func (b *Binding) Bindings() *sx.Pair
- func (b *Binding) Freeze()
- func (b *Binding) GoString() string
- func (b *Binding) IsAtom() bool
- func (b *Binding) IsEqual(other sx.Object) bool
- func (b *Binding) IsFrozen() bool
- func (b *Binding) IsNil() bool
- func (b *Binding) Lookup(sym *sx.Symbol) (sx.Object, bool)
- func (b *Binding) LookupN(sym *sx.Symbol, n int) (sx.Object, bool)
- func (b *Binding) MakeChildBinding(name string, sizeHint int) *Binding
- func (b *Binding) Name() string
- func (b *Binding) Parent() *Binding
- func (b *Binding) Resolve(sym *sx.Symbol) (sx.Object, bool)
- func (b *Binding) ResolveN(sym *sx.Symbol, n int) (sx.Object, bool)
- func (b *Binding) String() string
- func (b *Binding) Symbols() []*sx.Symbol
- type Builtin
- func (b *Builtin) Call(env *Environment, args sx.Vector) (sx.Object, error)
- func (b *Builtin) Call0(env *Environment) (sx.Object, error)
- func (b *Builtin) Call1(env *Environment, arg sx.Object) (sx.Object, error)
- func (b *Builtin) Call2(env *Environment, arg0, arg1 sx.Object) (sx.Object, error)
- func (b *Builtin) GoString() string
- func (b *Builtin) IsAtom() bool
- func (b *Builtin) IsEqual(other sx.Object) bool
- func (b *Builtin) IsNil() bool
- func (b *Builtin) IsPure(objs sx.Vector) bool
- func (b *Builtin) String() string
- type BuiltinCallExpr
- type CallError
- type CallExpr
- type Callable
- type ConstObjectExpr
- type Environment
- func (env *Environment) Bind(sym *sx.Symbol, obj sx.Object) error
- func (env *Environment) Binding() *Binding
- func (env *Environment) Call(fn Callable, args sx.Vector) (res sx.Object, err error)
- func (env *Environment) Compile(obj sx.Object) (Expr, error)
- func (env *Environment) Eval(obj sx.Object) (sx.Object, error)
- func (env *Environment) Execute(expr Expr) (res sx.Object, err error)
- func (env *Environment) ExecuteTCO(expr Expr) (sx.Object, error)
- func (env *Environment) FindBinding(sym *sx.Symbol) *Binding
- func (env *Environment) Lookup(sym *sx.Symbol) (sx.Object, bool)
- func (env *Environment) LookupNWithError(sym *sx.Symbol, n int) (sx.Object, error)
- func (env *Environment) MakeNotBoundError(sym *sx.Symbol) NotBoundError
- func (env *Environment) MakeParseEnvironment() *ParseEnvironment
- func (env *Environment) MakeReworkEnvironment() *ReworkEnvironment
- func (env *Environment) NewLexicalEnvironment(parent *Binding, name string, numBindings int) *Environment
- func (env *Environment) Parse(obj sx.Object) (Expr, error)
- func (env *Environment) RebindExecutionEnvironment(bind *Binding) *Environment
- func (env *Environment) Resolve(sym *sx.Symbol) (sx.Object, bool)
- func (env *Environment) ResolveNWithError(sym *sx.Symbol, skip int) (sx.Object, error)
- func (env *Environment) ResolveUnboundWithError(sym *sx.Symbol) (sx.Object, error)
- func (env *Environment) Rework(expr Expr) Expr
- func (env *Environment) Run(expr Expr) (sx.Object, error)
- func (env *Environment) SetExecutor(observe ExecuteObserver) *Environment
- func (env *Environment) SetParseObserver(observe ParseObserver) *Environment
- func (env *Environment) SetReworkObserver(observe ReworkObserver) *Environment
- func (env *Environment) String() string
- type EnvironmentExpr
- type ErrBindingFrozen
- type ExecuteError
- type ExecuteObserver
- type Expr
- type ExprObj
- type LookupSymbolExpr
- func (lse *LookupSymbolExpr) Compute(env *Environment) (sx.Object, error)
- func (lse *LookupSymbolExpr) GetLevel() int
- func (lse *LookupSymbolExpr) GetSymbol() *sx.Symbol
- func (lse *LookupSymbolExpr) Improve(*ReworkEnvironment) Expr
- func (lse LookupSymbolExpr) Print(w io.Writer) (int, error)
- func (lse *LookupSymbolExpr) Unparse() sx.Object
- type NotBoundError
- type NotCallableError
- type ObjExpr
- type ParseEnvironment
- func (pf *ParseEnvironment) Bind(sym *sx.Symbol, obj sx.Object) error
- func (pf *ParseEnvironment) Binding() *Binding
- func (pf *ParseEnvironment) MakeChildFrame(name string, baseSize int) *ParseEnvironment
- func (pf *ParseEnvironment) Parse(form sx.Object) (expr Expr, err error)
- func (pf *ParseEnvironment) ParseAgain(form sx.Object) error
- func (pf *ParseEnvironment) Resolve(sym *sx.Symbol) (sx.Object, bool)
- type ParseObserver
- type ResolveSymbolExpr
- func (use ResolveSymbolExpr) Compute(env *Environment) (sx.Object, error)
- func (rse ResolveSymbolExpr) GetSymbol() *sx.Symbol
- func (rse ResolveSymbolExpr) Improve(re *ReworkEnvironment) Expr
- func (use ResolveSymbolExpr) Print(w io.Writer) (int, error)
- func (rse ResolveSymbolExpr) Unparse() sx.Object
- type ReworkEnvironment
- func (re *ReworkEnvironment) Bind(sym *sx.Symbol) error
- func (re *ReworkEnvironment) Binding() *Binding
- func (re *ReworkEnvironment) Call(fn Callable, args sx.Vector) (sx.Object, error)
- func (re *ReworkEnvironment) Height() int
- func (re *ReworkEnvironment) MakeChildEnvironment(name string, baseSize int) *ReworkEnvironment
- func (re *ReworkEnvironment) Resolve(sym *sx.Symbol) (sx.Object, int, bool)
- func (re *ReworkEnvironment) Rework(expr Expr) Expr
- type ReworkObserver
- type Special
- type SymbolExpr
- type Syntax
- type UnboundSymbolExpr
- func (use UnboundSymbolExpr) Compute(env *Environment) (sx.Object, error)
- func (use UnboundSymbolExpr) GetSymbol() *sx.Symbol
- func (use UnboundSymbolExpr) Improve(re *ReworkEnvironment) Expr
- func (use UnboundSymbolExpr) Print(w io.Writer) (int, error)
- func (use UnboundSymbolExpr) Unparse() sx.Object
Constants ¶
This section is empty.
Variables ¶
ErrNoArgs signals that no arguments were given.
var NilExpr = nilExpr{}
NilExpr returns always Nil
Functions ¶
func AssertPure ¶
AssertPure is a TestPure function that alsways returns true.
Types ¶
type Binding ¶
type Binding struct {
// contains filtered or unexported fields
}
Binding is a binding based on maps.
func GetBinding ¶
GetBinding returns the object as a binding, if possible.
func MakeRootBinding ¶
MakeRootBinding creates a new root binding.
func (*Binding) Bind ¶
Bind creates a local mapping with a given symbol and object.
A previous mapping will be overwritten.
func (*Binding) BindBuiltin ¶
BindBuiltin binds the given builtin with its given name.
func (*Binding) Bindings ¶
Bindings returns all bindings as an a-list in some random order.
func (*Binding) IsFrozen ¶
IsFrozen returns true if binding is frozen.
func (*Binding) Lookup ¶
Lookup will search for a local binding of the given symbol. If not found, the search will *not* be continued in the parent binding. Use the global `Resolve` function, if you want a search up to the parent.
func (*Binding) LookupN ¶
LookupN will lookup the symbol in the N-th parent.
func (*Binding) MakeChildBinding ¶
MakeChildBinding creates a new binding with a given parent.
func (*Binding) Resolve ¶
Resolve a symbol in a binding and all of its parent bindings.
func (*Binding) ResolveN ¶
ResolveN resolves a symbol in the N-th parent binding and all of its parent bindings.
type Builtin ¶
type Builtin struct { // The canonical Name of the builtin Name string // Minimum and maximum arity. If MaxArity < 0, maximum arity is unlimited MinArity, MaxArity int16 // Test builtin to be independent of the environment and does not produce some side effect TestPure func(sx.Vector) bool // The actual builtin function, with no argument Fn0 func(*Environment) (sx.Object, error) // The actual builtin function, with one argument Fn1 func(*Environment, sx.Object) (sx.Object, error) // The actual builtin function, with two arguments Fn2 func(*Environment, sx.Object, sx.Object) (sx.Object, error) // The actual builtin function, with any number of arguments Fn func(*Environment, sx.Vector) (sx.Object, error) // Do not add a CallError NoCallError bool }
Builtin is the type for normal predefined functions.
func (*Builtin) Call ¶
Call the builtin function with the given environment and arguments.
func (*Builtin) Call0 ¶
func (b *Builtin) Call0(env *Environment) (sx.Object, error)
Call0 the builtin function with the given environment and no arguments.
func (*Builtin) Call1 ¶
Call1 the builtin function with the given environment and one argument.
func (*Builtin) Call2 ¶
Call2 the builtin function with the given environment and two arguments.
func (*Builtin) GoString ¶
GoString returns the go string representation.
func (*Builtin) IsAtom ¶
IsAtom returns true iff the object is an object that is not further decomposable.
func (*Builtin) IsEqual ¶
IsEqual compare two objects for deep equality.
func (*Builtin) IsPure ¶
IsPure returns true if builtin is a pure function.
type BuiltinCallExpr ¶
BuiltinCallExpr calls a builtin and returns the resulting object. It is an optimization of `CallExpr.`
func (*BuiltinCallExpr) Compute ¶
func (bce *BuiltinCallExpr) Compute(env *Environment) (sx.Object, error)
func (*BuiltinCallExpr) Improve ¶
func (bce *BuiltinCallExpr) Improve(re *ReworkEnvironment) Expr
func (*BuiltinCallExpr) String ¶
func (bce *BuiltinCallExpr) String() string
func (*BuiltinCallExpr) Unparse ¶
func (bce *BuiltinCallExpr) Unparse() sx.Object
type CallError ¶
CallError encapsulate an error that occured during a call.
type CallExpr ¶
CallExpr calls a procedure and returns the resulting objects.
func (*CallExpr) Improve ¶
func (ce *CallExpr) Improve(re *ReworkEnvironment) Expr
type Callable ¶
type Callable interface { sx.Object // IsPure checks if the callable is independent of a full environment and // does not produce any side effects. IsPure(sx.Vector) bool // Call0 with no argument in given environment. Call0(*Environment) (sx.Object, error) // Call1 with one argument in given environment. Call1(*Environment, sx.Object) (sx.Object, error) // Call2 with two arguments in given environment. Call2(*Environment, sx.Object, sx.Object) (sx.Object, error) // Call the value with the given args in the given environment. Call(*Environment, sx.Vector) (sx.Object, error) }
Callable is an sx.Object which can be called for evaluation.
type ConstObjectExpr ¶
ConstObjectExpr is an Expr that results in a specific, constant sx.Object.
func GetConstExpr ¶
func GetConstExpr(expr Expr) (ConstObjectExpr, bool)
GetConstExpr returns the Expr as a ConstObjectExpr, if possible.
type Environment ¶
type Environment struct {
// contains filtered or unexported fields
}
Environment is a runtime object of the current computing environment.
func MakeExecutionEnvironment ¶
func MakeExecutionEnvironment(bind *Binding) *Environment
MakeExecutionEnvironment creates an environment for later execution of an expression.
func (*Environment) Binding ¶
func (env *Environment) Binding() *Binding
func (*Environment) Call ¶
Call the given Callable with the arguments.
func (*Environment) Compile ¶
func (env *Environment) Compile(obj sx.Object) (Expr, error)
Compile the given object and return the reworked expression.
func (*Environment) Eval ¶
Eval parses the given object and runs it in the environment.
func (*Environment) Execute ¶
func (env *Environment) Execute(expr Expr) (res sx.Object, err error)
Execute the given expression.
func (*Environment) ExecuteTCO ¶
func (env *Environment) ExecuteTCO(expr Expr) (sx.Object, error)
ExecuteTCO is called when the expression should be executed at last position, aka as tail call order.
func (*Environment) FindBinding ¶
func (env *Environment) FindBinding(sym *sx.Symbol) *Binding
func (*Environment) LookupNWithError ¶
func (*Environment) MakeNotBoundError ¶
func (env *Environment) MakeNotBoundError(sym *sx.Symbol) NotBoundError
func (*Environment) MakeParseEnvironment ¶
func (env *Environment) MakeParseEnvironment() *ParseEnvironment
func (*Environment) MakeReworkEnvironment ¶
func (env *Environment) MakeReworkEnvironment() *ReworkEnvironment
func (*Environment) NewLexicalEnvironment ¶
func (env *Environment) NewLexicalEnvironment(parent *Binding, name string, numBindings int) *Environment
func (*Environment) Parse ¶
func (env *Environment) Parse(obj sx.Object) (Expr, error)
Parse the given object.
func (*Environment) RebindExecutionEnvironment ¶
func (env *Environment) RebindExecutionEnvironment(bind *Binding) *Environment
RebindExecutionEnvironment clones the original environment, but uses the given binding.
func (*Environment) ResolveNWithError ¶
func (*Environment) ResolveUnboundWithError ¶
func (*Environment) Rework ¶
func (env *Environment) Rework(expr Expr) Expr
Rework the given expression.
func (*Environment) Run ¶
func (env *Environment) Run(expr Expr) (sx.Object, error)
Run the given expression.
func (*Environment) SetExecutor ¶
func (env *Environment) SetExecutor(observe ExecuteObserver) *Environment
SetExecutor sets the given executor.
func (*Environment) SetParseObserver ¶
func (env *Environment) SetParseObserver(observe ParseObserver) *Environment
SetParseObserver sets the given parsing observer.
func (*Environment) SetReworkObserver ¶
func (env *Environment) SetReworkObserver(observe ReworkObserver) *Environment
SetReworkObserver sets the given rework observer.
func (*Environment) String ¶
func (env *Environment) String() string
type EnvironmentExpr ¶
type EnvironmentExpr struct { Env *Environment Expr Expr }
type ErrBindingFrozen ¶
type ErrBindingFrozen struct{ Binding *Binding }
ErrBindingFrozen is returned when trying to update a frozen binding.
func (ErrBindingFrozen) Error ¶
func (err ErrBindingFrozen) Error() string
type ExecuteError ¶
type ExecuteError struct { Stack []EnvironmentExpr // contains filtered or unexported fields }
func (ExecuteError) Error ¶
func (ee ExecuteError) Error() string
func (ExecuteError) PrintStack ¶
func (ExecuteError) Unwrap ¶
func (ee ExecuteError) Unwrap() error
type ExecuteObserver ¶
type ExecuteObserver interface { // BeforeExecution is called immediate before the given expression is executed. // The observer may change the expression or abort execution with an error. BeforeExecution(*Environment, Expr) (Expr, error) // AfterExecution is called immediate after the given expression was executed, // resulting in an `sx.Object` and an error. AfterExecution(*Environment, Expr, sx.Object, error) }
ExecuteObserver observes the execution of expressions.
type Expr ¶
type Expr interface { // Unparse the expression as an sx.Object Unparse() sx.Object // Improve the expressions to a possible simpler one. Improve(*ReworkEnvironment) Expr // Compute the expression in a frame and return the result. // It may have side-effects, on the given environment, or on the // general environment of the system. Compute(*Environment) (sx.Object, error) // Print the expression on the given writer. Print(io.Writer) (int, error) }
Expr are values that are computed for evaluation in an environment.
type ExprObj ¶
type ExprObj struct {
// contains filtered or unexported fields
}
ExprObj encapsulates an Expr in an sx.Object.
func GetExprObj ¶
GetExprObj returns the object as a expression object, if possible.
func MakeExprObj ¶
MakeExprObj creates an ExprObj from an Expr.
type LookupSymbolExpr ¶
type LookupSymbolExpr struct {
// contains filtered or unexported fields
}
LookupSymbolExpr is a special UnboundSymbolExpr that gives an indication about the nesting level of `Binding`s, where the symbol will be bound.
func (*LookupSymbolExpr) Compute ¶
func (lse *LookupSymbolExpr) Compute(env *Environment) (sx.Object, error)
func (*LookupSymbolExpr) GetLevel ¶
func (lse *LookupSymbolExpr) GetLevel() int
func (*LookupSymbolExpr) GetSymbol ¶
func (lse *LookupSymbolExpr) GetSymbol() *sx.Symbol
func (*LookupSymbolExpr) Improve ¶
func (lse *LookupSymbolExpr) Improve(*ReworkEnvironment) Expr
func (*LookupSymbolExpr) Unparse ¶
func (lse *LookupSymbolExpr) Unparse() sx.Object
type NotBoundError ¶
NotBoundError signals that a symbol was not found in a binding.
func (NotBoundError) Error ¶
func (e NotBoundError) Error() string
type NotCallableError ¶
NotCallableError signals that a value cannot be called when it must be called.
func (NotCallableError) Error ¶
func (e NotCallableError) Error() string
func (NotCallableError) String ¶
func (e NotCallableError) String() string
type ObjExpr ¶
ObjExpr returns the stored object.
func (ObjExpr) Improve ¶
func (oe ObjExpr) Improve(re *ReworkEnvironment) Expr
type ParseEnvironment ¶
type ParseEnvironment struct {
// contains filtered or unexported fields
}
ParseEnvironment is a parsing environment.
func (*ParseEnvironment) Bind ¶
func (*ParseEnvironment) Binding ¶
func (pf *ParseEnvironment) Binding() *Binding
func (*ParseEnvironment) MakeChildFrame ¶
func (pf *ParseEnvironment) MakeChildFrame(name string, baseSize int) *ParseEnvironment
func (*ParseEnvironment) Parse ¶
func (pf *ParseEnvironment) Parse(form sx.Object) (expr Expr, err error)
func (*ParseEnvironment) ParseAgain ¶
func (pf *ParseEnvironment) ParseAgain(form sx.Object) error
type ParseObserver ¶
type ParseObserver interface { // BeforeParse is called immediate before the given form is parsed. // The observer may change the form and abort the parse with an error. BeforeParse(*ParseEnvironment, sx.Object) (sx.Object, error) // AfterParse is called immediate after the given form was parsed to the expression. AfterParse(*ParseEnvironment, sx.Object, Expr, error) }
ParseObserver monitors the parsing process.
type ResolveSymbolExpr ¶
type ResolveSymbolExpr struct {
// contains filtered or unexported fields
}
ResolveSymbolExpr is a special `UnboundSymbolExpr` that must be resolved in the base environment. Traversal through all nested lexical bindings is not needed.
func (ResolveSymbolExpr) Compute ¶
func (use ResolveSymbolExpr) Compute(env *Environment) (sx.Object, error)
func (ResolveSymbolExpr) GetSymbol ¶
func (rse ResolveSymbolExpr) GetSymbol() *sx.Symbol
func (ResolveSymbolExpr) Improve ¶
func (rse ResolveSymbolExpr) Improve(re *ReworkEnvironment) Expr
func (ResolveSymbolExpr) Unparse ¶
func (rse ResolveSymbolExpr) Unparse() sx.Object
type ReworkEnvironment ¶
type ReworkEnvironment struct {
// contains filtered or unexported fields
}
ReworkEnvironment guides the Expr.Rework operation.
func (*ReworkEnvironment) Bind ¶
func (re *ReworkEnvironment) Bind(sym *sx.Symbol) error
Bind the undefined value to the symbol in the current environment.
func (*ReworkEnvironment) Binding ¶
func (re *ReworkEnvironment) Binding() *Binding
Binding returns the binding of this environment.
func (*ReworkEnvironment) Call ¶
Call a function for constant folding.
It is only called, if no full execution environment is needed, only a binding.
func (*ReworkEnvironment) Height ¶
func (re *ReworkEnvironment) Height() int
Height returns the difference between the acual and the base height.
func (*ReworkEnvironment) MakeChildEnvironment ¶
func (re *ReworkEnvironment) MakeChildEnvironment(name string, baseSize int) *ReworkEnvironment
MakeChildEnvironment creates a subordinate rework environment with a new binding.
func (*ReworkEnvironment) Resolve ¶
Resolve the symbol into an object, and return the binding depth plus an indication about the const-ness of the value. If the symbol could not be resolved, depth has the value of `math.MinInt`. If the symbol was found in the base environment, depth is set to -1, to indicate a possible unbound situation.
func (*ReworkEnvironment) Rework ¶
func (re *ReworkEnvironment) Rework(expr Expr) Expr
Rework the given expression. Do not call `expr.Rework()` directly.
type ReworkObserver ¶
type ReworkObserver interface { // BeforeRework is called immediate before the given expression is reworked. BeforeRework(*ReworkEnvironment, Expr) Expr // AfterRework is called after the given expression was reworked to a // possibly simpler one. AfterRework(*ReworkEnvironment, Expr, Expr) }
ReworkObserver monitors the inner workings of the rework process.
type Special ¶
Special represents all predefined syntax constructing functions implemented in Go.
func (*Special) Parse ¶
Parse the args by calling the syntax function.
type SymbolExpr ¶
SymbolExpr is the common interface of Expr that handles symbols.
type Syntax ¶
type Syntax interface { // Parse the args. Parse(pf *ParseEnvironment, args *sx.Pair) (Expr, error) }
Syntax is a form that produces an expression by parsing.
It is not the same as interface `Parser`, because the second parameter is a pair.
type UnboundSymbolExpr ¶
type UnboundSymbolExpr struct {
// contains filtered or unexported fields
}
UnboundSymbolExpr resolves the given symbol in an environment and returns its value.
func (UnboundSymbolExpr) Compute ¶
func (use UnboundSymbolExpr) Compute(env *Environment) (sx.Object, error)
func (UnboundSymbolExpr) GetSymbol ¶
func (use UnboundSymbolExpr) GetSymbol() *sx.Symbol
func (UnboundSymbolExpr) Improve ¶
func (use UnboundSymbolExpr) Improve(re *ReworkEnvironment) Expr
func (UnboundSymbolExpr) Unparse ¶
func (use UnboundSymbolExpr) Unparse() sx.Object