Skip to content

Commit

Permalink
Move operator related trait_mods to much later in the setting
Browse files Browse the repository at this point in the history
So that they can be made much more like regular Raku code
  • Loading branch information
lizmat committed Aug 24, 2023
1 parent 50a4f69 commit 3937c86
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 51 deletions.
44 changes: 44 additions & 0 deletions src/core.c/Routine.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,48 @@ my class Routine { # declared in BOOTSTRAP
}
}

multi sub trait_mod:<is>(Routine:D $r, :&equiv! --> Nil) {
$r.equiv(&equiv)
}
multi sub trait_mod:<is>(Routine:D $r, Str:D :$equiv! --> Nil) {
nqp::isgt_i((my int $i = nqp::index($r.name,':')),0)
?? $r.equiv(::(
'&' ~ nqp::substr($r.name,0,$i+1) ~ '<' ~ nqp::escape($equiv) ~ '>'
))
!! (die "Routine given to 'is equiv' does not appear to be an operator")
}

multi sub trait_mod:<is>(Routine:D $r, :&tighter! --> Nil) {
$r.tighter(&tighter)
}
multi sub trait_mod:<is>(Routine:D $r, Str:D :$tighter!) {
nqp::isgt_i((my int $i = nqp::index($r.name,':')),0)
?? $r.tighter(::(
'&' ~ nqp::substr($r.name,0,$i+1) ~ '<' ~ nqp::escape($tighter) ~ '>'
))
!! (die "Routine given to 'is tighter' does not appear to be an operator")
}

multi sub trait_mod:<is>(Routine:D $r, :&looser! --> Nil) {
$r.looser(&looser)
}
multi sub trait_mod:<is>(Routine:D $r, Str:D :$looser!) {
nqp::isgt_i((my int $i = nqp::index($r.name,':')),0)
?? $r.looser(::(
'&' ~ nqp::substr($r.name,0,$i+1) ~ '<' ~ nqp::escape($looser) ~ '>'
))
!! (die "Routine given to 'is looser' does not appear to be an operator")
}

multi sub trait_mod:<is>(Routine:D $r, :$assoc! --> Nil) { # --> Nil
$r.assoc($assoc)
}

# old interface, should probably be marked DEPRECATED
multi sub trait_mod:<is>(Routine:D $r, :%prec! --> Nil) {
nqp::bindattr($r,Routine,'$!op_props',
OperatorProperties.new-compat(|%prec)
)
}

# vim: expandtab shiftwidth=4
26 changes: 0 additions & 26 deletions src/core.c/operators.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -430,32 +430,6 @@ multi sub infix:<orelse>(+$) {
Nil) # We were given no args, return Nil
}

# next three sub would belong to traits.pm6 if PseudoStash were available
# so early in the setting compunit
multi sub trait_mod:<is>(Routine $r, str :$equiv! --> Nil) {
nqp::isgt_i((my int $i = nqp::index($r.name,':')),0)
?? $r.equiv(::(
'&' ~ nqp::substr($r.name,0,$i+1) ~ '<' ~ nqp::escape($equiv) ~ '>'
))
!! (die "Routine given to 'is equiv' does not appear to be an operator")
}

multi sub trait_mod:<is>(Routine $r, Str :$tighter!) {
nqp::isgt_i((my int $i = nqp::index($r.name,':')),0)
?? $r.tighter(::(
'&' ~ nqp::substr($r.name,0,$i+1) ~ '<' ~ nqp::escape($tighter) ~ '>'
))
!! (die "Routine given to 'is tighter' does not appear to be an operator")
}

multi sub trait_mod:<is>(Routine $r, Str :$looser!) {
nqp::isgt_i((my int $i = nqp::index($r.name,':')),0)
?? $r.looser(::(
'&' ~ nqp::substr($r.name,0,$i+1) ~ '<' ~ nqp::escape($looser) ~ '>'
))
!! (die "Routine given to 'is looser' does not appear to be an operator")
}

proto sub infix:<o> (&?, &?, *%) {*}
multi sub infix:<o> () { -> \v { v } }
multi sub infix:<o> (&f) { &f }
Expand Down
25 changes: 0 additions & 25 deletions src/core.c/traits.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -200,31 +200,6 @@ multi sub trait_mod:<is>(Routine:D $r, :onlystar($)!) {
$r.set_onlystar();
}

# old interface, should probably be marked DEPRECATED
multi sub trait_mod:<is>(Routine:D $r, :%prec!) { # --> Nil
nqp::bindattr($r,Routine,'$!op_props',
OperatorProperties.new-compat(|%prec)
);
Nil
}
# three other trait_mod sub for equiv/tighter/looser in operators.pm6
multi sub trait_mod:<is>(Routine:D $r, :&equiv!) { # --> Nil
$r.equiv(&equiv);
Nil
}
multi sub trait_mod:<is>(Routine:D $r, :&tighter!) { # --> Nil
$r.tighter(&tighter);
Nil
}
multi sub trait_mod:<is>(Routine:D $r, :&looser!) { # --> Nil
$r.looser(&looser);
Nil
}
multi sub trait_mod:<is>(Routine:D $r, :$assoc!) { # --> Nil
$r.assoc($assoc);
Nil
}

# Since trait_mod:<is> to set onlystar isn't there at the
# point we wrote its proto, we do it manually here.
BEGIN &trait_mod:<is>.set_onlystar();
Expand Down

0 comments on commit 3937c86

Please sign in to comment.