Author: pmichaud
Date: Fri Mar 28 11:50:18 2008
New Revision: 26597
Modified:
trunk/compilers/nqp/src/Grammar.pg
trunk/compilers/nqp/src/Grammar/Actions.pir
trunk/compilers/nqp/t/14-op.t
Log:
[nqp]:
* Add some useful operators such as ++, --, <, <=, >, >=.
* Patch courtesy Klass-Jan Stol <[EMAIL PROTECTED]> (kjs++)
* (some Pm modifications to patch before applying)
Modified: trunk/compilers/nqp/src/Grammar.pg
==============================================================================
--- trunk/compilers/nqp/src/Grammar.pg (original)
+++ trunk/compilers/nqp/src/Grammar.pg Fri Mar 28 11:50:18 2008
@@ -345,7 +345,7 @@
}
-token typename {
+token typename {
<name> {*} #= name
}
@@ -375,8 +375,16 @@
is parsed(&term)
{ ... }
+## Postfix operators
+proto postfix:<++> is looser('term:')
+ is pasttype('inline')
+ { ... }
+proto postfix:<--> is equiv(postfix:<++>)
+ is pasttype('inline')
+ { ... }
+
## Unary prefix operators
-proto prefix:<+> is looser('term:')
+proto prefix:<+> is looser(postfix:<++>)
is pasttype('inline')
{ ... }
proto prefix:<~> is equiv(prefix:<+>)
@@ -424,6 +432,19 @@
proto infix:<!=> is equiv(infix:<==>)
is pasttype('inline')
{ ... }
+proto infix:«>=» is equiv(infix:<==>)
+ is pasttype('inline')
+ { ... }
+proto infix:«<=» is equiv(infix:<==>)
+ is pasttype('inline')
+ { ... }
+proto infix:«>» is equiv(infix:<==>)
+ is pasttype('inline')
+ { ... }
+proto infix:«<» is equiv(infix:<==>)
+ is pasttype('inline')
+ { ... }
+
proto infix:<eq> is equiv(infix:<==>)
is pasttype('inline')
{ ... }
Modified: trunk/compilers/nqp/src/Grammar/Actions.pir
==============================================================================
--- trunk/compilers/nqp/src/Grammar/Actions.pir (original)
+++ trunk/compilers/nqp/src/Grammar/Actions.pir Fri Mar 28 11:50:18 2008
@@ -25,6 +25,18 @@
%r = $N0
END
+ optable['postfix:++'; 'inline'] = <<" END"
+ ## inline postfix:++
+ clone %r, %0
+ inc %0
+ END
+
+ optable['postfix:--'; 'inline'] = <<" END"
+ ## inline postfix:--
+ clone %r, %0
+ dec %0
+ END
+
optable['infix:=='; 'inline'] = <<" END"
## inline infix:==
$I0 = cmp_num %0, %1
@@ -41,6 +53,38 @@
%r = $I0
END
+ optable['infix:<'; 'inline'] = <<" END"
+ ## inline infix:!=
+ $I0 = cmp_num %0, %1
+ $I0 = islt $I0, 0
+ %r = new 'Integer'
+ %r = $I0
+ END
+
+ optable['infix:<='; 'inline'] = <<" END"
+ ## inline infix:!=
+ $I0 = cmp_num %0, %1
+ $I0 = isle $I0, 0
+ %r = new 'Integer'
+ %r = $I0
+ END
+
+ optable['infix:>'; 'inline'] = <<" END"
+ ## inline infix:!=
+ $I0 = cmp_num %0, %1
+ $I0 = isgt $I0, 0
+ %r = new 'Integer'
+ %r = $I0
+ END
+
+ optable['infix:>='; 'inline'] = <<" END"
+ ## inline infix:!=
+ $I0 = cmp_num %0, %1
+ $I0 = isge $I0, 0
+ %r = new 'Integer'
+ %r = $I0
+ END
+
optable['infix:eq'; 'inline'] = <<" END"
## inline infix:eq
$S0 = %0
Modified: trunk/compilers/nqp/t/14-op.t
==============================================================================
--- trunk/compilers/nqp/t/14-op.t (original)
+++ trunk/compilers/nqp/t/14-op.t Fri Mar 28 11:50:18 2008
@@ -2,7 +2,7 @@
# checking basic operands and circumfix:( )
-plan(17);
+plan(29);
##Additive operators
ok( 1+2 == 3, 'Checking addition 1+2');
@@ -28,3 +28,21 @@
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) );
+