mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2024-11-19 12:53:41 +00:00
41 lines
940 B
Go
41 lines
940 B
Go
|
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||
|
|
||
|
package eval
|
||
|
|
||
|
// An Expr is an arithmetic expression.
|
||
|
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
|
||
|
}
|
||
|
|
||
|
//!+ast
|
||
|
|
||
|
// A Var identifies a variable, e.g., x.
|
||
|
type Var string
|
||
|
|
||
|
// A literal is a numeric constant, e.g., 3.141.
|
||
|
type literal float64
|
||
|
|
||
|
// A unary represents a unary operator expression, e.g., -x.
|
||
|
type unary struct {
|
||
|
op rune // one of '+', '-'
|
||
|
x Expr
|
||
|
}
|
||
|
|
||
|
// A binary represents a binary operator expression, e.g., x+y.
|
||
|
type binary struct {
|
||
|
op rune // one of '+', '-', '*', '/'
|
||
|
x, y Expr
|
||
|
}
|
||
|
|
||
|
// A call represents a function call expression, e.g., sin(x).
|
||
|
type call struct {
|
||
|
fn string // one of "pow", "sin", "sqrt"
|
||
|
args []Expr
|
||
|
}
|
||
|
|
||
|
//!-ast
|