Skip to content

Commit

Permalink
Move infix:<-> into the setting, making it overloadable as well.
Browse files Browse the repository at this point in the history
Now we just need to follow this general pattern for the remaining
builtins.
  • Loading branch information
pmichaud committed Aug 27, 2009
1 parent 3a274d9 commit 62f5110
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 41 deletions.
1 change: 0 additions & 1 deletion build/gen_junction_pir.pl
Expand Up @@ -8,7 +8,6 @@
my @binary = qw(
infix:**
infix:* infix:/ infix:% infix:div infix:mod
infix:-
infix:~
infix:== infix:!= infix:< infix:> infix:<= infix:>=
infix:eq infix:ne infix:lt infix:gt infix:le infix:ge
Expand Down
2 changes: 1 addition & 1 deletion build/gen_whatever_pir.pl
Expand Up @@ -6,7 +6,7 @@
use warnings;

my @ops = qw(
infix:** infix:* infix:/ infix:% infix:div infix:mod infix:-
infix:** infix:* infix:/ infix:% infix:div infix:mod
infix:== infix:!= infix:< infix:> infix:<= infix:>= infix:<=>
infix:.. infix:^.. infix:..^ infix:^..^
prefix:+ prefix:- prefix:~ prefix:? prefix:! prefix:^
Expand Down
16 changes: 0 additions & 16 deletions src/builtins/op.pir
Expand Up @@ -280,22 +280,6 @@ src/builtins/op.pir - Perl 6 builtin operators

## additive

.sub 'infix:-' :multi(_,_)
.param num a
.param num b
$N0 = a - b
.return ($N0)
.end


.sub 'infix:-' :multi(Integer,Integer)
.param num a
.param num b
$N0 = a - b
.tailcall '!upgrade_to_num_if_needed'($N0)
.end


.sub 'infix:~' :multi(_,_)
.param string a
.param string b
Expand Down
20 changes: 0 additions & 20 deletions src/classes/Complex.pir
Expand Up @@ -173,26 +173,6 @@ Casts a value to a complex number.
.return (a)
.end

=item infix:-

=cut

.sub 'infix:-' :multi('Complex', _)
.param pmc a
.param pmc b
b = b.'Complex'()
sub $P0, a, b
.return ($P0)
.end

.sub 'infix:-' :multi(_, 'Complex')
.param pmc a
.param pmc b
a = a.'Complex'()
sub $P0, a, b
.return ($P0)
.end

=item prefix:-

=cut
Expand Down
28 changes: 26 additions & 2 deletions src/setting/Complex.pm
@@ -1,22 +1,46 @@
multi sub infix:<+>(Complex $a, $b) is default {
Q:PIR {
$P0 = find_lex '$a'
$P0 = deobjectref $P0
$P1 = find_lex '$b'
$P1 = $P1.'Complex'()
$P0 = deobjectref $P0
$P1 = deobjectref $P1
%r = add $P0, $P1
}
}
multi sub infix:<+>($a, Complex $b is ref) {
multi sub infix:<+>($a, Complex $b) {
Q:PIR {
$P0 = find_lex '$a'
$P0 = $P0.'Complex'()
$P1 = find_lex '$b'
$P0 = deobjectref $P0
$P1 = deobjectref $P1
%r = add $P0, $P1
}
}
multi sub infix:<->(Complex $a, $b) is default {
Q:PIR {
$P0 = find_lex '$a'
$P1 = find_lex '$b'
$P1 = $P1.'Complex'()
$P0 = deobjectref $P0
$P1 = deobjectref $P1
%r = sub $P0, $P1
}
}
multi sub infix:<->($a, Complex $b) {
Q:PIR {
$P0 = find_lex '$a'
$P0 = $P0.'Complex'()
$P1 = find_lex '$b'
$P0 = deobjectref $P0
$P1 = deobjectref $P1
%r = sub $P0, $P1
}
}
# vim: ft=perl6
22 changes: 22 additions & 0 deletions src/setting/Operators.pm
Expand Up @@ -109,5 +109,27 @@ multi sub infix:<+>(Int $a, Int $b) {
}
}
multi sub infix:<->($a, $b) {
Q:PIR {
$P0 = find_lex '$a'
$N0 = $P0
$P1 = find_lex '$b'
$N1 = $P1
$N2 = $N0 - $N1
%r = box $N2
}
}
multi sub infix:<->(Int $a, Int $b) {
Q:PIR {
$P0 = find_lex '$a'
$N0 = $P0
$P1 = find_lex '$b'
$N1 = $P1
$N2 = $N0 - $N1
%r = '!upgrade_to_num_if_needed'($N2)
}
}
# vim: ft=perl6
12 changes: 11 additions & 1 deletion src/setting/Whatever.pm
Expand Up @@ -14,7 +14,8 @@ class Whatever is also {
# multi sub infix:<+>(Whatever $a, $b) {
# WhateverCode(-> $_ { $_ + $b })
# }
# but Rakudo doesn't support block coercion yet.
# but Rakudo doesn't support block coercion yet, so we have
# a helper function to build it for us.

multi sub infix:<+>(Whatever $a, $b) is default
{ WhateverCodeX('infix:+', $a, $b) }
Expand All @@ -25,4 +26,13 @@ multi sub infix:<+>($a, Whatever $b)
multi sub infix:<+>($a, Whatever $b)
{ WhateverCodeX('infix:+', $a, $b) }

multi sub infix:<->(Whatever $a, $b) is default
{ WhateverCodeX('infix:-', $a, $b) }
multi sub infix:<->(WhateverCode $a, $b) is default
{ WhateverCodeX('infix:-', $a, $b) }
multi sub infix:<->($a, Whatever $b)
{ WhateverCodeX('infix:-', $a, $b) }
multi sub infix:<->($a, Whatever $b)
{ WhateverCodeX('infix:-', $a, $b) }

# vim: ft=perl6

0 comments on commit 62f5110

Please sign in to comment.