Skip to content

Commit

Permalink
Introduce ^... and ^...^ sequence generator operators
Browse files Browse the repository at this point in the history
The ... and ...^ already existed, but ^... and ^...^ were curiously
missing.  Please note that you can also use these operators with an
array on the left hand side:

    my @A = 1,2,4;
    say @A ^... 64; # all even multiples in a quadratic sequence
    # (2 4 8 16 32 64)

In any case, it feels that they should exists, for consistency.
They are implemented as shims around infix:<...> .
  • Loading branch information
lizmat committed Apr 1, 2020
1 parent 4fe18a4 commit 371590f
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 17 deletions.
36 changes: 22 additions & 14 deletions src/Perl6/Actions.nqp
Expand Up @@ -421,10 +421,14 @@ sub unwanted($ast, $by) {
if $infix {
my $sym := $infix<sym>;
if $sym eq ',' || $sym eq 'xx' { unwantall($node, $byby) }
elsif $sym eq '...' ||
$sym eq '...^' ||
$sym eq '…' ||
$sym eq '…^'
elsif $sym eq '...'
|| $sym eq '...^'
|| $sym eq '^...'
|| $sym eq '^...^'
|| $sym eq '…'
|| $sym eq '…^'
|| $sym eq '^…'
|| $sym eq '^…^'
{
$node.annotate('useless', $sym);
$node.node.worry("Useless use of $sym in sink context");
Expand Down Expand Up @@ -9992,21 +9996,25 @@ class Perl6::Actions is HLL::Actions does STDActions {

my %curried;
INIT {
%curried{'&infix:<...>'} := 0;
%curried{'&infix:<…>'} := 0;
%curried{'&infix:<...^>'} := 0;
%curried{'&infix:<…^>'} := 0;
%curried{'&infix:<=>'} := 0;
%curried{'&infix:<:=>'} := 0;
%curried{'&infix:<~~>'} := 1;
%curried{'&infix:<∘>'} := 1;
%curried{'&infix:<o>'} := 1;
%curried{'&infix:<...>'} := 0;
%curried{'&infix:<…>'} := 0;
%curried{'&infix:<...^>'} := 0;
%curried{'&infix:<…^>'} := 0;
%curried{'&infix:<^...>'} := 0;
%curried{'&infix:<^…>'} := 0;
%curried{'&infix:<^...^>'} := 0;
%curried{'&infix:<^…^>'} := 0;
%curried{'&infix:<=>'} := 0;
%curried{'&infix:<:=>'} := 0;
%curried{'&infix:<~~>'} := 1;
%curried{'&infix:<∘>'} := 1;
%curried{'&infix:<o>'} := 1;
%curried{'&infix:<..>'} := 2;
%curried{'&infix:<..^>'} := 2;
%curried{'&infix:<^..>'} := 2;
%curried{'&infix:<^..^>'} := 2;
%curried{'&infix:<xx>'} := 2;
%curried{'callmethod'} := 3;
%curried{'callmethod'} := 3;
%curried{'p6callmethodhow'} := 3;
%curried{'&postcircumfix:<[ ]>'} := 3;
%curried{'&postcircumfix:<{ }>'} := 3;
Expand Down
4 changes: 4 additions & 0 deletions src/Perl6/Grammar.nqp
Expand Up @@ -4301,6 +4301,10 @@ grammar Perl6::Grammar is HLL::Grammar does STD {
token infix:sym<> { <sym> <O(|%list_infix)> }
token infix:sym<...^> { <sym> <O(|%list_infix)> }
token infix:sym<…^> { <sym> <O(|%list_infix)> }
token infix:sym<^...> { <sym> <O(|%list_infix)> }
token infix:sym<^…> { <sym> <O(|%list_infix)> }
token infix:sym<^...^> { <sym> <O(|%list_infix)> }
token infix:sym<^…^> { <sym> <O(|%list_infix)> }
# token term:sym<...> { <sym> <args>**0..1 <O(|%list_prefix)> }
token infix:sym<?> { <sym> {} <![?]> <?before <.-[;]>*?':'> <.obs('? and : for the ternary conditional operator', '?? and !!')> <O(|%conditional)> }
Expand Down
24 changes: 21 additions & 3 deletions src/core.c/operators.pm6
Expand Up @@ -167,17 +167,35 @@ multi sub infix:<...>(|lol) {
}
}

# U+2026 HORIZONTAL ELLIPSIS
my constant &infix:<> := &infix:<...>;

proto sub infix:<...^>($, Mu, *%) {*}
multi sub infix:<...^>(\a, Mu \b) {
Seq.new(SEQUENCE(a, b, :exclude_end))
}

# U+2026 HORIZONTAL ELLIPSIS
my constant &infix:<> := &infix:<...>;

# U+2026 HORIZONTAL ELLIPSIS, U+005E CIRCUMFLEX ACCENT
my constant &infix:<…^> := &infix:<...^>;

proto sub infix:<^...>(|) {*}
multi sub infix:<^...>(|c) {
Seq.new: Rakudo::Iterator.AllButFirst(infix:<...>(|c).iterator)
}

# U+005E CIRCUMFLEX ACCENT, U+2026 HORIZONTAL ELLIPSIS
my constant &infix:<^…> := &infix:<^...>;

proto sub infix:<^...^>(|) {*}
multi sub infix:<^...^>(|c) {
Seq.new: Rakudo::Iterator.AllButLast(
Rakudo::Iterator.AllButFirst(infix:<...>(|c).iterator)
)
}

# U+005E CIRCUMFLEX ACCENT, U+2026 HORIZONTAL ELLIPSIS, U+005E CIRCUMFLEX ACCENT
my constant &infix:<^…^> := &infix:<^...^>;

proto sub undefine(Mu, *%) is raw {*}
multi sub undefine(Mu \x) is raw { x = Nil }
multi sub undefine(Array \x) is raw { x = Empty }
Expand Down
2 changes: 2 additions & 0 deletions src/core.c/precedence.pm6
Expand Up @@ -160,6 +160,8 @@ BEGIN {
trait_mod:<is>(&infix:<X>, :prec($list_infix));
trait_mod:<is>(&infix:<...>, :prec($list_infix));
trait_mod:<is>(&infix:<...^>, :prec($list_infix));
trait_mod:<is>(&infix:<^...>, :prec($list_infix));
trait_mod:<is>(&infix:<^...^>, :prec($list_infix));
trait_mod:<is>(&infix:<minmax>, :prec($list_infix));

trait_mod:<is>(&infix:<=>, :prec($list_prefix));
Expand Down
4 changes: 4 additions & 0 deletions t/02-rakudo/03-corekeys-6c.t
Expand Up @@ -196,6 +196,8 @@ my @expected = (
Q{&infix:<->},
Q{&infix:<...>},
Q{&infix:<...^>},
Q{&infix:<^...>},
Q{&infix:<^...^>},
Q{&infix:<..>},
Q{&infix:<..^>},
Q{&infix:<//>},
Expand Down Expand Up @@ -257,6 +259,8 @@ my @expected = (
Q{&infix:<÷>},
Q{&infix:<…>},
Q{&infix:<…^>},
Q{&infix:<^…>},
Q{&infix:<^…^>},
Q{&infix:<∈>},
Q{&infix:<∉>},
Q{&infix:<∋>},
Expand Down
4 changes: 4 additions & 0 deletions t/02-rakudo/03-corekeys-6d.t
Expand Up @@ -196,6 +196,8 @@ my @expected = (
Q{&infix:<->},
Q{&infix:<...>},
Q{&infix:<...^>},
Q{&infix:<^...>},
Q{&infix:<^...^>},
Q{&infix:<..>},
Q{&infix:<..^>},
Q{&infix:<//>},
Expand Down Expand Up @@ -257,6 +259,8 @@ my @expected = (
Q{&infix:<÷>},
Q{&infix:<…>},
Q{&infix:<…^>},
Q{&infix:<^…>},
Q{&infix:<^…^>},
Q{&infix:<∈>},
Q{&infix:<∉>},
Q{&infix:<∋>},
Expand Down
4 changes: 4 additions & 0 deletions t/02-rakudo/03-corekeys-6e.t
Expand Up @@ -198,6 +198,8 @@ my @expected = (
Q{&infix:<->},
Q{&infix:<...>},
Q{&infix:<...^>},
Q{&infix:<^...>},
Q{&infix:<^...^>},
Q{&infix:<..>},
Q{&infix:<..^>},
Q{&infix:<//>},
Expand Down Expand Up @@ -259,6 +261,8 @@ my @expected = (
Q{&infix:<÷>},
Q{&infix:<…>},
Q{&infix:<…^>},
Q{&infix:<^…>},
Q{&infix:<^…^>},
Q{&infix:<∈>},
Q{&infix:<∉>},
Q{&infix:<∋>},
Expand Down
4 changes: 4 additions & 0 deletions t/02-rakudo/03-corekeys.t
Expand Up @@ -199,6 +199,8 @@ my @allowed =
Q{&infix:<->},
Q{&infix:<...>},
Q{&infix:<...^>},
Q{&infix:<^...>},
Q{&infix:<^...^>},
Q{&infix:<..>},
Q{&infix:<..^>},
Q{&infix:<//>},
Expand Down Expand Up @@ -260,6 +262,8 @@ my @allowed =
Q{&infix:<÷>},
Q{&infix:<…>},
Q{&infix:<…^>},
Q{&infix:<^…>},
Q{&infix:<^…^>},
Q{&infix:<∈>},
Q{&infix:<∉>},
Q{&infix:<∋>},
Expand Down
4 changes: 4 additions & 0 deletions t/02-rakudo/04-settingkeys-6c.t
Expand Up @@ -195,6 +195,8 @@ my %allowed = (
Q{&infix:<->},
Q{&infix:<...>},
Q{&infix:<...^>},
Q{&infix:<^...>},
Q{&infix:<^...^>},
Q{&infix:<..>},
Q{&infix:<..^>},
Q{&infix:<//>},
Expand Down Expand Up @@ -256,6 +258,8 @@ my %allowed = (
Q{&infix:<÷>},
Q{&infix:<…>},
Q{&infix:<…^>},
Q{&infix:<^…>},
Q{&infix:<^…^>},
Q{&infix:<∈>},
Q{&infix:<∉>},
Q{&infix:<∋>},
Expand Down
4 changes: 4 additions & 0 deletions t/02-rakudo/04-settingkeys-6e.t
Expand Up @@ -195,6 +195,8 @@ my %allowed = (
Q{&infix:<->},
Q{&infix:<...>},
Q{&infix:<...^>},
Q{&infix:<^...>},
Q{&infix:<^...^>},
Q{&infix:<..>},
Q{&infix:<..^>},
Q{&infix:<//>},
Expand Down Expand Up @@ -256,6 +258,8 @@ my %allowed = (
Q{&infix:<÷>},
Q{&infix:<…>},
Q{&infix:<…^>},
Q{&infix:<^…>},
Q{&infix:<^…^>},
Q{&infix:<∈>},
Q{&infix:<∉>},
Q{&infix:<∋>},
Expand Down

0 comments on commit 371590f

Please sign in to comment.