Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Expressions

This chapter specifies the expression syntax of Dada.

Expr definition

An expression Expr is parsed using precedence climbing. From lowest to highest precedence:


Expr ::= AssignExpr

AssignExpr definition

The assignment operator = assigns a value to a place expression. It has the lowest precedence among binary operators:


AssignExpr ::= OrExpr

OrExpr definition

The logical OR operator || performs short-circuit boolean logic:


OrExpr ::= AndExpr

AndExpr definition

The logical AND operator && performs short-circuit boolean logic:


AndExpr ::= CompareExpr

CompareExpr definition

The comparison operators compare two values and produce a boolean result:


CompareExpr ::= AddExpr

CompareOp ::= == | < | > | <= | >=

AddExpr definition

The additive operators perform addition and subtraction:


AddExpr ::= MulExpr

MulExpr definition

The multiplicative operators perform multiplication and division:


MulExpr ::= UnaryExpr

UnaryExpr definition

A unary expression applies a prefix operator to a postfix expression:


UnaryExpr ::= UnaryOp* PostfixExpr
UnaryOp ::= ! | -
  • .not ! performs logical negation.
  • .negate - performs arithmetic negation.

Newline Sensitivity

A binary operator must appear on the same line as its left operand. An operator on a new line begins a new expression or is interpreted as a prefix operator.

PostfixExpr definition

A postfix expression applies zero or more postfix operators to a primary expression:


PostfixExpr ::= PrimaryExpr PostfixOp*

PostfixOp definition

A postfix operator PostfixOp is one of the following:


PostfixOp ::= FieldAccess
              | Call
              | Await
              | PermissionOp

FieldAccess definition

A field access FieldAccess uses dot notation to access a field or name a method:


FieldAccess ::= . Identifier

Call definition

A function or method call Call follows an expression with parenthesized arguments separated by commas. The opening parenthesis must appear on the same line as the callee:


Call ::= ( Expr,* )

Await definition

The .await postfix operator awaits the result of a future:


Await ::= . await

PermissionOp definition

A permission operation PermissionOp requests specific permissions on a value:


PermissionOp ::= give
                 | share
                 | lease
                 | ref

PrimaryExpr definition

A primary expression PrimaryExpr is one of the following:


PrimaryExpr ::= Literal
                | identifier
                | self
                | IfExpr
                | ReturnExpr
                | ConstructorExpr
                | paren-expr
                | block-expr

IfExpr definition

An if expression IfExpr evaluates a condition and executes a block:


IfExpr ::= if Expr Block (else if Expr Block)* (else Block)?

An if expression may have an else clause.

Multiple conditions may be chained with else if.

ReturnExpr definition

A return expression ReturnExpr exits the enclosing function, optionally with a value. The value, if present, must appear on the same line as return:


ReturnExpr ::= return Expr?

ConstructorExpr definition

A constructor expression ConstructorExpr creates a new instance of a class or struct. The opening brace must appear on the same line as the type name:


ConstructorExpr ::= Identifier { ConstructorField,* }
ConstructorField ::= Identifier : Expr