1.3. Statements

A quirrel program is a simple sequence of statements.:

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

Statements in quirrel 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 ‘}’.

1.3.1. Block

stat := '{' stats '}'

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

1.3.2. Control Flow Statements

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

Quirrel 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.:

if (a > b)
    a = b
    b = a
if ( a == 10 ) {
    b = a + b
    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

    while(1) {
        if ( a < 0 ) break;
        a -= 1;
} 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
do {
    a += 1
} 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.

1.3.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
for (glob=0;glob<10;glob+=1) {
for (;;) {
    println("loops forever")
stat:= 'for' '(' variable ':' [startValue ',' ] limitValue [ ',' step] ')' statement
stat:= 'for' '(' count ')' statement
stat:= 'for' '(' [startValue ',' ] limitValue ')' statement

This is sytnax sugar over regular ‘for’ loop which are being desugared into the following general form::

for (variable = startValue; variable < limitValue; variable += step)
    println($"indexed range loop: {variable}")
for ($i = 0; $i < count; ++$i)
    println("count range loop")
for ($i = startValue; $i < limitValue; ++$i)
    println("bounded range loop")

Here if ‘startValue’ is not explicitly provided it is ‘0’ by default. If ‘step’ is not provided it is ‘1’ by default 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.:

let a=[10,23,33,41,589,56]
foreach (idx, val in a)
    println($"index={idx} value={val}")
foreach (val in a)
    println ($"value={val}")

1.3.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;

1.3.5. continue

stat := 'continue'

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

1.3.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.

1.3.7. yield

stat := yield [exp]

(see Generators).

1.3.8. Local variables declaration ( local )

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)

1.3.9. Named bindings declaration ( let )

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

Named bindings like 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. Named bindings MUST be initialized. Named bindings works exactly as const in JavaScript ES6 (Note - big there is siginificant difference with const in Squirrel). The main difference between local variables and named bindings is that named bindings can’t be reassigned. So if you see somewhere in function scope let foo = you can be sure that foo will always reference object on initialization

local foo = 2
foo = 3
print(foo) //3

let foo = 2
foo = 3 //error in script compilation


While named bindings looks like constants they do not provide immutability. Named bindings can reference mutable objects (like array or instance or table)

1.3.10. Function declaration

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

creates a new function.

1.3.11. Class declaration

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

creates a new class.

1.3.12. 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 exception-handling code. When a catch clause catches an exception, its id is bound to that exception.

1.3.13. throw

stat:= 'throw' exp

Throws an exception. Any value can be thrown.

1.3.14. const

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

Declares a constant (see Constants & Enumerations).

1.3.15. enum

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

Declares an enumeration (see Constants & Enumerations).

1.3.16. Expression statement

stat := exp

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