I forgot to mention, some of the features of Match objects need the latest Rakudo development version, the release lags behind.
Is the string eval reasonable here?
Yes, though you can easily avoid it with a hash:
my %actions =
'*' => { $^a * $^b },
'/' => { $^a / $^b },
'+' => { $^a + $^b },
'' => { $^a  $^b };
...
make %actions{$<op>.ast}.($/<lhs rhs>)
Again, hash slices on Match objects likely require a new version of Rakudo built from source.
Can the expression rule be handled without the separate naming of the lhs and rhs? Is there a way to access each numeric in something like: rule expression { <numeric> <op> <numeric> }
First of all you can do the renaming inline:
rule expression { <lhs=.numeric> <op> <rhs=.numeric> }
I'd prefer this solution for clarity. However you can also use <numeric> twice in the same regex, in which case $<numeric> becomes and array. Then you can access the left and right side as $<numeric>[0] and $<numeric>[1].
rule expression { <lhs> ( <op> <rhs> )+ }
The corresponding action method might look like this:
method numeric($/) { make +$/ }
# ^ convert to a number
# propagate the number as the AST:
method lhs($/) { make $<numeric>.ast }
method rhs($/) { make $<numeric>.ast }
method expression($/) {
my $value = $<lhs>.ast;
# iterate over all matches of the first (...) group
for $0.list > $m {
$value = %actions{$m<op>.ast}.($value, $m<rhs>.ast);
}
make $value;
}
I hope this helps.
Update:
Here's a complete, working example that also passes the action down as an AST:
use v6;
grammar Calc {
token TOP { <expression> }
rule expression { <lhs=.numeric> ( <op> <rhs=.numeric> )* }
token numeric { \d+[\.\d*]? }
token op { ''  '+'  '*'  '/'  'x' }
}
my %actions =
'*' => { $^a * $^b },
'x' => { $^a * $^b },
'/' => { $^a / $^b },
'+' => { $^a + $^b },
'' => { $^a  $^b };
class Calc::Actions {
method TOP($/) { make $<expression>.ast }
method expression($/) {
my $value = $<lhs>.ast;
for $0.list > $m {
$value = $m<op>.ast.($value, $m<rhs>.ast);
}
make $value;
}
method numeric($/) { make +$/ }
method op($/) { make %actions{$/} }
}
my $m = Calc.parse( "8.8 x 5.0  2", :actions( Calc::Actions));
die "dying no match" unless $m;
say "$m<expression> = $m.ast()";
# vim: ft=perl6
Perl 6  links to (nearly) everything that is Perl 6.
