Comments on: Recursion Safe PARSE Rules
Ladislav has created a useful function, an idiom, for recursive (and someday threaded) PARSE rules.
This function is useful because PARSE rules often store data in variables which, depending on how you handle them, can be overwritten by rule recursion. (Which is done often in parsing.)
Ladislav points out an example rule like this:
"b" (print 1) |
a: "a" rule "a" b: (print subtract index? b index? a)
Here, because the a: is set before the re-entry into rule, it will be overwritten.
Ladislav's solution is called use-rules (as in the use function for rules). It is posted on REBOL.org at use-rule function. It includes a simple example.
A handy idiom.
Ladislav pointed out that it could be more efficient, and more elegant, but it will definitely be useful. It's worth considering for standard inclusion IMO.
I would still love to see RT, or some REBOL gurus do some sample toy language implementations that show off parse and act as primers.
http://www.rebol.org/view-script.r?script=use-rule.r updated. Changes: now it implements both USE-RULE variants as described in http://www.rebol.net/wiki/Parse_Project .|
Regarding the "toy language" note. How about the http://www.fm.tul.cz/~ladislav/rebol/evaluate.r ?|
The idea looks a bit like what we did with proper tail recursion some years ago, using 'use as a a closure to create a "hidden stack" frame. Nice use case.|
Post a Comment:
You can post a comment here. Keep it on-topic.