Skip to content

Commit

Permalink
Fix parsing single-statement if/for/while
Browse files Browse the repository at this point in the history
  • Loading branch information
bacek committed Mar 2, 2011
1 parent 6e00eab commit 2ea3b0f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
5 changes: 5 additions & 0 deletions compilers/opsc/src/Ops/Compiler/Actions.pm
Expand Up @@ -596,6 +596,11 @@ method statement_control:sym<continue> ($/) {
make PAST::Stmts.new($past);
}

method statement_or_block ($/) {
$<labeled_statement>
?? make $<labeled_statement>.ast
!! make $<blockoid>.ast
}

method circumfix:sym<( )> ($/) {
my $past := $<EXPR>.ast;
Expand Down
13 changes: 9 additions & 4 deletions compilers/opsc/src/Ops/Compiler/Grammar.pm
Expand Up @@ -193,18 +193,18 @@ proto rule statement_control { <...> }

rule statement_control:sym<if> {
<sym> '(' <EXPR> ')'
<then=.statement_list>
[ 'else' <else=.statement_list> ]?
<then=.statement_or_block>
[ 'else' <else=.statement_or_block> ]?
}

rule statement_control:sym<while> {
<sym> '(' <condition=.EXPR> ')'
<statement_list>
<statement_list=.statement_or_block>
}

rule statement_control:sym<for> {
<sym> '(' <init=.EXPR>? ';' <test=.EXPR>? ';' <step=.EXPR>? ')'
<statement_list>
<statement_list=.statement_or_block>
}

rule statement_control:sym<do-while> {
Expand All @@ -221,6 +221,11 @@ rule statement_control:sym<switch> {
rule statement_control:sym<break> { <sym> }
rule statement_control:sym<continue> { <sym> }

token statement_or_block {
| <labeled_statement> <.eat_terminator>
| <blockoid>
}

# HACK to support for INT_FMT "\n"
token term:sym<concatenate_strings> { # Long-long name as LTM workaround
<identifier> \s+ <quote>
Expand Down
8 changes: 8 additions & 0 deletions t/compilers/opsc/03-past-declarator.t
Expand Up @@ -78,6 +78,14 @@ $past := $compiler.compile($buf, target => 'past');
$op := $past<ops>[0];
is( $op[0][0]<pasttype>, 'for', "for loop generates right pasttype");

$buf := q|
inline op bar(out PMC, in INT) {
if (foo) bar(); baz();
}|;
$past := $compiler.compile($buf, target => 'past');
$op := $past<ops>[0];
# if, baz, WB, goto
is( +@($op[0]), 4, "Properly handle single statement in 'if'");


done_testing();
Expand Down

0 comments on commit 2ea3b0f

Please sign in to comment.