Skip to content

Commit

Permalink
internal/core/adt: use value array for Disjunction
Browse files Browse the repository at this point in the history
We want to represent pattern expressions as Disjunctions.
Since having a disjunction of values then becomes much
more common, it makes sense to not require it to be a vertex.

Signed-off-by: Marcel van Lohuizen <mpvl@gmail.com>
Change-Id: If453fd0d56f7def2606ba22f8e93a2f45407c1a6
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/551884
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
Unity-Result: CUEcueckoo <cueckoo@cuelang.org>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
  • Loading branch information
mpvl committed Mar 31, 2023
1 parent fa4f2d3 commit 166ac88
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
11 changes: 8 additions & 3 deletions internal/core/adt/composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,9 +546,14 @@ func toDataAll(ctx *OpContext, v BaseValue) BaseValue {
// to avoid issues with the closedness algorithm down the line.
case *Disjunction:
d := *x
d.Values = make([]*Vertex, len(x.Values))
d.Values = make([]Value, len(x.Values))
for i, v := range x.Values {
d.Values[i] = v.ToDataAll(ctx)
switch x := v.(type) {
case *Vertex:
d.Values[i] = x.ToDataAll(ctx)
default:
d.Values[i] = x
}
}
return &d

Expand Down Expand Up @@ -739,7 +744,7 @@ func (v *Vertex) IsClosedList() bool {
func (v *Vertex) Accept(ctx *OpContext, f Feature) bool {
if x, ok := v.BaseValue.(*Disjunction); ok {
for _, v := range x.Values {
if v.Accept(ctx, f) {
if x, ok := v.(*Vertex); ok && x.Accept(ctx, f) {
return true
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/core/adt/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (v *Vertex) Default() *Vertex {
case 0:
return v
case 1:
w = d.Values[0].Default()
w = ToVertex(Default(d.Values[0]))
default:
x := *v
x.state = nil
Expand Down
6 changes: 5 additions & 1 deletion internal/core/adt/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,10 @@ func (c *OpContext) unify(v *Vertex, state vertexStatus) {
// The conjuncts will have too much information. Better have no
// information than incorrect information.
for _, d := range d.Values {
d, ok := d.(*Vertex)
if !ok {
continue
}
// We clear the conjuncts for now. As these disjuncts are for API
// use only, we will fill them out when necessary (using Defaults).
d.Conjuncts = nil
Expand Down Expand Up @@ -884,7 +888,7 @@ func isCyclePlaceholder(v BaseValue) bool {
}

func (n *nodeContext) createDisjunct() *Disjunction {
a := make([]*Vertex, len(n.disjuncts))
a := make([]Value, len(n.disjuncts))
p := 0
hasDefaults := false
for i, x := range n.disjuncts {
Expand Down
4 changes: 2 additions & 2 deletions internal/core/adt/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -1769,8 +1769,8 @@ type Disjunction struct {
Src ast.Expr

// Values are the non-error disjuncts of this expression. The first
// NumDefault values are default values.
Values []*Vertex
// NumDefaults values are default values.
Values []Value

Errors *Bottom // []bottom

Expand Down
12 changes: 5 additions & 7 deletions pkg/path/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,20 @@ var (
// windowsDefault is the default for VolumeName.
windowsDefault = &adt.Disjunction{
NumDefaults: 1,
Values: append([]*adt.Vertex{
Values: append([]adt.Value{
newStr("windows"),
newStr("unix"),
newStr("plan9")}, unixOS...),
}

allOS = append([]*adt.Vertex{
allOS = append([]adt.Value{
newStr("unix"),
newStr("windows"),
newStr("plan9"),
}, unixOS...)

// These all fall back to unix
unixOS = []*adt.Vertex{
unixOS = []adt.Value{
newStr("aix"),
newStr("android"),
newStr("darwin"),
Expand All @@ -70,10 +70,8 @@ var (
}
)

func newStr(s string) *adt.Vertex {
v := &adt.Vertex{}
v.SetValue(nil, &adt.String{Str: s})
return v
func newStr(s string) adt.Value {
return &adt.String{Str: s}
}

var pkg = &internal.Package{
Expand Down

0 comments on commit 166ac88

Please sign in to comment.