# Expressions

## Contents

Impala is an expression-oriented language. This means, that if-, while- and for-constructs are not Statements but Expressions that yield values. It is perfectly fine and even encouraged to nest expressions.

## Syntax

```
expr ::= primary_expr
| block_expr
| for_expr
| if_expr
| while_expr
| with_expr
| definite_array_expr
| function_expr
| indefinite_array_expr
| simd_expr
| struct_expr
| tuple_expr
| infix_expr
| postfix_expr
| prefix_expr
| cast_expr
| field_expr
| map_expr
| type_application_expr
```

## Precedence

Nesting of expressions is disambiguated according to this table:

Operator | Description | Associativity |
---|---|---|

`++` `--` `()` `[]` `.` |
Postfix Expression (increment/decrement) Map Expression Type Application Expression Field Expression |
left-to-right |

`++` `--` `+` `-` `!` `~` `*` `&` `&mut` `|/*....*/|` |
Prefix Expression (increment/decrement) Prefix Expression (unary plus/minus) Prefix Expression (logical/bitwise NOT) Prefix Expression (alloc) Prefix Expression (dereference) Prefix Expression (address-of/mutable address-of) Function Expression |
right-to-left |

`as` |
Cast Expression | left-to-right |

`*` `/` `%` |
Infix Expression (multiplication/division/remainder) | left-to-right |

`+` `-` |
Infix Expression (addition/subtraction) | left-to-right |

`<<` `>>` |
Infix Expression (bitwise left/right shift) | left-to-right |

`&` |
Infix Expression (bitwise AND) | left-to-right |

`^` |
Infix Expression (bitwise XOR) | left-to-right |

`|` |
Infix Expression (bitwise OR) | left-to-right |

`==` `!=` `<` `<=` `>` `>=` |
Infix Expression (equal/not equal) Infix Expression (less/less equal/greater/greater equal) |
left-to-right |

`&&` |
Infix Expression (logical AND) | left-to-right |

`||` |
Infix Expression (logical OR) | left-to-right |

`=` `*=` `/=` `%=` `+=` `-=` `<<=` `>>=` `&=` `^=` `|=` |
Infix Expression (assignment) Infix Expression (assign by sum/difference) Infix Expression (assign by product/quotient/remainder) Infix Expression (assign by bitwise left/right shift) Infix Expression (assign by bitwise AND/XOR/OR) |
left-to-right |

## Comparison with C

- The as binds strongest of all binary operators. This one does not exist in C.
`&`

,`^`

and`|`

bind stronger than assignments. This is more intuitive.-
All relations share the same precedence. This means

`a == b < c`

binds in Impala like this because all infix operators bind from left to right:

`(a == b) < c`

Whereas C binds like this because

`==`

binds weaker than`<`

:`a == (b < c)`

Rationale: This is such a subtle thing hardly anyone knows. Some C-compilers emit a warning if the programmer relies on this precedence (

`-Wparentheses`

implied by`-Wall`

in`gcc`

). - Assignmentsâ€™ associativity is left-to-right instead of right-to-left.
So
`a += b += c`

binds in Impala like this:

`(a += b) += c`

Whereas C binds like this:

`a += (b += c)`

Rationale: This makes all binary operator consistently left-to-right associative. However, the expression doesnâ€™t type anyway because all assignments yield unit

`()`

. So there is not much point for complaints.