Skip to content

Instantly share code, notes, and snippets.

@bduggan
Last active August 30, 2016 13:37
Show Gist options
  • Save bduggan/59e60a5710782e0cb613ecef97a093e7 to your computer and use it in GitHub Desktop.
Save bduggan/59e60a5710782e0cb613ecef97a093e7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl6
grammar divspl {
rule TOP { <x=int>..<y=int> <assignment>+ }
rule assignment { <word> '=' <mod=int> }
token word { \w+ }
token int { \d+ }
}
# Custom operator:
# "fizz" 〜 2 == "fizz"
# 2 〜 "buzz" == "buzz"
# "fizz" 〜 "buzz" == "fizzbuzz"
sub infix:<〜>($x,$y) {
return $x if $x and $y ~~ /\d/;
return $y if $y and $x ~~ /\d/;
return $x ~ $y;
}
class actions {
method TOP($/) {
# Filter lists, and merge using 〜
$/.make: [Z〜] $<assignment>».made().map:
{ (+$<x>..+$<y>).map: $^filter }
}
method assignment($/) {
# Turn a multiple of $<mod> into $<word>
$/.make: { $<word> x ( $^arg %% $<mod> ) or $^arg }
}
}
sub MAIN($filename) {
my $parsed = divspl.parse($filename.IO.lines, :actions(actions))
or die "sorry, invalid divspl";
say $parsed.made.join("\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment