Expressions
This article has examples in the following target languages:
- C
- C++
- Python
- Rust
- TypeScript
A subset of LF syntax is used to write expressions, which represent values in the target language. Expressions are used to initialize state variable and to give values to parameters. Arbitrary expressions in the target language can always be given within delimiters {= ... =}
, but simple forms do not require the delimiters. These simple forms are documented here.
Basic expressions​
The most basic expression forms, which are supported by all target languages, are the following:
-
Literals:
- Numeric literals, e.g.
1
,-120
,1.5
,3.14e10
. Note that the sign, if any, is part of the literal and must not be separated by whitespace. - String literals, e.g.
"abcd"
. String literals always use double-quotes, even in languages which support other forms (like Python). - Character literals. e.g.
'a'
. Single-quoted literals must be exactly one character long --even in Python. - Boolean literals:
true
,false
,True
,False
. The latter two are there for Python.
- Numeric literals, e.g.
-
Parameter references, which are simple identifiers (e.g.
foo
). Any identifier in expression position must refer to a parameter of the enclosing reactor. -
Time values, e.g.
1 msec
or10 seconds
. The syntax of time values isinteger time_unit
, wheretime_unit
is one of the following:- nsec or ns: nanoseconds
- usec or us: microseconds
- msec or ms: milliseconds
- sec, second, or s: seconds
- minute or min: 60 seconds
- hour: 60 minutes
- day: 24 hours
- week: 7 days
Each of these units also support a plural version (e.g.,
nsecs
,minutes
, anddays
), which means the same thing.The time value
0
need not be given a unit, but for all other values, the unit is required.Time values are compatible with the
time
type. -
Escaped target-language expression, e.g.
{= foo() =}
. This syntax is used to write any expression which does not fall into one of the other forms described here. The contents are not parsed and are used verbatim in the generated file.
For instance, to have a 2-dimensional array as a parameter in C:
Both int[][]
and { {1}, {2} }
are C fragments here, not LF.
For instance, to assign a 2-dimensional list as an initial value to a parameter in the Python target:
Collections​
To avoid the awkwardness of using the code delimiters {= ... =}
, Lingua Franca supports initialization of simple arrays and similar structures. The interpretation is slightly different in each target language.
In C, a parameter or state may be given an array value as in the following example:
The parameter named param
will become an array of length three. When instantiating this reactor, the default parameter value can be overridden using a similar syntax:
See the Target Language Details for details and alternative syntaxes.
In C++, initial values for a parameter or state can be used to pass arguments to a constructor, as in the following example:
Here, the type int[]
is translated by the code generator into std::vector
and the (2, 0)
to constructor arguments, as in new std::vector(2,0)
, which creates a vector of length 2 filled with elements with value 0. See the Target Language Details for details and alternative syntaxes.
In Python, a parameter or state variable may be assigned a list expression as its initial value, as in the following example:
The param
parameter and x
state variable become Python lists.
Their elements may be accessed as arrays, for example self.x[i]
, where i
is an array index.
The parameter may be overridden with a different list at instantiation:
See the Target Language Details for more details.
In TypeScript, a parameter or state variable may be assigned an array expression as its initial value, as in the following example:
See the TypeScript reactor documentation for details and alternative syntaxes.
FIXME: Rust