Skip to content

Commit

Permalink
rewrite equality logic to instead compare, so it can be re-used for l…
Browse files Browse the repository at this point in the history
…t/gt
  • Loading branch information
xrstf committed Dec 3, 2023
1 parent 7bea6fb commit 3727aa8
Show file tree
Hide file tree
Showing 14 changed files with 746 additions and 485 deletions.
1 change: 1 addition & 0 deletions cmd/rudi/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (

require (
github.com/MichaelMure/go-term-text v0.3.1 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/gomarkdown/markdown v0.0.0-20231115200524-a660076da3fd // indirect
github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f // indirect
Expand Down
2 changes: 2 additions & 0 deletions cmd/rudi/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/MichaelMure/go-term-text v0.3.1 h1:Kw9kZanyZWiCHOYu9v/8pWEgDQ6UVN9/ix2Vd2zzWf0=
github.com/MichaelMure/go-term-text v0.3.1/go.mod h1:QgVjAEDUnRMlzpS6ky5CGblux7ebeiLnuy9dAaFZu8o=
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
github.com/chzyer/logex v1.2.1 h1:XHDu3E6q+gdHgsdTPH6ImJMIp436vR6MPtH8gP05QzM=
github.com/chzyer/logex v1.2.1/go.mod h1:JLbx6lG2kDbNRFnfkgvh4eRJRPX1QCoOIWomwysCBrQ=
github.com/chzyer/readline v1.5.1 h1:upd/6fQk4src78LMRzh5vItIt361/o4uq553V8B5sGI=
Expand Down
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Welcome to the Rudi documentation :smile:
* [`identical?`](functions/comparisons-identical.md) – like `eq?`, but always uses strict coalecsing
* [`like?`](functions/comparisons-like.md) – like `eq?`, but always uses humane coalecsing
* [`lt?`](functions/comparisons-lt.md) – returns a < b
* [`lte?`](functions/comparisons-lte.md)return a <= b
* [`lte?`](functions/comparisons-lte.md)returns a <= b

## Dates Functions

Expand Down
2 changes: 1 addition & 1 deletion docs/functions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ applied to all functions (so technically `eq?!` is valid, though weird looking).
* [`identical?`](../functions/comparisons-identical.md) – like `eq?`, but always uses strict coalecsing
* [`like?`](../functions/comparisons-like.md) – like `eq?`, but always uses humane coalecsing
* [`lt?`](../functions/comparisons-lt.md) – returns a < b
* [`lte?`](../functions/comparisons-lte.md)return a <= b
* [`lte?`](../functions/comparisons-lte.md)returns a <= b

## Dates Functions

Expand Down
56 changes: 49 additions & 7 deletions pkg/coalescing/humane.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,34 @@ import (
"go.xrstf.de/rudi/pkg/lang/ast"
)

type CustomNullCoalescer interface {
CoalesceToNull(c Coalescer) (bool, error)
}

type CustomBoolCoalescer interface {
CoalesceToBool(c Coalescer) (bool, error)
}

type CustomInt64Coalescer interface {
CoalesceToInt64(c Coalescer) (int64, error)
}

type CustomFloat64Coalescer interface {
CoalesceToFloat64(c Coalescer) (float64, error)
}

type CustomStringCoalescer interface {
CoalesceToString(c Coalescer) (string, error)
}

type CustomVectorCoalescer interface {
CoalesceToVector(c Coalescer) ([]any, error)
}

type CustomObjectCoalescer interface {
CoalesceToObject(c Coalescer) (map[string]any, error)
}

type humane struct{}

func NewHumane() Coalescer {
Expand All @@ -20,7 +48,7 @@ func NewHumane() Coalescer {

var _ Coalescer = humane{}

func (humane) ToNull(val any) (bool, error) {
func (h humane) ToNull(val any) (bool, error) {
switch v := deliteral(val).(type) {
case nil:
return true, nil
Expand Down Expand Up @@ -69,12 +97,14 @@ func (humane) ToNull(val any) (bool, error) {
return false, errors.New("cannot coalesce non-empty object into null")
}
return true, nil
case CustomNullCoalescer:
return v.CoalesceToNull(h)
default:
return false, fmt.Errorf("cannot coalesce %T into null", v)
}
}

func (humane) ToBool(val any) (bool, error) {
func (h humane) ToBool(val any) (bool, error) {
switch v := deliteral(val).(type) {
case nil:
return false, nil
Expand All @@ -100,12 +130,14 @@ func (humane) ToBool(val any) (bool, error) {
return len(v) > 0, nil
case map[string]any:
return len(v) > 0, nil
case CustomBoolCoalescer:
return v.CoalesceToBool(h)
default:
return false, fmt.Errorf("cannot coalesce %T into bool", v)
}
}

func (humane) ToFloat64(val any) (float64, error) {
func (h humane) ToFloat64(val any) (float64, error) {
switch v := deliteral(val).(type) {
case nil:
return 0, nil
Expand Down Expand Up @@ -136,12 +168,14 @@ func (humane) ToFloat64(val any) (float64, error) {
return 0, fmt.Errorf("cannot coalesce %T into float64", v)
}
return parsed, nil
case CustomFloat64Coalescer:
return v.CoalesceToFloat64(h)
default:
return 0, fmt.Errorf("cannot coalesce %T into float64", v)
}
}

func (humane) ToInt64(val any) (int64, error) {
func (h humane) ToInt64(val any) (int64, error) {
switch v := deliteral(val).(type) {
case nil:
return 0, nil
Expand Down Expand Up @@ -184,6 +218,8 @@ func (humane) ToInt64(val any) (int64, error) {
return 0, fmt.Errorf("cannot coalesce %T into int64", v)
}
return parsed, nil
case CustomInt64Coalescer:
return v.CoalesceToInt64(h)
default:
return 0, fmt.Errorf("cannot coalesce %T into int64", v)
}
Expand All @@ -193,7 +229,7 @@ func (h humane) ToNumber(val any) (ast.Number, error) {
return toNumber(h, val)
}

func (humane) ToString(val any) (string, error) {
func (h humane) ToString(val any) (string, error) {
switch v := deliteral(val).(type) {
case nil:
return "", nil
Expand All @@ -209,6 +245,8 @@ func (humane) ToString(val any) (string, error) {
return formatFloat(v), nil
case string:
return v, nil
case CustomStringCoalescer:
return v.CoalesceToString(h)
default:
return "", fmt.Errorf("cannot coalesce %T into string", v)
}
Expand All @@ -223,7 +261,7 @@ func formatFloat(f float64) string {
return strings.TrimSuffix(formatted, ".")
}

func (humane) ToVector(val any) ([]any, error) {
func (h humane) ToVector(val any) ([]any, error) {
switch v := deliteral(val).(type) {
case nil:
return []any{}, nil
Expand All @@ -235,12 +273,14 @@ func (humane) ToVector(val any) ([]any, error) {
} else {
return nil, fmt.Errorf("cannot coalesce %T into vector", v)
}
case CustomVectorCoalescer:
return v.CoalesceToVector(h)
default:
return nil, fmt.Errorf("cannot coalesce %T into vector", v)
}
}

func (humane) ToObject(val any) (map[string]any, error) {
func (h humane) ToObject(val any) (map[string]any, error) {
switch v := deliteral(val).(type) {
case nil:
return map[string]any{}, nil
Expand All @@ -252,6 +292,8 @@ func (humane) ToObject(val any) (map[string]any, error) {
}
case map[string]any:
return v, nil
case CustomObjectCoalescer:
return v.CoalesceToObject(h)
default:
return nil, fmt.Errorf("cannot coalesce %T into object", v)
}
Expand Down
9 changes: 1 addition & 8 deletions pkg/debug/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"io"

"go.xrstf.de/rudi/pkg/eval/types"
"go.xrstf.de/rudi/pkg/lang/ast"
)

Expand Down Expand Up @@ -69,13 +68,7 @@ func dumpAny(val any, out io.Writer, depth int) error {
return DumpProgram(&asserted, out, depth)
}

wrapped, err := types.WrapNative(val)
if err != nil {
return fmt.Errorf("cannot dump values of type %T", val)
}

// as long as dumpAny() can handle all possible types returned by WrapNative, this won't be an infinite loop
return dumpAny(wrapped, out, depth)
return fmt.Errorf("cannot dump values of type %T", val)
}

func writeString(out io.Writer, str string) error {
Expand Down
Loading

0 comments on commit 3727aa8

Please sign in to comment.