eval

package
v0.0.0-...-2d0cd67 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2022 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Env

type Env map[Var]float64

对包含变量的表达式进行求值,需要一个上下文(environment)来把变量映射到数值

type Expr

type Expr interface {
	Eval(env Env) float64
	// Check 方法报告表达式中的错误,并把表达式中额变量加入 Vars 中
	// Check 的输入参数是一个 Var 集合,它收集在表达式中发现的变量名
	// 从逻辑来讲,这个集合应当是 Check 的输出结果而不是输入参数,
	// 但因为这个方法是递归的,在这种情况下使用参数更为方便
	// 调用方在最初时需要提供一个空的集合
	// 构建接口的时候可以不要定义Check方法,
	// 实现了上面的Eval方法之后,再考虑写一个Check方法来做检查
	Check(vars map[Var]bool) error
}

需要为每种变量表达式定义 Eval 方法来返回表达式在一个给定上下文下的值。 每个表达式都必须提供提供这个方法,把它加到Expr接口中去

func Parse

func Parse(input string) (_ Expr, err error)

type Var

type Var string

Var 表示一个变量,比如 x。可导出的

func (Var) Check

func (v Var) Check(vars map[Var]bool) error

Var 和 literal 的求值不可能出错,直接返回 nil

func (Var) Eval

func (v Var) Eval(env Env) float64

从 Env 中查询结果,如果变量不存在,返回值是0

Jump to

Keyboard shortcuts

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