Files @ f55ca167cf51
Branch filter:

Location: CSY/reowolf/docs/spec/statements.md - annotation

MH
expand section on language validation
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
8e4c0fc8b4df
# Statements

Alongside expressions, statements are the main building block of the imperative procedure bodies. We'll define all of the possible statements in this document, and combine them at the end of this chapter into a `Stmt`.

## Block Statement

A block statement is a list of statements wrapped in curly braces. It has the additional meaning of scoping all of its defined variables. That is: it can access variables from its outer scopes. But not of the outer scopes can access the variables inside the inner block statement. We'll expand on the scoping rules in a later chapter.

A block statement is defined as:

```
StmtBlock = "{" Stmt* "}"
```

## If Statement

An if-statement contains a test expression that is evaluated and should resolve to a boolean. If that boolean is true then we take the "then" branch of the if-statement. If that boolean is false then we can take "else" branch if it is defined by the programmer, otherwise simply skip the "then" branch. An if statement is defined as:

```
StmtIf = 
    KwIf "(" Expr ")" Stmt
    (KwElse Stmt)?
```

## While Statement

A while statement is a loop that continues executing based on a test expression. If the test expression is true, then we enter the inner statement and execute it. If the test expression is false then we go to the next statement. If we have entered the inner statement, then upon reaching its end we go back to the while statement and re-evaluate the test expression. A while statement is defined as:

```
StmtWhile = KwWhile "(" Expr ")" Stmt
```

## Labeled Statement

A labeled statement attaches a label to a particular nested label. This label can be used for goto-statements. Secondly, if the label is placed before a loop statement, then one may use break- and continue-statements to refer to that particular loop.

```
Label = Ident
StmtLabeled = Label ":" Stmt
```

## Break Statement

A break statement may halt executing a loop body and jump to the statement coming after the loop statement. It may optionally have a label to break a particular loop. It is defined as:

```
StmtBreak = KwBreak Label? ";"
```

Some (contrived) examples:

```
u32 counter = 0;
while (true) {
    if (counter == 10) break;
    counter += 1;
}

u32 i = 0;
u32 counter = 0;

outer: while (i < 10) {
    u32 j = 0;
    while (j < 10) {
        if (counter == 42) break outer;
        j += 1;
        counter += 1;
    }
    i += 1;
}
```

## Continue Statement

A continue statement may halt executing a loop body and jump to the loop statement in order to evaluate its test expression (and optionally execute the loop body) again. It may optionally have a label to continue at a particular loop. It is defined as:

```
StmtContinue = KwContinue Label? ";"
```

## Return Statement

A return statement is used in function bodies in order to halt the execution of the function, and provide a return value to the caller. It is defined as:

```
StmtReturn = KwReturn Expr ";"
```

**note**: Like with functions, in the future we will likely support multiple return types, hence the syntax may change in the future.

## Goto Statement

A goto statement is used to jump to a particular label within a procedure body. it is defined as:

```
StmtGoto = KwGoto Label ";"
```

## New Statement

A new statement instantiates a primitive component. It is defined as:

```
StmtNew = KwNew CallExpr ";"
```

## Local Variable Statement

A local variable statement instantiates a new variable which will have assigned a particular initial value. It is defined as:

```
VarRef = Ident
StmtLocVar = TypeRef VarRef "=" Expr ";"
```

**note**: We may change local variable statements to act as expressions in the future. This has no practical effect on the manner in which valid code can be written. See the comment at "expression statement"

## Local Channel Statement

A local channel statement instantiates a new channel. It returns the two ports that compose the channel. It is defined as:

```
PolyArgs = "<" (TypeRef ",")* TypeRef ">"
StmtLocChan = KwChannel PolyArgs? VarRef "->" VarRef ";"
```

Some examples are:

```
channel input_of_channel -> output_from_channel
channel<u32> output_of_component -> input_of_component
```

Although we'll expand on types later. One should note that in practice a channel only accepts one polymorphic argument. The port on the left hand side of the channel we call the output port (with type `out<T>`) and the right hand side we call the input port (with type `in<T>`). These names depend on the manner in which one views the two ends of the channel. From the point of view of the channel data flows in from the left hand side and out from the right hand side. However, we take the point of view from the component that uses these ports. Hence we view the left hand side as the `out` port, because a component puts data into it, and the right hand side as the `in` port, because a component gets data from it.

## Expression Statement

An expression statement is simply an expression placed in a statement. Hence it is simply defined as:

```
StmtExpr = Expr ";"
```

**note**: Should I "formally" define assignment expressions here as well? They act like statements, but in code we treat them as expressions (and apply some checks to ensure they're used at the statement level) because this simplifies all code a lot. Maybe we make the document simpler as well by simply stating that they're expressions?

## Combining All Possible Statements

Combining all of the possible statements, we arrive at:

```
Stmt = 
    StmtBlock | StmtLabeled
    StmtIf | StmtWhile | 
    StmtBreak | StmtContinue | StmtReturn | StmtGoto |
    StmtLocVar | StmtLocChan | StmtExpr
```