Skip to content

Instantly share code, notes, and snippets.

use v6;
my token param-x {
<[ab]>+ { say <matched word only> } ||
[ \w+ { say <after first word> } '=' \w+ ]
}
my token param-y {
[ \w+ {say 'after first word'} '=' {say <after equals> } \w+ ] ||
<[ab]>+
}
@ronaldxs
ronaldxs / match-token-backwards
Created March 27, 2018 17:58
P6 looks like it is incorrectly matching a token backwards for some reason
#use Grammar::Tracer;
grammar g {
token keywords { 'or' }
token BASEIDENT {\w+<!after <keywords>>}
token BASEIDENT_SIMPLER {\w+<!after 'or'>}
token TOP { <BASEIDENT> }
}
@ronaldxs
ronaldxs / missing-mutator.p6
Created March 24, 2018 17:37
Shouldn't there be an auto-generated mutator for is rw
use v6;
class C {
has $.a is rw = 3
}
my $o = C.new;
say $o.a;
$o.a = 4; # ok
say $o.a;
$o.a(5);
@ronaldxs
ronaldxs / missing-accessor.p6
Created March 24, 2018 17:33
Shouldn't there be an auto-generated accessor here
use v6;
class C { has $.a = 3;
multi method a(Int $i) {
$!a = $i * 2
}
}
my $o = C.new;
$o.a(4);
@ronaldxs
ronaldxs / ws-grammar-photo-shop.t
Last active March 20, 2018 18:22
White space oddness with grammars
#!perl6
use v6;
use Test;
plan 4;
# based on within clause from Grammar::Modelica
grammar TestWithSemi {
rule TOP {^<ps>$}
@ronaldxs
ronaldxs / lame-mutator.t
Created March 14, 2018 14:02
mutator test using lexical as backing store instead of attribute
use v6;
# this tests that you can define mutators, that do more interesting
# things than merely assigning the value!
# For this variant we do some fakery and use a lexical as backing
# store instead of an attribute.
use Test;
plan 10;
@ronaldxs
ronaldxs / proxy-doc-ex1-refer-self.p6
Last active March 13, 2018 11:27
Perl6 hangs on Proxy referring to self
class c {
sub double() is rw {
my $storage = 0;
Proxy.new(
FETCH => method () { say self.WHAT; $storage * 2 },
STORE => method ($new) { $storage = $new },
)
}
method m {
y = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
x=''
for (i = 0; i < 100000; i++) {
x += y
}
console.log(x.length)
# Perl 6 version that runs faster than Perl 5 equivalent
my int ($a, $one, $three, $limit) = (42, 1, 3, 10000000);
loop (my int $i = 0; $i < $limit; $i++) {
$a += $one + $a%$three
}
say $a
# Perl 5 equivalent
use MONKEY-SEE-NO-EVAL;
my $simple_arith_sub = q:to/END_SIMPLE_ARITH_SUB/;
sub {
my $r = 0;
for (1..10_000_000) {
$r += 1 + $r % 3
}
print $r, "\n";
}