2.4. Statements

A squirrel program is a simple sequence of statements.:

stats := stat [';'|'\n'] stats

Statements in squirrel are comparable to the C-Family languages (C/C++, Java, C# etc...): assignment, function calls, program flow control structures etc.. plus some custom statement like yield, table and array constructors (All those will be covered in detail later in this document). Statements can be separated with a new line or ‘;’ (or with the keywords case or default if inside a switch/case statement), both symbols are not required if the statement is followed by ‘}’.

2.4.1. Block

stat := '{' stats '}'

A sequence of statements delimited by curly brackets ({ }) is called block; a block is a statement itself.

2.4.2. Control Flow Statements

squirrel implements the most common control flow statements: if, while, do-while, switch-case, for, foreach true and false

Squirrel has a boolean type(bool) however like C++ it considers null, 0(integer) and 0.0(float) as false, any other value is considered true. if/else statement

stat:= 'if' '(' exp ')' stat ['else' stat]

Conditionally execute a statement depending on the result of an expression.:

    return a;
} while statement

stat:= 'while' '(' exp ')' stat

Executes a statement while the condition is true.:

function testy(n)
    local a=0;
    while(a<n) a+=1;

        if(a<0) break;
} do/while statement

stat:= 'do' stat 'while' '(' expression ')'

Executes a statement once, and then repeats execution of the statement until a condition expression evaluates to false.:

local a=0;
} while(a>100) switch statement

stat := 'switch' ''( exp ')' '{'
'case' case_exp ':'
['default' ':'

Switch is a control statement allows multiple selections of code by passing control to one of the case statements within its body. The control is transferred to the case label whose case_exp matches with exp if none of the case match will jump to the default label (if present). A switch statement can contain any number if case instances, if 2 case have the same expression result the first one will be taken in account first. The default label is only allowed once and must be the last one. A break statement will jump outside the switch block.

2.4.3. Loops for

stat:= 'for' '(' [initexp] ';' [condexp] ';' [incexp] ')' statement

Executes a statement as long as a condition is different than false.:

for(local a=0;a<10;a+=1)
glob <- null
    print(loops forever+"\n");
} foreach

'foreach' '(' [index_id','] value_id 'in' exp ')' stat

Executes a statement for every element contained in an array, table, class, string or generator. If exp is a generator it will be resumed every iteration as long as it is alive; the value will be the result of ‘resume’ and the index the sequence number of the iteration starting from 0.:

local a=[10,23,33,41,589,56]
foreach(idx,val in a)
    print("index="+idx+" value="+val+"\n");
foreach(val in a)

2.4.4. break

stat := 'break'

The break statement terminates the execution of a loop (for, foreach, while or do/while) or jumps out of switch statement;

2.4.5. continue

stat := 'continue'

The continue operator jumps to the next iteration of the loop skipping the execution of the following statements.

2.4.6. return

stat:= return [exp]

The return statement terminates the execution of the current function/generator and optionally returns the result of an expression. If the expression is omitted the function will return null. If the return statement is used inside a generator, the generator will not be resumable anymore.

2.4.7. yield

stat := yield [exp]

(see Generators).

2.4.8. Local variables declaration

initz := id [= exp][',' initz]
stat := 'local' initz

Local variables can be declared at any point in the program; they exist between their declaration to the end of the block where they have been declared. EXCEPTION: a local declaration statement is allowed as first expression in a for loop.:

for(local a=0;a<10;a+=1)

2.4.9. Function declaration

funcname := id ['::' id]
stat:= 'function' id ['::' id]+ '(' args ')' stat

creates a new function.

2.4.10. Class declaration

memberdecl := id '=' exp [';'] |    '[' exp ']' '=' exp [';'] | functionstat | 'constructor' functionexp
stat:= 'class' derefexp ['extends' derefexp] '{'

creates a new class.

2.4.11. try/catch

stat:= 'try' stat 'catch' '(' id ')' stat

The try statement encloses a block of code in which an exceptional condition can occur, such as a runtime error or a throw statement. The catch clause provides the exceptionhandling code. When a catch clause catches an exception, its id is bound to that exception.

2.4.12. throw

stat:= 'throw' exp

Throws an exception. Any value can be thrown.

2.4.13. const

stat:= 'const' id '=' 'Integer | Float | StringLiteral

Declares a constant (see Constants & Enumerations).

2.4.14. enum

enumerations := ( 'id' '=' Integer | Float | StringLiteral ) [',']
stat:= 'enum' id '{' enumerations '}'

Declares an enumeration (see Constants & Enumerations).

2.4.15. Expression statement

stat := exp

In Squirrel every expression is also allowed as statement, if so, the result of the expression is thrown away.