Statements
There are 4 statement types in SGScript:
Container statements
There are two types of container statements: expression statements and block statements.
- Expression statements contain zero or more expressions, separated by commas (",") and ending with semicolons (";").
- Block statements contain zero or more statements.
Format:
- expression statement:
<expression>[, <expression>[, <expression> ...]] ;
- block statement:
{ [<statement>[, <statement>...]] }
Examples:
a = 5; a = 1, b = 2;
Control flow statements
These statements decide which code gets executed when. They include loops (while, for, foreach, do/while), the if/else construct, break/continue/return.
Formats:
- if/else:
if( <expression> ) <statement> [ else <statement> ]
- while:
while( <expression> ) <statement>
- for:
for( <expression-list>; <expression-list>; <expression-list> ) <statement>
- foreach:
foreach( [<identifier>,][<identifier>] : <expression> ) <statement>
- do/while:
do <statement> while( <expression> )
- break:
break <N>;
- continue:
continue <N>;
- return:
return <expression-list>;
- defer:
defer <statement>
Examples:
if( a ) { print( "a is true" ); } else print( "a is not true" ); while( b > 0 ) b--;
defer
is a special case since it moves the given statement to the end of the block (however that is reached). This can be useful for resource acquisitions:
rsrc = acquire_resource(); defer release_resource( rsrc ); if( work_with( rsrc ) ) return; // release_resource is called here more_work_with( rsrc ); // .. and here
Declaration statements
Statements that begin with "var", "global", "function" or "class" declare and set variables.
Examples:
var x = 5, y = 6, z; global World = null; function fn(){ INFO( "'fn' was called" ); } class C { var s = 1; function f(){ INFO( "'C.f' was called" ); } }
Function call statements
These statements are converted directly to function calls. Currently, 'include' and 'print' functions are available as statements.
Formats:
- include:
include [<file1>[, <file2>...]];
- for each entry, a function call is generated - print:
print [<string>[, <string>...]];
- one function call is generated for all items
Examples:
include "io", "string"; print "Count: ", count;