Expressions

An expression specifies the computation of a value by applying the operators and functions to operands.

Operands and primary expressions

Operands denote the elementary values in an expression.

Primary expressions are the operands for unary and binary expressions. A primary expressions may be a literal, an identifier denoting a variable, or a parenthesized expression.

  1. PrimaryExpression = identifier | Literal | "(" Expression ")" .

Literals

Literals construct a value.

  1. Literal = int_lit
  2. | float_lit
  3. | string_lit
  4. | regex_lit
  5. | duration_lit
  6. | date_time_lit
  7. | pipe_receive_lit
  8. | RecordLiteral
  9. | ArrayLiteral
  10. | FunctionLiteral .

Record literals

Record literals construct a value with the record type.

  1. RecordLiteral = "{" RecordBody "}" .
  2. RecordBody = WithProperties | PropertyList .
  3. WithProperties = identifier "with" PropertyList .
  4. PropertyList = [ Property { "," Property } ] .
  5. Property = identifier [ ":" Expression ]
  6. | string_lit ":" Expression .

Examples

  1. {a: 1, b: 2, c: 3}
  2. {a, b, c}
  3. {o with x: 5, y: 5}
  4. {o with a, b}

Array literals

Array literals construct a value with the array type.

  1. ArrayLiteral = "[" ExpressionList "]" .
  2. ExpressionList = [ Expression { "," Expression } ] .

Function literals

A function literal defines a new function with a body and parameters. The function body may be a block or a single expression. The function body must have a return statement if it is an explicit block, otherwise the expression is the return value.

  1. FunctionLiteral = FunctionParameters "=>" FunctionBody .
  2. FunctionParameters = "(" [ ParameterList [ "," ] ] ")" .
  3. ParameterList = Parameter { "," Parameter } .
  4. Parameter = identifier [ "=" Expression ] .
  5. FunctionBody = Expression | Block .
Examples of function literals
  1. () => 1 // function returns the value 1
  2. (a, b) => a + b // function returns the sum of a and b
  3. (x=1, y=1) => x * y // function with default values
  4. (a, b, c) => { // function with a block body
  5. d = a + b
  6. return d / c
  7. }

All function literals are anonymous. A function may be given a name using a variable assignment.

  1. add = (a,b) => a + b
  2. mul = (a,b) => a * b

Function literals are closures and may refer to variables defined in a surrounding block. Those variables are shared between the function literal and the surrounding block.

Call expressions

A call expression invokes a function with the provided arguments. Arguments must be specified using the argument name. Positional arguments are not supported. Argument order does not matter. When an argument has a default value, it is not required to be specified.

  1. CallExpression = "(" PropertyList ")" .
Examples of call expressions
  1. f(a:1, b:9.6)
  2. float(v:1)

Use short notation in a call expression when the name of every argument matches the name of every parameter.

Examples of short notation in call expressions
  1. add(a: a, b: b) //long notation
  2. add(a, b) // short notation equivalent
  3. add = (a,b) => a + b
  4. a = 1
  5. b = 2
  6. // Don't mix short and long notation.
  7. add(a: a, b)
  8. add(a, b: b)

Pipe expressions

A pipe expression is a call expression with an implicit piped argument. Pipe expressions simplify creating long nested call chains.

Pipe expressions pass the result of the left hand expression as the pipe argument to the right hand call expression. Function literals specify which if any argument is the pipe argument using the pipe literal as the argument’s default value. It is an error to use a pipe expression if the function does not declare a pipe argument.

  1. pipe_receive_lit = "<-" .
Examples of pipe expressions
  1. foo = () => // function body elided
  2. bar = (x=<-) => // function body elided
  3. baz = (y=<-) => // function body elided
  4. foo() |> bar() |> baz() // equivalent to baz(x:bar(y:foo()))

Index expressions

Index expressions access a value from an array based on a numeric index.

  1. IndexExpression = "[" Expression "]" .

Member expressions

Member expressions access a property of a record. They are specified using an expression in one of the following forms:

  1. rec.k
  2. // or
  3. rec["k"]

The property being accessed must be either an identifier or a string literal. In either case the literal value is the name of the property being accessed, the identifier is not evaluated. It is not possible to access a record’s property using an arbitrary expression.

If rec contains an entry with property k, both rec.k and rec["k"] return the value associated with k. If rec does not contain an entry with property k, both rec.k and rec["k"] return null.

  1. MemberExpression = DotExpression | MemberBracketExpression .
  2. DotExpression = "." identifier .
  3. MemberBracketExpression = "[" string_lit "]" .

Conditional expressions

Conditional expressions evaluate a boolean-valued condition. If the result is true, the expression that follows the then keyword is evaluated and returned. If the result is false, the expression that follows the else keyword is evaluated and returned. In either case, only the branch taken is evaluated and only side effects associated this branch will occur.

  1. ConditionalExpression = "if" Expression "then" Expression "else" Expression .
Conditional expression example
  1. color = if code == 0 then "green" else if code == 1 then "yellow" else "red"

According to the definition above, if a condition evaluates to a null or unknown value, the else branch is evaluated.

Operators

Operators combine operands into expressions. The precedence of the operators is given in the table below. Operators with a lower number have higher precedence.

PrecedenceOperatorDescription
1a()Function call
a[]Member or index access
.Member access
2|>Pipe forward
3^Exponentiation
4* / %Multiplication, division, and modulo
5+ -Addition and subtraction
6== !=Comparison operators
< <=
> >=
=~ !~
7notUnary logical operator
existsNull check operator
8andLogical AND
9orLogical OR
10if then elseConditional

The operator precedence is encoded directly into the grammar as the following.

  1. Expression = ConditionalExpression .
  2. ConditionalExpression = LogicalExpression
  3. | "if" Expression "then" Expression "else" Expression .
  4. LogicalExpression = UnaryLogicalExpression
  5. | LogicalExpression LogicalOperator UnaryLogicalExpression .
  6. LogicalOperator = "and" | "or" .
  7. UnaryLogicalExpression = ComparisonExpression
  8. | UnaryLogicalOperator UnaryLogicalExpression .
  9. UnaryLogicalOperator = "not" | "exists" .
  10. ComparisonExpression = AdditiveExpression
  11. | ComparisonExpression ComparisonOperator AdditiveExpression .
  12. ComparisonOperator = "==" | "!=" | "<" | "<=" | ">" | ">=" | "=~" | "!~" .
  13. AdditiveExpression = MultiplicativeExpression
  14. | AdditiveExpression AdditiveOperator MultiplicativeExpression .
  15. AdditiveOperator = "+" | "-" .
  16. MultiplicativeExpression = PipeExpression
  17. | MultiplicativeExpression MultiplicativeOperator PipeExpression .
  18. MultiplicativeOperator = "*" | "/" | "%" | "^" .
  19. PipeExpression = PostfixExpression
  20. | PipeExpression PipeOperator UnaryExpression .
  21. PipeOperator = "|>" .
  22. UnaryExpression = PostfixExpression
  23. | PrefixOperator UnaryExpression .
  24. PrefixOperator = "+" | "-" .
  25. PostfixExpression = PrimaryExpression
  26. | PostfixExpression PostfixOperator .
  27. PostfixOperator = MemberExpression
  28. | CallExpression
  29. | IndexExpression .

Dividing by 0 or using the mod operator with a divisor of 0 will result in an error.

Also see Flux Operators.