eval

package
v0.0.0-...-94c99f4 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2019 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Define a new concrete type that satisfies the `Expr` interface and provides a new operation such as computing the minimum value of its operands. Since the `Parse` function does not create instances of this new type, to use it you will need to construct a syntax tree directly (or extend the parser).

Add a `String` method to `Expr` to pretty-print the syntax tree. Check that the results, when parsed again, yield an equivalent tree.

Package eval provides an expression evaluator.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format

func Format(e Expr) string

Format formats an expression as a string. It does not attempt to remove unnecessary parens.

Types

type Env

type Env map[Var]float64

type Expr

type Expr interface {
	// Eval returns the value of this Expr in the environment env.
	Eval(env Env) float64
	// Check reports errors in this Expr and adds its Vars to the set.
	Check(vars map[Var]bool) error
	// Pretty-print the syntax tree
	String() string
	// Brace() reports if this Expr has an enclosing brace
	Brace() bool
	// Vars() returns a slice of Var types representing all variables in the expression.
	Vars() []Var
}

An Expr is an arithmetic expression.

func Parse

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

Parse parses the input string as an arithmetic expression.

expr = num                         a literal number, e.g., 3.14159
     | id                          a variable name, e.g., x
     | id '(' expr ',' ... ')'     a function call
     | '-' expr                    a unary operator (+-)
     | expr '+' expr               a binary operator (+-*/)

type Var

type Var string

A Var identifies a variable, e.g., x.

func (Var) Brace

func (v Var) Brace() bool

func (Var) Check

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

func (Var) Eval

func (v Var) Eval(env Env) float64

func (Var) String

func (v Var) String() string

func (Var) Vars

func (v Var) Vars() []Var

Jump to

Keyboard shortcuts

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