Skip to content

MASTER expression syntax

Tim Vaughan edited this page Mar 17, 2015 · 9 revisions

The predicate, rateMultiplier and function elements contain simple arithmetic expressions following the syntax described on this page. The syntax is essentially C/Java-like and adheres to the following grammar:

EXPR ::= "(" EXPR ")"                                           # Bracketed
       |  "{" EXPR ("," EXPR)* "}"                              # Array
       |  EXPR '[' EXPR ']'                                     # Array subscripting
       |  ID "(" EXPR ("," EXPR)* ")"                           # Function
       |  ("exp"|"log"|"sqrt"|"sum"|"theta"|"abs") "(" EXPR ")" # Unary Op
       |  "-" EXPR                                              # Negation
       |  EXPR "!"                                              # Factorial
       |  EXPR "^" EXPR                                         # Exponentiation
       |  EXPR ("*"|"/"|"%") EXPR                               # Mul/div/mod
       |  EXPR ("+"|"-") EXPR                                   # Add/sub
       |  EXPR ("=="|"!="|"<"|">"|"<="|">=") EXPR               # Equality
       |  EXPR ("&&"|"||") EXPR                                 # BooleanOp
       |  EXPR "?" EXPR ":" EXPR                                # IfThenElse
       |  ID                                                    # Variable
       |  NNFLOAT | NNINT                                       # Number

NNINT : "0" | NZD D* ;
NNFLOAT : NNINT ("." D*) ([eE] "-"? D+)? ;
D : [0-9] ;
NZD : [1-9] ;

ID : [a-zA-Z_][a-zA-Z_0-9]* ;

(The grammar above is essentially EBNF but with regexp-style character sets such as [0-9] and [a-zA-Z].) The alternatives are listed in order of precedence from highest (bracketed expression) to lowest.

All expressions evaluate to arrays of one or more double-precision numbers. Binary operators act on each vector in an element-by-element way, with the shorter vector being repeated as many times as necessary to make up for any difference in length. (This follows the convention of R.)

Equality operators such as == return 1.0 if the equality holds or 0.0 if the equality does not. (If the operands are vectors, the comparison is done in an element-by-element way and the result is a vector of 1.0s and 0.0s.)

The Boolean expression "exp1 && exp2" evaluates to 0.0 if one or both of the operands evaluate to 0.0 and evaluates to 1.0 otherwise. The expression "exp1 || exp2" evaluates to 0.0 only if both operands evaluate to "0.0".

The conditional expression "cond ? exp1 : exp2" evaluates to exp1 if "cond" evaluates to something other than 0.0 and evaluates to exp2 otherwise.

All operators are left associative, with the exception of the exponentiation binary operator and the if-then-else ternary operator which are both right associative.

Both C-style multi-line comments

      /* comment
         comment */

and C++-style single-line comments

      // comment until end of line

are supported.