Skip to content

Latest commit

 

History

History
104 lines (93 loc) · 4.26 KB

Expressions.md

File metadata and controls

104 lines (93 loc) · 4.26 KB
title parent weight
Expressions
Impala.md
7

Impala is an expression-oriented language. This means, that [if-]({% link If-Expression.md %}), [while-]({% link While-Expression.md %}) and [for-constructs]({% link For-Expression.md %}) are not [Statements] but [Expressions] that yield values. It is perfectly fine and even encouraged to nest expressions.

[Expressions]: {% link Expressions.md %} [Statements]: {% link Statements.md %}

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
`
/..../ `
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)
== !=
< <= > >=
[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] (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)

[Postfix Expression]: {% link Postfix-Expression.md %} [Map Expression]: {% link Map-Expression.md %} [Type Application Expression]: {% link Type-Application-Expression.md %} [Field Expression]: {% link Field-Expression.md %} [Prefix Expression]: {% link Prefix-Expression.md %} [Function Expression]: {% link Function-Expression.md %} [Cast Expression]: {% link Cast-Expression.md %} [Infix Expression]: {% link Infix-Expression.md %}

Comparison with C

  • The [as][Cast Expression] 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][Infix Expression] 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.