I'm reordering this post rather than retype stuff. Forgive me. --- Uri Guttman <[EMAIL PROTECTED]> wrote:
> for the p6 regex impaired among us, please explain that. it might > make a nice tute for the docs. i get the general picture but i don't > follow how it works regarding the color checking. Of course, the color checking is the part that I messed up. See below. > >>>>> "AH" == Austin Hastings <[EMAIL PROTECTED]> writes: > Disclaimer: I'm *SO* clueless about this stuff... > AH> grammar Rainbow; > > AH> rule color {...}; # this one's on you. Yary posited a color class, so I accept that he can recognize them. I called it Colorific, just because. So my first mistake was probably failure-to-declare: grammar Rainbow; use Colorific; # Import C<rule color;> and C<new>, among others. What I don't know is how to recognize a color, which is to say I don't know how to write the <color> rule -- because I don't know what this is being applied to. Is this reading pixels, interpreting the results of radio telescopy, or consuming Lucky Charms breakfast cereal bits? I don't know, so I'm just going to assume that Yary can write that for me -- it's his class, after all. And the Colorific class supposedly has a way to determine if two colors look about like each other. Again, I don't know how that works, but I don't need to. > AH> rule same_color($color is Colorific) > AH> { > AH> <color> ::: { fail unless $1.looks_like($color); } > AH> } This is really probably bad code. Maybe a better rule would be: rule same_color($color is Colorific) { <color> ::: { fail unless $color.looks_like($1); } } I KNOW that $color is an object-of-type-Colorific, while I'm not sure, frankly, what <color> is returning. Let Colorific handle that. Also, please note that the only reason that C<same_color> is a separate rule is because I haven't learned, yet, how to do that in one amazing line of P6 code. I suspect that it could have been written all inside the C<band> rule, if I were smarter. > AH> rule band($color is Colorific) > AH> { > AH> <same_color($color)>+ > AH> } Here, I'm just saying that a 'band' of the rainbow is made up of at-least-one-maybe-more bit of a color that looks like $color. So, for a "color band" of, say, red, I'll take red and require that there be a bunch of stuff that looks like red, using the same_color rule (that, in turn, uses the Colorific::looks_like function, which someone else wrote). But that's the key: once I know how to recognize a band of color, I can look up my old ROY G. BIV mnemonic from Astronomy (or what that electronics? Too many dead brain cells:- I apologize if my rainbow is actually a RadioShack-bow.) So I need to declare what a Rainbow looks like. I'll use my band "shortcut" to specify the seven different colors. Note that rexen / rules are declarative, not imperative. The lines are each pattern-invocations, and there aren't any semicolons. If I want procedural, I need to open a sub-block to drop into "perl command mode", like I did in the C<same_color> rule, above. Since there's no alternation(|) or grouping or anything here, these declarations are straight-line: they all apply, one after the other. So a Rainbow is recognized when there's a band-of-red followed immediately by a band-of-orange followed immediately by a ... band-of-violet. Then, if you're lucky, there might be a pot-o-gold at the end. :-) rule pot_o_gold { $lucky := <leprechaun> # You *DO* know how to catch a Leprechaun, # don't you? { if (trick($lucky)) { print "Begorrah! I'm rich!"; } else { print "Always after me Lucky Charms ..."; } } } } > AH> rule Rainbow > AH> { > AH> <band(new Color("red"))> > AH> <band(new Color("orange"))> > AH> <band(new Color("yellow"))> > AH> <band(new Color("green"))> > AH> <band(new Color("blue"))> > AH> <band(new Color("indigo"))> > AH> <band(new Color("violet"))> > AH> <pot_o_gold>? > AH> } This really is bad code on my part. s/Color/Colorific/, please. rule Rainbow { <band(new Colorific("red"))> <band(new Colorific("orange"))> <band(new Colorific("yellow"))> <band(new Colorific("green"))> <band(new Colorific("blue"))> <band(new Colorific("indigo"))> <band(new Colorific("violet"))> <pot_o_gold>? } > > for the p6 regex impaired among us, please explain that. it might > make a > nice tute for the docs. i get the general picture but i don't follow > how > it works regarding the color checking.