mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2024-11-24 15:18:57 +00:00
71 lines
1.2 KiB
Go
71 lines
1.2 KiB
Go
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
|
|
// See page 198.
|
|
|
|
// Package eval provides an expression evaluator.
|
|
package eval
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
)
|
|
|
|
//!+env
|
|
|
|
type Env map[Var]float64
|
|
|
|
//!-env
|
|
|
|
//!+Eval1
|
|
|
|
func (v Var) Eval(env Env) float64 {
|
|
return env[v]
|
|
}
|
|
|
|
func (l literal) Eval(_ Env) float64 {
|
|
return float64(l)
|
|
}
|
|
|
|
//!-Eval1
|
|
|
|
//!+Eval2
|
|
|
|
func (u unary) Eval(env Env) float64 {
|
|
switch u.op {
|
|
case '+':
|
|
return +u.x.Eval(env)
|
|
case '-':
|
|
return -u.x.Eval(env)
|
|
}
|
|
panic(fmt.Sprintf("unsupported unary operator: %q", u.op))
|
|
}
|
|
|
|
func (b binary) Eval(env Env) float64 {
|
|
switch b.op {
|
|
case '+':
|
|
return b.x.Eval(env) + b.y.Eval(env)
|
|
case '-':
|
|
return b.x.Eval(env) - b.y.Eval(env)
|
|
case '*':
|
|
return b.x.Eval(env) * b.y.Eval(env)
|
|
case '/':
|
|
return b.x.Eval(env) / b.y.Eval(env)
|
|
}
|
|
panic(fmt.Sprintf("unsupported binary operator: %q", b.op))
|
|
}
|
|
|
|
func (c call) Eval(env Env) float64 {
|
|
switch c.fn {
|
|
case "pow":
|
|
return math.Pow(c.args[0].Eval(env), c.args[1].Eval(env))
|
|
case "sin":
|
|
return math.Sin(c.args[0].Eval(env))
|
|
case "sqrt":
|
|
return math.Sqrt(c.args[0].Eval(env))
|
|
}
|
|
panic(fmt.Sprintf("unsupported function call: %s", c.fn))
|
|
}
|
|
|
|
//!-Eval2
|