Files
@ b06a29504c82
Branch filter:
Location: CSY/reowolf/docs/spec/statements.md
b06a29504c82
5.5 KiB
text/markdown
more documentation of language validation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | # 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
```
|