cue: use Kind and Op from Package adt
Also reuse some errors and reoder args.
Change-Id: I7a7d13ad050d8e9fa98794ccf7f10bbdaec30c7c
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/6512
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/cue/builtin.go b/cue/builtin.go
index 8532b7b..147ed41 100644
--- a/cue/builtin.go
+++ b/cue/builtin.go
@@ -253,7 +253,7 @@
func convertBuiltin(v evaluated) evaluated {
x, ok := v.(*builtin)
if ok && x.isValidator() {
- return &customValidator{v.base(), []evaluated{}, x}
+ return &customValidator{v.base(), x, []evaluated{}}
}
return v
}
@@ -264,7 +264,7 @@
}
if len(x.Params)-1 == len(args) && x.Result == boolKind {
// We have a custom builtin
- return &customValidator{src.base(), args, x}
+ return &customValidator{src.base(), x, args}
}
switch {
case len(x.Params) < len(args):
@@ -297,7 +297,7 @@
case *valueError:
return v.err
}
- return convert(ctx, x, true, call.ret)
+ return convertVal(ctx, x, true, call.ret)
}
func processErr(call *callCtxt, errVal interface{}, ret value) value {
diff --git a/cue/builtinutil.go b/cue/builtinutil.go
index dedf174..e99f132 100644
--- a/cue/builtinutil.go
+++ b/cue/builtinutil.go
@@ -51,7 +51,7 @@
for i := len(path) - 1; i >= 0; i-- {
x = map[string]interface{}{path[i]: x}
}
- value := convert(ctx, root, false, x)
+ value := convertVal(ctx, root, false, x)
eval := binOp(ctx, baseValue{}, opUnify, root, value)
return newValueRoot(ctx, eval)
}
diff --git a/cue/go.go b/cue/go.go
index ece6aa6..bfbd92b 100644
--- a/cue/go.go
+++ b/cue/go.go
@@ -49,7 +49,7 @@
func convertValue(r *Runtime, x interface{}, nilIsTop bool) Value {
ctx := r.index().newContext()
- v := convert(ctx, baseValue{}, nilIsTop, x)
+ v := convertVal(ctx, baseValue{}, nilIsTop, x)
return newValueRoot(ctx, v)
}
@@ -189,7 +189,7 @@
}
}
-func convert(ctx *context, src source, nilIsTop bool, x interface{}) evaluated {
+func convertVal(ctx *context, src source, nilIsTop bool, x interface{}) evaluated {
v := convertRec(ctx, src, nilIsTop, x)
if v == nil {
return ctx.mkErr(baseValue{}, "unsupported Go type (%v)", v)
diff --git a/cue/go_test.go b/cue/go_test.go
index a17573f..fd5a03f 100644
--- a/cue/go_test.go
+++ b/cue/go_test.go
@@ -152,7 +152,7 @@
for _, tc := range testCases {
ctx := inst.newContext()
t.Run("", func(t *testing.T) {
- v := convert(ctx, newNode(b), true, tc.goVal)
+ v := convertVal(ctx, newNode(b), true, tc.goVal)
got := debugStr(ctx, v)
if got != tc.want {
t.Errorf("got %q; want %q", got, tc.want)
diff --git a/cue/instance.go b/cue/instance.go
index e51c2bb..33e9ad9 100644
--- a/cue/instance.go
+++ b/cue/instance.go
@@ -337,7 +337,7 @@
for i := len(path) - 1; i >= 0; i-- {
x = map[string]interface{}{path[i]: x}
}
- value := convert(ctx, root, true, x)
+ value := convertVal(ctx, root, true, x)
eval := binOp(ctx, baseValue{}, opUnify, root, value)
// TODO: validate recursively?
err := inst.Err
diff --git a/cue/op.go b/cue/op.go
index a1d7812..b313289 100644
--- a/cue/op.go
+++ b/cue/op.go
@@ -14,53 +14,51 @@
package cue
-import "cuelang.org/go/cue/token"
+import (
+ "cuelang.org/go/cue/token"
+ "cuelang.org/go/internal/core/adt"
+)
// Op indicates the operation at the top of an expression tree of the expression
// use to evaluate a value.
-type Op int
-
-func (o Op) String() string {
- return opToString[o]
-}
+type Op = adt.Op
// Values of Op.
const (
- NoOp Op = iota
+ NoOp = adt.NoOp
- AndOp
- OrOp
+ AndOp = adt.AndOp
+ OrOp = adt.OrOp
- SelectorOp
- IndexOp
- SliceOp
- CallOp
+ SelectorOp = adt.SelectorOp
+ IndexOp = adt.IndexOp
+ SliceOp = adt.SliceOp
+ CallOp = adt.CallOp
- BooleanAndOp
- BooleanOrOp
+ BooleanAndOp = adt.BoolAndOp
+ BooleanOrOp = adt.BoolOrOp
- EqualOp
- NotOp
- NotEqualOp
- LessThanOp
- LessThanEqualOp
- GreaterThanOp
- GreaterThanEqualOp
+ EqualOp = adt.EqualOp
+ NotOp = adt.NotOp
+ NotEqualOp = adt.NotEqualOp
+ LessThanOp = adt.LessThanOp
+ LessThanEqualOp = adt.LessEqualOp
+ GreaterThanOp = adt.GreaterThanOp
+ GreaterThanEqualOp = adt.GreaterEqualOp
- RegexMatchOp
- NotRegexMatchOp
+ RegexMatchOp = adt.MatchOp
+ NotRegexMatchOp = adt.NotMatchOp
- AddOp
- SubtractOp
- MultiplyOp
- FloatQuotientOp
- FloatRemainOp
- IntQuotientOp
- IntRemainderOp
- IntDivideOp
- IntModuloOp
+ AddOp = adt.AddOp
+ SubtractOp = adt.SubtractOp
+ MultiplyOp = adt.MultiplyOp
+ FloatQuotientOp = adt.FloatQuotientOp
+ IntQuotientOp = adt.IntQuotientOp
+ IntRemainderOp = adt.IntRemainderOp
+ IntDivideOp = adt.IntDivideOp
+ IntModuloOp = adt.IntModuloOp
- InterpolationOp
+ InterpolationOp = adt.InterpolationOp
)
var opToOp = map[op]Op{
@@ -85,44 +83,12 @@
opSub: SubtractOp,
opMul: MultiplyOp,
opQuo: FloatQuotientOp,
- opRem: FloatRemainOp,
opIQuo: IntQuotientOp,
opIRem: IntRemainderOp,
opIDiv: IntDivideOp,
opIMod: IntModuloOp,
}
-var opToString = map[Op]string{
- AndOp: "&",
- OrOp: "|",
- BooleanAndOp: "&&",
- BooleanOrOp: "||",
- EqualOp: "==",
- NotOp: "!",
- NotEqualOp: "!=",
- LessThanOp: "<",
- LessThanEqualOp: "<=",
- GreaterThanOp: ">",
- GreaterThanEqualOp: ">=",
- RegexMatchOp: "=~",
- NotRegexMatchOp: "!~",
- AddOp: "+",
- SubtractOp: "-",
- MultiplyOp: "*",
- FloatQuotientOp: "/",
- FloatRemainOp: "%",
- IntQuotientOp: "quo",
- IntRemainderOp: "rem",
- IntDivideOp: "div",
- IntModuloOp: "mod",
-
- SelectorOp: ".",
- IndexOp: "[]",
- SliceOp: "[:]",
- CallOp: "()",
- InterpolationOp: `\()`,
-}
-
func opIn(op op, anyOf ...op) bool {
for _, o := range anyOf {
if o == op {
diff --git a/cue/types.go b/cue/types.go
index 3def979..3461339 100644
--- a/cue/types.go
+++ b/cue/types.go
@@ -21,7 +21,6 @@
"io"
"math"
"math/big"
- "math/bits"
"strconv"
"strings"
"unicode"
@@ -32,104 +31,50 @@
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/token"
"cuelang.org/go/internal"
+ "cuelang.org/go/internal/core/adt"
)
// Kind determines the underlying type of a Value.
-type Kind int
+type Kind = adt.Kind
const BottomKind Kind = 0
const (
// NullKind indicates a null value.
- NullKind Kind = 1 << iota
+ NullKind Kind = adt.NullKind
// BoolKind indicates a boolean value.
- BoolKind
+ BoolKind = adt.BoolKind
// IntKind represents an integral number.
- IntKind
+ IntKind = adt.IntKind
// FloatKind represents a decimal float point number that cannot be
// converted to an integer. The underlying number may still be integral,
// but resulting from an operation that enforces the float type.
- FloatKind
+ FloatKind = adt.FloatKind
// StringKind indicates any kind of string.
- StringKind
+ StringKind = adt.StringKind
// BytesKind is a blob of data.
- BytesKind
+ BytesKind = adt.BytesKind
// StructKind is a kev-value map.
- StructKind
+ StructKind = adt.StructKind
// ListKind indicates a list of values.
- ListKind
-
- nextKind
+ ListKind = adt.ListKind
// _numberKind is used as a implementation detail inside
// Kind.String to indicate NumberKind.
- _numberKind
// NumberKind represents any kind of number.
NumberKind = IntKind | FloatKind
+
+ TopKind = Kind(adt.TopKind)
)
-// String returns the representation of the Kind as
-// a CUE expression. For example:
-//
-// (IntKind|ListKind).String()
-//
-// will return:
-//
-// (int|[...])
-func (k Kind) String() string {
- if k == BottomKind {
- return "_|_"
- }
- if (k & NumberKind) == NumberKind {
- k = (k &^ NumberKind) | _numberKind
- }
- var buf strings.Builder
- multiple := bits.OnesCount(uint(k)) > 1
- if multiple {
- buf.WriteByte('(')
- }
- for count := 0; ; count++ {
- n := bits.TrailingZeros(uint(k))
- if n == bits.UintSize {
- break
- }
- bit := Kind(1 << uint(n))
- k &^= bit
- s, ok := kindStrs[bit]
- if !ok {
- s = fmt.Sprintf("bad(%d)", n)
- }
- if count > 0 {
- buf.WriteByte('|')
- }
- buf.WriteString(s)
- }
- if multiple {
- buf.WriteByte(')')
- }
- return buf.String()
-}
-
-var kindStrs = map[Kind]string{
- NullKind: "null",
- BoolKind: "bool",
- IntKind: "int",
- FloatKind: "float",
- StringKind: "string",
- BytesKind: "bytes",
- StructKind: "{...}",
- ListKind: "[...]",
- _numberKind: "number",
-}
-
// An structValue represents a JSON object.
//
// TODO: remove
@@ -1596,7 +1541,7 @@
for i := len(path) - 1; i >= 0; i-- {
x = map[string]interface{}{path[i]: x}
}
- value := convert(ctx, root, true, x)
+ value := convertVal(ctx, root, true, x)
a := v.path.arc
a.v = mkBin(ctx, v.Pos(), opUnify, root, value)
a.cache = a.v.evalPartial(ctx)
diff --git a/cue/types_test.go b/cue/types_test.go
index 0421920..482ddde 100644
--- a/cue/types_test.go
+++ b/cue/types_test.go
@@ -55,7 +55,7 @@
}{{ // Not a concrete value.
value: `v: _`,
kind: BottomKind,
- incompleteKind: nextKind - 1,
+ incompleteKind: TopKind,
}, {
value: `v: _|_`,
kind: BottomKind,
@@ -2770,7 +2770,7 @@
if op == NoOp {
return debugStr(v.ctx(), v.path.v)
}
- s := opToString[op]
+ s := op.String()
s += "("
for i, v := range operands {
if i > 0 {
@@ -2781,54 +2781,3 @@
s += ")"
return s
}
-
-func TestKindString(t *testing.T) {
- testCases := []struct {
- input Kind
- want string
- }{{
- input: BottomKind,
- want: "_|_",
- }, {
- input: IntKind | ListKind,
- want: `(int|[...])`,
- }, {
- input: NullKind,
- want: "null",
- }, {
- input: IntKind,
- want: "int",
- }, {
- input: FloatKind,
- want: "float",
- }, {
- input: StringKind,
- want: "string",
- }, {
- input: BytesKind,
- want: "bytes",
- }, {
- input: StructKind,
- want: "{...}",
- }, {
- input: ListKind,
- want: "[...]",
- }, {
- input: NumberKind,
- want: "number",
- }, {
- input: BoolKind | NumberKind | ListKind,
- want: "(bool|[...]|number)",
- }, {
- input: 1 << 20,
- want: "bad(20)",
- }}
- for _, tc := range testCases {
- t.Run(tc.want, func(t *testing.T) {
- got := tc.input.String()
- if got != tc.want {
- t.Errorf("\n got %v;\nwant %v", got, tc.want)
- }
- })
- }
-}
diff --git a/cue/value.go b/cue/value.go
index 67c83ad..8207235 100644
--- a/cue/value.go
+++ b/cue/value.go
@@ -1444,8 +1444,8 @@
type customValidator struct {
baseValue
- Args []evaluated // any but the first value
Builtin *builtin // function must return a bool
+ Args []evaluated // any but the first value
}
func (x *customValidator) Kind() kind {