5.2.6. Statements
Most of Spicy’s statements are pretty standard stuff. We summarize them briefly in the following.
5.2.6.1. assert
assert EXPR;
assert EXPR : MSG;
Ensures at runtime that EXPR evaluates to a True value. If it
doesn’t, an exception gets thrown that will typically abort execution.
EXPR must either be of boolean type to begin with, or support
coercion into it. If MSG is specified, it must be a string and
will be carried along with the exception.
5.2.6.2. break
break;
Inside a for or while loop,
break aborts the loop’s body, with execution then continuing
right after the loop construct.
5.2.6.3. confirm
confirm;
If the parser is currently in trial mode, confirm that the unit is successfully
synchronized to the input; the unit is then put into regular parsing mode
again. If the unit is not in trial mode confirm has no effect.
See reject to reject the synchronization instead.
confirm can only be invoked from hooks.
5.2.6.4. for
for ( ID in ITERABLE )
BLOCK
Loops over all the elements of an iterable value. ID is an
identifier that will become local variable inside BLOCK, with the
current loop element assigned on each round. ITERABLE is a value
of any type that provides iterators.
Examples:
module Test;
for ( i in [1, 2, 3] )
print i;
for ( i in b"abc" ) {
print i;
}
local v = vector("a", "b", "c");
for ( i in v )
print i;
# spicyc -j for.spicy
1
2
3
97
98
99
a
b
c
5.2.6.5. if
if ( EXPR )
BLOCK
if ( EXPR )
BLOCK
else
BLOCK
A classic if-statement branching based on a boolean expression
EXPR.
5.2.6.6. import
import MODULE;
Makes the content of another module available, see Modules for more.
5.2.6.7. print
print EXPR;
print EXPR_1, ..., EXPR_N;
Prints one or more expressions to standard output. This is supported for expressions of any type, with each type knowing how to render its values into a readable representation. If multiple expressions are specified, commas will separate them in the output.
Note
A particular use-case combines print with string interpolation
(i.e., string::Modulo):
module Test;
print "Hello, %s!" % "World";
print "%s=%d" % ("x", 1);
# spicyc -j print.spicy
Hello, World!
x=1
5.2.6.8. reject
reject;
If the parse is currently in trial mode, reject the synchronization; this immediately fails parsing of the unit and raises the parse error which caused the unit to be put into trial mode. If the unit is not in trial mode this triggers a generic parse error.
See confirm to confirm the synchronization instead.
reject can only be invoked from hooks.
5.2.6.9. return
return;
return EXPR;
Inside a function or hook, return yields control back to the
caller. If it’s a function with a non-void return value, the
return must provide a corresponding EXPR.
5.2.6.10. stop
stop;
Inside a foreach container hook (see here), aborts
the parsing loop without adding the current (final) value to the
container.
5.2.6.11. switch
switch ( [local IDENT =] CTRL_EXPR ) {
case EXPR [, ..., EXPR]:
BLOCK;
...
case EXPR [, ..., EXPR]:
BLOCK;
[default:
BLOCK]
}
Branches across a set of alternatives based on the value of an control
expression. CTRL_EXPR is compared against all the case
expressions through the type’s equality operator, coercing
CTRL_EXPR accordingly first where necessary. If local IDENT is
specified, the blocks have access to a corresponding local variable
that holds the value of the control expression. If no default is
given, the runtime will throw an UnhandledSwitchCase exception if
there’s no matching case.
Note
Don’t confuse the switch statement with the unit type’s
switch parsing construct. They look similar,
but do different things.
5.2.6.12. throw
throw EXPR;
Triggers a parse error exception with the message indicated by EXPR. EXPR needs
to be a String. throw aborts parsing.
5.2.6.13. try/catch
Todo
This isn’t available in Spicy yet (#89).
try
BLOCK
catch [(TYPE IDENT)]
BLOCK
...
catch [(TYPE IDENT)]
BLOCK
Catches any exception thrown in the try block that match one of
the types in any of catch headers, which must be
Exception types. A catch without a type matches any
exception. If no catch matches an exception thrown in the try
block, it’ll be propagated further up the stack. A bare throw
statement can be used inside a catch block to rethrow the current
exception.
5.2.6.14. while
while ( COND )
BLOCK
while ( local IDENT = EXPR; COND )
BLOCK
while introduces a loop that executes BLOCK for as long as the
boolean COND evaluates to true. The second form initializes a new
local variable IDENT with EXPR, and makes it available inside
both COND and BLOCK.