PDA

View Full Version : Recursive Forth


Mark Bresler
02-17-10, 01:14 PM
Hi,
I am developing a word that I think should call itself. I think I have seen a reference for how this can be done, but don't recall where. Can you point me in the right direction?
Thanks,
Mark

RMDumse
02-17-10, 03:20 PM
Hi Mark, nice to see you post again. I delete multiple spam posts trying to clog up our bulletin board with graffiti like spam several times a day, so nice to see a real post come through.

Yes, a word calling itself is called recursion, and there are quite a few resources talking about it which turn up in a Google search.

It is rather a rare procedure in an embedded machine though. Reason being stack spaces are fairly limited, and packed in tight, in the small amount of RAM available, so you have to be very careful about limiting levels of recursion. But if that is considered, and there are strong reasons to use recursion it is possible.

The problem in doing it, relates to how a definition is laid in memory, and unsmudged, therefore unreferencable by name, during its own construction. Typically this is by passed by creating a word like "MYSELF" and substituting it in for the name of the routine under construction.

I don't remember the details of MYSELF off the top of my head, and need to research it a bit, but if you don't mind a response in generatl terms right now to get you started, this is what I remember.

I believe MYSELF is an immediate word that compiles a reference to the word under construction. As I recall it, it goes to LATEST to find the word under construction and comma,s it into line. MYSELF must be immediate so as not to compile the MYSELF definintion to run later, but to run immediately, and to compile the self-reference instead. There may be some details involved in converting the pointer in LATEST into the CFA of the word under construction, and that is what I need to research a bit further before giving a more definitive answer. I'll see what I can find.

Mark Bresler
02-17-10, 04:13 PM
Thanks for the insight. I think I have been able to break apart the tasks that need to be done such that I might not need it for this instance. It is always good to know in case it is needed.
Thanks again
Mark

RMDumse
02-18-10, 05:22 PM
The pervailing wisdom is, there is no problem that can be accomplished with recursion, that cannot be equally programmed well without using recursion. So using recursion is not strictly necessary. Because of the dangers of the stack thing mentioned, it is generally poo-hah'd. But as with all such issues, there are two divided camps on the subject.

Since you've already by passed the need, that's probably best.

I found some Forth versions have MYSELF as I suggested, and some have RECURSE (or the like) words.