diff --git a/app/calc.go b/app/calc.go index e19d6b5..0137138 100644 --- a/app/calc.go +++ b/app/calc.go @@ -96,21 +96,21 @@ func (c *Calc) Raise(err error) { func (c *Calc) Label() string { if c.stack.Len() == 0 { - panic(msg.ErrStackEmpty()) + panic(msg.ErrStackEmpty) } return c.stack.Get(0).Label } func (c *Calc) Unit() string { if c.stack.Len() == 0 { - panic(msg.ErrStackEmpty()) + panic(msg.ErrStackEmpty) } return c.stack.Get(0).Unit } func (c *Calc) SetLabel(label string) { if c.stack.Len() == 0 { - panic(msg.ErrStackEmpty()) + panic(msg.ErrStackEmpty) } item := c.stack.Get(0) item.Label = label @@ -119,7 +119,7 @@ func (c *Calc) SetLabel(label string) { func (c *Calc) SetUnit(unit string) { if c.stack.Len() == 0 { - panic(msg.ErrStackEmpty()) + panic(msg.ErrStackEmpty) } item := c.stack.Get(0) item.Unit = unit @@ -150,7 +150,7 @@ func (c *Calc) Reset() { c.temp.Clear() clear(c.mem) clear(c.state) - c.Notify(msg.Reset()) + c.Notify(msg.Reset) } func (c *Calc) Eval(line string) error { @@ -321,5 +321,5 @@ func (c *Calc) errTypeMismatch(op zc.Op) error { return msg.ErrUnexpectedType(arg.Val()) } } - return msg.ErrNotEnoughArgs() + return msg.ErrNotEnoughArgs } diff --git a/app/funcs/basic.go b/app/funcs/basic.go index ea9d8d6..a4e5137 100644 --- a/app/funcs/basic.go +++ b/app/funcs/basic.go @@ -105,7 +105,7 @@ func DivBigInt(c zc.Calc) { defer zc.BigInt.Recycle(y) if y.Cmp(&zero) == 0 { - c.Raise(msg.ErrDivisionByZero()) + c.Raise(msg.ErrDivisionByZero) return } x.Div(x, y) @@ -127,7 +127,7 @@ func DivDecimal(c zc.Calc) { cond, err := d.Quo(x, x, y) if cond.DivisionByZero() { - c.Raise(msg.ErrDivisionByZero()) + c.Raise(msg.ErrDivisionByZero) return } if err != nil { @@ -144,7 +144,7 @@ func DivRat(c zc.Calc) { defer zc.Rat.Recycle(y) if y.Cmp(&zero) == 0 { - c.Raise(msg.ErrDivisionByZero()) + c.Raise(msg.ErrDivisionByZero) return } @@ -161,7 +161,7 @@ func DivModBigInt(c zc.Calc) { defer zc.BigInt.Recycle(y) if y.Cmp(&zero) == 0 { - c.Raise(msg.ErrDivisionByZero()) + c.Raise(msg.ErrDivisionByZero) return } x.DivMod(x, y, m) @@ -245,7 +245,7 @@ func ModBigInt(c zc.Calc) { defer zc.BigInt.Recycle(y) if y.Cmp(&zero) == 0 { - c.Raise(msg.ErrDivisionByZero()) + c.Raise(msg.ErrDivisionByZero) return } x.Mod(x, y) @@ -358,7 +358,7 @@ func RemBigInt(c zc.Calc) { defer zc.BigInt.Recycle(y) if y.Cmp(&zero) == 0 { - c.Raise(msg.ErrDivisionByZero()) + c.Raise(msg.ErrDivisionByZero) return } x.Rem(x, y) @@ -372,7 +372,7 @@ func RemDecimal(c zc.Calc) { defer zc.Decimal.Recycle(y) if y.IsZero() { - c.Raise(msg.ErrDivisionByZero()) + c.Raise(msg.ErrDivisionByZero) return } _, err := d.Rem(x, x, y) @@ -484,7 +484,7 @@ func QuoBigInt(c zc.Calc) { defer zc.BigInt.Recycle(y) if y.Cmp(&zero) == 0 { - c.Raise(msg.ErrDivisionByZero()) + c.Raise(msg.ErrDivisionByZero) return } x.Quo(x, y) @@ -500,7 +500,7 @@ func QuoRemBigInt(c zc.Calc) { defer zc.BigInt.Recycle(y) if y.Cmp(&zero) == 0 { - c.Raise(msg.ErrDivisionByZero()) + c.Raise(msg.ErrDivisionByZero) return } x.QuoRem(x, y, r) diff --git a/app/funcs/dec.go b/app/funcs/dec.go index 7ef895b..cc66160 100644 --- a/app/funcs/dec.go +++ b/app/funcs/dec.go @@ -20,7 +20,7 @@ func DecRat(c zc.Calc) { d := zc.Decimal.New() d.SetFloat64(f) if !exact { - c.Notify(msg.Inexact()) + c.Notify(msg.Inexact) } zc.Decimal.Push(c, d) } diff --git a/app/funcs/float.go b/app/funcs/float.go index 12c3388..3be588f 100644 --- a/app/funcs/float.go +++ b/app/funcs/float.go @@ -45,7 +45,7 @@ func DivBigFloat(c zc.Calc) { x := zc.BigFloat.Pop(c) if y.Cmp(&zero) == 0 { - c.Raise(msg.ErrDivisionByZero()) + c.Raise(msg.ErrDivisionByZero) return } @@ -58,7 +58,7 @@ func DivFloat32(c zc.Calc) { y := zc.Float32.Pop(c) x := zc.Float32.Pop(c) if y == 0 { - c.Raise(msg.ErrDivisionByZero()) + c.Raise(msg.ErrDivisionByZero) return } zc.Float32.Push(c, x/y) @@ -68,7 +68,7 @@ func DivFloat64(c zc.Calc) { y := zc.Float64.Pop(c) x := zc.Float64.Pop(c) if y == 0 { - c.Raise(msg.ErrDivisionByZero()) + c.Raise(msg.ErrDivisionByZero) return } zc.Float64.Push(c, x/y) diff --git a/app/funcs/hof.go b/app/funcs/hof.go index 653f0f4..cc0a30d 100644 --- a/app/funcs/hof.go +++ b/app/funcs/hof.go @@ -12,7 +12,7 @@ func Apply(c zc.Calc) { nArgs := int(zc.Uint32.Pop(c)) opName := zc.String.Pop(c) if c.Len() < nArgs { - c.Raise(msg.ErrNotEnoughArgs()) + c.Raise(msg.ErrNotEnoughArgs) return } var args []string @@ -36,7 +36,7 @@ func Filter(c zc.Calc) { dc.Push(v) dc.Eval(opName) if dc.Len() == 0 { - c.Raise(msg.ErrNoReturnValues()) + c.Raise(msg.ErrNoReturnValues) return } out := dc.Pop() @@ -60,7 +60,7 @@ func Fold(c zc.Calc) { return } if c.Len() >= before { - c.Raise(msg.ErrDoesNotReduce()) + c.Raise(msg.ErrDoesNotReduce) return } } diff --git a/app/funcs/stack.go b/app/funcs/stack.go index 57096d0..66141ed 100644 --- a/app/funcs/stack.go +++ b/app/funcs/stack.go @@ -13,19 +13,19 @@ func Clear(c zc.Calc) { func Copy(c zc.Calc) { if c.Len() == 0 { - c.Raise(msg.ErrStackEmpty()) + c.Raise(msg.ErrStackEmpty) return } s := zc.DupItems(c.Stack()) t := c.Temp() t = append(t, s...) c.SetTemp(t) - c.Notify(msg.Copied()) + c.Notify(msg.Copied) } func Down(c zc.Calc) { if c.Len() == 0 { - c.Raise(msg.ErrStackEmpty()) + c.Raise(msg.ErrStackEmpty) return } if c.Len() == 1 { @@ -67,7 +67,7 @@ func Load(c zc.Calc) { func Pop(c zc.Calc) { t := c.Temp() if len(t) == 0 { - c.Raise(msg.ErrStackEmpty()) + c.Raise(msg.ErrStackEmpty) return } l := len(t) @@ -80,7 +80,7 @@ func PopAll(c zc.Calc) { s := c.Stack() t := c.Temp() if len(t) == 0 { - c.Raise(msg.ErrStackEmpty()) + c.Raise(msg.ErrStackEmpty) return } s = append(s, t...) @@ -90,7 +90,7 @@ func PopAll(c zc.Calc) { func Push(c zc.Calc) { if c.Len() == 0 { - c.Raise(msg.ErrStackEmpty()) + c.Raise(msg.ErrStackEmpty) return } x := c.Pop() @@ -101,7 +101,7 @@ func Push(c zc.Calc) { func PushAll(c zc.Calc) { if c.Len() == 0 { - c.Raise(msg.ErrStackEmpty()) + c.Raise(msg.ErrStackEmpty) return } s := c.Stack() @@ -133,7 +133,7 @@ func Size(c zc.Calc) { func Store(c zc.Calc) { n := zc.String.Pop(c) c.Store(n) - c.Notify(msg.Stored()) + c.Notify(msg.Stored) } func Swap(c zc.Calc) { diff --git a/app/funcs/time.go b/app/funcs/time.go index 507bdc6..0462089 100644 --- a/app/funcs/time.go +++ b/app/funcs/time.go @@ -109,7 +109,7 @@ func NowSet(c zc.Calc) { func NowReset(c zc.Calc) { v := vars.ForTime(c) v.Now = func() time.Time { return time.Now() } - c.Notify(msg.Reset()) + c.Notify(msg.Reset) } func SecondsTime(c zc.Calc) { diff --git a/app/repl/commands.go b/app/repl/commands.go index 384c1c8..b32f3e7 100644 --- a/app/repl/commands.go +++ b/app/repl/commands.go @@ -78,7 +78,7 @@ func redo(r *Repl, _ []scan.Token) error { func reset(r *Repl, _ []scan.Token) error { r.Calc.Reset() - r.Calc.Notify(msg.Reset()) + r.Calc.Notify(msg.Reset) return nil } diff --git a/math.go b/math.go index 8e349c0..0c18b62 100644 --- a/math.go +++ b/math.go @@ -26,7 +26,7 @@ func IsFloatErr(c Calc, f float64) bool { c.Raise(msg.ErrInfinity(1)) return true case math.IsNaN(f): - c.Raise(msg.ErrNotANumber()) + c.Raise(msg.ErrNotANumber) return true default: return false diff --git a/msg/error.go b/msg/error.go index fcf3410..a9d6026 100644 --- a/msg/error.go +++ b/msg/error.go @@ -7,90 +7,62 @@ import ( "strings" ) -func ErrDivisionByZero() error { - return errors.New("division by zero") -} - -func ErrDoesNotReduce() error { - return fmt.Errorf("does not reduce") -} - -func ErrFeatureNotSupported(name string) error { - return fmt.Errorf("feature not supported: %v", name) -} - -func ErrIndexOutOfRange(i int) error { - return fmt.Errorf("index out of range: %v", i) -} - -func ErrInfinity(sign int) error { - var inf string - switch { - case sign < 0: - inf = "-infinity" - case sign > 0: - inf = "+infinity" - default: - inf = "infinity" +var ( + ErrDivisionByZero = errors.New("division by zero") + ErrDoesNotReduce = errors.New("does not reduce") + ErrFeatureNotSupported = func(name string) error { + return fmt.Errorf("feature not supported: %v", name) } - return errors.New(inf) -} - -func ErrInvalidRoundingMode(m string) error { - return fmt.Errorf("invalid rounding mode: %v", m) -} - -func ErrInvalidArg(format string, args ...any) error { - return fmt.Errorf("invalid argument, %v", fmt.Sprintf(format, args...)) -} - -func ErrMemoryEmpty(name string) error { - return fmt.Errorf("memory empty: %v", name) -} - -func ErrNoReturnValues() error { - return errors.New("no return values") -} - -func ErrNoSuchOp(name string) error { - return fmt.Errorf("no such operation: %v", name) -} - -func ErrNotANumber() error { - return errors.New("not a number") -} - -func ErrNotEnoughArgs() error { - return errors.New("not enough arguments") -} - -func ErrOp(name string, err error) error { - return fmt.Errorf("%v: %v", name, err) -} - -func ErrOverflow(val string) error { - return fmt.Errorf("overflow: %v", Abbr(val)) -} - -func ErrStackEmpty() error { - return errors.New("stack empty") -} - -func ErrUnderflow(val string) error { - return fmt.Errorf("underflow: %v", Abbr(val)) -} - -func ErrUnexpectedType(val string) error { - return fmt.Errorf("unexpected type: %v", Quote(Abbr(val))) -} - -func ErrUnknownTimeZone(zone string) error { - return fmt.Errorf("unknown time zone: %v", Quote(zone)) -} - -func ErrWrongGoType(want string, have any) error { - return fmt.Errorf("expected type %v, got: %v of type %v", want, Quote(fmt.Sprint(have)), goName(have)) -} + ErrIndexOutOfRange = func(i int) error { + return fmt.Errorf("index out of range: %v", i) + } + ErrInfinity = func(sign int) error { + var inf string + switch { + case sign < 0: + inf = "-infinity" + case sign > 0: + inf = "+infinity" + default: + inf = "infinity" + } + return errors.New(inf) + } + ErrInvalidRoundingMode = func(m string) error { + return fmt.Errorf("invalid rounding mode: %v", m) + } + ErrInvalidArg = func(format string, args ...any) error { + return fmt.Errorf("invalid argument, %v", fmt.Sprintf(format, args...)) + } + ErrMemoryEmpty = func(name string) error { + return fmt.Errorf("memory empty: %v", name) + } + ErrNoReturnValues = errors.New("no return values") + ErrNoSuchOp = func(name string) error { + return fmt.Errorf("no such operation: %v", name) + } + ErrNotANumber = errors.New("not a number") + ErrNotEnoughArgs = errors.New("not enough arguments") + ErrOp = func(name string, err error) error { + return fmt.Errorf("%v: %v", name, err) + } + ErrOverflow = func(val string) error { + return fmt.Errorf("overflow: %v", Abbr(val)) + } + ErrStackEmpty = errors.New("stack empty") + ErrUnderflow = func(val string) error { + return fmt.Errorf("underflow: %v", Abbr(val)) + } + ErrUnexpectedType = func(val string) error { + return fmt.Errorf("unexpected type: %v", Quote(Abbr(val))) + } + ErrUnknownTimeZone = func(zone string) error { + return fmt.Errorf("unknown time zone: %v", Quote(zone)) + } + ErrWrongGoType = func(want string, have any) error { + return fmt.Errorf("expected type %v, got: %v of type %v", want, Quote(fmt.Sprint(have)), goName(have)) + } +) func goName(v any) string { if v == nil { diff --git a/msg/notice.go b/msg/notice.go index 7136f75..69d2719 100644 --- a/msg/notice.go +++ b/msg/notice.go @@ -5,38 +5,24 @@ import ( "math/big" ) -func Copied() string { - return "copied" -} - -func Inexact() string { - return "inexact" -} - -func LocalTimeZoneSet(z string) string { - return fmt.Sprintf("local time zone is now %v", z) -} - -func NowSet(dt string) string { - return fmt.Sprintf("now set to %v", Quote(dt)) -} - -func PrecisionSet(p uint) string { - return fmt.Sprintf("precision set to %v", p) -} - -func RoundingModeSet(m string) string { - return fmt.Sprintf("rounding mode set to %v", m) -} - -func Reset() string { - return "reset" -} - -func SeedSet(s *big.Int) string { - return fmt.Sprintf("seed set to %v", s) -} - -func Stored() string { - return "stored" -} +var ( + Copied = "copied" + Inexact = "inexact" + LocalTimeZoneSet = func(z string) string { + return fmt.Sprintf("local time zone is now %v", z) + } + NowSet = func(dt string) string { + return fmt.Sprintf("now set to %v", Quote(dt)) + } + PrecisionSet = func(p uint) string { + return fmt.Sprintf("precision set to %v", p) + } + RoundingModeSet = func(m string) string { + return fmt.Sprintf("rounding mode set to %v", m) + } + Reset = "reset" + SeedSet = func(s *big.Int) string { + return fmt.Sprintf("seed set to %v", s) + } + Stored = "stored" +) diff --git a/types.go b/types.go index 91e01e2..4f26d23 100644 --- a/types.go +++ b/types.go @@ -428,7 +428,7 @@ func (t DecimalType) Push(c Calc, val *apd.Decimal) { // FIXME: Does the Sign have the direction? c.Raise(msg.ErrInfinity(0)) case apd.NaN: - c.Raise(msg.ErrNotANumber()) + c.Raise(msg.ErrNotANumber) default: c.Push(Item{TypeVal: val, Type: t}) } @@ -659,7 +659,7 @@ func (t Float64Type) As(a any) float64 { func (t Float64Type) Push(c Calc, val float64) { switch { case math.IsNaN(val): - c.Raise(msg.ErrNotANumber()) + c.Raise(msg.ErrNotANumber) case math.IsInf(val, 1): c.Raise(msg.ErrInfinity(1)) case math.IsInf(val, -1): @@ -709,7 +709,7 @@ func (t Float32Type) Push(c Calc, val float32) { val64 := float64(val) switch { case math.IsNaN(val64): - c.Raise(msg.ErrNotANumber()) + c.Raise(msg.ErrNotANumber) case math.IsInf(val64, 1): c.Raise(msg.ErrInfinity(1)) case math.IsInf(val64, -1):