mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2024-11-25 07:38:57 +00:00
59 lines
1.1 KiB
Go
59 lines
1.1 KiB
Go
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
|
|
package eval
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
//!+Check
|
|
|
|
func (v Var) Check(vars map[Var]bool) error {
|
|
vars[v] = true
|
|
return nil
|
|
}
|
|
|
|
func (literal) Check(vars map[Var]bool) error {
|
|
return nil
|
|
}
|
|
|
|
func (u unary) Check(vars map[Var]bool) error {
|
|
if !strings.ContainsRune("+-", u.op) {
|
|
return fmt.Errorf("unexpected unary op %q", u.op)
|
|
}
|
|
return u.x.Check(vars)
|
|
}
|
|
|
|
func (b binary) Check(vars map[Var]bool) error {
|
|
if !strings.ContainsRune("+-*/", b.op) {
|
|
return fmt.Errorf("unexpected binary op %q", b.op)
|
|
}
|
|
if err := b.x.Check(vars); err != nil {
|
|
return err
|
|
}
|
|
return b.y.Check(vars)
|
|
}
|
|
|
|
func (c call) Check(vars map[Var]bool) error {
|
|
arity, ok := numParams[c.fn]
|
|
if !ok {
|
|
return fmt.Errorf("unknown function %q", c.fn)
|
|
}
|
|
if len(c.args) != arity {
|
|
return fmt.Errorf("call to %s has %d args, want %d",
|
|
c.fn, len(c.args), arity)
|
|
}
|
|
for _, arg := range c.args {
|
|
if err := arg.Check(vars); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
var numParams = map[string]int{"pow": 2, "sin": 1, "sqrt": 1}
|
|
|
|
//!-Check
|