Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Operator expressions grammar #141

Merged
merged 3 commits into from
Mar 10, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 115 additions & 1 deletion src/expressions/operator-expr.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Operator expressions

> **<sup>Syntax</sup>**
> _OperatorExpression_ :
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing GroupedExpression.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grouped expression will be located directly on the Expression production. I think it would be out of place if put together with the OperatorExpressions -- even though it is in the same section. I put it in the operator expression section mainly because I think it is too small to be on a separate section by itself.

What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think grammar should be associated with its prose. GroupedExpression is on this page, so it should show up here. If it feels out of place in the grammar, it should feel out of place on the prose and moved to its own page, albeit the shortness is concerning. Although some examples could help with that.

> &nbsp;&nbsp; &nbsp;&nbsp; [_BorrowExpression_]
> &nbsp;&nbsp; | [_DereferenceExpression_]
> &nbsp;&nbsp; | [_ErrorPropagationExpression_]
> &nbsp;&nbsp; | [_NegationExpression_]
> &nbsp;&nbsp; | [_ArithmeticOrLogicalExpression_]
> &nbsp;&nbsp; | [_ComparisonExpression_]
> &nbsp;&nbsp; | [_LazyBooleanExpression_]
> &nbsp;&nbsp; | [_TypeCastExpression_]
> &nbsp;&nbsp; | [_AssignmentExpression_]
> &nbsp;&nbsp; | [_CompoundAssignmentExpression_]
Operators are defined for built in types by the Rust language. Many of the
following operators can also be overloaded using traits in `std::ops` or
`std::cmp`.
Expand All @@ -21,6 +34,10 @@ overflow:

## Grouped expressions

> **<sup>Syntax</sup>**
> _GroupedExpression_ :
> &nbsp;&nbsp; `(` [_Expression_] `)`
An expression enclosed in parentheses evaluates to the result of the enclosed
expression. Parentheses can be used to explicitly specify evaluation order
within an expression.
Expand All @@ -38,6 +55,11 @@ assert_eq!(y, 20);

## Borrow operators

> **<sup>Syntax</sup>**
> _BorrowExpression_ :
> &nbsp;&nbsp; &nbsp;&nbsp; (`&`|`&&`) [_Expression_]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the && variant here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the lexer will give us && when it finds two contiguous ampersands, since && is a valid symbol. The same problem occurs with << and >> in generics/paths, and || in closures. Example:

let & & mut a = & & mut 10;
let &&  mut a = &&  mut 10;

I'm currently very busy, but I'll put some explanation in the next days, since it isn't really obvious why && is accepted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

> &nbsp;&nbsp; | (`&`|`&&`) `mut` [_Expression_]
The `&` (shared borrow) and `&mut` (mutable borrow) operators are unary prefix
operators. When applied to a [place expression], this expressions produces a
reference (pointer) to the location that the value refers to. The memory
Expand All @@ -63,8 +85,26 @@ let mut array = [-2, 3, 9];
}
```

Even though `&&` is a single token ([the lazy 'and' operator](#lazy-boolean-operators)),
when used in the context of borrow expressions it works as two borrows:

```rust
// same meanings:
let a = && 10;
let a = & & 10;

// same meanings:
let a = &&&& mut 10;
let a = && && mut 10;
let a = & & & & mut 10;
```

## The dereference operator

> **<sup>Syntax</sup>**
> _DereferenceExpression_ :
> &nbsp;&nbsp; `*` [_Expression_]
The `*` (dereference) operator is also a unary prefix operator. When applied to
a [pointer](types.html#pointer-types) it denotes the pointed-to location. If
the expression is of type `&mut T` and `*mut T`, and is either a local
Expand All @@ -86,6 +126,10 @@ assert_eq!(*y, 11);

## The question mark operator

> **<sup>Syntax</sup>**
> _ErrorPropagationExpression_ :
> &nbsp;&nbsp; [_Expression_] `?`
The question mark operator (`?`) unwraps valid values or returns errornous
values, propagating them to the calling function. It is a unary postfix
operator that can only be applied to the types `Result<T, E>` and `Option<T>`.
Expand Down Expand Up @@ -130,6 +174,11 @@ assert_eq!(try_option_none(), None);

## Negation operators

> **<sup>Syntax</sup>**
> _NegationExpression_ :
> &nbsp;&nbsp; &nbsp;&nbsp; `-` [_Expression_]
> &nbsp;&nbsp; | `!` [_Expression_]
These are the last two unary operators. This table summarizes the behavior of
them on primitive types and which traits are used to overload these operators
for other types. Remember that signed integers are always represented using
Expand All @@ -154,6 +203,19 @@ assert_eq!(true, !false);

## Arithmetic and Logical Binary Operators

> **<sup>Syntax</sup>**
> _ArithmeticOrLogicalExpression_ :
> &nbsp;&nbsp; &nbsp;&nbsp; [_Expression_] `+` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `-` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `*` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `/` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `%` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `&` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `|` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `^` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `<<` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `>>` [_Expression_]
Binary operators expressions are all written with infix notation. This table
summarizes the behavior of arithmetic and logical binary operators on
primitive types and which traits are used to overload these operators for other
Expand Down Expand Up @@ -194,6 +256,15 @@ assert_eq!(-10 >> 2, -3);

## Comparison Operators

> **<sup>Syntax</sup>**
> _ComparisonExpression_ :
> &nbsp;&nbsp; &nbsp;&nbsp; [_Expression_] `==` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `!=` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `>` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `<` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `>=` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `<=` [_Expression_]
Comparison operators are also defined both for primitive types and many type in
the standard library. Parentheses are required when chaining comparison
operators. For example, the expression `a == b == c` is invalid and may be
Expand Down Expand Up @@ -238,6 +309,11 @@ assert!("World" >= "Hello");

## Lazy boolean operators

> **<sup>Syntax</sup>**
> _LazyBooleanExpression_ :
> &nbsp;&nbsp; &nbsp;&nbsp; [_Expression_] `||` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `&&` [_Expression_]
The operators `||` and `&&` may be applied to operands of boolean type. The
`||` operator denotes logical 'or', and the `&&` operator denotes logical
'and'. They differ from `|` and `&` in that the right-hand operand is only
Expand All @@ -253,6 +329,10 @@ let y = false && panic!(); // false, doesn't evaluate `panic!()`

## Type cast expressions

> **<sup>Syntax</sup>**
> _TypeCastExpression_ :
> &nbsp;&nbsp; [_Expression_] `as` [_PathInExpression_]
A type cast expression is denoted with the binary operator `as`.

Executing an `as` expression casts the value on the left-hand side to the type
Expand Down Expand Up @@ -321,8 +401,15 @@ same trait object.
* `u8` to `char` cast
* Casts to the `char` with the corresponding code point.

[float-int]: https://github.com/rust-lang/rust/issues/10184
[float-float]: https://github.com/rust-lang/rust/issues/15536

## Assignment expressions

> **<sup>Syntax</sup>**
> _AssignmentExpression_ :
> &nbsp;&nbsp; | [_Expression_] `=` [_Expression_]
An _assignment expression_ consists of a [place expression] followed by an
equals sign (`=`) and a [value expression].

Expand All @@ -341,6 +428,19 @@ x = y;

## Compound assignment expressions

> **<sup>Syntax</sup>**
> _CompoundAssignmentExpression_ :
> &nbsp;&nbsp; &nbsp;&nbsp; [_Expression_] `+=` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `-=` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `*=` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `/=` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `%=` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `&=` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `|=` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `^=` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `<<=` [_Expression_]
> &nbsp;&nbsp; | [_Expression_] `>>=` [_Expression_]
The `+`, `-`, `*`, `/`, `%`, `&`, `|`, `^`, `<<`, and `>>` operators may be
composed with the `=` operator. The expression `place_exp OP= value` is
equivalent to `place_expr = place_expr OP val`. For example, `x = x + 1` may be
Expand All @@ -361,4 +461,18 @@ assert_eq!(x, 14);
[temporary value]: expressions.html#temporary-lifetimes
[float-int]: https://github.com/rust-lang/rust/issues/10184
[float-float]: https://github.com/rust-lang/rust/issues/15536
[`unit` type]: types.html#tuple-types
[`unit` type]: types.html#tuple-types

[_BorrowExpression_]: #borrow-operators
[_DereferenceExpression_]: #the-dereference-operator
[_ErrorPropagationExpression_]: #the--operator
[_NegationExpression_]: #negation-operators
[_ArithmeticOrLogicalExpression_]: #arithmetic-and-logical-binary-operators
[_ComparisonExpression_]: #comparison-operators
[_LazyBooleanExpression_]: #lazy-boolean-operators
[_TypeCastExpression_]: #type-cast-expressions
[_AssignmentExpression_]: #assignment-expressions
[_CompoundAssignmentExpression_]: #compound-assignment-expressions

[_Expression_]: expressions.html
[_PathInExpression_]: paths.html