MCPcopy Create free account
hub / github.com/cel-expr/cel-go / makeQuantifier

Function makeQuantifier

parser/macro.go:558–595  ·  view source on GitHub ↗
(kind quantifierKind, eh ExprHelper, target ast.Expr, args []ast.Expr)

Source from the content-addressed store, hash-verified

556}
557
558func makeQuantifier(kind quantifierKind, eh ExprHelper, target ast.Expr, args []ast.Expr) (ast.Expr, *common.Error) {
559 v, found := extractIdent(args[0])
560 if !found {
561 return nil, eh.NewError(args[0].ID(), "argument must be a simple name")
562 }
563 accu := eh.AccuIdentName()
564 if v == accu || v == AccumulatorName {
565 return nil, eh.NewError(args[0].ID(), "iteration variable overwrites accumulator variable")
566 }
567
568 var init ast.Expr
569 var condition ast.Expr
570 var step ast.Expr
571 var result ast.Expr
572 switch kind {
573 case quantifierAll:
574 init = eh.NewLiteral(types.True)
575 condition = eh.NewCall(operators.NotStrictlyFalse, eh.NewAccuIdent())
576 step = eh.NewCall(operators.LogicalAnd, eh.NewAccuIdent(), args[1])
577 result = eh.NewAccuIdent()
578 case quantifierExists:
579 init = eh.NewLiteral(types.False)
580 condition = eh.NewCall(
581 operators.NotStrictlyFalse,
582 eh.NewCall(operators.LogicalNot, eh.NewAccuIdent()))
583 step = eh.NewCall(operators.LogicalOr, eh.NewAccuIdent(), args[1])
584 result = eh.NewAccuIdent()
585 case quantifierExistsOne:
586 init = eh.NewLiteral(types.Int(0))
587 condition = eh.NewLiteral(types.True)
588 step = eh.NewCall(operators.Conditional, args[1],
589 eh.NewCall(operators.Add, eh.NewAccuIdent(), eh.NewLiteral(types.Int(1))), eh.NewAccuIdent())
590 result = eh.NewCall(operators.Equals, eh.NewAccuIdent(), eh.NewLiteral(types.Int(1)))
591 default:
592 return nil, eh.NewError(args[0].ID(), fmt.Sprintf("unrecognized quantifier '%v'", kind))
593 }
594 return eh.NewComprehension(target, v, accu, init, condition, step, result), nil
595}
596
597func extractIdent(e ast.Expr) (string, bool) {
598 switch e.Kind() {

Callers 3

MakeAllFunction · 0.85
MakeExistsFunction · 0.85
MakeExistsOneFunction · 0.85

Calls 9

IntTypeAlias · 0.92
extractIdentFunction · 0.70
NewErrorMethod · 0.65
IDMethod · 0.65
AccuIdentNameMethod · 0.65
NewLiteralMethod · 0.65
NewCallMethod · 0.65
NewAccuIdentMethod · 0.65
NewComprehensionMethod · 0.65

Tested by

no test coverage detected