Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
cue: use Kind and Op from Package adt
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
mpvl committed Jul 19, 2020
1 parent 03092d9 commit b6a3a4b
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 203 deletions.
6 changes: 3 additions & 3 deletions cue/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func (x *builtin) isValidator() bool {
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
}
Expand All @@ -264,7 +264,7 @@ func (x *builtin) call(ctx *context, src source, args ...evaluated) (ret value)
}
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):
Expand Down Expand Up @@ -297,7 +297,7 @@ func (x *builtin) call(ctx *context, src source, args ...evaluated) (ret value)
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 {
Expand Down
2 changes: 1 addition & 1 deletion cue/builtinutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func fill(v Value, x interface{}, path ...string) Value {
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)
}
4 changes: 2 additions & 2 deletions cue/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func init() {

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)
}

Expand Down Expand Up @@ -189,7 +189,7 @@ func isZero(v reflect.Value) bool {
}
}

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)
Expand Down
2 changes: 1 addition & 1 deletion cue/go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func TestConvert(t *testing.T) {
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)
Expand Down
2 changes: 1 addition & 1 deletion cue/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ func (inst *Instance) Fill(x interface{}, path ...string) (*Instance, error) {
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
Expand Down
112 changes: 39 additions & 73 deletions cue/op.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

AndOp
OrOp

SelectorOp
IndexOp
SliceOp
CallOp

BooleanAndOp
BooleanOrOp

EqualOp
NotOp
NotEqualOp
LessThanOp
LessThanEqualOp
GreaterThanOp
GreaterThanEqualOp

RegexMatchOp
NotRegexMatchOp

AddOp
SubtractOp
MultiplyOp
FloatQuotientOp
FloatRemainOp
IntQuotientOp
IntRemainderOp
IntDivideOp
IntModuloOp

InterpolationOp
NoOp = adt.NoOp

AndOp = adt.AndOp
OrOp = adt.OrOp

SelectorOp = adt.SelectorOp
IndexOp = adt.IndexOp
SliceOp = adt.SliceOp
CallOp = adt.CallOp

BooleanAndOp = adt.BoolAndOp
BooleanOrOp = adt.BoolOrOp

EqualOp = adt.EqualOp
NotOp = adt.NotOp
NotEqualOp = adt.NotEqualOp
LessThanOp = adt.LessThanOp
LessThanEqualOp = adt.LessEqualOp
GreaterThanOp = adt.GreaterThanOp
GreaterThanEqualOp = adt.GreaterEqualOp

RegexMatchOp = adt.MatchOp
NotRegexMatchOp = adt.NotMatchOp

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 = adt.InterpolationOp
)

var opToOp = map[op]Op{
Expand All @@ -85,44 +83,12 @@ var opToOp = map[op]Op{
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 {
Expand Down
81 changes: 13 additions & 68 deletions cue/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"io"
"math"
"math/big"
"math/bits"
"strconv"
"strings"
"unicode"
Expand All @@ -32,103 +31,49 @@ import (
"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
)

// 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",
}
TopKind = Kind(adt.TopKind)
)

// An structValue represents a JSON object.
//
Expand Down Expand Up @@ -1596,7 +1541,7 @@ func (v Value) Fill(x interface{}, path ...string) Value {
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)
Expand Down

0 comments on commit b6a3a4b

Please sign in to comment.