Flow Control
As in PASM, flow control in PIR is done entirely with conditional and unconditional branches. This may seem simplistic, but remember PIR is a thin overlay on the assembly language of a virtual processor. For the average assembly language, the jump is the fundamental unit of flow control.
Any PASM branch instruction is valid, but IMCC has some high-level
constructs of its own. The most basic is the unconditional branch:
goto
.
.sub _main goto L1 print "never printed" L1: print "after branch\n" end .end
The first print
statement never runs because the
goto
always skips over it to the label
L1
.
The conditional branches combine if
or
unless
with goto
.
.sub _main $I0 = 42 if $I0 goto L1 print "never printed" L1: print "after branch\n" end .end
In this example, the goto
branches to the label
L1
only if the value stored in
$I0
is true. The
unless
statement
is quite similar, but branches when the tested value is false. An
undefined value, 0, or an empty string are all false values. The
if ... goto
statement is translated directly to
Parrot’s if
, and
unless
translates to Parrot’s
unless
.
The comparison operators (<
,
<=
, = =
,
!=
, >
,
>=
) combine with if ... goto
. These branch when the comparison is true:
.sub _main $I0 = 42 $I1 = 43 if $I0 < $I1 goto L1 print "never printed" L1: print "after branch\n" end .end
This example compares $I0
to
$I1
and branches to the label
L1
if $I0
is less than
$I1
. The if $I0 < $I1 goto L1
statement translates directly to the PASM
lt
branch operation.
Get Perl 6 Essentials now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.