Skip to content

Commit

Permalink
Fixed up while and not-quite-R for, tightened rep()
Browse files Browse the repository at this point in the history
  • Loading branch information
jayemerson committed Jul 17, 2011
1 parent 24c2284 commit 15160ce
Show file tree
Hide file tree
Showing 6 changed files with 252 additions and 79 deletions.
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
0.0.1, July 15, 2011.
0.0.2, July 16, 2011.
51 changes: 29 additions & 22 deletions src/nqr/Actions.pm
Expand Up @@ -81,6 +81,7 @@ method function_definition($/) {
make $past;
}

# This might need to be broken up for assignment to subsets.
method statement:sym<assignment>($/) {
my $lhs := $<primary>.ast;
my $rhs := $<EXPR>.ast;
Expand Down Expand Up @@ -171,11 +172,8 @@ method for_init($/) {
$iter.isdecl(1);
$iter.scope('lexical');
## the identifier is initialized with this expression
$iter.viviself( $<EXPR>.ast );
## JAY: initialize at 0.
#$iter.viviself(0);

#my $set := $<EXPR>.ast;
$iter.viviself( $<forint>.ast ); # was $<EXPR>.ast

## enter the loop variable into the symbol table.
$?BLOCK.symbol($iter.name(), :scope('lexical'));
Expand All @@ -194,42 +192,43 @@ method statement:sym<for>($/) {
:scope('lexical'), # Was lexical
:node($/) );

## the body of the loop consists of the statements written by the user and
## the increment instruction of the loop iterator.
## the body of the loop consists of the statements written
## by the user and the increment instruction of the loop iterator.

my $body := @?BLOCK.shift();
$?BLOCK := @?BLOCK[0];
for $<statement> {
$body.push($_.ast);
}

#my $loop := PAST::Op.new( $iter, $body, :pasttype('for'), :node($/) );

#my $step;
#if $<step> {
# my $stepsize := $<step>[0].ast;
# $step := PAST::Op.new( $iter, $stepsize,
# :pirop('add__0P+'), :node($/) );
#}
#else { ## default is increment by 1
# $step := PAST::Op.new( $iter, :pirop('inc'), :node($/) );
#}
# NOTE: simplified, got rid of the more general step

my $step := PAST::Op.new( $iter, :pirop('inc'), :node($/) );
$body.push($step);

# while loop iterator <= end-expression
my $cond := PAST::Op.new( :pirop<isle__IPP>,
$iter,
$<EXPR>.ast );
my $loop := PAST::Op.new( $cond, $body, :pasttype('while'), :node($/) );
$<forint>.ast ); # was $<EXPR>.ast
my $loop := PAST::Op.new( $cond, $body, :pasttype('while'),
:node($/) );

make PAST::Stmts.new( $init, $loop, :node($/) );
}

method forint($/) {
make PAST::Val.new(:value($<integer>.ast), :returns<Integer>);
}

############ end working on 'for' actions...

# NOTE: out action is R-consistent, but if the condition has
# length > 1 we would like to throw a warning.
# moritz++ on this one, extracting the literal from the PMC:
method statement:sym<if>($/) {
my $cond := $<EXPR>.ast;
my $cond := PAST::Op.new( :pirop<set__iQi>,
$<EXPR>.ast, 0 );
#my $cond := $<EXPR>.ast;
my $past := PAST::Op.new( $cond, $<then>.ast,
:pasttype('if'),
:node($/) );
Expand Down Expand Up @@ -259,8 +258,12 @@ method arguments($/) {
make $past;
}

# NOTE: out action is R-consistent, but if the condition has
# length > 1 we would like to throw a warning.
method statement:sym<while>($/) {
my $cond := $<EXPR>.ast;
my $cond := PAST::Op.new( :pirop<set__iQi>,
$<EXPR>.ast, 0 );
#my $cond := $<EXPR>.ast;
my $body := $<block>.ast;
make PAST::Op.new( $cond, $body, :pasttype('while'), :node($/) );
}
Expand Down Expand Up @@ -367,6 +370,10 @@ method term:sym<integer_constant>($/) {
#make PAST::Val.new(:value($<integer>.ast), :returns<Integer>);
}

method term:sym<forint>($/) {
make PAST::Val.new(:value($<integer>.ast), :returns<Integer>);
}

method term:sym<string_constant>($/) { make $<string_constant>.ast; }
method string_constant($/) {
my $past := $<quote>.ast;
Expand Down Expand Up @@ -415,7 +422,7 @@ method circumfix:sym<[ ]>($/) {
## create an array,
## using the "anonymous" sub !array
## (which is not a valid nqr name)
my $past := PAST::Op.new( :name('!array'),
my $past := PAST::Op.new( :name('!array'), # was !array
:pasttype('call'),
:node($/) );
for $<EXPR> {
Expand Down
15 changes: 9 additions & 6 deletions src/nqr/Grammar.pm
Expand Up @@ -80,14 +80,16 @@ rule statement:sym<myfor2> {
# JAY: minor modification of original syntax:

rule statement:sym<for> {
<sym> <for_init> <EXPR> ')'
<sym> <for_init> <forint> ')'
'{' <statement>* '}'
}

rule for_init {
'(' <identifier> 'from' <EXPR> 'to'
'(' <identifier> 'in' <forint> 'to'
}

rule forint { <integer> }

############################## end of 'for' attempts

rule statement:sym<if> {
Expand Down Expand Up @@ -225,10 +227,11 @@ token infix:sym<-> { <sym> <O('%additive')> }
# Not valid R syntax, but perhaps needed for tests?
#token infix:sym<..> { <sym> <O('%additive, :pirop<concat>')> }

token infix:sym«<» { <sym> <O('%relational, :pirop<islt iPP>')> }
token infix:sym«<=» { <sym> <O('%relational, :pirop<isle iPP>')> }
token infix:sym«>» { <sym> <O('%relational, :pirop<isgt iPP>')> }
token infix:sym«>=» { <sym> <O('%relational, :pirop<isge iPP>')> }
# Vectorized:
token infix:sym«<» { <sym> <O('%relational')> }
token infix:sym«<=» { <sym> <O('%relational')> }
token infix:sym«>» { <sym> <O('%relational')> }
token infix:sym«>=» { <sym> <O('%relational')> }

# Vectorized:
token infix:sym«==» { <sym> <O('%relational')> }
Expand Down

0 comments on commit 15160ce

Please sign in to comment.