mirror of
https://github.com/Icinga/icinga2.git
synced 2025-07-25 22:54:57 +02:00
Merge pull request #7442 from Icinga/feature/ternary-operator
DSL: introduce x?y:z
This commit is contained in:
commit
1869dff746
@ -208,6 +208,7 @@ Operator | Precedence | Examples (Result) | Descript
|
|||||||
`&&` | 13 | true && false (false), 3 && 7 (7), 0 && 7 (0) | Logical AND
|
`&&` | 13 | true && false (false), 3 && 7 (7), 0 && 7 (0) | Logical AND
|
||||||
`=` | 14 | a = 3 | Assignment
|
`=` | 14 | a = 3 | Assignment
|
||||||
`=>` | 15 | x => x * x (function with arg x) | Lambda, for loop
|
`=>` | 15 | x => x * x (function with arg x) | Lambda, for loop
|
||||||
|
`?` | 16 | (2 * 3 > 5) ? 1 : 0 (1) | [Ternary operator](17-language-reference.md#conditional-statements-ternary)
|
||||||
|
|
||||||
### References <a id="references"></a>
|
### References <a id="references"></a>
|
||||||
|
|
||||||
@ -977,6 +978,8 @@ function MakeHelloFunction(name) {
|
|||||||
|
|
||||||
## Conditional Statements <a id="conditional-statements"></a>
|
## Conditional Statements <a id="conditional-statements"></a>
|
||||||
|
|
||||||
|
### Conditional Statements: if/else <a id="conditional-statements-if-else"></a>
|
||||||
|
|
||||||
Sometimes it can be desirable to only evaluate statements when certain conditions are met. The if/else
|
Sometimes it can be desirable to only evaluate statements when certain conditions are met. The if/else
|
||||||
construct can be used to accomplish this.
|
construct can be used to accomplish this.
|
||||||
|
|
||||||
@ -1011,6 +1014,56 @@ This example prints the log message "Taking the 'true' branch" and the `a` varia
|
|||||||
|
|
||||||
The value of an if/else construct is null if the condition evaluates to false and no else branch is given.
|
The value of an if/else construct is null if the condition evaluates to false and no else branch is given.
|
||||||
|
|
||||||
|
### Conditional Statements: Ternary Operator <a id="conditional-statements-ternary"></a>
|
||||||
|
|
||||||
|
Instead of if/else condition chains, you can also use the ternary operator `?`
|
||||||
|
with assignments. Values are separated with a colon `:` character.
|
||||||
|
|
||||||
|
```
|
||||||
|
cond ? cond_val_true : cond_val_false
|
||||||
|
```
|
||||||
|
|
||||||
|
Whether the first condition matches, the first value is returned, if not, the else and second
|
||||||
|
branch value is returned.
|
||||||
|
|
||||||
|
The following example evaluates a condition and either assigns `1` or `0`
|
||||||
|
to the local variable.
|
||||||
|
|
||||||
|
```
|
||||||
|
<1> => var x = (2 * 3 > 5) ? 1 : 0
|
||||||
|
null
|
||||||
|
<2> => x
|
||||||
|
1.000000
|
||||||
|
<3> => var x = (2 * 3 > 7) ? 1 : 0
|
||||||
|
null
|
||||||
|
<4> => x
|
||||||
|
0.000000
|
||||||
|
```
|
||||||
|
|
||||||
|
Additional examples with advanced condition chaining:
|
||||||
|
|
||||||
|
```
|
||||||
|
<1> => 1 ? 2 : 3 ? 4 : 5 ? 6 : 7
|
||||||
|
2.000000
|
||||||
|
<2> => 0 ? 2 : 3 ? 4 : 5 ? 6 : 7
|
||||||
|
4.000000
|
||||||
|
<3> => 0 ? 2 : 0 ? 4 : 5 ? 6 : 7
|
||||||
|
6.000000
|
||||||
|
<4> => 0 ? 2 : 0 ? 4 : 0 ? 6 : 7
|
||||||
|
7.000000
|
||||||
|
<5> => 1 + 0 ? 2 : 3 + 4
|
||||||
|
2.000000
|
||||||
|
<6> => 0 + 0 ? 2 : 3 + 4
|
||||||
|
7.000000
|
||||||
|
<7> => (()=>{ return 1 ? 2 : 3 })()
|
||||||
|
2.000000
|
||||||
|
<8> => var x = 1 ? 2 : 3
|
||||||
|
null
|
||||||
|
<9> => x
|
||||||
|
2.000000
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
## While Loops <a id="while-loops"></a>
|
## While Loops <a id="while-loops"></a>
|
||||||
|
|
||||||
The `while` statement checks a condition and executes the loop body when the condition evaluates to `true`.
|
The `while` statement checks a condition and executes the loop body when the condition evaluates to `true`.
|
||||||
|
@ -197,6 +197,7 @@ static void MakeRBinaryOp(Expression** result, Expression *left, Expression *rig
|
|||||||
%right T_INCLUDE T_INCLUDE_RECURSIVE T_INCLUDE_ZONES T_OBJECT T_TEMPLATE T_APPLY T_IMPORT T_ASSIGN T_IGNORE T_WHERE
|
%right T_INCLUDE T_INCLUDE_RECURSIVE T_INCLUDE_ZONES T_OBJECT T_TEMPLATE T_APPLY T_IMPORT T_ASSIGN T_IGNORE T_WHERE
|
||||||
%right T_FUNCTION T_FOR
|
%right T_FUNCTION T_FOR
|
||||||
%left T_SET T_SET_ADD T_SET_SUBTRACT T_SET_MULTIPLY T_SET_DIVIDE T_SET_MODULO T_SET_XOR T_SET_BINARY_AND T_SET_BINARY_OR
|
%left T_SET T_SET_ADD T_SET_SUBTRACT T_SET_MULTIPLY T_SET_DIVIDE T_SET_MODULO T_SET_XOR T_SET_BINARY_AND T_SET_BINARY_OR
|
||||||
|
%right '?' ':'
|
||||||
%left T_LOGICAL_OR
|
%left T_LOGICAL_OR
|
||||||
%left T_LOGICAL_AND
|
%left T_LOGICAL_AND
|
||||||
%left T_RETURN T_BREAK T_CONTINUE
|
%left T_RETURN T_BREAK T_CONTINUE
|
||||||
@ -831,6 +832,10 @@ rterm_side_effect: rterm '(' rterm_items ')'
|
|||||||
|
|
||||||
$$ = new ConditionalExpression(std::unique_ptr<Expression>($3), std::unique_ptr<Expression>($5), std::move(afalse), @$);
|
$$ = new ConditionalExpression(std::unique_ptr<Expression>($3), std::unique_ptr<Expression>($5), std::move(afalse), @$);
|
||||||
}
|
}
|
||||||
|
| rterm '?' rterm ':' rterm
|
||||||
|
{
|
||||||
|
$$ = new ConditionalExpression(std::unique_ptr<Expression>($1), std::unique_ptr<Expression>($3), std::unique_ptr<Expression>($5), @$);
|
||||||
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
rterm_no_side_effect_no_dict: T_STRING
|
rterm_no_side_effect_no_dict: T_STRING
|
||||||
|
Loading…
x
Reference in New Issue
Block a user