Skip to content

Commit

Permalink
move text messages to msg package
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-mcgann committed Sep 19, 2024
1 parent e70c7b7 commit 7d04a0c
Show file tree
Hide file tree
Showing 27 changed files with 380 additions and 340 deletions.
25 changes: 13 additions & 12 deletions app/calc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/blackchip-org/scan"
"github.com/blackchip-org/zc/v6"
"github.com/blackchip-org/zc/v6/msg"
"github.com/blackchip-org/zc/v6/pkg/coll"
)

Expand Down Expand Up @@ -95,21 +96,21 @@ func (c *Calc) Raise(err error) {

func (c *Calc) Label() string {
if c.stack.Len() == 0 {
panic(zc.ErrStackEmpty)
panic(msg.ErrStackEmpty())
}
return c.stack.Get(0).Label
}

func (c *Calc) Unit() string {
if c.stack.Len() == 0 {
panic(zc.ErrStackEmpty)
panic(msg.ErrStackEmpty())
}
return c.stack.Get(0).Unit
}

func (c *Calc) SetLabel(label string) {
if c.stack.Len() == 0 {
panic(zc.ErrStackEmpty)
panic(msg.ErrStackEmpty())
}
item := c.stack.Get(0)
item.Label = label
Expand All @@ -118,7 +119,7 @@ func (c *Calc) SetLabel(label string) {

func (c *Calc) SetUnit(unit string) {
if c.stack.Len() == 0 {
panic(zc.ErrStackEmpty)
panic(msg.ErrStackEmpty())
}
item := c.stack.Get(0)
item.Unit = unit
Expand All @@ -138,7 +139,7 @@ func (c *Calc) Store(name string) {
func (c *Calc) Load(name string) {
s, ok := c.mem[name]
if !ok {
c.Raise(zc.ErrMemoryEmpty(name))
c.Raise(msg.ErrMemoryEmpty(name))
return
}
c.Push(s...)
Expand Down Expand Up @@ -187,7 +188,7 @@ func (c *Calc) evalValue(val string) {
func (c *Calc) evalName(name string) {
op, ok := c.Catalog.OpFor(name)
if !ok {
c.Raise(zc.ErrNoSuchOp(name))
c.Raise(msg.ErrNoSuchOp(name))
return
}
if len(op.Macro) > 0 {
Expand All @@ -197,10 +198,10 @@ func (c *Calc) evalName(name string) {
fn, ok, err := c.ResolveOp(op)
switch {
case err != nil:
c.Raise(zc.ErrOp(name, err))
c.Raise(msg.ErrOp(name, err))
return
case !ok:
c.Raise(zc.ErrOp(name, c.errTypeMismatch(op)))
c.Raise(msg.ErrOp(name, c.errTypeMismatch(op)))
return
}

Expand All @@ -213,13 +214,13 @@ func (c *Calc) evalName(name string) {
ok, err := c.isFuncMatch(fn.Returns, fn.VarReturn)
switch {
case err != nil:
c.Raise(zc.ErrOp(name, err))
c.Raise(msg.ErrOp(name, err))
return
case !ok:
panic("return mismatch: " + name)
}
} else {
c.Err = zc.ErrOp(name, c.Err)
c.Err = msg.ErrOp(name, c.Err)
}
}

Expand Down Expand Up @@ -317,8 +318,8 @@ func (c *Calc) errTypeMismatch(op zc.Op) error {
}
}
if !good {
return zc.ErrUnexpectedType(arg.Val())
return msg.ErrUnexpectedType(arg.Val())
}
}
return zc.ErrNotEnoughArgs
return msg.ErrNotEnoughArgs()
}
10 changes: 5 additions & 5 deletions app/defs/time.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ setup:
ops:
- name: add
title: Addition
subtitle: time or duration
subtitle: time or duration
ident: AddTime
funcs:
- name: AddDuration
Expand Down Expand Up @@ -133,17 +133,17 @@ ops:
- i: now time
o: [3:04:05pm -0700 MST]
- i: c /est local.zone
notice: local time zone is now 'EST'
notice: local time zone is now EST
- i: now time
o: [5:04:05pm -0500 EST]
- i: c /Asia/Jakarta local.zone
notice: local time zone is now 'Asia/Jakarta'
notice: local time zone is now Asia/Jakarta
- i: now time
o: [5:04:05am +0700 WIB]

- name: minutes
title: Minutes operation
stub: true
stub: true

- name: minutes
title: Convert to minutes
Expand Down Expand Up @@ -197,7 +197,7 @@ ops:
- name: seconds
title: Seconds operation
stub: true
stub: true

- name: seconds
title: Convert time to seconds
Expand Down
25 changes: 13 additions & 12 deletions app/funcs/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/blackchip-org/zc/v6"
"github.com/blackchip-org/zc/v6/app/vars"
"github.com/blackchip-org/zc/v6/msg"
"github.com/cockroachdb/apd/v3"
)

Expand Down Expand Up @@ -73,7 +74,7 @@ func CbrtDecimal(c zc.Calc) {
x := zc.Decimal.Pop(c)

if x.Cmp(&zero) < 0 {
c.Raise(zc.ErrInvalidArg("%v < 0", x))
c.Raise(msg.ErrInvalidArg("%v < 0", x))
return
}
_, err := d.Cbrt(x, x)
Expand Down Expand Up @@ -104,7 +105,7 @@ func DivBigInt(c zc.Calc) {
defer zc.BigInt.Recycle(y)

if y.Cmp(&zero) == 0 {
c.Raise(zc.ErrDivisionByZero)
c.Raise(msg.ErrDivisionByZero())
return
}
x.Div(x, y)
Expand All @@ -126,7 +127,7 @@ func DivDecimal(c zc.Calc) {

cond, err := d.Quo(x, x, y)
if cond.DivisionByZero() {
c.Raise(zc.ErrDivisionByZero)
c.Raise(msg.ErrDivisionByZero())
return
}
if err != nil {
Expand All @@ -143,7 +144,7 @@ func DivRat(c zc.Calc) {
defer zc.Rat.Recycle(y)

if y.Cmp(&zero) == 0 {
c.Raise(zc.ErrDivisionByZero)
c.Raise(msg.ErrDivisionByZero())
return
}

Expand All @@ -160,7 +161,7 @@ func DivModBigInt(c zc.Calc) {
defer zc.BigInt.Recycle(y)

if y.Cmp(&zero) == 0 {
c.Raise(zc.ErrDivisionByZero)
c.Raise(msg.ErrDivisionByZero())
return
}
x.DivMod(x, y, m)
Expand Down Expand Up @@ -244,7 +245,7 @@ func ModBigInt(c zc.Calc) {
defer zc.BigInt.Recycle(y)

if y.Cmp(&zero) == 0 {
c.Raise(zc.ErrDivisionByZero)
c.Raise(msg.ErrDivisionByZero())
return
}
x.Mod(x, y)
Expand Down Expand Up @@ -357,7 +358,7 @@ func RemBigInt(c zc.Calc) {
defer zc.BigInt.Recycle(y)

if y.Cmp(&zero) == 0 {
c.Raise(zc.ErrDivisionByZero)
c.Raise(msg.ErrDivisionByZero())
return
}
x.Rem(x, y)
Expand All @@ -371,7 +372,7 @@ func RemDecimal(c zc.Calc) {
defer zc.Decimal.Recycle(y)

if y.IsZero() {
c.Raise(zc.ErrDivisionByZero)
c.Raise(msg.ErrDivisionByZero())
return
}
_, err := d.Rem(x, x, y)
Expand Down Expand Up @@ -407,7 +408,7 @@ func SqrtBigInt(c zc.Calc) {
var zero big.Int
x := zc.BigInt.Pop(c)
if x.Cmp(&zero) < 0 {
c.Raise(zc.ErrInvalidArg("%v < 0", x.String()))
c.Raise(msg.ErrInvalidArg("%v < 0", x.String()))
return
}
x.Sqrt(x)
Expand All @@ -425,7 +426,7 @@ func SqrtDecimal(c zc.Calc) {
d := vars.ForDec(c).Math
x := zc.Decimal.Pop(c)
if x.Cmp(&zero) < 0 {
c.Raise(zc.ErrInvalidArg("%v < 0", x.String()))
c.Raise(msg.ErrInvalidArg("%v < 0", x.String()))
return
}

Expand Down Expand Up @@ -483,7 +484,7 @@ func QuoBigInt(c zc.Calc) {
defer zc.BigInt.Recycle(y)

if y.Cmp(&zero) == 0 {
c.Raise(zc.ErrDivisionByZero)
c.Raise(msg.ErrDivisionByZero())
return
}
x.Quo(x, y)
Expand All @@ -499,7 +500,7 @@ func QuoRemBigInt(c zc.Calc) {
defer zc.BigInt.Recycle(y)

if y.Cmp(&zero) == 0 {
c.Raise(zc.ErrDivisionByZero)
c.Raise(msg.ErrDivisionByZero())
return
}
x.QuoRem(x, y, r)
Expand Down
5 changes: 3 additions & 2 deletions app/funcs/dec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package funcs
import (
"github.com/blackchip-org/zc/v6"
"github.com/blackchip-org/zc/v6/app/vars"
"github.com/blackchip-org/zc/v6/msg"
)

func Dec(c zc.Calc) {
Expand All @@ -19,7 +20,7 @@ func DecRat(c zc.Calc) {
d := zc.Decimal.New()
d.SetFloat64(f)
if !exact {
c.Notify(zc.NoticeInexact)
c.Notify(msg.Inexact())
}
zc.Decimal.Push(c, d)
}
Expand All @@ -28,7 +29,7 @@ func RoundingModeSet(c zc.Calc) {
conf := vars.ForDec(c)
mode := zc.String.Pop(c)
if err := conf.SetRoundingMode(mode); err != nil {
c.Raise(zc.ErrInvalidArg(err.Error()))
c.Raise(msg.ErrInvalidArg(err.Error()))
return
}
c.Notify("rounding mode set to %v", mode)
Expand Down
13 changes: 7 additions & 6 deletions app/funcs/epsg.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ import (
"strconv"

"github.com/blackchip-org/zc/v6"
"github.com/blackchip-org/zc/v6/msg"
)

func UTM(c zc.Calc) {
p0 := []rune(zc.String.Pop(c))
if len(p0) == 0 {
c.Raise(zc.ErrInvalidArg("hemisphere"))
a := zc.String.Pop(c)
if len(a) == 0 {
c.Raise(msg.ErrInvalidArg(a))
return
}

rZone, hemi := p0[:len(p0)-1], p0[len(p0)-1]
rZone, hemi := a[:len(a)-1], a[len(a)-1]

var base int
switch hemi {
Expand All @@ -23,13 +24,13 @@ func UTM(c zc.Calc) {
case 's', 'S':
base = 32700
default:
c.Raise(zc.ErrInvalidArg("hemisphere"))
c.Raise(msg.ErrInvalidArg(a))
return
}

zone, err := strconv.Atoi(string(rZone))
if err != nil || zone < 0 || zone > 60 {
c.Raise(zc.ErrInvalidArg("zone"))
c.Raise(msg.ErrInvalidArg(a))
return
}
r0 := fmt.Sprintf("EPSG:%v", base+zone)
Expand Down
14 changes: 7 additions & 7 deletions app/funcs/float.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func AddFloat64(c zc.Calc) {
func CbrtFloat64(c zc.Calc) {
x := zc.Float64.Pop(c)
if x < 0 {
c.Raise(zc.ErrInvalidArg("%v < 0", x))
c.Raise(msg.ErrInvalidArg("%v < 0", x))
return
}
z := math.Cbrt(x)
Expand All @@ -45,7 +45,7 @@ func DivBigFloat(c zc.Calc) {
x := zc.BigFloat.Pop(c)

if y.Cmp(&zero) == 0 {
c.Raise(zc.ErrDivisionByZero)
c.Raise(msg.ErrDivisionByZero())
return
}

Expand All @@ -58,7 +58,7 @@ func DivFloat32(c zc.Calc) {
y := zc.Float32.Pop(c)
x := zc.Float32.Pop(c)
if y == 0 {
c.Raise(zc.ErrDivisionByZero)
c.Raise(msg.ErrDivisionByZero())
return
}
zc.Float32.Push(c, x/y)
Expand All @@ -68,7 +68,7 @@ func DivFloat64(c zc.Calc) {
y := zc.Float64.Pop(c)
x := zc.Float64.Pop(c)
if y == 0 {
c.Raise(zc.ErrDivisionByZero)
c.Raise(msg.ErrDivisionByZero())
return
}
zc.Float64.Push(c, x/y)
Expand Down Expand Up @@ -139,7 +139,7 @@ func PrecFloat(c zc.Calc) {
func PrecFloatGet(c zc.Calc) {
v := vars.ForFloat(c)
zc.Uint.Push(c, v.Prec)
c.SetLabel(msg.Precision)
c.SetLabel(msg.Precision())
}

func SignBigFloat(c zc.Calc) {
Expand All @@ -163,7 +163,7 @@ func SqrtBigFloat(c zc.Calc) {
var zero big.Float
x := zc.BigFloat.Pop(c)
if x.Cmp(&zero) < 0 {
c.Raise(zc.ErrInvalidArg("%v < 0", x.String()))
c.Raise(msg.ErrInvalidArg("%v < 0", x.String()))
return
}
x.Sqrt(x)
Expand All @@ -173,7 +173,7 @@ func SqrtBigFloat(c zc.Calc) {
func SqrtFloat64(c zc.Calc) {
x := zc.Float64.Pop(c)
if x < 0 {
c.Raise(zc.ErrInvalidArg("%v < 0", x))
c.Raise(msg.ErrInvalidArg("%v < 0", x))
return
}
zc.Float64.Push(c, math.Sqrt(x))
Expand Down
3 changes: 2 additions & 1 deletion app/funcs/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/blackchip-org/zc/v6"
"github.com/blackchip-org/zc/v6/app/vars"
"github.com/blackchip-org/zc/v6/msg"
"github.com/cockroachdb/apd/v3"
)

Expand All @@ -15,7 +16,7 @@ func RoundComplex(c zc.Calc) {
x := zc.Complex.Pop(c)

if p < 0 {
c.Raise(zc.ErrInvalidArg("%v < 0", p))
c.Raise(msg.ErrInvalidArg("%v < 0", p))
return
}

Expand Down
Loading

0 comments on commit 7d04a0c

Please sign in to comment.