Skip to content

Commit e841714

Browse files
committed
internal/core: move equality checks from eval to adt
So that it can be used by adt itself without cycles. Also seems to be the more appropriate place. Change-Id: Iad699b75071f41c02690a2dffa06a5d51c76c358 Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7883 Reviewed-by: CUE cueckoo <[email protected]> Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent ff3e32f commit e841714

File tree

4 files changed

+23
-25
lines changed

4 files changed

+23
-25
lines changed

cue/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1785,7 +1785,7 @@ func (v Value) Equals(other Value) bool {
17851785
if v.v == nil || other.v == nil {
17861786
return false
17871787
}
1788-
return eval.Equal(v.ctx().opCtx, v.v, other.v)
1788+
return adt.Equal(v.ctx().opCtx, v.v, other.v)
17891789
}
17901790

17911791
// Format prints a debug version of a value.

internal/core/eval/equality.go renamed to internal/core/adt/equality.go

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,20 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package eval
15+
package adt
1616

17-
import "cuelang.org/go/internal/core/adt"
18-
19-
func Equal(ctx *adt.OpContext, v, w adt.Value) bool {
20-
if x, ok := v.(*adt.Vertex); ok {
17+
func Equal(ctx *OpContext, v, w Value) bool {
18+
if x, ok := v.(*Vertex); ok {
2119
return equalVertex(ctx, x, w)
2220
}
23-
if y, ok := w.(*adt.Vertex); ok {
21+
if y, ok := w.(*Vertex); ok {
2422
return equalVertex(ctx, y, v)
2523
}
2624
return equalTerminal(ctx, v, w)
2725
}
2826

29-
func equalVertex(ctx *adt.OpContext, x *adt.Vertex, v adt.Value) bool {
30-
y, ok := v.(*adt.Vertex)
27+
func equalVertex(ctx *OpContext, x *Vertex, v Value) bool {
28+
y, ok := v.(*Vertex)
3129
if !ok {
3230
return false
3331
}
@@ -69,42 +67,42 @@ loop1:
6967
// return false
7068
// }
7169

72-
v, ok1 := x.BaseValue.(adt.Value)
73-
w, ok2 := y.BaseValue.(adt.Value)
70+
v, ok1 := x.BaseValue.(Value)
71+
w, ok2 := y.BaseValue.(Value)
7472
if !ok1 && !ok2 {
7573
return true // both are struct or list.
7674
}
7775

7876
return equalTerminal(ctx, v, w)
7977
}
8078

81-
func equalTerminal(ctx *adt.OpContext, v, w adt.Value) bool {
79+
func equalTerminal(ctx *OpContext, v, w Value) bool {
8280
if v == w {
8381
return true
8482
}
8583

8684
switch x := v.(type) {
87-
case *adt.Num, *adt.String, *adt.Bool, *adt.Bytes:
88-
if b, ok := adt.BinOp(ctx, adt.EqualOp, v, w).(*adt.Bool); ok {
85+
case *Num, *String, *Bool, *Bytes:
86+
if b, ok := BinOp(ctx, EqualOp, v, w).(*Bool); ok {
8987
return b.B
9088
}
9189
return false
9290

9391
// TODO: for the remainder we are dealing with non-concrete values, so we
9492
// could also just not bother.
9593

96-
case *adt.BoundValue:
97-
if y, ok := w.(*adt.BoundValue); ok {
94+
case *BoundValue:
95+
if y, ok := w.(*BoundValue); ok {
9896
return x.Op == y.Op && Equal(ctx, x.Value, y.Value)
9997
}
10098

101-
case *adt.BasicType:
102-
if y, ok := w.(*adt.BasicType); ok {
99+
case *BasicType:
100+
if y, ok := w.(*BasicType); ok {
103101
return x.K == y.K
104102
}
105103

106-
case *adt.Conjunction:
107-
y, ok := w.(*adt.Conjunction)
104+
case *Conjunction:
105+
y, ok := w.(*Conjunction)
108106
if !ok || len(x.Values) != len(y.Values) {
109107
return false
110108
}
@@ -116,10 +114,10 @@ func equalTerminal(ctx *adt.OpContext, v, w adt.Value) bool {
116114
}
117115
return true
118116

119-
case *adt.Disjunction:
117+
case *Disjunction:
120118
// The best way to compute this is with subsumption, but even that won't
121119
// be too accurate. Assume structural equivalence for now.
122-
y, ok := w.(*adt.Disjunction)
120+
y, ok := w.(*Disjunction)
123121
if !ok || len(x.Values) != len(y.Values) {
124122
return false
125123
}
@@ -130,7 +128,7 @@ func equalTerminal(ctx *adt.OpContext, v, w adt.Value) bool {
130128
}
131129
return true
132130

133-
case *adt.BuiltinValidator:
131+
case *BuiltinValidator:
134132
}
135133

136134
return false

internal/core/adt/simplify.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ func SimplifyValidator(ctx *OpContext, v, w Validator) Validator {
230230
return nil
231231
}
232232
for i, a := range x.Args {
233-
if !test(ctx, EqualOp, a, y.Args[i]) {
233+
if !Equal(ctx, a, y.Args[i]) {
234234
return nil
235235
}
236236
}

internal/core/eval/disjunct.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func (n *nodeContext) updateResult(state adt.VertexStatus) (isFinal bool) {
158158
}
159159

160160
for _, v := range d.Values {
161-
if Equal(n.ctx, v, &result) {
161+
if adt.Equal(n.ctx, v, &result) {
162162
return isFinal
163163
}
164164
}

0 commit comments

Comments
 (0)