Statements

There are 4 statement types in SGScript:

Container statements

There are two types of container statements: expression statements and block statements.

Format:

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:

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:

Examples:

include "io", "string";
print "Count: ", count;