TODO: Work in progress (last update on 2024/05/07, 07:15 am)
1. Expr
Expr is a GO package capable of analysing, interpreting and calculating expressions.
1.1. dev-expr
test tool
dev-expr
is a simple program that can be used to evaluate expressions interactively. As its name suggests, it was created for testing purpose. In fact, beyond in additon to the automatic test suite based on the Go test framework, dev-expr
provides an important aid for quickly testing of new features during their development.
It cat work as a REPL, *R*ead-*E*xecute-*P*rint-*L*oop, or it can process expression acquired from files or standard input.
The program in located in the tools directory. Here are some examples of execution.
dev-expr
in REPL mode and ask for help# Assume the expr source directory. Type 'exit' or Ctrl+D to quit the program.
[user]$ tools/expr -- Expressions calculator v1.7.0,2024/05/08 (celestino.amoroso@portale-stac.it)
Type help to get the list of command.
See also https://git.portale-stac.it/go-pkg/expr/src/branch/main/README.adoc
>>> help
--- REPL commands:
base -- Set the integer output base: 2, 8, 10, or 16
exit -- Exit the program
help -- Show command list
ml -- Enable/Disable multi-line output
mods -- List builtin modules
source -- Load a file as input
tty -- Enable/Disable ansi output (1)
--- Command line options:
-b <builtin> Import builtin modules.
<builtin> can be a list of module names or a glob-pattern.
Use the special value 'all' or the pattern '*' to import all modules.
-e <expression> Evaluate <expression> instead of standard-input
-i Force REPL operation when all -e occurences have been processed
-h, --help, help Show this help menu
-m, --modules List all builtin modules
-p Print prefix form
-t Print tree form (2)
>>>
1 | Only available for single fraction values |
2 | Work in progress |
[user]$ tools/expr -- Expressions calculator v1.6.1,2024/05/06 (celestino.amoroso@portale-stac.it)
Type help to get the list of command.
See also https://git.portale-stac.it/go-pkg/expr/src/branch/main/README.adoc
>>> 2+3
5
>>> 2+3*(4-1.5)
9.5
>>> 0xFD + 0b1 + 0o1 (1)
255
>>> 1|2 + 2|3 (2)
7|6
>>> ml (3)
>>> 1|2 + 2|3
7
-
6
>>> 1+2 but 5|2+0.5 (4)
3
>>> 1+2; 5|2+0.5 (5)
3
>>>
1 | Number bases: 0x = hexadecimal, 0o = octal, 0b = binary. |
2 | Fractions: numerator | denominator. |
3 | Activate multi-line output of fractions. |
4 | But operator, see but operator. |
5 | Multi-expression: the same result of the previous single expression but this it is obtained with two separated calculations. |
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 pseudo-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 expressions of any type. 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 variables 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> ::= <select-expression> "?" <selector-case> { ":" <selector-case> } ["::" <default-multi-expression>]
<selector-case> ::= [<match-list>] <case-value>
<match-list> ::= "["<item>{","<items>}"]"
<item> ::= <expression
<case-multi-expression> ::= "{" <multi-expression> "}"
<multi-expression> ::= <expression> {";" <expression>}
In other words, the selector operator evaluates the expression (<select-expression>
) on the left-hand side of the ?
symbol; it then compares the result obtained with the values listed in the <match-list>’s. If the comparision find a match with a value in a match-list, the associated `<case-multi-expression>
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 <selector-case>
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.
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 |
---|---|---|---|---|
ITEM |
|
Infix |
Item |
collection |
INC |
|
Postfix |
Post increment |
integer-variable |
|
Postfix |
Next item |
iterator |
|
FACT |
|
Postfix |
Factorial |
integer |
SIGN |
|
Prefix |
Change-sign |
( |
|
Prefix |
Lenght-of |
|
|
|
Prefix |
Size-of |
|
|
PROD |
|
Infix |
Product |
number |
|
Infix |
String-repeat |
string |
|
|
Infix |
Division |
number |
|
|
Infix |
Float-division |
number |
|
|
Infix |
Integer-remainder |
integer |
|
SUM |
|
Infix |
Sum |
number |
|
Infix |
String-concat |
(string|number) |
|
|
Infix |
List-join |
list |
|
|
Infix |
Subtraction |
number |
|
|
Infix |
List-difference |
list |
|
RELATION |
|
Infix |
less |
comparable |
|
Infix |
less-equal |
comparable |
|
|
Infix |
greater |
comparable |
|
|
Infix |
greater-equal |
comparable |
|
|
Infix |
equal |
comparable |
|
|
Infix |
not-equal |
comparable |
|
NOT |
|
Prefix |
not |
|
AND |
|
Infix |
and |
boolean |
|
Infix |
and |
boolean |
|
OR |
|
Infix |
or |
boolean |
|
Infix |
or |
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.