Expressions

An expression is a combination of expressions and values, with operators. It may be assigned to a variable or used as condition in a conditional structure, as if, while, and so ones...

There are two main kinds of expressions in Scriptol:
- relational expression, or logical one that return boolean values, true or false.
- arithmetical expression that return integer or real values.
A value in an expression may be a literal, a variable, a call of function (providing the function returns a value)... Operators are listed below...

Unlike C and derivated languages, an instruction is not an expression. This principle inherited from the Bcpl language leads to confusing, for example when the = symbol is used instead of ==, and also this limits the language.

Precedence

Many programming language, and mainly C, use precedence rules, and thus parenthesis may be omitted in expressions.
 But parenthesis are required to be readable and omitting them may lead to error, and so parenthesis can't be omitted in Scriptol.
 Precedence is actually implemented in the compiler, but error messages are sent when parenthesis are missing.
 There is a case where a precedence rule is admitted, this is for unary operator as: "not", "~" as these operators applies just to the following term.
 If a unary operator applies to a compound expression, this expression must be enclosed in parenthesis.


Exemple or unary operators applied to variable and expression. boolean a = true
boolean b = false

if not a and b print "first case"

if not (a and b) print "second case"
Displays: > second cas


Explaining:
- not a and b is not true and false, thus false and false, and the intersection is false, the condition is not true and the print statement is not processed.
- not (a and b) is not (true and false), thus not false, thus true, the condition is matched and the message is displayed.

 Finally, take note that meanwhile if precedence is not used by Scriptol, you may have to know it to convert programme written in C or Java.


Table of precedences, from the highest to the lowest

[] () .
not, ~, + -
* / mod
^^
+ -
<< >>
<  > <=  >=
=  <>
&
|
^
and
or
compound
indices, grouping, reference
logical not, binary not, unary plus or minus
multiplicating, dividing, modulo
puissance
binary adding, substracting
shifting
comparing
test if equal
intersection
union
exclusion
logical and
logical or
compound assignement.


 Exercises

 

1) Precedence of operators has the following order
a) not
b) * /
c) + -
Considering the following exxpression in a Java program:
x = a + b * c - d / e
Convert the expression to Scriptol.

Answer