Luke Palmer wrote: > I know you heavyweights are working out how to specify the return value > from a rule or a capture.
Larry says if the hypothetical variable $0 is assigned to, that assigned value becomes the (only) return value of the rule. It's an elegant solution to the problem. > > Now, could you just do > > > > rule leftop ($leftop, $op) { > > <$leftop> [$op <$leftop>]* > > } > > > > rule leftop ($leftop, $op, $rightop) { > > <$leftop> [$op <$rightop>]* > > } > > I should hope that rules can take multiple arguments. They will. Though I'd be inclined to write C<leftop> like this: rule leftop ($leftop, $op, $rightop //= $leftop) { $0 := <$leftop> # build 1st node [ <$op> # operator <$rightop> # right operand { let $0 := {left=>$0, op=>$op, right=>$rightop} } # build next node ]* # repeat } This elegantly constructs and returns a tree structure which encodes the associativity of the left operator as well as its operands. Writing C<rightop> is left as an exercise for the reader (sample solution encrypted at the end of this message ;-) > Here's something that made me wonder, though: the <$leftop> there is meant to > call a rule. But, that syntax -- <$whatever> -- is how you embed a regex in > another regex. So how do we know that $leftop is not a regex wanting to match > "atom"? How do we force it to be a rule call? Is it like <$leftop:>? A rule *is* a regex. There is no distinction. The only issue is whether you happen to be passing arguments into the sub-rule/sub-regex. > Can you do this? > > rule leftop ($leftop, $op) { > @rands := [ (<$leftop:>) [ <$op> (<$leftop:>) ] ] { > return \@rands; > } > } No. You do this: rule leftop ($leftop, $op) { $0 := [ (<$leftop>) [<$op> (<$leftop>)]* ] } > Could you do the same for captures? > > / ( @foo := [ (<atom> and) ] { return \@foo } ) { > $1[1] # Second atom > } / Err, no. I'm sure there's a way to do whatever you're trying to do. Could you explain what that is? > I still thing P6 regexen are gorgeous :) Likewise :-) Damian -----cut----------cut----------cut----------cut----------cut----------cut----- C<rightop> solution (rot13'd): ehyr evtugbc ($yrsgbc, $bc, $evtugbc //= $yrsgbc) { [ <$yrsgbc> <$bc> ]* $0:=<$evtugbc> { juvyr @$yrsgbc { yrg $0 := {yrsg=>cbc $yrsgbc, bc=>cbc $bc, evtug=>$0} } }