Skip to content

Commit

Permalink
[dotnet] implement coercive lt, le, gt, ge; add tests to 10-cmp.t
Browse files Browse the repository at this point in the history
update LHF.txt ;)
  • Loading branch information
diakopter committed Nov 11, 2010
1 parent 0f23784 commit 0d13a6c
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 10 deletions.
16 changes: 16 additions & 0 deletions common/NQP/NQPSetting.pm
Expand Up @@ -224,6 +224,22 @@ sub &infix:<ne>($x, $y) {
!nqp::equal_strs($x.Str, $y.Str) !nqp::equal_strs($x.Str, $y.Str)
} }


sub &infix:<ge>($x, $y) {
nqp::greater_than_or_equal_strs($x.Str, $y.Str)
}

sub &infix:<gt>($x, $y) {
nqp::greater_than_strs($x.Str, $y.Str)
}

sub &infix:<le>($x, $y) {
nqp::less_than_or_equal_strs($x.Str, $y.Str)
}

sub &infix:<lt>($x, $y) {
nqp::less_than_strs($x.Str, $y.Str)
}

sub &infix:<=:=>($x, $y) { sub &infix:<=:=>($x, $y) {
nqp::equal_refs($x, $y) nqp::equal_refs($x, $y)
} }
Expand Down
12 changes: 3 additions & 9 deletions dotnet/LHF.txt
Expand Up @@ -8,13 +8,7 @@ on freenode.


String comparative operators String comparative operators
---------------------------- ----------------------------
DIFFICULTY: Easy-Intermediate DIFFICULTY: Intermediate-Impossible
SKILLS: NQP, C# SKILLS: Being jnthn
DETAILS: DETAILS:
At the moment, there is no support for the operators lt, le, gt and Add task items to this file.
ge. Two things are needed to make this happen. First, you'll need to
make some new entries in runtime/Runtime/Ops as a way to actually
provide the low-level comparative logic. See equal_strings for
example. Then you'll need to add some entries to NQPSetting.pm (it's
outside of the dotnet tree, in /common/NQP). Finally, see about
getting the relevant tests running from the nqp-rx test suite.
56 changes: 56 additions & 0 deletions dotnet/runtime/Runtime/Ops/Comparison.cs
Expand Up @@ -177,6 +177,62 @@ public static RakudoObject greater_than_or_equal_ints(ThreadContext TC, RakudoOb
return Ops.box_int(TC, return Ops.box_int(TC,
(Ops.unbox_int(TC, x) >= Ops.unbox_int(TC, y) ? 1 : 0), (Ops.unbox_int(TC, x) >= Ops.unbox_int(TC, y) ? 1 : 0),
TC.DefaultBoolBoxType); TC.DefaultBoolBoxType);
}

/// <summary>
/// Compares two strings for greater-than inequality.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="ResultType"></param>
/// <returns></returns>
public static RakudoObject greater_than_strs(ThreadContext TC, RakudoObject x, RakudoObject y)
{
return Ops.box_int(TC,
String.Compare(Ops.unbox_str(TC, x), Ops.unbox_str(TC, y)) > 0 ? 1 : 0,
TC.DefaultBoolBoxType);
}

/// <summary>
/// Compares two strings for greater-than-or-equal inequality.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="ResultType"></param>
/// <returns></returns>
public static RakudoObject greater_than_or_equal_strs(ThreadContext TC, RakudoObject x, RakudoObject y)
{
return Ops.box_int(TC,
String.Compare(Ops.unbox_str(TC, x), Ops.unbox_str(TC, y)) >= 0 ? 1 : 0,
TC.DefaultBoolBoxType);
}

/// <summary>
/// Compares two strings for less-than inequality.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="ResultType"></param>
/// <returns></returns>
public static RakudoObject less_than_strs(ThreadContext TC, RakudoObject x, RakudoObject y)
{
return Ops.box_int(TC,
String.Compare(Ops.unbox_str(TC, x), Ops.unbox_str(TC, y)) < 0 ? 1 : 0,
TC.DefaultBoolBoxType);
}

/// <summary>
/// Compares two strings for less-than-or-equal inequality.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="ResultType"></param>
/// <returns></returns>
public static RakudoObject less_than_or_equal_strs(ThreadContext TC, RakudoObject x, RakudoObject y)
{
return Ops.box_int(TC,
String.Compare(Ops.unbox_str(TC, x), Ops.unbox_str(TC, y)) <= 0 ? 1 : 0,
TC.DefaultBoolBoxType);
} }
} }
} }
32 changes: 31 additions & 1 deletion t/nqp/10-cmp.t
Expand Up @@ -2,7 +2,7 @@


# check comparisons # check comparisons


say('1..19'); say('1..25');


##Integers, positive and negative ##Integers, positive and negative


Expand Down Expand Up @@ -96,3 +96,33 @@ if $x =:= $z {
print("not "); print("not ");
} }
say("ok 19 # container equality, string value"); say("ok 19 # container equality, string value");

if "x" gt "y" {
print("not ");
}
say("ok 20 # string greater-than inequality");

unless "y" gt "x" {
print("not ");
}
say("ok 21 # string less-than inequality");

unless "y" ge "y" {
print("not ");
}
say("ok 22 # string greater-than-or-equal inequality");

unless "y" ge "xx" {
print("not ");
}
say("ok 23 # string greater-than-or-equal inequality");

if "y" le "xx" {
print("not ");
}
say("ok 24 # string less-than-or-equal inequality");

unless "y" le "y" {
print("not ");
}
say("ok 25 # string less-than-or-equal inequality");

0 comments on commit 0d13a6c

Please sign in to comment.