6 rule statement
{ <key
>\h
*\
=\h
*<value
>\n* }
7 rule config
{ [\n*<statement
>]* }
16 my $config = $data ~~ /<config>/;
17 say match_describe
( $config,0);
19 for $config<config
><statement
> -> $o {
20 say "$o<key> == $o<value> == {evaluate("$o<value
>")}";
24 sub match_describe
(Match
$o, Num
$indent) {
27 $desc ~= "[\n" ~ join("" , map { match_describe
($_, $indent + 1) }, @
$o ) ~ "{"\t" x $indent}],";
29 elsif %$o.keys.elems
{
30 $desc ~= "{"\t" x $indent}\{\n";
33 $desc ~= "{"\t" x ($indent+1)}'$_' := { match_describe($o.{$_},$indent + 1)}\n";
35 $desc ~= "{"\t" x $indent}\},\n";
43 # RPN calc stolen from examples/rpn/p6/RPN.pm
45 sub evaluate
(Str
$expr) returns Int
{
47 for $expr.split() -> $tok {
48 if $tok ~~ /\-? \d+/ {
52 my $x = @stack.pop() orelse
die "Stack underflow\n";
53 my $y = @stack.pop() orelse
die "Stack underflow\n";
55 # given/when is a sexy new P6 construct that can avoid
56 # long if/elsif/else chains
58 when '+' { @stack.push($y + $x) }
59 when '-' { @stack.push($y - $x) }
60 when '*' { @stack.push($y * $x) }
61 when '/' { @stack.push(int($y / $x)) }
62 default { die "Invalid token:\"$tok\"\n" }
66 @stack.elems
== 1 or die "Invalid stack:[@stack[]]\n";