(kind quantifierKind, eh ExprHelper, target ast.Expr, args []ast.Expr)
| 556 | } |
| 557 | |
| 558 | func 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 | |
| 597 | func extractIdent(e ast.Expr) (string, bool) { |
| 598 | switch e.Kind() { |
no test coverage detected