From ee58ecd64cae77dc1eef4b4b569cf4c61da95a34 Mon Sep 17 00:00:00 2001 From: diakopter Date: Sun, 31 Oct 2010 15:49:20 -0700 Subject: [PATCH] implemented (copypaste) >,>=,<,<= for NQPInts & NQPNums --- common/NQP/NQPSetting.pm | 32 ++++++++++ dotnet/runtime/Runtime/Ops.cs | 116 +++++++++++++++++++++++++++++++++- t/nqp/13-op.t | 45 +++++++++++++ 3 files changed, 191 insertions(+), 2 deletions(-) create mode 100644 t/nqp/13-op.t diff --git a/common/NQP/NQPSetting.pm b/common/NQP/NQPSetting.pm index 75c36db..5e96083 100644 --- a/common/NQP/NQPSetting.pm +++ b/common/NQP/NQPSetting.pm @@ -172,6 +172,38 @@ multi sub &infix:(NQPNum $x, NQPNum $y) { nqp::logical_not_int(nqp::equal_nums($x, $y)) } +proto sub &infix:«<=»($x, $y) { * } +multi sub &infix:«<=»(NQPInt $x, NQPInt $y) { + nqp::less_than_or_equal_ints($x, $y) +} +multi sub &infix:«<=»(NQPNum $x, NQPNum $y) { + nqp::less_than_or_equal_nums($x, $y) +} + +proto sub &infix:«<»($x, $y) { * } +multi sub &infix:«<»(NQPInt $x, NQPInt $y) { + nqp::less_than_ints($x, $y) +} +multi sub &infix:«<»(NQPNum $x, NQPNum $y) { + nqp::less_than_nums($x, $y) +} + +proto sub &infix:«>=»($x, $y) { * } +multi sub &infix:«>=»(NQPInt $x, NQPInt $y) { + nqp::greater_than_or_equal_ints($x, $y) +} +multi sub &infix:«>=»(NQPNum $x, NQPNum $y) { + nqp::greater_than_or_equal_nums($x, $y) +} + +proto sub &infix:«>»($x, $y) { * } +multi sub &infix:«>»(NQPInt $x, NQPInt $y) { + nqp::greater_than_ints($x, $y) +} +multi sub &infix:«>»(NQPNum $x, NQPNum $y) { + nqp::greater_than_nums($x, $y) +} + sub &infix:($x, $y) { nqp::equal_strs($x.Str, $y.Str) } diff --git a/dotnet/runtime/Runtime/Ops.cs b/dotnet/runtime/Runtime/Ops.cs index 14288b0..8213ebc 100644 --- a/dotnet/runtime/Runtime/Ops.cs +++ b/dotnet/runtime/Runtime/Ops.cs @@ -449,8 +449,120 @@ public static RakudoObject equal_strs(ThreadContext TC, RakudoObject x, RakudoOb /// public static RakudoObject equal_refs(ThreadContext TC, RakudoObject x, RakudoObject y) { - return Ops.box_int(TC, x == y ? 1 : 0, TC.DefaultBoolBoxType); - } + return Ops.box_int(TC, x == y ? 1 : 0, TC.DefaultBoolBoxType); + } + + /// + /// Compares two floating point numbers for less-than inequality. + /// + /// + /// + /// + /// + public static RakudoObject less_than_nums(ThreadContext TC, RakudoObject x, RakudoObject y) + { + return Ops.box_int(TC, + (Ops.unbox_num(TC, x) < Ops.unbox_num(TC, y) ? 1 : 0), + TC.DefaultBoolBoxType); + } + + /// + /// Compares two integers for less-than inequality. + /// + /// + /// + /// + /// + public static RakudoObject less_than_ints(ThreadContext TC, RakudoObject x, RakudoObject y) + { + return Ops.box_int(TC, + (Ops.unbox_int(TC, x) < Ops.unbox_int(TC, y) ? 1 : 0), + TC.DefaultBoolBoxType); + } + + /// + /// Compares two floating point numbers for less-than-or-equal inequality. + /// + /// + /// + /// + /// + public static RakudoObject less_than_or_equal_nums(ThreadContext TC, RakudoObject x, RakudoObject y) + { + return Ops.box_int(TC, + (Ops.unbox_num(TC, x) <= Ops.unbox_num(TC, y) ? 1 : 0), + TC.DefaultBoolBoxType); + } + + /// + /// Compares two integers for less-than-or-equal inequality. + /// + /// + /// + /// + /// + public static RakudoObject less_than_or_equal_ints(ThreadContext TC, RakudoObject x, RakudoObject y) + { + return Ops.box_int(TC, + (Ops.unbox_int(TC, x) <= Ops.unbox_int(TC, y) ? 1 : 0), + TC.DefaultBoolBoxType); + } + + /// + /// Compares two floating point numbers for greater-than inequality. + /// + /// + /// + /// + /// + public static RakudoObject greater_than_nums(ThreadContext TC, RakudoObject x, RakudoObject y) + { + return Ops.box_int(TC, + (Ops.unbox_num(TC, x) > Ops.unbox_num(TC, y) ? 1 : 0), + TC.DefaultBoolBoxType); + } + + /// + /// Compares two integers for greater-than inequality. + /// + /// + /// + /// + /// + public static RakudoObject greater_than_ints(ThreadContext TC, RakudoObject x, RakudoObject y) + { + return Ops.box_int(TC, + (Ops.unbox_int(TC, x) > Ops.unbox_int(TC, y) ? 1 : 0), + TC.DefaultBoolBoxType); + } + + /// + /// Compares two floating point numbers for greater-than-or-equal inequality. + /// + /// + /// + /// + /// + public static RakudoObject greater_than_or_equal_nums(ThreadContext TC, RakudoObject x, RakudoObject y) + { + return Ops.box_int(TC, + (Ops.unbox_num(TC, x) >= Ops.unbox_num(TC, y) ? 1 : 0), + TC.DefaultBoolBoxType); + } + + /// + /// Compares two integers for greater-than-or-equal inequality. + /// + /// + /// + /// + /// + public static RakudoObject greater_than_or_equal_ints(ThreadContext TC, RakudoObject x, RakudoObject y) + { + return Ops.box_int(TC, + (Ops.unbox_int(TC, x) >= Ops.unbox_int(TC, y) ? 1 : 0), + TC.DefaultBoolBoxType); + } /// /// Logical not. diff --git a/t/nqp/13-op.t b/t/nqp/13-op.t new file mode 100644 index 0000000..87360b4 --- /dev/null +++ b/t/nqp/13-op.t @@ -0,0 +1,45 @@ +#!./parrot nqp.pbc + +# checking basic operands and circumfix:( ) + +plan(8); + +###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 '); + +##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) ); + +##Bitwise operators +#ok( (1 +| 3) == 3, 'Checking 1 +| 3' ); +#ok( (3 +& 2) == 2, 'Checking 3 +& 2' ); +#ok( (3 +^ 3) == 0, 'Checking 3 +^ 3' );