Grammar for formulas - Show-your-Heart/ShowYourHeart-docs GitHub Wiki
Several fields contain expressions need to be computed in runtime. For instance, indicator conditions, formulas, and validations. Here go some examples:
ADD EXAMPLES HERE!
Formula:
statement=Statement
;
UnaryNumeric:
unary_numeric_function=UNARY_NUMERIC_FUNCTION
"(" expression=Expression ")"
;
BinaryNumeric:
binary_numeric_function=BINARY_NUMERIC_FUNCTION
"(" expressionl=Expression "," (INT | STRING) ")"
;
Statistical:
statistical_function=STATISTICAL_FUNCTION
"(" "["referenceIndicator=[Indicator]"]" ")"
//Constraint: only direct indicators should be used
;
Statement:
expression=Expression | if_statement=If_statement
;
If_statement:
"IF" expression=Expression "THEN" thenStatement=Statement
(=>"ELSE" elseStatement=Statement)?
;
Expression:
simpleExpressionl=Simple_expression ( ("=" | "<>" | "<" | "<=" | ">" | ">=" | "==")
simpleExpressionr=Simple_expression)?
;
Simple_expression:
terml=Term ((("+" | "-") | "OR") termr=Term)*
;
Term:
factorl=Factor ((("*" | "/") | "AND") factorr=Factor)*
;
Factor:
basel=Base ("^" baser=Base)?
;
Base:
( "(" expression=Expression ")" | "["referenceIndicator=[Indicator] "]" |
statistic=Statistical | unarynumeric=UnaryNumeric |
binarynumeric=BinaryNumeric | BOOLEAN | STRING | INT | DOUBLE )
;
enum UNARY_NUMERIC_FUNCTION: absolute="abs" | int="int" ;
enum BINARY_NUMERIC_FUNCTION: roundup="roundUp" | rounddown="roundDown" |
round="round" | count="count" | countif="countIf" ;
enum STATISTICAL_FUNCTION: minimum="min" | maximum="max" |
sum="sum" | mean="avg" | mode="mode" | median="median";
terminal BOOLEAN : ("true"|"false");
terminal DOUBLE: INT "." INT;
- In formulas, indicators should not reference themselves.
- Statistical functions cannot be used in indicator conditions or in validation rules.
We can interpret the meaning of the functions in the same way as Google Sheets. But note that the syntax is different:
- abs: https://support.google.com/docs/answer/3093459
- int: https://support.google.com/docs/answer/3093490
- roundUp: https://support.google.com/docs/answer/3093443
- roundDown: https://support.google.com/docs/answer/3093442
- round: https://support.google.com/docs/answer/3093440
- count: https://support.google.com/docs/answer/3093620
- countIf: https://support.google.com/docs/answer/3093480
- avg: https://support.google.com/docs/answer/3093615
- median: https://support.google.com/docs/answer/3094025
- mode: https://support.google.com/docs/answer/3094029
- min: https://support.google.com/docs/answer/3094017
- max: https://support.google.com/docs/answer/3094013
PENDING TO EXPRESS THE SYNTAX AND SEMANTICS OF FUNCTIONS BETTER