69 lines
1.5 KiB
Go
69 lines
1.5 KiB
Go
package engine
|
|
|
|
import "fmt"
|
|
|
|
// Operator is a scalar comparison used by generalised constraints like
|
|
// Altitude and Time. A constraint fires when its Operator.Test(value, limit)
|
|
// returns true.
|
|
type Operator int
|
|
|
|
const (
|
|
OpLess Operator = iota // value < limit
|
|
OpLessEqual // value ≤ limit
|
|
OpGreater // value > limit
|
|
OpGreaterEqual // value ≥ limit
|
|
OpEqual // value == limit
|
|
)
|
|
|
|
// Test evaluates op(value, limit).
|
|
func (o Operator) Test(value, limit float64) bool {
|
|
switch o {
|
|
case OpLess:
|
|
return value < limit
|
|
case OpLessEqual:
|
|
return value <= limit
|
|
case OpGreater:
|
|
return value > limit
|
|
case OpGreaterEqual:
|
|
return value >= limit
|
|
case OpEqual:
|
|
return value == limit
|
|
}
|
|
return false
|
|
}
|
|
|
|
// String returns the symbol "<", "<=", ">", ">=", "==".
|
|
func (o Operator) String() string {
|
|
switch o {
|
|
case OpLess:
|
|
return "<"
|
|
case OpLessEqual:
|
|
return "<="
|
|
case OpGreater:
|
|
return ">"
|
|
case OpGreaterEqual:
|
|
return ">="
|
|
case OpEqual:
|
|
return "=="
|
|
}
|
|
return "?"
|
|
}
|
|
|
|
// ParseOperator maps a textual operator to its Operator constant.
|
|
// Accepts "<", "<=", "le", ">", ">=", "ge", "==", "eq".
|
|
func ParseOperator(s string) (Operator, error) {
|
|
switch s {
|
|
case "<", "lt":
|
|
return OpLess, nil
|
|
case "<=", "le":
|
|
return OpLessEqual, nil
|
|
case ">", "gt":
|
|
return OpGreater, nil
|
|
case ">=", "ge":
|
|
return OpGreaterEqual, nil
|
|
case "==", "eq":
|
|
return OpEqual, nil
|
|
default:
|
|
return 0, fmt.Errorf("unknown operator %q", s)
|
|
}
|
|
}
|