| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package ext |
| 16 |
|
| 17 |
import ( |
| 18 |
"fmt" |
| 19 |
"math" |
| 20 |
"strings" |
| 21 |
|
| 22 |
"github.com/google/cel-go/cel" |
| 23 |
"github.com/google/cel-go/common/ast" |
| 24 |
"github.com/google/cel-go/common/types" |
| 25 |
"github.com/google/cel-go/common/types/ref" |
| 26 |
"github.com/google/cel-go/common/types/traits" |
| 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 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
func Math(options ...MathOption) cel.EnvOption { |
| 346 |
m := &mathLib{version: math.MaxUint32} |
| 347 |
for _, o := range options { |
| 348 |
m = o(m) |
| 349 |
} |
| 350 |
return cel.Lib(m) |
| 351 |
} |
| 352 |
|
| 353 |
const ( |
| 354 |
mathNamespace = "math" |
| 355 |
leastMacro = "least" |
| 356 |
greatestMacro = "greatest" |
| 357 |
|
| 358 |
|
| 359 |
minFunc = "math.@min" |
| 360 |
maxFunc = "math.@max" |
| 361 |
|
| 362 |
|
| 363 |
ceilFunc = "math.ceil" |
| 364 |
floorFunc = "math.floor" |
| 365 |
roundFunc = "math.round" |
| 366 |
truncFunc = "math.trunc" |
| 367 |
|
| 368 |
|
| 369 |
isInfFunc = "math.isInf" |
| 370 |
isNanFunc = "math.isNaN" |
| 371 |
isFiniteFunc = "math.isFinite" |
| 372 |
|
| 373 |
|
| 374 |
absFunc = "math.abs" |
| 375 |
signFunc = "math.sign" |
| 376 |
|
| 377 |
|
| 378 |
sqrtFunc = "math.sqrt" |
| 379 |
|
| 380 |
|
| 381 |
bitAndFunc = "math.bitAnd" |
| 382 |
bitOrFunc = "math.bitOr" |
| 383 |
bitXorFunc = "math.bitXor" |
| 384 |
bitNotFunc = "math.bitNot" |
| 385 |
bitShiftLeftFunc = "math.bitShiftLeft" |
| 386 |
bitShiftRightFunc = "math.bitShiftRight" |
| 387 |
) |
| 388 |
|
| 389 |
var ( |
| 390 |
errIntOverflow = types.NewErr("integer overflow") |
| 391 |
) |
| 392 |
|
| 393 |
|
| 394 |
type MathOption func(*mathLib) *mathLib |
| 395 |
|
| 396 |
|
| 397 |
func MathVersion(version uint32) MathOption { |
| 398 |
return func(lib *mathLib) *mathLib { |
| 399 |
lib.version = version |
| 400 |
return lib |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
type mathLib struct { |
| 405 |
version uint32 |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
func (*mathLib) LibraryName() string { |
| 410 |
return "cel.lib.ext.math" |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
func (lib *mathLib) CompileOptions() []cel.EnvOption { |
| 415 |
opts := []cel.EnvOption{ |
| 416 |
cel.Macros( |
| 417 |
|
| 418 |
cel.ReceiverVarArgMacro(leastMacro, mathLeast), |
| 419 |
|
| 420 |
cel.ReceiverVarArgMacro(greatestMacro, mathGreatest), |
| 421 |
), |
| 422 |
cel.Function(minFunc, |
| 423 |
cel.Overload("math_@min_double", []*cel.Type{cel.DoubleType}, cel.DoubleType, |
| 424 |
cel.UnaryBinding(identity)), |
| 425 |
cel.Overload("math_@min_int", []*cel.Type{cel.IntType}, cel.IntType, |
| 426 |
cel.UnaryBinding(identity)), |
| 427 |
cel.Overload("math_@min_uint", []*cel.Type{cel.UintType}, cel.UintType, |
| 428 |
cel.UnaryBinding(identity)), |
| 429 |
cel.Overload("math_@min_double_double", []*cel.Type{cel.DoubleType, cel.DoubleType}, cel.DoubleType, |
| 430 |
cel.BinaryBinding(minPair)), |
| 431 |
cel.Overload("math_@min_int_int", []*cel.Type{cel.IntType, cel.IntType}, cel.IntType, |
| 432 |
cel.BinaryBinding(minPair)), |
| 433 |
cel.Overload("math_@min_uint_uint", []*cel.Type{cel.UintType, cel.UintType}, cel.UintType, |
| 434 |
cel.BinaryBinding(minPair)), |
| 435 |
cel.Overload("math_@min_int_uint", []*cel.Type{cel.IntType, cel.UintType}, cel.DynType, |
| 436 |
cel.BinaryBinding(minPair)), |
| 437 |
cel.Overload("math_@min_int_double", []*cel.Type{cel.IntType, cel.DoubleType}, cel.DynType, |
| 438 |
cel.BinaryBinding(minPair)), |
| 439 |
cel.Overload("math_@min_double_int", []*cel.Type{cel.DoubleType, cel.IntType}, cel.DynType, |
| 440 |
cel.BinaryBinding(minPair)), |
| 441 |
cel.Overload("math_@min_double_uint", []*cel.Type{cel.DoubleType, cel.UintType}, cel.DynType, |
| 442 |
cel.BinaryBinding(minPair)), |
| 443 |
cel.Overload("math_@min_uint_int", []*cel.Type{cel.UintType, cel.IntType}, cel.DynType, |
| 444 |
cel.BinaryBinding(minPair)), |
| 445 |
cel.Overload("math_@min_uint_double", []*cel.Type{cel.UintType, cel.DoubleType}, cel.DynType, |
| 446 |
cel.BinaryBinding(minPair)), |
| 447 |
cel.Overload("math_@min_list_double", []*cel.Type{cel.ListType(cel.DoubleType)}, cel.DoubleType, |
| 448 |
cel.UnaryBinding(minList)), |
| 449 |
cel.Overload("math_@min_list_int", []*cel.Type{cel.ListType(cel.IntType)}, cel.IntType, |
| 450 |
cel.UnaryBinding(minList)), |
| 451 |
cel.Overload("math_@min_list_uint", []*cel.Type{cel.ListType(cel.UintType)}, cel.UintType, |
| 452 |
cel.UnaryBinding(minList)), |
| 453 |
), |
| 454 |
cel.Function(maxFunc, |
| 455 |
cel.Overload("math_@max_double", []*cel.Type{cel.DoubleType}, cel.DoubleType, |
| 456 |
cel.UnaryBinding(identity)), |
| 457 |
cel.Overload("math_@max_int", []*cel.Type{cel.IntType}, cel.IntType, |
| 458 |
cel.UnaryBinding(identity)), |
| 459 |
cel.Overload("math_@max_uint", []*cel.Type{cel.UintType}, cel.UintType, |
| 460 |
cel.UnaryBinding(identity)), |
| 461 |
cel.Overload("math_@max_double_double", []*cel.Type{cel.DoubleType, cel.DoubleType}, cel.DoubleType, |
| 462 |
cel.BinaryBinding(maxPair)), |
| 463 |
cel.Overload("math_@max_int_int", []*cel.Type{cel.IntType, cel.IntType}, cel.IntType, |
| 464 |
cel.BinaryBinding(maxPair)), |
| 465 |
cel.Overload("math_@max_uint_uint", []*cel.Type{cel.UintType, cel.UintType}, cel.UintType, |
| 466 |
cel.BinaryBinding(maxPair)), |
| 467 |
cel.Overload("math_@max_int_uint", []*cel.Type{cel.IntType, cel.UintType}, cel.DynType, |
| 468 |
cel.BinaryBinding(maxPair)), |
| 469 |
cel.Overload("math_@max_int_double", []*cel.Type{cel.IntType, cel.DoubleType}, cel.DynType, |
| 470 |
cel.BinaryBinding(maxPair)), |
| 471 |
cel.Overload("math_@max_double_int", []*cel.Type{cel.DoubleType, cel.IntType}, cel.DynType, |
| 472 |
cel.BinaryBinding(maxPair)), |
| 473 |
cel.Overload("math_@max_double_uint", []*cel.Type{cel.DoubleType, cel.UintType}, cel.DynType, |
| 474 |
cel.BinaryBinding(maxPair)), |
| 475 |
cel.Overload("math_@max_uint_int", []*cel.Type{cel.UintType, cel.IntType}, cel.DynType, |
| 476 |
cel.BinaryBinding(maxPair)), |
| 477 |
cel.Overload("math_@max_uint_double", []*cel.Type{cel.UintType, cel.DoubleType}, cel.DynType, |
| 478 |
cel.BinaryBinding(maxPair)), |
| 479 |
cel.Overload("math_@max_list_double", []*cel.Type{cel.ListType(cel.DoubleType)}, cel.DoubleType, |
| 480 |
cel.UnaryBinding(maxList)), |
| 481 |
cel.Overload("math_@max_list_int", []*cel.Type{cel.ListType(cel.IntType)}, cel.IntType, |
| 482 |
cel.UnaryBinding(maxList)), |
| 483 |
cel.Overload("math_@max_list_uint", []*cel.Type{cel.ListType(cel.UintType)}, cel.UintType, |
| 484 |
cel.UnaryBinding(maxList)), |
| 485 |
), |
| 486 |
} |
| 487 |
if lib.version >= 1 { |
| 488 |
opts = append(opts, |
| 489 |
|
| 490 |
cel.Function(ceilFunc, |
| 491 |
cel.Overload("math_ceil_double", []*cel.Type{cel.DoubleType}, cel.DoubleType, |
| 492 |
cel.UnaryBinding(ceil))), |
| 493 |
cel.Function(floorFunc, |
| 494 |
cel.Overload("math_floor_double", []*cel.Type{cel.DoubleType}, cel.DoubleType, |
| 495 |
cel.UnaryBinding(floor))), |
| 496 |
cel.Function(roundFunc, |
| 497 |
cel.Overload("math_round_double", []*cel.Type{cel.DoubleType}, cel.DoubleType, |
| 498 |
cel.UnaryBinding(round))), |
| 499 |
cel.Function(truncFunc, |
| 500 |
cel.Overload("math_trunc_double", []*cel.Type{cel.DoubleType}, cel.DoubleType, |
| 501 |
cel.UnaryBinding(trunc))), |
| 502 |
|
| 503 |
|
| 504 |
cel.Function(isInfFunc, |
| 505 |
cel.Overload("math_isInf_double", []*cel.Type{cel.DoubleType}, cel.BoolType, |
| 506 |
cel.UnaryBinding(isInf))), |
| 507 |
cel.Function(isNanFunc, |
| 508 |
cel.Overload("math_isNaN_double", []*cel.Type{cel.DoubleType}, cel.BoolType, |
| 509 |
cel.UnaryBinding(isNaN))), |
| 510 |
cel.Function(isFiniteFunc, |
| 511 |
cel.Overload("math_isFinite_double", []*cel.Type{cel.DoubleType}, cel.BoolType, |
| 512 |
cel.UnaryBinding(isFinite))), |
| 513 |
|
| 514 |
|
| 515 |
cel.Function(absFunc, |
| 516 |
cel.Overload("math_abs_double", []*cel.Type{cel.DoubleType}, cel.DoubleType, |
| 517 |
cel.UnaryBinding(absDouble)), |
| 518 |
cel.Overload("math_abs_int", []*cel.Type{cel.IntType}, cel.IntType, |
| 519 |
cel.UnaryBinding(absInt)), |
| 520 |
cel.Overload("math_abs_uint", []*cel.Type{cel.UintType}, cel.UintType, |
| 521 |
cel.UnaryBinding(identity)), |
| 522 |
), |
| 523 |
cel.Function(signFunc, |
| 524 |
cel.Overload("math_sign_double", []*cel.Type{cel.DoubleType}, cel.DoubleType, |
| 525 |
cel.UnaryBinding(sign)), |
| 526 |
cel.Overload("math_sign_int", []*cel.Type{cel.IntType}, cel.IntType, |
| 527 |
cel.UnaryBinding(sign)), |
| 528 |
cel.Overload("math_sign_uint", []*cel.Type{cel.UintType}, cel.UintType, |
| 529 |
cel.UnaryBinding(sign)), |
| 530 |
), |
| 531 |
|
| 532 |
|
| 533 |
cel.Function(bitAndFunc, |
| 534 |
cel.Overload("math_bitAnd_int_int", []*cel.Type{cel.IntType, cel.IntType}, cel.IntType, |
| 535 |
cel.BinaryBinding(bitAndPairInt)), |
| 536 |
cel.Overload("math_bitAnd_uint_uint", []*cel.Type{cel.UintType, cel.UintType}, cel.UintType, |
| 537 |
cel.BinaryBinding(bitAndPairUint)), |
| 538 |
), |
| 539 |
cel.Function(bitOrFunc, |
| 540 |
cel.Overload("math_bitOr_int_int", []*cel.Type{cel.IntType, cel.IntType}, cel.IntType, |
| 541 |
cel.BinaryBinding(bitOrPairInt)), |
| 542 |
cel.Overload("math_bitOr_uint_uint", []*cel.Type{cel.UintType, cel.UintType}, cel.UintType, |
| 543 |
cel.BinaryBinding(bitOrPairUint)), |
| 544 |
), |
| 545 |
cel.Function(bitXorFunc, |
| 546 |
cel.Overload("math_bitXor_int_int", []*cel.Type{cel.IntType, cel.IntType}, cel.IntType, |
| 547 |
cel.BinaryBinding(bitXorPairInt)), |
| 548 |
cel.Overload("math_bitXor_uint_uint", []*cel.Type{cel.UintType, cel.UintType}, cel.UintType, |
| 549 |
cel.BinaryBinding(bitXorPairUint)), |
| 550 |
), |
| 551 |
cel.Function(bitNotFunc, |
| 552 |
cel.Overload("math_bitNot_int_int", []*cel.Type{cel.IntType}, cel.IntType, |
| 553 |
cel.UnaryBinding(bitNotInt)), |
| 554 |
cel.Overload("math_bitNot_uint_uint", []*cel.Type{cel.UintType}, cel.UintType, |
| 555 |
cel.UnaryBinding(bitNotUint)), |
| 556 |
), |
| 557 |
cel.Function(bitShiftLeftFunc, |
| 558 |
cel.Overload("math_bitShiftLeft_int_int", []*cel.Type{cel.IntType, cel.IntType}, cel.IntType, |
| 559 |
cel.BinaryBinding(bitShiftLeftIntInt)), |
| 560 |
cel.Overload("math_bitShiftLeft_uint_int", []*cel.Type{cel.UintType, cel.IntType}, cel.UintType, |
| 561 |
cel.BinaryBinding(bitShiftLeftUintInt)), |
| 562 |
), |
| 563 |
cel.Function(bitShiftRightFunc, |
| 564 |
cel.Overload("math_bitShiftRight_int_int", []*cel.Type{cel.IntType, cel.IntType}, cel.IntType, |
| 565 |
cel.BinaryBinding(bitShiftRightIntInt)), |
| 566 |
cel.Overload("math_bitShiftRight_uint_int", []*cel.Type{cel.UintType, cel.IntType}, cel.UintType, |
| 567 |
cel.BinaryBinding(bitShiftRightUintInt)), |
| 568 |
), |
| 569 |
) |
| 570 |
} |
| 571 |
if lib.version >= 2 { |
| 572 |
opts = append(opts, |
| 573 |
cel.Function(sqrtFunc, |
| 574 |
cel.Overload("math_sqrt_double", []*cel.Type{cel.DoubleType}, cel.DoubleType, |
| 575 |
cel.UnaryBinding(sqrt)), |
| 576 |
cel.Overload("math_sqrt_int", []*cel.Type{cel.IntType}, cel.DoubleType, |
| 577 |
cel.UnaryBinding(sqrt)), |
| 578 |
cel.Overload("math_sqrt_uint", []*cel.Type{cel.UintType}, cel.DoubleType, |
| 579 |
cel.UnaryBinding(sqrt)), |
| 580 |
), |
| 581 |
) |
| 582 |
} |
| 583 |
return opts |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
func (*mathLib) ProgramOptions() []cel.ProgramOption { |
| 588 |
return []cel.ProgramOption{} |
| 589 |
} |
| 590 |
|
| 591 |
func mathLeast(meh cel.MacroExprFactory, target ast.Expr, args []ast.Expr) (ast.Expr, *cel.Error) { |
| 592 |
if !macroTargetMatchesNamespace(mathNamespace, target) { |
| 593 |
return nil, nil |
| 594 |
} |
| 595 |
switch len(args) { |
| 596 |
case 0: |
| 597 |
return nil, meh.NewError(target.ID(), "math.least() requires at least one argument") |
| 598 |
case 1: |
| 599 |
if isListLiteralWithNumericArgs(args[0]) || isNumericArgType(args[0]) { |
| 600 |
return meh.NewCall(minFunc, args[0]), nil |
| 601 |
} |
| 602 |
return nil, meh.NewError(args[0].ID(), "math.least() invalid single argument value") |
| 603 |
case 2: |
| 604 |
err := checkInvalidArgs(meh, "math.least()", args) |
| 605 |
if err != nil { |
| 606 |
return nil, err |
| 607 |
} |
| 608 |
return meh.NewCall(minFunc, args...), nil |
| 609 |
default: |
| 610 |
err := checkInvalidArgs(meh, "math.least()", args) |
| 611 |
if err != nil { |
| 612 |
return nil, err |
| 613 |
} |
| 614 |
return meh.NewCall(minFunc, meh.NewList(args...)), nil |
| 615 |
} |
| 616 |
} |
| 617 |
|
| 618 |
func mathGreatest(mef cel.MacroExprFactory, target ast.Expr, args []ast.Expr) (ast.Expr, *cel.Error) { |
| 619 |
if !macroTargetMatchesNamespace(mathNamespace, target) { |
| 620 |
return nil, nil |
| 621 |
} |
| 622 |
switch len(args) { |
| 623 |
case 0: |
| 624 |
return nil, mef.NewError(target.ID(), "math.greatest() requires at least one argument") |
| 625 |
case 1: |
| 626 |
if isListLiteralWithNumericArgs(args[0]) || isNumericArgType(args[0]) { |
| 627 |
return mef.NewCall(maxFunc, args[0]), nil |
| 628 |
} |
| 629 |
return nil, mef.NewError(args[0].ID(), "math.greatest() invalid single argument value") |
| 630 |
case 2: |
| 631 |
err := checkInvalidArgs(mef, "math.greatest()", args) |
| 632 |
if err != nil { |
| 633 |
return nil, err |
| 634 |
} |
| 635 |
return mef.NewCall(maxFunc, args...), nil |
| 636 |
default: |
| 637 |
err := checkInvalidArgs(mef, "math.greatest()", args) |
| 638 |
if err != nil { |
| 639 |
return nil, err |
| 640 |
} |
| 641 |
return mef.NewCall(maxFunc, mef.NewList(args...)), nil |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
func identity(val ref.Val) ref.Val { |
| 646 |
return val |
| 647 |
} |
| 648 |
|
| 649 |
func ceil(val ref.Val) ref.Val { |
| 650 |
v := val.(types.Double) |
| 651 |
return types.Double(math.Ceil(float64(v))) |
| 652 |
} |
| 653 |
|
| 654 |
func floor(val ref.Val) ref.Val { |
| 655 |
v := val.(types.Double) |
| 656 |
return types.Double(math.Floor(float64(v))) |
| 657 |
} |
| 658 |
|
| 659 |
func round(val ref.Val) ref.Val { |
| 660 |
v := val.(types.Double) |
| 661 |
return types.Double(math.Round(float64(v))) |
| 662 |
} |
| 663 |
|
| 664 |
func trunc(val ref.Val) ref.Val { |
| 665 |
v := val.(types.Double) |
| 666 |
return types.Double(math.Trunc(float64(v))) |
| 667 |
} |
| 668 |
|
| 669 |
func isInf(val ref.Val) ref.Val { |
| 670 |
v := val.(types.Double) |
| 671 |
return types.Bool(math.IsInf(float64(v), 0)) |
| 672 |
} |
| 673 |
|
| 674 |
func isFinite(val ref.Val) ref.Val { |
| 675 |
v := float64(val.(types.Double)) |
| 676 |
return types.Bool(!math.IsInf(v, 0) && !math.IsNaN(v)) |
| 677 |
} |
| 678 |
|
| 679 |
func isNaN(val ref.Val) ref.Val { |
| 680 |
v := val.(types.Double) |
| 681 |
return types.Bool(math.IsNaN(float64(v))) |
| 682 |
} |
| 683 |
|
| 684 |
func absDouble(val ref.Val) ref.Val { |
| 685 |
v := float64(val.(types.Double)) |
| 686 |
return types.Double(math.Abs(v)) |
| 687 |
} |
| 688 |
|
| 689 |
func absInt(val ref.Val) ref.Val { |
| 690 |
v := int64(val.(types.Int)) |
| 691 |
if v == math.MinInt64 { |
| 692 |
return errIntOverflow |
| 693 |
} |
| 694 |
if v >= 0 { |
| 695 |
return val |
| 696 |
} |
| 697 |
return -types.Int(v) |
| 698 |
} |
| 699 |
|
| 700 |
func sign(val ref.Val) ref.Val { |
| 701 |
switch v := val.(type) { |
| 702 |
case types.Double: |
| 703 |
if isNaN(v) == types.True { |
| 704 |
return v |
| 705 |
} |
| 706 |
zero := types.Double(0) |
| 707 |
if v > zero { |
| 708 |
return types.Double(1) |
| 709 |
} |
| 710 |
if v < zero { |
| 711 |
return types.Double(-1) |
| 712 |
} |
| 713 |
return zero |
| 714 |
case types.Int: |
| 715 |
return v.Compare(types.IntZero) |
| 716 |
case types.Uint: |
| 717 |
if v == types.Uint(0) { |
| 718 |
return types.Uint(0) |
| 719 |
} |
| 720 |
return types.Uint(1) |
| 721 |
default: |
| 722 |
return maybeSuffixError(val, "math.sign") |
| 723 |
} |
| 724 |
} |
| 725 |
|
| 726 |
|
| 727 |
func sqrt(val ref.Val) ref.Val { |
| 728 |
switch v := val.(type) { |
| 729 |
case types.Double: |
| 730 |
return types.Double(math.Sqrt(float64(v))) |
| 731 |
case types.Int: |
| 732 |
return types.Double(math.Sqrt(float64(v))) |
| 733 |
case types.Uint: |
| 734 |
return types.Double(math.Sqrt(float64(v))) |
| 735 |
default: |
| 736 |
return types.NewErr("no such overload: sqrt") |
| 737 |
} |
| 738 |
} |
| 739 |
|
| 740 |
|
| 741 |
func bitAndPairInt(first, second ref.Val) ref.Val { |
| 742 |
l := first.(types.Int) |
| 743 |
r := second.(types.Int) |
| 744 |
return l & r |
| 745 |
} |
| 746 |
|
| 747 |
func bitAndPairUint(first, second ref.Val) ref.Val { |
| 748 |
l := first.(types.Uint) |
| 749 |
r := second.(types.Uint) |
| 750 |
return l & r |
| 751 |
} |
| 752 |
|
| 753 |
func bitOrPairInt(first, second ref.Val) ref.Val { |
| 754 |
l := first.(types.Int) |
| 755 |
r := second.(types.Int) |
| 756 |
return l | r |
| 757 |
} |
| 758 |
|
| 759 |
func bitOrPairUint(first, second ref.Val) ref.Val { |
| 760 |
l := first.(types.Uint) |
| 761 |
r := second.(types.Uint) |
| 762 |
return l | r |
| 763 |
} |
| 764 |
|
| 765 |
func bitXorPairInt(first, second ref.Val) ref.Val { |
| 766 |
l := first.(types.Int) |
| 767 |
r := second.(types.Int) |
| 768 |
return l ^ r |
| 769 |
} |
| 770 |
|
| 771 |
func bitXorPairUint(first, second ref.Val) ref.Val { |
| 772 |
l := first.(types.Uint) |
| 773 |
r := second.(types.Uint) |
| 774 |
return l ^ r |
| 775 |
} |
| 776 |
|
| 777 |
func bitNotInt(value ref.Val) ref.Val { |
| 778 |
v := value.(types.Int) |
| 779 |
return ^v |
| 780 |
} |
| 781 |
|
| 782 |
func bitNotUint(value ref.Val) ref.Val { |
| 783 |
v := value.(types.Uint) |
| 784 |
return ^v |
| 785 |
} |
| 786 |
|
| 787 |
func bitShiftLeftIntInt(value, bits ref.Val) ref.Val { |
| 788 |
v := value.(types.Int) |
| 789 |
bs := bits.(types.Int) |
| 790 |
if bs < types.IntZero { |
| 791 |
return types.NewErr("math.bitShiftLeft() negative offset: %d", bs) |
| 792 |
} |
| 793 |
return v << bs |
| 794 |
} |
| 795 |
|
| 796 |
func bitShiftLeftUintInt(value, bits ref.Val) ref.Val { |
| 797 |
v := value.(types.Uint) |
| 798 |
bs := bits.(types.Int) |
| 799 |
if bs < types.IntZero { |
| 800 |
return types.NewErr("math.bitShiftLeft() negative offset: %d", bs) |
| 801 |
} |
| 802 |
return v << bs |
| 803 |
} |
| 804 |
|
| 805 |
func bitShiftRightIntInt(value, bits ref.Val) ref.Val { |
| 806 |
v := value.(types.Int) |
| 807 |
bs := bits.(types.Int) |
| 808 |
if bs < types.IntZero { |
| 809 |
return types.NewErr("math.bitShiftRight() negative offset: %d", bs) |
| 810 |
} |
| 811 |
return types.Int(types.Uint(v) >> bs) |
| 812 |
} |
| 813 |
|
| 814 |
func bitShiftRightUintInt(value, bits ref.Val) ref.Val { |
| 815 |
v := value.(types.Uint) |
| 816 |
bs := bits.(types.Int) |
| 817 |
if bs < types.IntZero { |
| 818 |
return types.NewErr("math.bitShiftRight() negative offset: %d", bs) |
| 819 |
} |
| 820 |
return v >> bs |
| 821 |
} |
| 822 |
|
| 823 |
func minPair(first, second ref.Val) ref.Val { |
| 824 |
cmp, ok := first.(traits.Comparer) |
| 825 |
if !ok { |
| 826 |
return types.MaybeNoSuchOverloadErr(first) |
| 827 |
} |
| 828 |
out := cmp.Compare(second) |
| 829 |
if types.IsUnknownOrError(out) { |
| 830 |
return maybeSuffixError(out, "math.@min") |
| 831 |
} |
| 832 |
if out == types.IntOne { |
| 833 |
return second |
| 834 |
} |
| 835 |
return first |
| 836 |
} |
| 837 |
|
| 838 |
func minList(numList ref.Val) ref.Val { |
| 839 |
l := numList.(traits.Lister) |
| 840 |
size := l.Size().(types.Int) |
| 841 |
if size == types.IntZero { |
| 842 |
return types.NewErr("math.@min(list) argument must not be empty") |
| 843 |
} |
| 844 |
min := l.Get(types.IntZero) |
| 845 |
for i := types.IntOne; i < size; i++ { |
| 846 |
min = minPair(min, l.Get(i)) |
| 847 |
} |
| 848 |
switch min.Type() { |
| 849 |
case types.IntType, types.DoubleType, types.UintType, types.UnknownType: |
| 850 |
return min |
| 851 |
default: |
| 852 |
return types.NewErr("no such overload: math.@min") |
| 853 |
} |
| 854 |
} |
| 855 |
|
| 856 |
func maxPair(first, second ref.Val) ref.Val { |
| 857 |
cmp, ok := first.(traits.Comparer) |
| 858 |
if !ok { |
| 859 |
return types.MaybeNoSuchOverloadErr(first) |
| 860 |
} |
| 861 |
out := cmp.Compare(second) |
| 862 |
if types.IsUnknownOrError(out) { |
| 863 |
return maybeSuffixError(out, "math.@max") |
| 864 |
} |
| 865 |
if out == types.IntNegOne { |
| 866 |
return second |
| 867 |
} |
| 868 |
return first |
| 869 |
} |
| 870 |
|
| 871 |
func maxList(numList ref.Val) ref.Val { |
| 872 |
l := numList.(traits.Lister) |
| 873 |
size := l.Size().(types.Int) |
| 874 |
if size == types.IntZero { |
| 875 |
return types.NewErr("math.@max(list) argument must not be empty") |
| 876 |
} |
| 877 |
max := l.Get(types.IntZero) |
| 878 |
for i := types.IntOne; i < size; i++ { |
| 879 |
max = maxPair(max, l.Get(i)) |
| 880 |
} |
| 881 |
switch max.Type() { |
| 882 |
case types.IntType, types.DoubleType, types.UintType, types.UnknownType: |
| 883 |
return max |
| 884 |
default: |
| 885 |
return types.NewErr("no such overload: math.@max") |
| 886 |
} |
| 887 |
} |
| 888 |
|
| 889 |
func checkInvalidArgs(meh cel.MacroExprFactory, funcName string, args []ast.Expr) *cel.Error { |
| 890 |
for _, arg := range args { |
| 891 |
err := checkInvalidArgLiteral(funcName, arg) |
| 892 |
if err != nil { |
| 893 |
return meh.NewError(arg.ID(), err.Error()) |
| 894 |
} |
| 895 |
} |
| 896 |
return nil |
| 897 |
} |
| 898 |
|
| 899 |
func checkInvalidArgLiteral(funcName string, arg ast.Expr) error { |
| 900 |
if !isNumericArgType(arg) { |
| 901 |
return fmt.Errorf("%s simple literal arguments must be numeric", funcName) |
| 902 |
} |
| 903 |
return nil |
| 904 |
} |
| 905 |
|
| 906 |
func isNumericArgType(arg ast.Expr) bool { |
| 907 |
switch arg.Kind() { |
| 908 |
case ast.LiteralKind: |
| 909 |
c := ref.Val(arg.AsLiteral()) |
| 910 |
switch c.(type) { |
| 911 |
case types.Double, types.Int, types.Uint: |
| 912 |
return true |
| 913 |
default: |
| 914 |
return false |
| 915 |
} |
| 916 |
case ast.ListKind, ast.MapKind, ast.StructKind: |
| 917 |
return false |
| 918 |
default: |
| 919 |
return true |
| 920 |
} |
| 921 |
} |
| 922 |
|
| 923 |
func isListLiteralWithNumericArgs(arg ast.Expr) bool { |
| 924 |
switch arg.Kind() { |
| 925 |
case ast.ListKind: |
| 926 |
list := arg.AsList() |
| 927 |
if list.Size() == 0 { |
| 928 |
return false |
| 929 |
} |
| 930 |
for _, e := range list.Elements() { |
| 931 |
if !isNumericArgType(e) { |
| 932 |
return false |
| 933 |
} |
| 934 |
} |
| 935 |
return true |
| 936 |
} |
| 937 |
return false |
| 938 |
} |
| 939 |
|
| 940 |
func maybeSuffixError(val ref.Val, suffix string) ref.Val { |
| 941 |
if types.IsError(val) { |
| 942 |
msg := val.(*types.Err).String() |
| 943 |
if !strings.Contains(msg, suffix) { |
| 944 |
return types.NewErr("%s: %s", msg, suffix) |
| 945 |
} |
| 946 |
} |
| 947 |
return val |
| 948 |
} |