10 KiB
Expr
1. Expr
Expr is a GO package capable of analysing, interpreting and calculating expressions.
2. Data types
Expr supports numerical, string, relational, boolean expressions, and mixed-type lists.
2.1. Numbers
Numbers can be integers (GO int64) or float (GO float64). In mixed operations involving integers and floats, integers are automatically promoted to floats.
Symbol | Operation | Description | Examples |
---|---|---|---|
|
change sign |
Change the sign of values |
|
|
sum |
Add two values |
|
|
subtraction |
Subtract the right value from the left one |
|
|
product |
Multiply two values |
|
|
Division |
Divide the left value by the right one |
|
|
Float division |
Force float division |
|
|
Modulo |
Remainder of the integer division |
|
2.2. String
Strings are character sequences enclosed between two double quote "
. Example: "I’m a string"
.
Some arithmetic operators can also be used with strings.
Symbol | Operation | Description | Examples |
---|---|---|---|
|
concatenation |
Join two strings or two stringable values |
|
|
repeat |
Make n copy of a string |
|
2.3. Boolean
Boolean data type has two values only: true and false. Relational and Boolean expressions produce Boolean values.
Symbol | Operation | Description | Examples |
---|---|---|---|
|
Equal |
True if the left value is equal to the right one |
|
|
Not Equal |
True if the left value is NOT equal to the right one |
|
|
Less |
True if the left value is less than the right one |
|
|
Less or Equal |
True if the left value is less than or equal to the right one |
|
|
Greater |
True if the left value is greater than the right one |
|
|
Greater or Equal |
True if the left value is greater than or equal to the right one |
|
Symbol | Operation | Description | Examples |
---|---|---|---|
|
Not |
True if the right value is false |
|
|
And |
True if both left and right values are true |
|
|
Or |
True if at least one of the left and right values integers true |
|
Currently, boolean operations are evaluated using short cut evaluation. This means that, if the left expression of operators Example
|
2.4. List
Expr supports list of mixed-type values, also specified by normal expressions.
[1, 2, 3] // List of integers
["one", "two", "three"] // List of strings
["one", 2, false, 4.1] // List of mixed-types
["one"+1, 2.0*(9-2)] // List of expressions
[ [1,"one"], [2,"two"]] // List of lists
Symbol | Operation | Description | Examples |
---|---|---|---|
|
Join |
Joins two lists |
|
|
Difference |
Left list without elements in the right list |
|
3. Variables
A variable is an identifier with an assigned value. Variables are stored in the object that implements the ExprContext interface.
a=1
x = 5.2 * (9-3)
x = 1; y = 2*x
4. Other operations
4.1. ;
operator
The semicolon operator ;
is an infixed operator. It evaluates the left expression first and then the right expression. The latter is the final result.
Technically ; is not treated as a real operator. It acts as a separator in lists of expressions.
|
; can be used to set some variables before the final calculation.
|
a=1; b=2; c=3; a+b+c // returns 6
4.2. but
operator
but
is an infixed operator. Its operands can be any type of expression. It evaluates the left expression first, then the right expression. The value of the right expression is the final result. Examples: 5 but 2
returns 2, x=2*3 but x-1
returns 5.
but
is very similar to ;
. The only difference is that ;
can’t be used inside parenthesis ;(
and )
.
4.3. Assignment operator =
The assignment operator =
is used to define variable in the evaluation context or to change their value (see ExprContext).
The value on the left side of =
must be an identifier. The value on the right side can be any expression and it becomes the result of the assignment operation.
a=15+1 // returns 16
4.4. Selector operator ? : ::
The selector operator is very similar to the switch/case/default statement available in many programming languages.
<selector-operator> ::= <expression> "?" <selector-case> { ":" <selector-case> } ["::" <case-value>]
<selector-case> ::= [<list>] <case-value>
<case-value> ::= "{" <multi-expr> "}"
<multi-expr> ::= <expr> {";" <expr>}
1 ? {"a"} : {"b"} // returns "b"
10 ? {"a"} : {"b"} :: {"c"} // returns "c"
10 ? {"a"} :[true, 2+8] {"b"} :: {"c"} // returns "b"
10 ? {"a"} :[true, 2+8] {"b"} ::[10] {"c"} // error: "... case list in default clause"
10 ? {"a"} :[10] {x="b" but x} :: {"c"} // returns "b"
10 ? {"a"} :[10] {x="b"; x} :: {"c"} // returns "b"
10 ? {"a"} : {"b"} // error: "... no case catches the value (10) of the selection expression
5. Priorities of operators
The table below shows all supported operators by decreasing priorities.
Priority | Operators | Position | Operation | Operands and results |
---|---|---|---|---|
FACT |
|
Postfix |
Factorial |
integer "!" → integer |
SIGN |
|
Prefix |
Change-sign |
("+"|"-") number → number |
PROD |
|
Infix |
Product |
number "*" number → number |
|
Infix |
String-repeat |
string "*" integer → string |
|
|
Infix |
Division |
number "/" number → number |
|
|
Infix |
Float-division |
number "./" number → float |
|
|
Infix |
Integer-remainder |
integer "%" integer → integer |
|
SUM |
|
Infix |
Sum |
number "+" number → number |
|
Infix |
String-concat |
(string|number) "+" (string|number) → string |
|
|
Infix |
List-join |
list "+" list → list |
|
|
Infix |
Subtraction |
number "-" number → number |
|
|
Infix |
List-difference |
list "-" list → list |
|
RELATION |
|
Infix |
less |
comparable "<" comparable → boolean |
|
Infix |
less-equal |
comparable "<=" comparable → boolean |
|
|
Infix |
greater |
comparable ">" comparable → boolean |
|
|
Infix |
greater-equal |
comparable ">=" comparable → boolean |
|
|
Infix |
equal |
comparable "==" comparable → boolean |
|
|
Infix |
not-equal |
comparable "!=" comparable → boolean |
|
NOT |
|
Prefix |
not |
"not" boolean → boolean |
AND |
|
Infix |
and |
boolean "and" boolean → boolean |
|
Infix |
and |
boolean "&&" boolean → boolean |
|
OR |
|
Infix |
or |
boolean "or" boolean → boolean |
|
Infix |
or |
boolean "||" boolean → boolean |
|
ASSIGN |
|
Infix |
assignment |
identifier "=" any → any |
BUT |
|
Infix |
but |
any "but" any → any |
6. Functions
Functions in Expr are very similar to functions in many programming languages.
In Expr functions compute values in a local context (scope) that do not make effects on the calling context. This is the normal behavior. Using the reference operator @
it is possibile to export local definition to the calling context.
6.1. Function calls
TODO: function calls operations
6.2. Function definitions
TODO: function definitions operations
7. Builtins
TODO: builtins
7.2. import()
import(<source-file>) loads the multi-expression contained in the specified source and returns its value.