eval

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 31, 2023 License: EUPL-1.2 Imports: 6 Imported by: 1

Documentation

Overview

Package eval 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

Constants

This section is empty.

Variables

View Source
var FalseExpr = falseExpr{}

FalseExpr returns always False

View Source
var NilExpr = nilExpr{}

NilExpr returns always Nil

View Source
var TrueExpr = trueExpr{}

TrueExpr returns always True

Functions

This section is empty.

Types

type Builtin

type Builtin interface {
	sxpf.Object
	Callable

	Name(*Engine) string
}

Builtin is a callable with a name

type BuiltinA

type BuiltinA func([]sxpf.Object) (sxpf.Object, error)

BuiltinA is the signature of all normal builtin functions.

func (BuiltinA) Call

func (b BuiltinA) Call(eng *Engine, _ sxpf.Environment, args []sxpf.Object) (sxpf.Object, error)

Call the builtin function.

func (BuiltinA) IsAtom

func (b BuiltinA) IsAtom() bool

func (BuiltinA) IsEql

func (b BuiltinA) IsEql(other sxpf.Object) bool

func (BuiltinA) IsEqual

func (b BuiltinA) IsEqual(other sxpf.Object) bool

func (BuiltinA) IsNil

func (b BuiltinA) IsNil() bool

func (BuiltinA) Name

func (b BuiltinA) Name(eng *Engine) string

func (BuiltinA) Print

func (b BuiltinA) Print(w io.Writer) (int, error)

func (BuiltinA) Repr

func (b BuiltinA) Repr() string

func (BuiltinA) String

func (b BuiltinA) String() string

type BuiltinCallExpr

type BuiltinCallExpr struct {
	Proc Builtin
	Args []Expr
}

BuiltinCallExpr calls a builtin and returns the resulting object. It is an optimization of `CallExpr.`

func (*BuiltinCallExpr) Compute

func (bce *BuiltinCallExpr) Compute(eng *Engine, env sxpf.Environment) (sxpf.Object, error)

func (*BuiltinCallExpr) String

func (bce *BuiltinCallExpr) String() string

type BuiltinEEA

type BuiltinEEA func(*Engine, sxpf.Environment, []sxpf.Object) (sxpf.Object, error)

BuiltinEEA is the signature of builtin functions that use all information, engine, environment, and arguments.

func (BuiltinEEA) Call

func (b BuiltinEEA) Call(eng *Engine, env sxpf.Environment, args []sxpf.Object) (sxpf.Object, error)

Call the builtin function.

func (BuiltinEEA) IsAtom

func (b BuiltinEEA) IsAtom() bool

func (BuiltinEEA) IsEql

func (b BuiltinEEA) IsEql(other sxpf.Object) bool

func (BuiltinEEA) IsEqual

func (b BuiltinEEA) IsEqual(other sxpf.Object) bool

func (BuiltinEEA) IsNil

func (b BuiltinEEA) IsNil() bool

func (BuiltinEEA) Name

func (b BuiltinEEA) Name(eng *Engine) string

func (BuiltinEEA) Print

func (b BuiltinEEA) Print(w io.Writer) (int, error)

func (BuiltinEEA) Repr

func (b BuiltinEEA) Repr() string

func (BuiltinEEA) String

func (b BuiltinEEA) String() string

type CallError

type CallError struct {
	Name string
	Err  error
}

CallError encapsulate an error that occured during a call.

func (CallError) Error

func (e CallError) Error() string

func (CallError) Unwrap

func (e CallError) Unwrap() error

type CallExpr

type CallExpr struct {
	Proc Expr
	Args []Expr
}

CallExpr calls a procedure and returns the resulting objects.

func (*CallExpr) Compute

func (ce *CallExpr) Compute(eng *Engine, env sxpf.Environment) (sxpf.Object, error)

func (*CallExpr) String

func (ce *CallExpr) String() string

type Callable

type Callable interface {
	// Call the value with the given args and environment.
	Call(*Engine, sxpf.Environment, []sxpf.Object) (sxpf.Object, error)
}

Callable is a value that can be called for evaluation.

func GetCallable

func GetCallable(obj sxpf.Object) (Callable, bool)

GetCallable returns the object as a Callable, if possible.

type Engine

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

Engine is the collection of all relevant data element to execute / evaluate an object.

func MakeEngine

func MakeEngine(sf sxpf.SymbolFactory, root sxpf.Environment, parser Parser, executor Executor) *Engine

MakeEngine creates a new engine.

func (*Engine) Bind

func (eng *Engine) Bind(name string, obj sxpf.Object) error

Bind a given object to a symbol of the given name in the engine's root environment.

func (*Engine) BindBuiltinA

func (eng *Engine) BindBuiltinA(name string, fn BuiltinA) error

BindBuiltinA binds a standard builtin function to the given name in the engine's root environment.

func (*Engine) BindBuiltinEEA

func (eng *Engine) BindBuiltinEEA(name string, fn BuiltinEEA) error

BindBuiltinEEA binds a special builtin function to the given name in the engine's root environment.

func (*Engine) BindSyntax

func (eng *Engine) BindSyntax(name string, fn SyntaxFn) error

BindSyntax binds a syntax parser to the given name in the engine's root environment. It also binds the parser to the symbol directly.

func (*Engine) BuiltinName

func (eng *Engine) BuiltinName(b Builtin) string

BuiltinName returns the name of the given Builtin.

func (*Engine) Call

func (eng *Engine) Call(env sxpf.Environment, fn Callable, args []sxpf.Object) (sxpf.Object, error)

func (*Engine) Eval

func (eng *Engine) Eval(env sxpf.Environment, obj sxpf.Object) (sxpf.Object, error)

Eval parses the given object and executes it in the environment.

func (*Engine) Execute

func (eng *Engine) Execute(env sxpf.Environment, expr Expr) (sxpf.Object, error)

Execute the given expression in the given environment.

func (*Engine) ExecuteTCO

func (eng *Engine) ExecuteTCO(env sxpf.Environment, expr Expr) (sxpf.Object, error)

ExecuteTCO the given expression in the given environment, but tail-call optimized.

func (*Engine) GetToplevelEnv

func (eng *Engine) GetToplevelEnv() sxpf.Environment

GetToplevelEnv returns the current top-level environment.

func (*Engine) Parse

func (eng *Engine) Parse(env sxpf.Environment, obj sxpf.Object) (Expr, error)

Parse the given object in the given environment.

func (*Engine) RootEnvironment

func (eng *Engine) RootEnvironment() sxpf.Environment

RootEnvironment returns the root environment of the engine.

func (*Engine) SetToplevelEnv

func (eng *Engine) SetToplevelEnv(env sxpf.Environment) error

SetToplevelEnv sets the given environment as the top-level environment. It must be the root environment or a child of it.

func (*Engine) SymbolFactory

func (eng *Engine) SymbolFactory() sxpf.SymbolFactory

SymbolFactory returns the symbol factory of the engine.

type Executor

type Executor interface {
	// Execute the expression in an environment and return the result.
	// It may have side-effects, on the given environment, or on the
	// general environment of the system.
	Execute(*Engine, sxpf.Environment, Expr) (sxpf.Object, error)
}

Executor is about controlling the execution of expressions. Do not call the method `Expr.Execute` directly, call the executor to do this.

func MakeSimpleExecutor

func MakeSimpleExecutor() Executor

MakeSimpleExecutor creates a new executor which just executes expressions.

type Expr

type Expr interface {
	// Compute the expression in an environment and return the result.
	// It may have side-effects, on the given environment, or on the
	// general environment of the system.
	Compute(*Engine, sxpf.Environment) (sxpf.Object, error)
}

Expr are values that are executed for evaluation in an environment.

type NotBoundError

type NotBoundError struct {
	Env sxpf.Environment
	Sym *sxpf.Symbol
}

NotBoundError signals that a symbol was not found in an environment.

func (NotBoundError) Error

func (e NotBoundError) Error() string

type NotCallableError

type NotCallableError struct {
	Obj sxpf.Object
}

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 Parser

type Parser interface {
	Parse(*Engine, sxpf.Environment, sxpf.Object) (Expr, error)
}

Parser transform an object into an executable expression.

func MakeDefaultParser

func MakeDefaultParser() Parser

MakeDefaultParser creates a new default parser.

type ResolveExpr

type ResolveExpr struct {
	Symbol *sxpf.Symbol
}

ResolveExpr resolves the given symbol in an environment and returns the value.

func (ResolveExpr) Compute

func (re ResolveExpr) Compute(_ *Engine, env sxpf.Environment) (sxpf.Object, error)

type SelfExpr

type SelfExpr struct {
	Obj sxpf.Object
}

SelfExpr returns the stored object.

func (SelfExpr) Compute

func (se SelfExpr) Compute(*Engine, sxpf.Environment) (sxpf.Object, error)

type Syntax

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

Syntax represents all syntax constructing functions implemented in Go.

func GetSyntax

func GetSyntax(obj sxpf.Object) (*Syntax, bool)

GetSyntax returns the object as a syntax value if possible.

func MakeSyntax

func MakeSyntax(name string, fn SyntaxFn) *Syntax

MakeSyntax creates a new special function.

func (*Syntax) IsAtom

func (sy *Syntax) IsAtom() bool

func (*Syntax) IsEql

func (sy *Syntax) IsEql(other sxpf.Object) bool

func (*Syntax) IsEqual

func (sy *Syntax) IsEqual(other sxpf.Object) bool

func (*Syntax) IsNil

func (sy *Syntax) IsNil() bool

func (*Syntax) Parse

func (sy *Syntax) Parse(eng *Engine, env sxpf.Environment, args *sxpf.List) (Expr, error)

Parse the args by calling the syntax function.

func (*Syntax) Print

func (sy *Syntax) Print(w io.Writer) (int, error)

func (*Syntax) Repr

func (sy *Syntax) Repr() string

func (*Syntax) String

func (sy *Syntax) String() string

type SyntaxFn

type SyntaxFn func(*Engine, sxpf.Environment, *sxpf.List) (Expr, error)

SpecialFn is the signature of all syntax constructing functions.

Jump to

Keyboard shortcuts

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