Last active
August 30, 2016 13:37
-
-
Save bduggan/59e60a5710782e0cb613ecef97a093e7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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