Declaration and control sequences |
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_?]* regular expression.
Examples of identifiers are x, y2, my_var and test?.
Some special predefined constants are:
The boolean constant true.
The boolean constant false.
The standard output stream.
The standard error stream.
Notice that streams can be used with the operators << as in C++:
|
mmx-light supports three types of literal constants:
Strings can be braced into double quotes
|
An integer literal is a sequence of digits, possible preceded by a minus sign. It matches the regular expression [-]?[0-9]+. Examples: 123456789123456789, -123.
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.
To assign a value to a variable, we use the operator :=, while constant values are defined by the operator ==, and cannot be modified hereafter.
Mmx] |
a1 := 1 |
Mmx] |
a1_? == 2; a1_? |
Mmx] |
cst: Int == 11111*111111 |
Mmx] |
mut: Int := 1010*1234 |
Mmx] |
cst := - cst |
::
,
cst := - cst
~~~
Mmx] |
mut := - mut |
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 unary
-
MINUS
infix unary
@-
OMINUS
infix unary
++
INC
infix unary
–
DEC
infix unary
#
SIZE
infix unary
^
POWER
++
INC
postfix unary
–
DEC
postfix unary
!
NOT
postfix unary
'
QUOTE
postfix unary
‘
BACKQUOTE
postfix unary
~
TILDA
postfix unary
.
ACCESS
Whenever writing a complex expression you should carefully consider these priority rules in order to avoid using extra parentheses. For example the expression
|
should be better written
|
The Mathemagix grammar is specified in the file mmx-parser.ypp of the basix module.
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.
|