Document operator precedence

fixes #7816
This commit is contained in:
Gunnar Beutner 2015-02-02 12:35:07 +01:00
parent 4771f79f35
commit 3b3a65c9ae
3 changed files with 44 additions and 40 deletions

View File

@ -147,36 +147,40 @@ strings and numbers.
### <a id="expression-operators"></a> Operators ### <a id="expression-operators"></a> Operators
The following operators are supported in expressions: The following operators are supported in expressions. The operators are by descending precedence.
Operator | Examples (Result) | Description Operator | Precedence | Examples (Result) | Description
---------|-----------------------------------------------|-------------------------------- ---------|------------|-----------------------------------------------|--------------------------------
! | !"Hello" (false), !false (true) | Logical negation of the operand () | 1 | (3 + 3) * 5 | Groups sub-expressions
~ | ~true (false) | Bitwise negation of the operand () | 1 | Math.random() | Calls a function
+ | 1 + 3 (4), "hello " + "world" ("hello world") | Adds two numbers; concatenates strings [] | 1 | a[3] | Array subscript
+ | +3 | Unary plus . | 1 | a.b | Element access
- | 3 - 1 (2) | Subtracts two numbers ! | 2 | !"Hello" (false), !false (true) | Logical negation of the operand
- | -3 | Unary minus ~ | 2 | ~true (false) | Bitwise negation of the operand
* | 5m * 10 (3000) | Multiplies two numbers + | 2 | +3 | Unary plus
/ | 5m / 5 (60) | Divides two numbers - | 2 | -3 | Unary minus
% | 17 % 12 (5) | Remainder after division * | 3 | 5m * 10 (3000) | Multiplies two numbers
^ | 17 ^ 12 (29) | Bitwise XOR / | 3 | 5m / 5 (60) | Divides two numbers
& | 7 & 3 (3) | Binary AND % | 3 | 17 % 12 (5) | Remainder after division
&#124; | 2 &#124; 3 (3) | Binary OR + | 4 | 1 + 3 (4), "hello " + "world" ("hello world") | Adds two numbers; concatenates strings
&& | true && false (false), 3 && 7 (7), 0 && 7 (0) | Logical AND - | 4 | 3 - 1 (2) | Subtracts two numbers
&#124;&#124; | true &#124;&#124; false (true), 0 || 7 (7)| Logical OR << | 5 | 4 << 8 (1024) | Left shift
< | 3 < 5 (true) | Less than >> | 5 | 1024 >> 4 (64) | Right shift
> | 3 > 5 (false) | Greater than < | 6 | 3 < 5 (true) | Less than
<= | 3 <= 3 (true) | Less than or equal > | 6 | 3 > 5 (false) | Greater than
>= | 3 >= 3 (true) | Greater than or equal <= | 6 | 3 <= 3 (true) | Less than or equal
<< | 4 << 8 (1024) | Left shift >= | 6 | 3 >= 3 (true) | Greater than or equal
>> | 1024 >> 4 (64) | Right shift in | 7 | "foo" in [ "foo", "bar" ] (true) | Element contained in array
== | "hello" == "hello" (true), 3 == 5 (false) | Equal to !in | 7 | "foo" !in [ "bar", "baz" ] (true) | Element not contained in array
!= | "hello" != "world" (true), 3 != 3 (false) | Not equal to == | 8 | "hello" == "hello" (true), 3 == 5 (false) | Equal to
in | "foo" in [ "foo", "bar" ] (true) | Element contained in array != | 8 | "hello" != "world" (true), 3 != 3 (false) | Not equal to
!in | "foo" !in [ "bar", "baz" ] (true) | Element not contained in array & | 9 | 7 & 3 (3) | Binary AND
() | (3 + 3) * 5 | Groups sub-expressions ^ | 10 | 17 ^ 12 (29) | Bitwise XOR
() | Math.random() | Calls a function &#124; | 11 | 2 &#124; 3 (3) | Binary OR
&& | 13 | true && false (false), 3 && 7 (7), 0 && 7 (0) | Logical AND
&#124;&#124; | 14 | true &#124;&#124; false (true), 0 || 7 (7)| Logical OR
= | 12 | a = 3 | Assignment
=> | 15 | x => x * x (function with arg x) | Lambda, for loop
### <a id="function-calls"></a> Function Calls ### <a id="function-calls"></a> Function Calls

View File

@ -210,20 +210,20 @@ static void MakeRBinaryOp(Expression** result, Expression *left, Expression *rig
%right T_FOLLOWS %right T_FOLLOWS
%right T_INCLUDE T_INCLUDE_RECURSIVE T_OBJECT T_TEMPLATE T_APPLY T_IMPORT T_ASSIGN T_IGNORE T_WHERE %right T_INCLUDE T_INCLUDE_RECURSIVE 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_LOGICAL_OR %left T_LOGICAL_OR
%left T_LOGICAL_AND %left T_LOGICAL_AND
%left T_RETURN %left T_RETURN
%left T_IDENTIFIER %left T_IDENTIFIER
%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
%nonassoc T_EQUAL T_NOT_EQUAL
%nonassoc T_LESS_THAN T_LESS_THAN_OR_EQUAL T_GREATER_THAN T_GREATER_THAN_OR_EQUAL
%left T_BINARY_OR %left T_BINARY_OR
%left T_XOR T_MODULO %left T_XOR
%left T_BINARY_AND %left T_BINARY_AND
%nonassoc T_EQUAL T_NOT_EQUAL
%left T_IN T_NOT_IN %left T_IN T_NOT_IN
%nonassoc T_LESS_THAN T_LESS_THAN_OR_EQUAL T_GREATER_THAN T_GREATER_THAN_OR_EQUAL
%left T_SHIFT_LEFT T_SHIFT_RIGHT %left T_SHIFT_LEFT T_SHIFT_RIGHT
%left T_PLUS T_MINUS %left T_PLUS T_MINUS
%left T_MULTIPLY T_DIVIDE_OP %left T_MULTIPLY T_DIVIDE_OP T_MODULO
%left UNARY_MINUS UNARY_PLUS %left UNARY_MINUS UNARY_PLUS
%right '!' '~' %right '!' '~'
%left '.' '(' '[' %left '.' '(' '['

View File

@ -268,15 +268,15 @@ BOOST_AUTO_TEST_CASE(advanced)
BOOST_CHECK(expr->Evaluate(frame)); BOOST_CHECK(expr->Evaluate(frame));
delete expr; delete expr;
expr = ConfigCompiler::CompileText("<test>", "7 | 8 == 15"); expr = ConfigCompiler::CompileText("<test>", "(7 | 8) == 15");
BOOST_CHECK(expr->Evaluate(frame)); BOOST_CHECK(expr->Evaluate(frame));
delete expr; delete expr;
expr = ConfigCompiler::CompileText("<test>", "7 ^ 8 == 15"); expr = ConfigCompiler::CompileText("<test>", "(7 ^ 8) == 15");
BOOST_CHECK(expr->Evaluate(frame)); BOOST_CHECK(expr->Evaluate(frame));
delete expr; delete expr;
expr = ConfigCompiler::CompileText("<test>", "7 & 15 == 7"); expr = ConfigCompiler::CompileText("<test>", "(7 & 15) == 7");
BOOST_CHECK(expr->Evaluate(frame)); BOOST_CHECK(expr->Evaluate(frame));
delete expr; delete expr;
@ -288,15 +288,15 @@ BOOST_AUTO_TEST_CASE(advanced)
BOOST_CHECK(expr->Evaluate(frame)); BOOST_CHECK(expr->Evaluate(frame));
delete expr; delete expr;
expr = ConfigCompiler::CompileText("<test>", "7 | 8 > 14"); expr = ConfigCompiler::CompileText("<test>", "(7 | 8) > 14");
BOOST_CHECK(expr->Evaluate(frame)); BOOST_CHECK(expr->Evaluate(frame));
delete expr; delete expr;
expr = ConfigCompiler::CompileText("<test>", "7 ^ 8 > 14"); expr = ConfigCompiler::CompileText("<test>", "(7 ^ 8) > 14");
BOOST_CHECK(expr->Evaluate(frame)); BOOST_CHECK(expr->Evaluate(frame));
delete expr; delete expr;
expr = ConfigCompiler::CompileText("<test>", "7 & 15 > 6"); expr = ConfigCompiler::CompileText("<test>", "(7 & 15) > 6");
BOOST_CHECK(expr->Evaluate(frame)); BOOST_CHECK(expr->Evaluate(frame));
delete expr; delete expr;