So I've browsed over Brad Rodriguez's "Moving Forth" a few times in the past as I try to wrap my head around how Forth works on an internal level, and I'm back to working through it as I try to roll up some sample code for this project to evaluate my own design decisions. I'm currently looking at the part where he explains DOES> - he makes a fair production out of it, and it seems to me like it's maybe not as complicated as all that, but on the other hand I'm not 100% sure I've got it, either. Seems to me, if I'm following it all correctly, that all the DODOES routine really is is a way for a common "handler" routine invoked by a class of words to get the "return address" - in other words, the address of the parameter field for any specific word invoking that handler - pushed onto the parameter stack, no?
Because if that's all it is, this becomes fairly trivial. I'm using an STC model, since the CPU architecture is designed around that, and based on Rodriguez's example, the DODOES subroutine could be implemented like so:
Code: Select all
CTD ; Pop top-of-return-stack to the parameter stack
; ( -- dodoes-return )
CTD ; Pop next item from return stack to parameter stack
; ( -- dodoes-return handler-return )
SWN ; Swap parameter-stack top and next items
; ( -- handler-return dodoes-return )
JMP ; Jump indirect to @TOS (return from DODOES)
; ( -- handler-return )