Skip to content

Commit

Permalink
more
Browse files Browse the repository at this point in the history
  • Loading branch information
johnkerl committed Aug 30, 2023
1 parent 33a0a28 commit 7a228d7
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 52 deletions.
34 changes: 25 additions & 9 deletions internal/pkg/bifs/relative_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func BIF_dhms2sec(input1 *mlrval.Mlrval) *mlrval.Mlrval {

input := input1.String()
if input == "" {
return mlrval.ERROR
return mlrval.FromNotStringError("dhms2sec", input1)
}

negate := false
Expand All @@ -39,7 +39,7 @@ func BIF_dhms2sec(input1 *mlrval.Mlrval) *mlrval.Mlrval {
return mlrval.FromError(err)
}
if len(rest) < 1 {
return mlrval.ERROR
return mlrval.FromErrorString("dhms2sec: input too short")
}
unitPart := rest[0]
remainingInput = rest[1:]
Expand All @@ -54,7 +54,13 @@ func BIF_dhms2sec(input1 *mlrval.Mlrval) *mlrval.Mlrval {
case 's':
seconds += n
default:
return mlrval.ERROR
return mlrval.FromError(
fmt.Errorf(
"dhms2sec(\"%s\"): unrecognized unit '%c'",
input1.OriginalString(),
unitPart,
),
)
}
}
if negate {
Expand All @@ -71,7 +77,7 @@ func BIF_dhms2fsec(input1 *mlrval.Mlrval) *mlrval.Mlrval {

input := input1.String()
if input == "" {
return mlrval.ERROR
return mlrval.FromNotStringError("dhms2fsec", input1)
}

negate := false
Expand All @@ -95,7 +101,7 @@ func BIF_dhms2fsec(input1 *mlrval.Mlrval) *mlrval.Mlrval {
return mlrval.FromError(err)
}
if len(rest) < 1 {
return mlrval.ERROR
return mlrval.FromErrorString("dhms2fsec: input too short")
}
unitPart := rest[0]
remainingInput = rest[1:]
Expand All @@ -110,7 +116,13 @@ func BIF_dhms2fsec(input1 *mlrval.Mlrval) *mlrval.Mlrval {
case 's':
seconds += f
default:
return mlrval.ERROR
return mlrval.FromError(
fmt.Errorf(
"dhms2fsec(\"%s\"): unrecognized unit '%c'",
input1.OriginalString(),
unitPart,
),
)
}
}
if negate {
Expand All @@ -125,7 +137,7 @@ func BIF_hms2sec(input1 *mlrval.Mlrval) *mlrval.Mlrval {
return mlrval.FromNotStringError("hms2sec", input1)
}
if input1.AcquireStringValue() == "" {
return mlrval.ERROR
return mlrval.FromNotStringError("hms2sec", input1)
}
var h, m, s int64

Expand All @@ -141,7 +153,9 @@ func BIF_hms2sec(input1 *mlrval.Mlrval) *mlrval.Mlrval {
}
}

return mlrval.ERROR
return mlrval.FromError(
fmt.Errorf("hsm2sec: could not parse input \"%s\"", input1.OriginalString()),
)
}

func BIF_hms2fsec(input1 *mlrval.Mlrval) *mlrval.Mlrval {
Expand All @@ -164,7 +178,9 @@ func BIF_hms2fsec(input1 *mlrval.Mlrval) *mlrval.Mlrval {
}
}

return mlrval.ERROR
return mlrval.FromError(
fmt.Errorf("hsm2fsec: could not parse input \"%s\"", input1.OriginalString()),
)
}

func BIF_sec2dhms(input1 *mlrval.Mlrval) *mlrval.Mlrval {
Expand Down
33 changes: 25 additions & 8 deletions internal/pkg/bifs/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bifs

import (
"bytes"
"fmt"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -161,7 +162,7 @@ func BIF_contains(input1, input2 *mlrval.Mlrval) *mlrval.Mlrval {
return mlrval.ABSENT
}
if input1.IsError() {
return mlrval.ERROR
return input1
}

return mlrval.FromBool(strings.Contains(input1.String(), input2.String()))
Expand Down Expand Up @@ -426,7 +427,15 @@ func bif_unformat_aux(input1, input2 *mlrval.Mlrval, inferTypes bool) *mlrval.Ml
remaining := input

if !strings.HasPrefix(remaining, templatePieces[0]) {
return mlrval.ERROR
return mlrval.FromError(
fmt.Errorf(
"unformat(\"%s\", \"%s\"): component \"%s\" lacks prefix \"%s\"",
input1.OriginalString(),
input2.OriginalString(),
remaining,
templatePieces[0],
),
)
}
remaining = remaining[len(templatePieces[0]):]
templatePieces = templatePieces[1:]
Expand All @@ -442,7 +451,15 @@ func bif_unformat_aux(input1, input2 *mlrval.Mlrval, inferTypes bool) *mlrval.Ml
} else {
index = strings.Index(remaining, templatePiece)
if index < 0 {
return mlrval.ERROR
return mlrval.FromError(
fmt.Errorf(
"unformat(\"%s\", \"%s\"): component \"%s\" lacks prefix \"%s\"",
input1.OriginalString(),
input2.OriginalString(),
remaining,
templatePiece,
),
)
}
}

Expand Down Expand Up @@ -475,7 +492,7 @@ func fmtnum_is(input1, input2 *mlrval.Mlrval) *mlrval.Mlrval {
formatString := input2.AcquireStringValue()
formatter, err := mlrval.GetFormatter(formatString)
if err != nil {
return mlrval.ERROR
return mlrval.FromError(err)
}

return formatter.Format(input1)
Expand All @@ -488,7 +505,7 @@ func fmtnum_fs(input1, input2 *mlrval.Mlrval) *mlrval.Mlrval {
formatString := input2.AcquireStringValue()
formatter, err := mlrval.GetFormatter(formatString)
if err != nil {
return mlrval.ERROR
return mlrval.FromError(err)
}

return formatter.Format(input1)
Expand All @@ -501,7 +518,7 @@ func fmtnum_bs(input1, input2 *mlrval.Mlrval) *mlrval.Mlrval {
formatString := input2.AcquireStringValue()
formatter, err := mlrval.GetFormatter(formatString)
if err != nil {
return mlrval.ERROR
return mlrval.FromError(err)
}

intMv := mlrval.FromInt(lib.BoolToInt(input1.AcquireBoolValue()))
Expand Down Expand Up @@ -557,7 +574,7 @@ func BIF_latin1_to_utf8(input1 *mlrval.Mlrval) *mlrval.Mlrval {
if err != nil {
// Somewhat arbitrary design decision
// return input1
return mlrval.ERROR
return mlrval.FromError(err)
} else {
return mlrval.FromString(output)
}
Expand All @@ -574,7 +591,7 @@ func BIF_utf8_to_latin1(input1 *mlrval.Mlrval) *mlrval.Mlrval {
if err != nil {
// Somewhat arbitrary design decision
// return input1
return mlrval.ERROR
return mlrval.FromError(err)
} else {
return mlrval.FromString(output)
}
Expand Down
10 changes: 5 additions & 5 deletions internal/pkg/dsl/cst/builtin_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ func (node *LogicalANDOperatorNode) Evaluate(
aout := node.a.Evaluate(state)
atype := aout.Type()
if !(atype == mlrval.MT_ABSENT || atype == mlrval.MT_BOOL) {
return mlrval.ERROR
return mlrval.FromNotNamedTypeError("&&", aout, "absent or boolean")
}
if atype == mlrval.MT_ABSENT {
return mlrval.ABSENT
Expand All @@ -768,7 +768,7 @@ func (node *LogicalANDOperatorNode) Evaluate(
bout := node.b.Evaluate(state)
btype := bout.Type()
if !(btype == mlrval.MT_ABSENT || btype == mlrval.MT_BOOL) {
return mlrval.ERROR
return mlrval.FromNotNamedTypeError("&&", bout, "absent or boolean")
}
if btype == mlrval.MT_ABSENT {
return mlrval.ABSENT
Expand Down Expand Up @@ -800,7 +800,7 @@ func (node *LogicalOROperatorNode) Evaluate(
aout := node.a.Evaluate(state)
atype := aout.Type()
if !(atype == mlrval.MT_ABSENT || atype == mlrval.MT_BOOL) {
return mlrval.ERROR
return mlrval.FromNotNamedTypeError("||", aout, "absent or boolean")
}
if atype == mlrval.MT_ABSENT {
return mlrval.ABSENT
Expand All @@ -816,7 +816,7 @@ func (node *LogicalOROperatorNode) Evaluate(
bout := node.b.Evaluate(state)
btype := bout.Type()
if !(btype == mlrval.MT_ABSENT || btype == mlrval.MT_BOOL) {
return mlrval.ERROR
return mlrval.FromNotNamedTypeError("||", bout, "absent or boolean")
}
if btype == mlrval.MT_ABSENT {
return mlrval.ABSENT
Expand Down Expand Up @@ -884,7 +884,7 @@ func (node *StandardTernaryOperatorNode) Evaluate(

boolValue, isBool := aout.GetBoolValue()
if !isBool {
return mlrval.ERROR
return mlrval.FromNotBooleanError("?:", aout)
}

// Short-circuit: defer evaluation unless needed
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/mlrval/mlrmap_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (entry *MlrmapEntry) JSONStringifyInPlace(
) {
outputBytes, err := entry.Value.MarshalJSON(jsonFormatting, false)
if err != nil {
entry.Value = ERROR
entry.Value = FromError(err)
} else {
entry.Value = FromString(string(outputBytes))
}
Expand All @@ -165,7 +165,7 @@ func (entry *MlrmapEntry) JSONParseInPlace() {
input := entry.Value.String()
err := entry.Value.UnmarshalJSON([]byte(input))
if err != nil {
entry.Value = ERROR
entry.Value = FromError(err)
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/mlrval/mlrval_cmp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var orderedMlrvals = []*Mlrval{
// FromMap(NewMlrmap()),

// TODO:
ERROR,
FromErrorString("error text goes here"),
NULL,
ABSENT,
}
Expand Down
10 changes: 5 additions & 5 deletions internal/pkg/mlrval/mlrval_collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ import (
// TODO: copy-reduction refactor
func (mv *Mlrval) ArrayGet(mindex *Mlrval) Mlrval {
if !mv.IsArray() {
return *ERROR
return *FromNotArrayError("array [] base", mv)
}
if !mindex.IsInt() {
return *ERROR
return *FromNotIntError("array [] index", mindex)
}
arrayval := mv.intf.([]*Mlrval)
value := arrayGetAliased(&arrayval, int(mindex.intf.(int64)))
Expand Down Expand Up @@ -223,12 +223,12 @@ func (mv *Mlrval) ArrayAppend(value *Mlrval) {
// ================================================================
func (mv *Mlrval) MapGet(key *Mlrval) Mlrval {
if !mv.IsMap() {
return *ERROR
return *FromNotMapError("map[]", mv)
}

mval, err := mv.intf.(*Mlrmap).GetWithMlrvalIndex(key)
if err != nil { // xxx maybe error-return in the API
return *ERROR
if err != nil {
return *FromError(err)
}
if mval == nil {
return *ABSENT
Expand Down
6 changes: 0 additions & 6 deletions internal/pkg/mlrval/mlrval_constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ var VOID = &Mlrval{
printrepValid: true,
}

var ERROR = &Mlrval{
mvtype: MT_ERROR,
printrep: ERROR_PRINTREP,
printrepValid: true,
}

var NULL = &Mlrval{
mvtype: MT_NULL,
printrep: "null",
Expand Down
18 changes: 9 additions & 9 deletions internal/pkg/mlrval/mlrval_is_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func TestIsLegit(t *testing.T) {
assert.False(t, ERROR.IsLegit())
assert.False(t, FromErrorString("foo").IsLegit())
assert.False(t, ABSENT.IsLegit())
assert.False(t, NULL.IsLegit())
assert.True(t, FromString("").IsLegit())
Expand All @@ -24,35 +24,35 @@ func TestIsLegit(t *testing.T) {
}

func TestIsErrorOrAbsent(t *testing.T) {
assert.True(t, ERROR.IsErrorOrAbsent())
assert.True(t, FromErrorString("foo").IsErrorOrAbsent())
assert.True(t, ABSENT.IsErrorOrAbsent())
assert.False(t, NULL.IsErrorOrAbsent())
assert.False(t, FromString("").IsErrorOrAbsent())
}

func TestIsError(t *testing.T) {
assert.True(t, ERROR.IsError())
assert.True(t, FromErrorString("foo").IsError())
assert.False(t, ABSENT.IsError())
assert.False(t, NULL.IsError())
assert.False(t, FromString("").IsError())
}

func TestIsAbsent(t *testing.T) {
assert.False(t, ERROR.IsAbsent())
assert.False(t, FromErrorString("foo").IsAbsent())
assert.True(t, ABSENT.IsAbsent())
assert.False(t, NULL.IsAbsent())
assert.False(t, FromString("").IsAbsent())
}

func TestIsNull(t *testing.T) {
assert.False(t, ERROR.IsNull())
assert.False(t, FromErrorString("foo").IsNull())
assert.False(t, ABSENT.IsNull())
assert.True(t, NULL.IsNull())
assert.False(t, FromString("").IsNull())
}

func TestIsVoid(t *testing.T) {
assert.False(t, ERROR.IsVoid())
assert.False(t, FromErrorString("foo").IsVoid())
assert.False(t, ABSENT.IsVoid())
assert.False(t, NULL.IsVoid())
assert.True(t, FromString("").IsVoid())
Expand All @@ -63,7 +63,7 @@ func TestIsVoid(t *testing.T) {
}

func TestIsEmptyString(t *testing.T) {
assert.False(t, ERROR.IsEmptyString())
assert.False(t, FromErrorString("foo").IsEmptyString())
assert.False(t, ABSENT.IsEmptyString())
assert.False(t, NULL.IsEmptyString())
assert.True(t, FromString("").IsEmptyString())
Expand All @@ -74,7 +74,7 @@ func TestIsEmptyString(t *testing.T) {
}

func TestIsString(t *testing.T) {
assert.False(t, ERROR.IsString())
assert.False(t, FromErrorString("foo").IsString())
assert.False(t, ABSENT.IsString())
assert.False(t, NULL.IsString())
assert.False(t, FromString("").IsString())
Expand All @@ -89,7 +89,7 @@ func TestIsString(t *testing.T) {
}

func TestIsStringOrVoid(t *testing.T) {
assert.False(t, ERROR.IsStringOrVoid())
assert.False(t, FromErrorString("foo").IsStringOrVoid())
assert.False(t, ABSENT.IsStringOrVoid())
assert.False(t, NULL.IsStringOrVoid())
assert.True(t, FromString("").IsStringOrVoid())
Expand Down
Loading

0 comments on commit 7a228d7

Please sign in to comment.