Skip to content
This repository has been archived by the owner on Feb 3, 2021. It is now read-only.

Commit

Permalink
[nqp]: More operators and tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
pmichaud committed Oct 26, 2009
1 parent 64e2c87 commit ccd1d5b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/NQP/Actions.pm
Expand Up @@ -227,3 +227,15 @@ NQP::Grammar.O(':prec<f=>, :assoc<list>', '%list_infix');
method nulltermish($/) {
make $<noun> ?? $<noun>.ast !! 0;
}

method postfix:sym<++>($/) {
make PAST::Op.new( :name('postfix:<++>'),
:inline(' clone %r, %0', ' inc %0'),
:pasttype('inline') );
}

method postfix:sym<-->($/) {
make PAST::Op.new( :name('postfix:<-->'),
:inline(' clone %r, %0', ' dec %0'),
:pasttype('inline') );
}
6 changes: 4 additions & 2 deletions src/NQP/Grammar.pm
Expand Up @@ -160,8 +160,10 @@ token postcircumfix:sym<[ ]> {
<O('%methodop')>
}

token prefix:sym<--> { <sym> <O('%autoincrement')> }
token postfix:sym<++> { <sym> <O('%autoincrement')> }
token prefix:sym<++> { <sym> <O('%autoincrement, :pirop<inc>')> }
token prefix:sym<--> { <sym> <O('%autoincrement, :pirop<dec>')> }
token postfix:sym<++> { <sym> <O('%autoincrement')> } # see Actions.pm
token postfix:sym<--> { <sym> <O('%autoincrement')> } # see Actions.pm

token infix:sym<**> { <sym> <O('%exponentiation, :pirop<pow>')> }

Expand Down
48 changes: 48 additions & 0 deletions t/nqp/13-op.t
@@ -0,0 +1,48 @@
#!./parrot nqp.pbc

# checking basic operands and circumfix:( )

plan(29);

##Additive operators
ok( 1+2 == 3, 'Checking addition 1+2');
ok( 10-9 == 1, 'Checking subtraction 10-9');
ok( 10-3+2 == 9, 'Checking compound statements 10-3+2');
ok( 10-(3+2) == 5, 'Checking parenthesized statement 10-(3+2)');

##Multiplicative operators
ok( 6*7 == 42, 'Checking multiplication 6*7');
ok( 36/6 == 6, 'Checking division 36/6');
ok( 4*3+5 == 17, 'Checking compound statements 4*3+5');
ok( 4*(3+5) == 32, 'Checking parenthesized statements 4*(3+5)');
ok( 12/4*3 == 9, 'Checking compound statements 12/4*3');
ok( 12/(4*3) == 1, 'Checking compound statements 12/(4*3)');
ok( 5-3*2 == -1, 'Checking compound statements 5-3*2');

##Modulo operator
ok( 8%3 == 2, 'Checking modulo 8%3');
ok( 8%3+2 == 4, 'Checking compound statement 8%3+2');
ok( 8%(3+2) == 3, 'Checking compound statement 8%(3+2)');

##Concatenation operator
ok( 'a' ~ 'b' eq 'ab', 'Checking concatenation "a" ~ "b"');
ok( 1 ~ 'b' eq '1b', 'Checking concatenation 1 ~ "b"');
ok( 'a' ~ 2 eq 'a2', 'Checking concatenation "a" ~ 2 ');

##Postfix operators
my $x := 0;
ok( $x++ == 0 );
ok( $x == 1 );
ok( $x-- == 1 );
ok( $x == 0 );

##Relational operators
ok( ?(1 < 2) );
ok( !(2 < 1) );
ok( ?(2 <= 2) );
ok( !(3 <= 2) );
ok( ?(2 > 1) );
ok( !(2 > 3) );
ok( ?(2 >= 1) );
ok( !(2 >= 3) );

0 comments on commit ccd1d5b

Please sign in to comment.