Hasty Briefsbeta

Bilingual

Implementing DOES> in Forth, the entire reason I started this mess

a year ago
  • #Forth
  • #Programming
  • #Compilers
  • The article discusses the implementation of `DOES>` in Forth, a word that modifies the behavior of newly created words.
  • `DOES>` has three temporal aspects: during compilation (time 1), when defining a new word (time 2), and when the new word is executed (time 3).
  • At time 1, `DOES>` compiles its runtime code into the current word and leaves a hook for future execution.
  • At time 2, `DOES>` modifies the execution token (xt) of the newly created word to include the code following `DOES>`.
  • At time 3, the modified word executes the code compiled by `DOES>`, combining the default action (pushing the body address) with the custom behavior.
  • The implementation involves a `JSR` instruction to a helper routine (`forth_core_create.does_hook`), which sets up the execution context for the custom code.
  • The article suggests that `DOES>` might be challenging to implement on modern systems due to restrictions on writable and executable memory.