Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Work on support for 'switch'
  • Loading branch information
bschmalhofer committed Feb 22, 2009
1 parent 4d3519a commit 170436e
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 4 deletions.
58 changes: 57 additions & 1 deletion src/pct/actions.pm
Expand Up @@ -427,8 +427,64 @@ method elseif_clause($/) {
);
}

# taken from lolcode
method switch_statement($/) {
my $past := PAST::Stmts.new( :name('switch') );
my $past;
my $it := $( $<expression> );
my $count := +$<literal>;
if $count >= 1 {
# there is at least a single case
$count := $count - 1;
my $val := $( $<literal>[$count] );
my $then := PAST::Block.new(
:blocktype('immediate'),
$( $<statement_list>[$count] )
);
$then.blocktype('immediate');
my $expr := PAST::Op.new(
:pasttype('call'),
:name('infix:=='),
$it,
$val
);
$past := PAST::Op.new(
:pasttype('if'),
:node( $/ ),
$expr,
$then
);
}
else {
# there are no cases, however there might be a 'default'
$past := PAST::Stmts.new();
}
if ( $<default> ) {
my $default := $( $<default>[0] );
$default.blocktype('immediate');
$past.push( $default );
}
while ($count > 0) {
$count := $count - 1;
my $val := $( $<literal>[$count] );
my $expr := PAST::Op.new(
:pasttype('call'),
:name('infix:=='),
$it,
$val
);
my $then := PAST::Block.new(
:blocktype('immediate'),
$( $<statement_list>[$count] )
);
$then.blocktype('immediate');
$past := PAST::Op.new(
:pasttype('if'),
:node( $/ ),
$expr,
$then,
$past
);
}

make $past;
}
Expand Down
7 changes: 6 additions & 1 deletion src/pct/grammar.pg
Expand Up @@ -210,9 +210,14 @@ rule else_clause {
'else' <block>
}


# taken from lolcode
rule switch_statement {
'switch' '(' <expression> ')'
'{' '}' {*}
'{'
[ 'case' <literal> ':' <statement_list> ]*
[ 'default' ':' <default=statement_list> ]?
'}' {*}
}

rule elseif_clause {
Expand Down
50 changes: 48 additions & 2 deletions t/php/control_flow.t
Expand Up @@ -20,7 +20,7 @@ use warnings;
use FindBin;
use lib "$FindBin::Bin/../../../../lib", "$FindBin::Bin/../../lib";

use Parrot::Test tests => 19;
use Parrot::Test tests => 23;

language_output_is( 'Pipp', <<'CODE', <<'OUT', 'if, one statement in block' );
<?php
Expand Down Expand Up @@ -308,7 +308,7 @@ round 8
round 9
OUT

language_output_is( 'Pipp', <<'CODE', '', 'switch without a case' );
language_output_is( 'Pipp', <<'CODE', '', 'switch, no case' );
<?php
switch (22) {
Expand All @@ -317,6 +317,52 @@ switch (22) {
?>
CODE

language_output_is( 'Pipp', <<'CODE', '', 'switch, single non-matching case' );
<?php
switch (22) {
case 11:
echo 'no match';
}
?>
CODE

language_output_is( 'Pipp', <<'CODE', 'match', 'switch, single matching case' );
<?php
switch (22) {
case 22:
echo 'match';
}
?>
CODE

language_output_is( 'Pipp', <<'CODE', 'default', 'switch, default case' );
<?php
switch (22) {
case 22:
echo 'default';
}
?>
CODE

language_output_is( 'Pipp', <<'CODE', 'twentytwo', 'switch, two cases' );
<?php
switch (22) {
case 21:
echo 'twentyone';
case 22:
echo 'twentytwo';
}
?>
CODE

# Local Variables:
# mode: cperl
# cperl-indent-level: 4
Expand Down

0 comments on commit 170436e

Please sign in to comment.