Tuesday, August 15, 2006

Formal stack effect declarations

Factor has had a stack effect inference feature for a long time now, since the JFactor days. Previously I used a rather elaborate algorithm to infer the stack effects of recursive words. In the last few days I found some cases where the algorithm would fail. Rather than fix it and make the compiler even more complicated, I've decided to take a different approach.

After all, why make the computer work really hard to figure out what the user wrote in their stack effect comments, when instead the compiler can just read them? ( ... ) is formal syntax now, and may only follow a word definition:
: foo ( a b -- c ) 2 + * ;

If a word is not recursive, the stack effect declaration is optional. If the inferred stack effect does not match the declared stack effect, the compiler issues an error. On the other hand, recursive words now require a stack effect declaration in order to compile. Stack effects of arbitrary quotations can still be inferred as before.

Note that because of how delegation is implemented, all generic words are recursive internally, and need a stack effect declaration in order to compile. And finally, if you add a stack effect declaration to a word that does not have a static stack effect, for whatever reason, the declaration is ignored.

2 comments:

Anonymous said...

Oh my god, pretty soon you'll be adding optional static type declarations! What's the world coming to?

Slava Pestov said...

There's no reason to rule out that possibility.