zscript-doc/language/Statements.md

290 lines
6.4 KiB
Markdown
Raw Permalink Normal View History

2019-04-02 06:36:03 -07:00
<!-- vim-markdown-toc GFM -->
2019-08-14 03:31:41 -07:00
* [Statements](#statements)
2019-04-02 06:36:03 -07:00
* [Compound Statements](#compound-statements)
* [Expression Statements](#expression-statements)
2019-08-14 03:31:41 -07:00
* [Example: Expression statements](#example-expression-statements)
2019-04-02 06:36:03 -07:00
* [Conditional Statements](#conditional-statements)
2019-08-14 03:31:41 -07:00
* [Example: Conditional statements](#example-conditional-statements)
2019-04-02 06:36:03 -07:00
* [Switch Statements](#switch-statements)
2019-08-14 03:31:41 -07:00
* [Example: Switch statements](#example-switch-statements)
2019-04-02 06:36:03 -07:00
* [Loop Statements](#loop-statements)
* [Control Flow Statements](#control-flow-statements)
2019-08-14 03:31:41 -07:00
* [Example: Control flow statements](#example-control-flow-statements)
2019-04-02 06:36:03 -07:00
* [Local Variable Statements](#local-variable-statements)
* [Multi-assignment Statements](#multi-assignment-statements)
2019-08-14 03:31:41 -07:00
* [Example: Multi-assignment statements](#example-multi-assignment-statements)
2019-04-02 06:36:03 -07:00
* [Static Array Statements](#static-array-statements)
* [Null Statements](#null-statements)
<!-- vim-markdown-toc -->
2019-08-14 03:31:41 -07:00
# Statements
2019-04-02 06:36:03 -07:00
All functions are made up of a list of *statements* enclosed with left and
right braces, which in and of itself is a statement called a *compound
statement*, or *block*.
2019-04-02 08:28:16 -07:00
# Compound Statements
2019-04-02 06:36:03 -07:00
A compound statement is formed as:
```
{
2019-08-14 03:38:33 -07:00
$[Statement...]$
2019-04-02 06:36:03 -07:00
}
```
Note that the statement list is optional, so an empty compound statement `{}`
is entirely valid.
2019-04-02 08:28:16 -07:00
# Expression Statements
2019-04-02 06:36:03 -07:00
An expression statement is the single most common type of statement in just
about any programming language. In ZScript, exactly like C and C++, an
expression statement is simply formed with any expression followed by a
semicolon. Function calls and variable assignments are expressions, for
instance, so it is quite clear why they are common.
Their syntax is:
```
Expression ;
```
## Example: Expression statements
```
2019-04-05 16:34:55 -07:00
// Some basic expressions.
2019-04-02 06:36:03 -07:00
MyCoolFunction(5, 4);
m_MyCoolMember = 500;
2019-04-05 16:34:55 -07:00
// Does nothing, of course, but valid.
5 * 5;
2019-04-02 06:36:03 -07:00
```
2019-04-02 08:28:16 -07:00
# Conditional Statements
2019-04-02 06:36:03 -07:00
A conditional statement will, conditionally, choose a statement (or none) to
execute. They work the same as in C and ACS:
```
if ( Expression ) Statement $[ else Statement]$
```
## Example: Conditional statements
```
2019-04-05 16:34:55 -07:00
// Simple conditional.
2019-04-02 06:36:03 -07:00
if(a)
2019-08-14 03:38:33 -07:00
B();
2019-04-02 06:36:03 -07:00
2019-04-05 16:34:55 -07:00
// Simple conditional, with else statement and a block.
2019-04-02 06:36:03 -07:00
if(a)
{
2019-08-14 03:38:33 -07:00
B();
c = d;
2019-04-02 06:36:03 -07:00
}
else
2019-08-14 03:38:33 -07:00
e = f;
2019-04-02 06:36:03 -07:00
```
2019-04-02 08:28:16 -07:00
# Switch Statements
2019-04-02 06:36:03 -07:00
A switch statement takes an expression of integer or name type and selects a
labeled statement to run. They work the same as in C and ACS:
```
switch ( Expression ) Statement
```
## Example: Switch statements
```
2019-04-05 16:34:55 -07:00
// A switch demonstrating fall-through and default cases.
2019-04-02 06:36:03 -07:00
switch(a)
{
2019-04-05 16:34:55 -07:00
case 500:
2019-08-14 03:38:33 -07:00
Console.PrintF("a is 500");
break;
2019-04-05 16:34:55 -07:00
case 501:
2019-08-14 03:38:33 -07:00
Console.PrintF("a is 501");
// Falls through to the next case.
2019-04-05 16:34:55 -07:00
case 502:
2019-08-14 03:38:33 -07:00
Console.PrintF("a is 501 or 502");
break;
2019-04-02 06:36:03 -07:00
default:
2019-08-14 03:38:33 -07:00
Console.PrintF("not sure what a is!");
// "break" is implied here.
2019-04-02 06:36:03 -07:00
}
```
2019-04-02 08:28:16 -07:00
# Loop Statements
2019-04-02 06:36:03 -07:00
ZScript has five loop statements, `for`, `while`, `until`, `do while` and `do
until`. `for`, `while` and `do while` work the same as in C, C++ and ACS, while
`until` and `do until` do the inverse of `while` and `do while`.
The `for` loop takes a limited statement and two optional expressions: The
statement for when the loop begins (which is scoped to the loop,) one
expression for checking if the loop should break, and one which is executed
every time the loop iterates. Its syntax is:
```
for ( $[Expression-or-Local-variable-statement]$ ; $[Expression]$ ; $[Expression]$ ) Statement
```
The `while` loop simply takes one expression for checking if the loop should
break, equivalent to `for(; a;)`.
The `until` loop is equivalent to `while(!a)`. Their syntax are:
```
while ( Expression ) Statement
until ( Expression ) Statement
```
`do while` and `do until` will only check the expression after the first
iteration is complete. The `do while` and `do until` loops are formed as such:
```
do
2019-08-14 03:38:33 -07:00
Statement
2019-04-02 06:36:03 -07:00
while ( Expression ) // unlike C, you don't need a semicolon here
do
2019-08-14 03:38:33 -07:00
Statement
2019-04-02 06:36:03 -07:00
until ( Expression )
```
2019-04-02 08:28:16 -07:00
# Control Flow Statements
2019-04-02 06:36:03 -07:00
As in C, there are three control flow statements that manipulate where the
program will execute statements next, which are available contextually. They
are `continue`, `break` and `return`.
`continue` is available in loop statements and will continue to the next
iteration immediately:
```
continue ;
```
`break` is available in loop statements and switch statements, and will break
out of the containing statement early:
```
break ;
```
`return` is available in functions. If the function does not return any values,
it may only be spelled `return;` and will simply exit the function early. If
the function does return values, it takes a comma-separated list for each value
returned:
```
return $[Expression $[ , Expression]$...]$ ;
```
## Example: Control flow statements
```
2019-04-05 16:34:55 -07:00
// Use of "continue."
2019-04-02 06:36:03 -07:00
for(int i = 0; i < 50; i++)
{
2019-08-14 03:38:33 -07:00
// Don't do anything when "i" is 25.
if(i == 25)
continue;
2019-04-02 06:36:03 -07:00
2019-08-14 03:38:33 -07:00
DoThing(i);
2019-04-02 06:36:03 -07:00
}
2019-04-05 16:34:55 -07:00
// Use of "break."
2019-04-02 06:36:03 -07:00
for(int i = 0; i < 50; i++)
{
2019-08-14 03:38:33 -07:00
// "break" when "i" is 25.
if(i == 25)
break;
2019-04-02 06:36:03 -07:00
2019-08-14 03:38:33 -07:00
DoThing(i);
2019-04-02 06:36:03 -07:00
}
2019-04-05 16:34:55 -07:00
// Use of `return` in various contexts.
2019-04-02 06:36:03 -07:00
void ReturnsNothing()
{
2019-08-14 03:38:33 -07:00
// Exit early if "m_Thing" isn't 50.
if(m_Thing != 50)
return;
2019-04-02 06:36:03 -07:00
2019-08-14 03:38:33 -07:00
DoThing(m_Thing);
2019-04-02 06:36:03 -07:00
}
int ReturnsInt()
{
2019-08-14 03:38:33 -07:00
// "m_Thing" is 50, so return 50.
if(m_Thing == 50)
return 50;
2019-04-02 06:36:03 -07:00
2019-08-14 03:38:33 -07:00
// Must have a return, eventually.
return 0;
2019-04-02 06:36:03 -07:00
}
int, int ReturnsTwoInts()
{
2019-08-14 03:38:33 -07:00
// Returns 1 and 2.
return 1, 2;
2019-04-02 06:36:03 -07:00
}
```
2019-04-02 08:28:16 -07:00
# Local Variable Statements
2019-04-02 06:36:03 -07:00
Local variable statements are formed in one of 2 ways. The `let` keyword can be
used to automatically determine the type of the variable from the initializer,
while the regular syntax uses an explicit type, and initialization is optional.
Variables' syntax are one of:
```
Variable-name
Variable-name = Expression
```
And local variable statements have the syntax of either:
```
let Identifier = Expression ;
Type Variable $[ , Variable]$... ;
```
2019-04-02 08:28:16 -07:00
# Multi-assignment Statements
2019-04-02 06:36:03 -07:00
Expressions or functions that return multiple values can be assigned into
multiple variables with the syntax:
```
[ Expression $[ , Expression]$... ] = Expression ;
```
Note that the surrounding brackets are literal and not an optional element.
## Example: Multi-assignment statements
```
2019-04-05 16:34:55 -07:00
// Getting the actor out of "A_SpawnItemEx."
Actor mo; bool spawned; [spawned, mo] = A_SpawnItemEx("MyCoolActor");
2019-04-02 06:36:03 -07:00
```
2019-04-02 08:28:16 -07:00
# Static Array Statements
2019-04-02 06:36:03 -07:00
Static arrays can be defined normally as a statement.
2019-04-02 08:28:16 -07:00
# Null Statements
2019-04-02 06:36:03 -07:00
A null statement does nothing, and is formed `;`. It is similar to an empty
compound statement.
<!-- EOF -->