1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
// Copyright 2020 CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package adt
import (
"bytes"
"strings"
)
// BinOp handles all operations except AndOp and OrOp. This includes processing
// unary comparators such as '<4' and '=~"foo"'.
//
// BinOp returns nil if not both left and right are concrete.
func BinOp(c *OpContext, op Op, left, right Value) Value {
leftKind := left.Kind()
rightKind := right.Kind()
const msg = "non-concrete value '%v' to operation '%s'"
if left.Concreteness() > Concrete {
return &Bottom{
Code: IncompleteError,
Err: c.Newf(msg, left, op),
Node: c.vertex,
}
}
if right.Concreteness() > Concrete {
return &Bottom{
Code: IncompleteError,
Err: c.Newf(msg, right, op),
Node: c.vertex,
}
}
if err := CombineErrors(c.src, left, right); err != nil {
return err
}
switch op {
case EqualOp:
switch {
case leftKind == NullKind && rightKind == NullKind:
return c.newBool(true)
case leftKind == NullKind || rightKind == NullKind:
return c.newBool(false)
case leftKind == BoolKind:
return c.newBool(c.BoolValue(left) == c.BoolValue(right))
case leftKind == StringKind:
// normalize?
return cmpTonode(c, op, strings.Compare(c.StringValue(left), c.StringValue(right)))
case leftKind == BytesKind:
return cmpTonode(c, op, bytes.Compare(c.bytesValue(left, op), c.bytesValue(right, op)))
case leftKind&NumberKind != 0 && rightKind&NumberKind != 0:
// n := c.newNum()
return cmpTonode(c, op, c.Num(left, op).X.Cmp(&c.Num(right, op).X))
case leftKind == ListKind && rightKind == ListKind:
x := c.Elems(left)
y := c.Elems(right)
if len(x) != len(y) {
return c.newBool(false)
}
for i, e := range x {
a, _ := c.concrete(nil, e, op)
b, _ := c.concrete(nil, y[i], op)
if !test(c, EqualOp, a, b) {
return c.newBool(false)
}
}
return c.newBool(true)
}
case NotEqualOp:
switch {
case leftKind == NullKind && rightKind == NullKind:
return c.newBool(false)
case leftKind == NullKind || rightKind == NullKind:
return c.newBool(true)
case leftKind == BoolKind:
return c.newBool(c.boolValue(left, op) != c.boolValue(right, op))
case leftKind == StringKind:
// normalize?
return cmpTonode(c, op, strings.Compare(c.StringValue(left), c.StringValue(right)))
case leftKind == BytesKind:
return cmpTonode(c, op, bytes.Compare(c.bytesValue(left, op), c.bytesValue(right, op)))
case leftKind&NumberKind != 0 && rightKind&NumberKind != 0:
// n := c.newNum()
return cmpTonode(c, op, c.Num(left, op).X.Cmp(&c.Num(right, op).X))
case leftKind == ListKind && rightKind == ListKind:
x := c.Elems(left)
y := c.Elems(right)
if len(x) != len(y) {
return c.newBool(true)
}
for i, e := range x {
a, _ := c.concrete(nil, e, op)
b, _ := c.concrete(nil, y[i], op)
if !test(c, EqualOp, a, b) {
return c.newBool(true)
}
}
return c.newBool(false)
}
case LessThanOp, LessEqualOp, GreaterEqualOp, GreaterThanOp:
switch {
case leftKind == StringKind && rightKind == StringKind:
// normalize?
return cmpTonode(c, op, strings.Compare(c.stringValue(left, op), c.stringValue(right, op)))
case leftKind == BytesKind && rightKind == BytesKind:
return cmpTonode(c, op, bytes.Compare(c.bytesValue(left, op), c.bytesValue(right, op)))
case leftKind&NumberKind != 0 && rightKind&NumberKind != 0:
// n := c.newNum(left, right)
return cmpTonode(c, op, c.Num(left, op).X.Cmp(&c.Num(right, op).X))
}
case BoolAndOp:
return c.newBool(c.boolValue(left, op) && c.boolValue(right, op))
case BoolOrOp:
return c.newBool(c.boolValue(left, op) || c.boolValue(right, op))
case MatchOp:
// if y.re == nil {
// // This really should not happen, but leave in for safety.
// b, err := Regexp.MatchString(str, x.str)
// if err != nil {
// return c.Errf(Src, "error parsing Regexp: %v", err)
// }
// return boolTonode(Src, b)
// }
return c.newBool(c.regexp(right).MatchString(c.stringValue(left, op)))
case NotMatchOp:
return c.newBool(!c.regexp(right).MatchString(c.stringValue(left, op)))
case AddOp:
switch {
case leftKind&NumberKind != 0 && rightKind&NumberKind != 0:
return c.Add(c.Num(left, op), c.Num(right, op))
case leftKind == StringKind && rightKind == StringKind:
return c.NewString(c.StringValue(left) + c.StringValue(right))
case leftKind == BytesKind && rightKind == BytesKind:
ba := c.bytesValue(left, op)
bb := c.bytesValue(right, op)
b := make([]byte, len(ba)+len(bb))
copy(b, ba)
copy(b[len(ba):], bb)
return c.newBytes(b)
case leftKind == ListKind && rightKind == ListKind:
return c.NewErrf("Addition of lists is superseded by list.Concat; see https://cuelang.org/e/v0.11-list-arithmetic")
}
case SubtractOp:
return c.Sub(c.Num(left, op), c.Num(right, op))
case MultiplyOp:
switch {
// float
case leftKind&NumberKind != 0 && rightKind&NumberKind != 0:
return c.Mul(c.Num(left, op), c.Num(right, op))
case leftKind == StringKind && rightKind == IntKind:
const as = "string multiplication"
return c.NewString(strings.Repeat(c.stringValue(left, as), int(c.uint64(right, as))))
case leftKind == IntKind && rightKind == StringKind:
const as = "string multiplication"
return c.NewString(strings.Repeat(c.stringValue(right, as), int(c.uint64(left, as))))
case leftKind == BytesKind && rightKind == IntKind:
const as = "bytes multiplication"
return c.newBytes(bytes.Repeat(c.bytesValue(left, as), int(c.uint64(right, as))))
case leftKind == IntKind && rightKind == BytesKind:
const as = "bytes multiplication"
return c.newBytes(bytes.Repeat(c.bytesValue(right, as), int(c.uint64(left, as))))
case leftKind == IntKind && rightKind == ListKind:
fallthrough
case leftKind == ListKind && rightKind == IntKind:
return c.NewErrf("Multiplication of lists is superseded by list.Repeat; see https://cuelang.org/e/v0.11-list-arithmetic")
}
case FloatQuotientOp:
if leftKind&NumberKind != 0 && rightKind&NumberKind != 0 {
return c.Quo(c.Num(left, op), c.Num(right, op))
}
case IntDivideOp:
if leftKind&IntKind != 0 && rightKind&IntKind != 0 {
return c.IntDiv(c.Num(left, op), c.Num(right, op))
}
case IntModuloOp:
if leftKind&IntKind != 0 && rightKind&IntKind != 0 {
return c.IntMod(c.Num(left, op), c.Num(right, op))
}
case IntQuotientOp:
if leftKind&IntKind != 0 && rightKind&IntKind != 0 {
return c.IntQuo(c.Num(left, op), c.Num(right, op))
}
case IntRemainderOp:
if leftKind&IntKind != 0 && rightKind&IntKind != 0 {
return c.IntRem(c.Num(left, op), c.Num(right, op))
}
}
return c.NewErrf("invalid operands %s and %s to '%s' (type %s and %s)",
left, right, op, left.Kind(), right.Kind())
}
func cmpTonode(c *OpContext, op Op, r int) Value {
result := false
switch op {
case LessThanOp:
result = r == -1
case LessEqualOp:
result = r != 1
case EqualOp, AndOp:
result = r == 0
case NotEqualOp:
result = r != 0
case GreaterEqualOp:
result = r != -1
case GreaterThanOp:
result = r == 1
}
return c.newBool(result)
}
|