Using the Python Interpreter as a Reference

Dave Angel d at davea.name
Tue Nov 29 03:53:16 EST 2011


On 11/29/2011 03:12 AM, Steven D'Aprano wrote:
> On Tue, 29 Nov 2011 13:57:32 +1100, Chris Angelico wrote:
>
>> I'm inclined toward an alternative: explicit recursion. Either a
>> different syntax, or a special-case on the use of the function's own
>> name, but whichever syntax you use, it compiles in a "recurse" opcode.
>> That way, if name bindings change, it's still going to recurse -
>> something few languages guarantee, and therefore few languages can
>> optimize.
> As I recall, Forth uses (or used) a special RECURSE word which turned on
> the recursion bit while compiling, so that the compiled word could see
> itself.
>
> By memory, the (incomplete) definition:
>
> : fact dup 1- fact * ;
>
> would fail, unless you happened to already have another word called fact
> existing at compilation time. To make it recurse correctly, the compiler
> needs to make sure that the namespace fact sees includes itself:
>
> RECURSE : fact dup 1- fact * ;
>
> which should work, apart from the embarrassing fact that I don't recall
> the syntax for conditional jumps and so the recursion never terminates.
>
> :)
>
The way I remember it, the current definition was "smudged" which made 
it invisible (it basically changed the name to something unlikely) 
during the compilation.  After all, if you actually ran it at compile 
time (which was frequently done), you could fall right into 
uninitialized space.  Anyway, some implementations had an immediate 
SMUDGE word, which toggled the smudge bit and made it visible again.

Other implementations had an immediate word RECURSE, which compiled in 
whatever word was being currently defined.
I'm pretty sure neither FIG nor Forth79 had either of these.  But I 
don't recall the ANSI standard (X3J14 ?), even though I was officially 
an observer.  I can't even remember what happened to my printed copy of 
the standard.

The easiest word for conditional is IF/ELSE/THEN.   IF will skip to the 
ELSE or THEN if the condition is false.  So something resembling:

: fact dup 1- dup 0<> if recurse * then ;

might do it.  That's very rough, however.  It's been a long time.

-- 

DaveA




More information about the Python-list mailing list