![]() |
The |
![]() |
This document describes the syntax of the
Identifiers are formed using letters, digits and the special characters _ and ?. Identifiers must start with a letter or _. Identifiers match the [a-zA-Z_]+[a-zA-Z0-9_?]*. Examples of identifiers are x, y2, my_var and test?.
Some special predefined constants are:
Notice that streams can be used with the operators << as in C++:
In addition to identifiers,
Strings can be braced into double quotes " ... ". Inside such a string a double quote must be blackslashed. In order to avoid blackslashing one can use the stronger string delimiters /" ... "/.
A floating literal is a sequence of digits with a decimal point inside and an optional exponent. It matches the regular expression [-]?[0-9]+[.][0-9]+[[eE][-]?[0-9]+]?. Some examples:
Note that 0. is not permited, one must write 0.0.
Here we list the operators in increasing priority order, together with their internal names:
== | DEFINE | |
:= | ASSIGN | |
==> | DEFINE_MACRO | |
:=> | ASSIGN_MACRO | |
+= | PLUS_ASSIGN | |
-= | MINUS_ASSIGN | |
*= | TIMES_ASSIGN | |
/= | OVER_ASSIGN | |
<< | LEFT_FLUX | |
<<* | LEFT_FLUX_VAR | |
<<% | LEFT_FLUX_STR | |
>> | RIGHT_FLUX | |
=> | IMPLIES | |
<=> | EQUIVALENT | |
or | SEQOR | |
OR | ||
xor | XOR | |
and | SEQAND | |
AND | ||
= | EQUAL | |
< | LESS | |
<= | LEQ | |
> | GREATER | |
>= | GEQ | |
!= | NOT_EQUAL | |
!< | NOT_LESS | |
!<= | NOT_LEQ | |
!> | NOT_GREATER | |
!>= | NOT_GEQ | |
-> | INTO | |
:-> | MAPSTO | |
.. | RANGE | |
to | RANGEEQ | |
+ | PLUS | |
- | MINUS | |
@+ | OPLUS | |
@- | OMINUS | |
* | TIMES | |
/ | OVER | |
@* | OTIMES | |
@/ | OOVER | |
div | DIV | |
mod | MOD | |
@ | COMPOSE | |
! | NOT | infix unitary |
- | MINUS | infix unitary |
@- | OMINUS | infix unitary |
++ | INC | infix unitary |
– | DEC | infix unitary |
# | SIZE | infix unitary |
^ | POWER | |
++ | INC | postfix unitary |
– | DEC | postfix unitary |
! | NOT | postfix unitary |
' | QUOTE | postfix unitary |
‘ | BACKQUOTE | postfix unitary |
~ | TILDA | postfix unitary |
. | ACCESS |
Whenever writing a complex expression you should carefully consider these priority rules in order to avoid using extra '(...)'. For example the expession
should be better written
a..b means the range [a,b], while a::b stands for the half open range [a,b).
Tuples are written inside (...). Elements are separated by ,. Row-tuples rows are separated by ;.
Lists are written inside [...].
Automatic constructions are possible through the | resp. || notation:
The default type of lists used in the interpreter is List(Generic).
Tables allow to store the association between keys of one type to values of another type. They are defined by providing a default value. In the following exemple, the default value is 1:
The definition of specific values is done as follows:
The default type of tables used in the interpreter is Table(Generic,Generic).
Any function definition can be preceded by quantifiers forall(...) or exists(...).
A function definition is done as follows:
Notice that any of the type specifications can be omitted, in which case the type is assumed to be Generic. Expression macros can be defined by using macro instead of lambda.
When function or macro have only one argument, () can be omited.
Concerning the coding style, notice that we prefer a space before ( and another after :.
A function or a macro foo is called in the usual way: foo(arg1, arg2,...).
If foo is unitary then () can be omited, but note that foo a b c is equivalent to foo(a(b(c))). Function call is always by value.
Instructions are separated with ;.
The construction is as follows: if condition then block1 else block2.
condition is any expression that evaluates to a boolean. block1 and block2 are either one instruction or a block of instructions braced into {...}. The else part is optional.
For the sake of readability do not write
but
instead, since {...} are not needed for a block with one instruction only. Notice that, according to the priority rules of the operators listed above, the expression
should be written
for the sake of readability.
Loops are constructed as follows: [for E1] [while E2] [until E3] [step E4] do block.
Here [...] means that the expression is optional. block is an instruction or a sequence of instruction delimited by {...}. break exits the loop, and continue goes to the next step of the loop.
Comments starting with // extend to the end of the physical line. Such a comment may appear at the start of a line or following whitespace or code, but not within a string literal. Multi-line comments must be braced into /{ ... }/ and can be nested.
Avoid lines with more than 80 characters. For long instructions, you should better rewrite
into
Add spaces around the main operators to ease readability. For instance the expression
should better be written
Align operators when useful for readability. For instance write
The Mathemagix grammar is specified in the file mmx-parser.ypp of the basix module.