Expressions are the most important building blocks of XSQL
. In XSQL
, almost everything that is written is an expression.
The simplest and most accurate way to define an expression is as "anything that has a value".
1 Simple Expressions
The most basic forms of expressions are constants and variables. When you write <set name='a'>5</set>, '5' is being assigned to the variable a. '5' has the value 5, or in other words, '5' is an expression with the value of 5 (in this case, '5' is an integer constant).
After this assignment, the value of a is also expected to be 5, so if you type b = a, this is expected to behave as if it were written b = 5. In other words, a is also an expression with value 5.
Assign the variable a = 5
<xsql-script> <body> <set name='a'>5</set> <println> a=<a /> </println> </body> </xsql-script>
a=5
XSQL
uses expressions much more extensively, in the same way that other languages do.
XSQL
is an expression-oriented language, in the sense that almost everything is an expression.
Let's consider the example that has already been discussed, 'a = 5'.
It is easy to see that here there are two values involved, the value of the entire constant '5', and the value of a that has been updated to 5 as well. Although, in reality, an additional value is involved here, namely the value of the assignment itself. Assignment evaluates the value assigned, in this case 5. In practice, this means that 'a = 5', and regardless of context, this expression has the value 5.
In this way, to write something like 'b = (a = 5)' is the same as writing 'a = 5; b = 5;'. Therefore, the following example shows how b takes the value of the expression 'a = 5'
Assign b = a = 5
<xsql-script> <body> <set name='b'> <set name='a'>5</set> </set> <println> a=<a /> b=<b /> </println> </body> </xsql-script>
a=5
b=5
Below is an example with more depth.
Assign a = b = c = 1
<xsql-script> <body> <set name='a'><set name='b'><set name='c'>1</set></set></set> <println>a = <a/>, b = <b/>, c = <c/>, ergo a = b = c</println> </body> </xsql-script>
a = 1, b = 1, c = 1, ergo a = b = c
2 Operators
The following section goes into more detail on operators, but an operator equivalency table is advanced.
2.1 Arithmetic
Arithmetic operators include the basic arithmetic symbols: addition (+), subtraction (-), multiplication (*), division (/) in addition to remainder (%), power (^) and negation or change of sign (-).
Operator | Java Equivalent | Description |
---|---|---|
<add> | + | Addition |
<sub> | - | Subtraction |
<mul> | * | Multiplication |
<div> | / | Division |
<mod> | % | Remainder |
<exp> | ^ | Power |
<neg> | - | Change of Sign |
2.2 Relational
Relational operators include:
Operator | Java Equivalent | Description |
---|---|---|
<eq> | = | Equal to |
<ne> | != | Not equal to |
<lt> | < | Less than |
<gt> | > | Greater than |
<le> | <= | Less than or equal to |
<ge> | >= | Greater than or equal to |
2.3 Logical
While arithmetic operators are mainly used with numbers, logical operators are intended to be used with logical values (true and false).
Operator | Java Equivalent | Description |
---|---|---|
<and> | & | AND |
<or> | | | O |
<not> | ! | No |
<between> | N/A | Between |
3 Complex Expressions
Let's move on to expressions that require precedence and that commonly require parentheses in other languages, such as Javascript.
Parentheses in XSQL
are expressed by the operation tag.
x = c / (a+b)
<xsql-script> <body> <set name='a'>10</set> <set name='b'>20</set> <set name='c'>300</set> <set name='x'> <div> <c/> <add> <a/> <b/> </add> </div> </set> <println><x /></println> </body> </xsql-script>
10
x = c / (a+b/2)
<xsql-script> <body> <set name='a'>10</set> <set name='b'>20</set> <set name='c'>300</set> <set name='x'> <div> <c/> <add> <a/> <div> <b/> 2 </div> </add> </div> </set> <println><x /></println> </body> </xsql-script>
15
3.1 Binary or Multiple Operations
One characteristic of XSQL
regarding expressions is that operators of algebraic evaluation
can operate on sets of variables. That is, they are not exclusively binary and therefore admit more
than two arguments.
x = a+b+c
<xsql-script> <body> <set name='a'>100</set> <set name='b'>10</set> <set name='c'>2</set> <set name='x'> <add> <a/> <b/> <c/> </add> </set> <println><x /></println> </body> </xsql-script>
112
4 Functions as Expressions
Functions are an example of somewhat more complex expressions. For example, consider the following function:
<xsql-script> <body> <function name='foo'> <body> <return>5</return> </body> </function> <set name='a'><foo /></set> <println> a = <a /> </println> </body> </xsql-script>
a=5
Assuming you are familiar with the concept of functions, you will assume correctly that typing a = foo () is essentially the same as typing a = 5. Functions are expressions with the value of their return values. Since foo () returns 5, the value of the expression 'foo ()' is 5. Normally functions not only return a static value, but also compute something.
5 Comparison Expressions
Comparison expressions are a very common expression type. These expressions evaluate whether something is FALSE or TRUE using comparison operators. XSQL
supports:
- <gt>, greater than
- <ge>, greater than or equal to
- <eq>, equal to
- <ne>, not equal to
- <lt>, less than
- <le>, less than or equal to
These expressions are used, above all, within conditional executions, such as the statement if.
Through the tag <if> you can change the execution flow. The condition must be contained in the <expr> tag. The set of actions to execute if this condition is fulfilled, must be within the <then> tag. And finally, the group of actions to execute if the condition is not fulfilled must be found in the <else> tag.
if then else
<xsql-script> <body> <set name='a'>5</set> <if> <expr> <gt><a/>1</gt> </expr> <then> <println>a is greater than 1</println> </then> <else> <println>a is not greater than 1</println> </else> </if> </body> </xsql-script>
a is greater than 1
5.1 True or False Value
In many cases, mainly in conditional executions and loops, we are not interested in the specific value of the expression, only if the value means TRUE or FALSE. The TRUE and FALSE constants are the two possible boolean values. When necessary, an expression is automatically converted to the boolean type.
6 Java EL Expressions
Axional Studio
incorporates Java EL
expressions under JSR 341
implementation (Expression Language 3.0).
Expression Language (EL) is a simple language originally designed
to meet the needs of web application developers.
You can use EL
for online operations within the expr
tag or within an assignment using the set
tag
and EL
notation. EL
expressions are indicated by the framing expression ${ ... }
.
For example:
<xsql-script> <body> <set name="a">1</set> <!-- direct assignment through eval --> <set name="b"><eval>${a+1}</eval></set> <!-- assignment through value --> <set name="c" value="${b+1}" /> <println>a=<a/></println> <println>b=<b/></println> <println>c=<c/></println> </body> </xsql-script>
a=1
b=2
c=3
You can also reference components of an array or a HashMap, as you can see in the following example:
<xsql-script> <body> <set name="a">1</set> <set name="b">2</set> <map name='c'> <item><string>A</string><string>X</string></item> </map> <set name='m_expr'><string>${(a == 1 or a+b == 3) and c["A"] IN ("X","Y") }</string></set> <!-- direct assignment through eval --> <set name="r"><eval><m_expr /></eval></set> <println><r /></println> <array name='d'> <string>A</string> <string>B</string> <string>C</string> </array> <println><eval>${d[1]}</eval></println> </body> </xsql-script>
true
B
6.1 Operators
You can use the following operators:
- Arithmetic: +, - (binary), *, / and div, % and mod, - (unary)
- Logical: and, &&, or, ||, not, !
- Relational: ==, eq, !=, ne, <, lt, >, gt, <=, ge, >=, le. Type comparisons of boolean, string, integer, or floating-point literals can be made.
- Empty: The empty operator can be used as a prefix to an operation to determine when a value is null or empty.
- Conditional: A ? B : C. Evaluate B or C, depending on the result of A.
Note that the equality operator is ==, not a single =.
The operator 'and' may be recommendable compared to && because the latter must be "escaped" in XML (&&) and therefore complicates the reading of the expression.
6.1.1 Precedence
The precedence of operators from highest to lowest, left to right is as follows:
- () (used to change the precendence of the operators)
- - (unary) not ! empty
- * / div % mod
- + - (binary)
- < > <= >= lt gt le ge
- == != eq ne
- && and
- || or
- ? :
6.2 Reserved Words
The following words are reserved in EL
and should not be used as identifiers:
and | or | not | |||
true | false | null | |||
empty | div | mod | |||
in | matches | ||||
eq | ne | lt | gt | le | ge |
6.3 Expressions
The following are examples of EL
expressions:
6.3.1 Expressions
Expression | Result |
---|---|
Boolean | |
1 > (2/4) | true |
4.0 >= 3 | true |
100.0 == 100 | true |
(10*10) ne 100 | false |
4 > 3 | true |
1 + 2 > 2 - 1 | true |
1 < 2 && 2 > 1 | true |
1 < 2 || 2 > 1 | true |
'a' < 'b' | true |
'hip' gt 'hit' | false |
!empty 'a' | true |
empty '' | true |
Aritmetic | |
1 + 2 | 3 |
1.2E4 + 1.4 | 12001.4 |
3 div 4 | 0.75 |
10 mod 4 | 2 |
Maps and arrays | |
{"one":1,"two":2,"three":3} | HashMap {one=1, two=2, three=3} |
{1,2,3} | List [1, 2, 3] |
6.3.2 Lambda Expressions
You can use lambda expressions in EL
.
<xsql-script> <body> <set name="L1"> <array> <number>10</number> <number>30</number> <number>20</number> <number>40</number> </array> </set> <println>Sort ASC L1: <eval>L1.stream().sorted((i,j)->i-j).toList()</eval></println> <println>Sort DESC L1: <eval>L1.stream().sorted((i,j)->j-i).toList()</eval></println> <println>Sort DESC : <eval>[1,5,3,7,4,2,8].stream().sorted((i,j)->j-i).toList()</eval></println> <println>Sort ASC : <eval>[1,5,3,7,4,2,8].stream().sorted((i,j)->i-j).toList()</eval></println> </body> </xsql-script>
Sort ASC L1: [10, 20, 30, 40]
Sort DESC L1: [40, 30, 20, 10]
Sort DESC : [8,7,5,4,3,2,1]
Sort DESC : [1,2,3,4,5,7,8]
7 <eval> tag
The XSQL
language includes an eval
tag that allows the evaluation of XSQL
or EL
expressions.
Originally the eval
tag was created to evaluate XSQL
expressions contained in text.
For example, if you had an XML text such as <add><a/><b/></add>
, you could evaluate it as such:
<xsql-script> <body> <set name='a'>10</set> <set name='b'>20</set> <set name='e1'><![CDATA[<add><a/><b/></add>]]></set> <println><eval><e1/></eval></println> </body> </xsql-script>
30
Through the introduction of EL
expressions, the eval
tag supports the evaluation of text expressions.
<xsql-script> <body> <set name='a'>10</set> <set name='b'>20</set> <set name='e1'>${a+b}</set> <!-- evalua a+b mediante EL --> <println><eval>${a+b}</eval></println> <!-- evaluate the content of e1 as a string EL --> <println><eval><e1/></eval></println> </body> </xsql-script>
30
30