Skip to content
This repository was archived by the owner on Mar 16, 2025. It is now read-only.

Commit 46a6c27

Browse files
committed
introduce identical?
1 parent 201842d commit 46a6c27

2 files changed

Lines changed: 25 additions & 43 deletions

File tree

pkg/eval/builtin/comparisons.go

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,50 +14,24 @@ import (
1414
"go.xrstf.de/rudi/pkg/lang/ast"
1515
)
1616

17-
func eqFunction(ctx types.Context, args []ast.Expression) (any, error) {
18-
if size := len(args); size != 2 {
19-
return nil, fmt.Errorf("expected exactly 2 arguments, got %d", size)
20-
}
21-
22-
_, leftData, err := eval.EvalExpression(ctx, args[0])
23-
if err != nil {
24-
return nil, fmt.Errorf("argument #0: %w", err)
25-
}
26-
27-
_, rightData, err := eval.EvalExpression(ctx, args[1])
28-
if err != nil {
29-
return nil, fmt.Errorf("argument #1: %w", err)
30-
}
31-
32-
equal, err := equality.EqualCoalesced(ctx.Coalesce(), leftData, rightData)
33-
if err != nil {
34-
return nil, err
35-
}
36-
37-
return equal, nil
38-
}
39-
40-
func likeFunction(ctx types.Context, args []ast.Expression) (any, error) {
41-
if size := len(args); size != 2 {
42-
return nil, fmt.Errorf("expected exactly 2 arguments, got %d", size)
43-
}
44-
45-
_, leftData, err := eval.EvalExpression(ctx, args[0])
46-
if err != nil {
47-
return nil, fmt.Errorf("argument #0: %w", err)
48-
}
17+
func makeEqualityFunc(coalescerGetter func(ctx types.Context) coalescing.Coalescer, desc string) types.Function {
18+
return types.BasicFunction(func(ctx types.Context, args []ast.Expression) (any, error) {
19+
if size := len(args); size != 2 {
20+
return nil, fmt.Errorf("expected exactly 2 arguments, got %d", size)
21+
}
4922

50-
_, rightData, err := eval.EvalExpression(ctx, args[1])
51-
if err != nil {
52-
return nil, fmt.Errorf("argument #1: %w", err)
53-
}
23+
_, leftData, err := eval.EvalExpression(ctx, args[0])
24+
if err != nil {
25+
return nil, fmt.Errorf("argument #0: %w", err)
26+
}
5427

55-
equal, err := equality.EqualCoalesced(coalescing.NewHumane(), leftData, rightData)
56-
if err != nil {
57-
return nil, err
58-
}
28+
_, rightData, err := eval.EvalExpression(ctx, args[1])
29+
if err != nil {
30+
return nil, fmt.Errorf("argument #1: %w", err)
31+
}
5932

60-
return equal, nil
33+
return equality.EqualCoalesced(coalescerGetter(ctx), leftData, rightData)
34+
}, desc)
6135
}
6236

6337
type intProcessor func(left, right int64) (bool, error)

pkg/eval/builtin/funcs.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package builtin
55

66
import (
7+
"go.xrstf.de/rudi/pkg/coalescing"
78
"go.xrstf.de/rudi/pkg/eval/types"
89
)
910

@@ -59,8 +60,15 @@ var Functions = types.Functions{
5960
"not": types.BasicFunction(notFunction, "negates the given argument"),
6061

6162
// comparisons
62-
"eq?": types.BasicFunction(eqFunction, "equality check: return true if both arguments are the same"),
63-
"like?": types.BasicFunction(likeFunction, `like eq?, but does lossless type conversions so 1 == "1"`),
63+
"eq?": makeEqualityFunc(func(ctx types.Context) coalescing.Coalescer {
64+
return ctx.Coalesce()
65+
}, "equality check: return true if both arguments are the same"),
66+
"identical?": makeEqualityFunc(func(ctx types.Context) coalescing.Coalescer {
67+
return coalescing.NewStrict()
68+
}, "like eq?, but always uses strict coalecsing"),
69+
"like?": makeEqualityFunc(func(ctx types.Context) coalescing.Coalescer {
70+
return coalescing.NewHumane()
71+
}, "like eq?, but always uses humane coalecsing"),
6472

6573
"lt?": makeNumberComparatorFunc(
6674
func(a, b int64) (bool, error) { return a < b, nil },

0 commit comments

Comments
 (0)