eval

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2022 License: Apache-2.0 Imports: 5 Imported by: 0

README

GoDoc

A tiny evaluator of scheme expressions

demo

expression

  • define-expr
define "{" 
    var value-expr | 
    proc"(" [arg,] ")" 
    "{" 
         expr [, expr]
    "}" 
"}"
  • assign-expr
set var value-expr
  • if-expr
if predict-expr
    "{" 
    expr [, expr]
    "}"

  • lambda-expr
lambda"("[arg,] ")" "{" expr [,expr] "}" ["(" [,para] ")"]
  • application-expr
proc-name"("[,para]")" 
  • perform-expr
perform application-expr

Remark

Core of evaluator is interleaving of eval and apply loop. Everytime eval enconters a application it call apply to make actually calling. A compound procedure is a sequence of expression to be evaluated within a given environment.

Example

compute fibonacci with evaluator:

Eval-go INPUT:
define fib(n) { 
    if ==(n, 0) {0} 
    if ==(n, 1) {1} 
    +(fib(-(n,1)), fib(-(n,2))) 
    };
Eval-go INPUT:
fib(10);
Eval-go OUTPUT:
 55 

document

godoc

Documentation

Overview

evaluator

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplicationExpr

func ApplicationExpr(expr Expression) bool

func Apply

func Apply(app interface{}, args ...interface{}) interface{}

func AssignExpr

func AssignExpr(expr Expression) bool

func DefineExpr

func DefineExpr(expr Expression) bool

func Eval

func Eval(e *Expression, env *Environment) interface{}

func EvalArgs

func EvalArgs(exprs []Expression, env *Environment) []interface{}

func EvalAssign

func EvalAssign(e *Expression, env *Environment)

func EvalDefine

func EvalDefine(e *Expression, env *Environment)

func EvalIf

func EvalIf(e *Expression, env *Environment) (interface{}, bool)

func IfExpr

func IfExpr(expr Expression) bool

func InitGlobal

func InitGlobal()

func IsCompound

func IsCompound(app interface{}) bool

func IsEmpty

func IsEmpty(env *Environment) bool

func IsPrimitive

func IsPrimitive(app interface{}) bool

func LambdaApplicationExpr

func LambdaApplicationExpr(expr Expression) bool

func LambdaExpr

func LambdaExpr(expr Expression) bool

func NumberExpr

func NumberExpr(expr Expression) bool

func PerformExpr

func PerformExpr(expr Expression) bool
Example
InitGlobal()
text := `
	   define l list('a,'b,'c)
	   define printlist(l) {
		   perform print(car(l))
		   if not-null?(cdr(l)) {
				printlist(cdr(l))
		   }
	   }
	 	printlist(l)  
	   `
expr := MakeExpr(text)
Eval(expr, GlobalEnv)
Output:

abc

func ProcedureExpr

func ProcedureExpr(expr Expression) bool

func ProcedureParas

func ProcedureParas(expr Expression) ([]string, int)

func ProcedureVar

func ProcedureVar(expr Expression) string

func SymbolExpr

func SymbolExpr(expr Expression) bool

Types

type CompoundProcedure

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

func Compound

func Compound(app interface{}) CompoundProcedure

func EvalLambda

func EvalLambda(e *Expression, env *Environment) CompoundProcedure

func NewCompoundProdedure

func NewCompoundProdedure(paras []string, body Expression, env *Environment) CompoundProcedure

func (*CompoundProcedure) Body

func (c *CompoundProcedure) Body() Expression

func (*CompoundProcedure) Env

func (c *CompoundProcedure) Env() *Environment

func (*CompoundProcedure) Paras

func (c *CompoundProcedure) Paras() []string

type Environment

type Environment struct {
	Enclose *Environment
	Frame   Frame
}
Example
vars := []string{"a", "b", "c"}
val := []interface{}{"a", 3.14, 1}

env := ExtendEnv(GlobalEnv, vars, val)
env.SetVariable("b", "b")
env.DefineVariable("d", "d")
vars = append(vars, "d")
for _, va := range vars {
	val := env.LookUpVariable(va)
	fmt.Println(val)
}
Output:

a
b
1
d
var GlobalEnv *Environment

func ExtendEnv

func ExtendEnv(enclose *Environment, vars []string, vals []interface{}) *Environment

func (*Environment) DefineVariable

func (env *Environment) DefineVariable(v string, val interface{})

func (*Environment) LookUpVariable

func (env *Environment) LookUpVariable(v string) interface{}

func (*Environment) SetVariable

func (env *Environment) SetVariable(v string, val interface{})

type Expression

type Expression []string

func ApplicationName

func ApplicationName(expr Expression) Expression

func ApplicationaParas

func ApplicationaParas(expr Expression) ([]Expression, int)

func ExtractParas

func ExtractParas(e Expression) (Expression, int)

func IfBody

func IfBody(expr Expression) (Expression, int)

func MakeExpr

func MakeExpr(text string) *Expression

func NextBlock

func NextBlock(e Expression) (Expression, int)

func NextSegment

func NextSegment(e Expression, start, end string) (Expression, int)

func ProcedureBody

func ProcedureBody(expr Expression) (Expression, int)

func (Expression) Advance

func (e Expression) Advance(n int) Expression

func (Expression) Rest

func (e Expression) Rest() Expression

type Frame

type Frame map[string]interface{}

func NewFrame

func NewFrame(vars []string, vals []interface{}) Frame

type Procedure

type Procedure func(...interface{}) interface{}

func Primitive

func Primitive(app interface{}) Procedure

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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