Author: duff
Date: Sat Jan 3 00:03:47 2009
New Revision: 34864
Modified:
trunk/languages/ecmascript/src/parser/actions.pm
trunk/languages/ecmascript/src/parser/grammar.pg
Log:
[ecmascript] implement variation of C-style for loops with variable declarations
Modified: trunk/languages/ecmascript/src/parser/actions.pm
==============================================================================
--- trunk/languages/ecmascript/src/parser/actions.pm (original)
+++ trunk/languages/ecmascript/src/parser/actions.pm Sat Jan 3 00:03:47 2009
@@ -138,7 +138,18 @@
## <step>
## }
##
-method for1_statement($/) {
+sub c_style_for($/,$var_decl) {
+
+ ## if there are variable declarations in the init portion of the C-style
+ ## for loop, we have to evaluate it first so that they are available
+ ## during the rest of loop
+ my $init;
+ if $var_decl {
+ $init := $( $<init> );
+ } else {
+ if $<init> { $init := $( $<init>[0] ); }
+ }
+
my $body := $( $<statement> );
## if there's a step, create a new compound statement node,
@@ -164,13 +175,14 @@
## if there's an init step, it is evaluated before the loop, so
## create a compound statement node ($init, $loop).
- if $<init> {
- my $init := $( $<init>[0] );
- make PAST::Stmts.new( $init, $loop, :node($/) );
- }
- else {
- make $loop;
+ if $init {
+ $loop := PAST::Stmts.new( $init, $loop, :node($/) );
}
+ make $loop;
+}
+
+method for1_statement($/) {
+ c_style_for($/,0);
}
method for2_statement($/) {
@@ -190,11 +202,7 @@
}
method for4_statement($/) {
- # XXX todo
- my $past;
- my $body := $( $<statement> );
- $past := $body;
- make $past;
+ c_style_for($/,1);
}
method labelled_statement($/) {
@@ -262,7 +270,7 @@
make $past;
}
-method variable_statement($/) {
+method variable_declaration_list($/) {
## each variable declared in this statement becomes a separate PIR
## statement; therefore create a Stmts node.
my $past := PAST::Stmts.new( :node($/) );
@@ -272,6 +280,10 @@
make $past;
}
+method variable_statement($/) {
+ make $( $<variable_declaration_list> )
+}
+
method variable_declaration($/) {
our $?BLOCK;
Modified: trunk/languages/ecmascript/src/parser/grammar.pg
==============================================================================
--- trunk/languages/ecmascript/src/parser/grammar.pg (original)
+++ trunk/languages/ecmascript/src/parser/grammar.pg Sat Jan 3 00:03:47 2009
@@ -121,7 +121,7 @@
}
rule for4_statement {
- 'for' '(' 'var' <variable_declaration_list>
+ 'for' '(' 'var' $<init>=<variable_declaration_list>
';' [$<cond>=<expression>]?
';' [$<step>=<expression>]? ')'
<statement>
@@ -169,7 +169,7 @@
}
rule variable_statement {
- 'var' <variable_declaration> [',' <variable_declaration>]* ';'
+ 'var' <variable_declaration_list> ';'
{*}
}