Expressions

Home Scripting Expressions

You can use the usual arithmetic and logical operators plus parentheses to formulate expressions in scripts, like the following examples:

a * b
1 - (1 / n)
(x >= 0) and (x <= 20)
field "First" + field "Second" - field "Third"

The operators take precedence according to the following table (from highest to lowest)

Group Operators
Parentheses (  )
Negation not
Multiply/divide *  /  and
Add/subtract +  -  or  &
Signs +  -
Comparisons =  <>  >  <  >=  <=

<> means unequal.

& is the string concatenation operator:

a & " and then also " & b

A lot of more "fancy" operators that other languages offer, like operators for bitwise logical operations, power operator, factorial operator, etc., are not yet implemented.

There are no explicit type transfers in the MoStack script language. The script interpreter tries its best to convert automatically whenever necessary. For example, you can mix integer and real numbers in expressions nearly as much as you like.

If you append a number to a string, the number is converted to text, just as expected:

"The number " & x & "is ok"

If x contains the integer value 3, the expression results in "The number 3 is ok".

Be aware however that conversions take place at the latest possible moment, so given a real field Real the following statement results in the field getting the value 1 and not 1.5, because the division is executed as an integer division that throws away any fractions:

put 15/10 into field "Real"

Also note that the field construct has sort of a very low precendence, expecting only a single value (a factor in "programmer's speak") after the keyword. That means that if you use string literals with the names of controls everything works as expected. E.g. with "big" in text field Text the following expression gives "big surprise":

field "Text" & " surprise"

Things only get a little more complicated if you want to dynamically decide which field to use in your script and calculate the field name somehow for achieving this. With a variable x containing the value 3 the following expressions tries to reference a field Text and append the string "3" to it:

field "Text" & x

If you want to reference field Text3 however, you have to use parantheses:

field ("Text" & x)

There are some possibilities concerning dates and expressions, explained in the separate chapter Date Expressions.