From c977e82d9ec2424680b14749737a0dfde4720f62 Mon Sep 17 00:00:00 2001 From: Celestino Amoroso Date: Mon, 6 May 2024 04:24:17 +0200 Subject: [PATCH] doc/Expr.adoc: description of the selector operator; added operators to the priority table --- doc/Expr.adoc | 76 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 45 insertions(+), 31 deletions(-) diff --git a/doc/Expr.adoc b/doc/Expr.adoc index 604b473..09d951d 100644 --- a/doc/Expr.adoc +++ b/doc/Expr.adoc @@ -169,7 +169,7 @@ x = 1; y = 2*x == Other operations === [blue]`;` operator -The semicolon operator [blue]`;` is an infixed operator. It evaluates the left expression first and then the right expression. The latter is the final result. +The semicolon operator [blue]`;` is an infixed pseudo-operator. It evaluates the left expression first and then the right expression. The latter is the final result. IMPORTANT: Technically [blue]`;` is not treated as a real operator. It acts as a separator in lists of expressions. @@ -187,7 +187,7 @@ a=1; b=2; c=3; a+b+c // returns 6 [blue]`but` is very similar to [blue]`;`. The only difference is that [blue]`;` can't be used inside parenthesis [blue]`(` and [blue]`)`. === Assignment operator [blue]`=` -The assignment operator [blue]`=` is used to define variable in the evaluation context or to change their value (see _ExprContext_). +The assignment operator [blue]`=` is used to define variables in the evaluation context or to change their value (see _ExprContext_). The value on the left side of [blue]`=` must be an identifier. The value on the right side can be any expression and it becomes the result of the assignment operation. .Example @@ -202,13 +202,22 @@ The _selector operator_ is very similar to the _switch/case/default_ statement a .Syntax [source,bnf] ---- - ::= "?" { ":" } ["::" ] - ::= [] - ::= "{" "}" - ::= {";" } + ::= "?" { ":" } ["::" ] + ::= [] + ::= "["{","}"]" + ::= ::= "{" "}" + ::= {";" } ---- -.Example +In other words, the selector operator evaluates the expression (``) on the left-hand side of the `?` symbol; it then compares the result obtained with the values listed in the ``'s. If the comparision find a match with a value in a match-list, the associated `` is evaluted, and its result will be the final result of the selection operation. + +The match lists are optional. In that case, the position, from left to right, of the `` is used as match-list. Of course, that only works if the select-expression results in an integer. + +The `:` symbol (colon) is the separator of the selector-cases. Note that if the value of the select-expression does not match any match-list, an error will be issued. Therefore, it is strongly recommended to provide a default (multi-)expression introduced by the `::` symbol (double-colon). Also note that the default expression has no match-list. + + +.Examples [source,go] ---- 1 ? {"a"} : {"b"} // returns "b" @@ -224,33 +233,38 @@ The _selector operator_ is very similar to the _switch/case/default_ statement a The table below shows all supported operators by decreasing priorities. .Operators priorities -[cols="^2,^2,^2,^5,<5"] +[cols="^2,^2,^2,^5,^5"] |=== | Priority | Operators | Position | Operation | Operands and results -1+|*FACT*| [blue]`!` | _Postfix_ | _Factorial_| _integer_ "!" -> _integer_ -1+|*SIGN*| [blue]`+`, [blue]`-` | _Prefix_ | _Change-sign_| ("+"\|"-") _number_ -> _number_ -.5+|*PROD*| [blue]`*` | _Infix_ | _Product_ | _number_ "*" _number_ -> _number_ -| [blue]`*` | _Infix_ | _String-repeat_ | _string_ "*" _integer_ -> _string_ -| [blue]`/` | _Infix_ | _Division_ | _number_ "/" _number_ -> _number_ -| [blue]`./` | _Infix_ | _Float-division_ | __number__ "./" _number_ -> _float_ -| [blue]`%` | _Infix_ | _Integer-remainder_ | _integer_ "%" _integer_ -> _integer_ -.5+|*SUM*| [blue]`+` | _Infix_ | _Sum_ | _number_ "+" _number_ -> _number_ -| [blue]`+` | _Infix_ | _String-concat_ | (_string_\|_number_) "+" (_string_\|_number_) -> _string_ -| [blue]`+` | _Infix_ | _List-join_ | _list_ "+" _list_ -> _list_ -| [blue]`-` | _Infix_ | _Subtraction_ | _number_ "-" _number_ -> _number_ -| [blue]`-` | _Infix_ | _List-difference_ | _list_ "-" _list_ -> _list_ -.6+|*RELATION*| [blue]`<` | _Infix_ | _less_ | _comparable_ "<" _comparable_ -> _boolean_ -| [blue]`\<=` | _Infix_ | _less-equal_ | _comparable_ "\<=" _comparable_ -> _boolean_ -| [blue]`>` | _Infix_ | _greater_ | _comparable_ ">" _comparable_ -> _boolean_ -| [blue]`>=` | _Infix_ | _greater-equal_ | _comparable_ ">=" _comparable_ -> _boolean_ -| [blue]`==` | _Infix_ | _equal_ | _comparable_ "==" _comparable_ -> _boolean_ -| [blue]`!=` | _Infix_ | _not-equal_ | _comparable_ "!=" _comparable_ -> _boolean_ -.1+|*NOT*| [blue]`not` | _Prefix_ | _not_ | "not" _boolean_ -> _boolean_ -.2+|*AND*| [blue]`and` | _Infix_ | _and_ | _boolean_ "and" _boolean_ -> _boolean_ -| [blue]`&&` | _Infix_ | _and_ | _boolean_ "&&" _boolean_ -> _boolean_ -.2+|*OR*| [blue]`or` | _Infix_ | _or_ | _boolean_ "or" _boolean_ -> _boolean_ -| [blue]`\|\|` | _Infix_ | _or_ | _boolean_ "\|\|" _boolean_ -> _boolean_ +.1+|*ITEM*| [blue]`.` | _Infix_ | _Item_| _collection_ `"."` _key_ -> _any_ +.2+|*INC*| [blue]`++` | _Postfix_ | _Post increment_| _integer-variable_ `"++"` -> _integer_ +| [blue]`++` | _Postfix_ | _Next item_ | _iterator_ `"++"` -> _any_ +.1+|*FACT*| [blue]`!` | _Postfix_ | _Factorial_| _integer_ `"!"` -> _integer_ +.3+|*SIGN*| [blue]`+`, [blue]`-` | _Prefix_ | _Change-sign_| (`"+"`\|`"-"`) _number_ -> _number_ +| [blue]`#` | _Prefix_ | _Lenght-of_ | `"#"` _collection_ -> _integer_ +| [blue]`#` | _Prefix_ | _Size-of_ | `"#"` _iterator_ -> _integer_ +.5+|*PROD*| [blue]`*` | _Infix_ | _Product_ | _number_ `"*"` _number_ -> _number_ +| [blue]`*` | _Infix_ | _String-repeat_ | _string_ `"*"` _integer_ -> _string_ +| [blue]`/` | _Infix_ | _Division_ | _number_ `"/"` _number_ -> _number_ +| [blue]`./` | _Infix_ | _Float-division_ | __number__ `"./"` _number_ -> _float_ +| [blue]`%` | _Infix_ | _Integer-remainder_ | _integer_ `"%"` _integer_ -> _integer_ +.5+|*SUM*| [blue]`+` | _Infix_ | _Sum_ | _number_ `"+"` _number_ -> _number_ +| [blue]`+` | _Infix_ | _String-concat_ | (_string_\|_number_) `"+"` (_string_\|_number_) -> _string_ +| [blue]`+` | _Infix_ | _List-join_ | _list_ `"+"` _list_ -> _list_ +| [blue]`-` | _Infix_ | _Subtraction_ | _number_ `"-"` _number_ -> _number_ +| [blue]`-` | _Infix_ | _List-difference_ | _list_ `"-"` _list_ -> _list_ +.6+|*RELATION*| [blue]`<` | _Infix_ | _less_ | _comparable_ `"<"` _comparable_ -> _boolean_ +| [blue]`\<=` | _Infix_ | _less-equal_ | _comparable_ `"\<="` _comparable_ -> _boolean_ +| [blue]`>` | _Infix_ | _greater_ | _comparable_ `">"` _comparable_ -> _boolean_ +| [blue]`>=` | _Infix_ | _greater-equal_ | _comparable_ `">="` _comparable_ -> _boolean_ +| [blue]`==` | _Infix_ | _equal_ | _comparable_ `"=="` _comparable_ -> _boolean_ +| [blue]`!=` | _Infix_ | _not-equal_ | _comparable_ `"!="` _comparable_ -> _boolean_ +.1+|*NOT*| [blue]`not` | _Prefix_ | _not_ | `"not"` _boolean_ -> _boolean_ +.2+|*AND*| [blue]`and` | _Infix_ | _and_ | _boolean_ `"and"` _boolean_ -> _boolean_ +| [blue]`&&` | _Infix_ | _and_ | _boolean_ `"&&"` _boolean_ -> _boolean_ +.2+|*OR*| [blue]`or` | _Infix_ | _or_ | _boolean_ `"or"` _boolean_ -> _boolean_ +| [blue]`\|\|` | _Infix_ | _or_ | _boolean_ `"\|\|"` _boolean_ -> _boolean_ .1+|*ASSIGN*| [blue]`=` | _Infix_ | _assignment_ | _identifier_ "=" _any_ -> _any_ .1+|*BUT*| [blue]`but` | _Infix_ | _but_ | _any_ "but" _any_ -> _any_ |===