6.11.1 Combined Words

Gforth allows you to define combined words – words that have an arbitrary combination of interpretation and compilation semantics.

interpret/compile: ( interp-xt comp-xt "name" –  ) gforth-0.2 “interpret/compile:”

This feature was introduced for implementing TO and S". I recommend that you do not define such words, as cute as they may be: they make it hard to get at both parts of the word in some contexts. E.g., assume you want to get an execution token for the compilation part. Instead, define two words, one that embodies the interpretation part, and one that embodies the compilation part. Once you have done that, you can define a combined word with interpret/compile: for the convenience of your users.

You might try to use this feature to provide an optimizing implementation of the default compilation semantics of a word. For example, by defining:

   foo bar ;
interpret/compile: opti-foobar

as an optimizing version of:

: foobar
    foo bar ;

Unfortunately, this does not work correctly with [compile], because [compile] assumes that the compilation semantics of all interpret/compile: words are non-default. I.e., [compile] opti-foobar would compile compilation semantics, whereas [compile] foobar would compile interpretation semantics.

Some people try to use state-smart words to emulate the feature provided by interpret/compile: (words are state-smart if they check STATE during execution). E.g., they would try to code foobar like this:

: foobar
  IF ( compilation state )
    foo bar
  ENDIF ; immediate

Although this works if foobar is only processed by the text interpreter, it does not work in other contexts (like ' or POSTPONE). E.g., ' foobar will produce an execution token for a state-smart word, not for the interpretation semantics of the original foobar; when you execute this execution token (directly with EXECUTE or indirectly through COMPILE,) in compile state, the result will not be what you expected (i.e., it will not perform foo bar). State-smart words are a bad idea. Simply don’t write them20!



For a more detailed discussion of this topic, see M. Anton Ertl, State-smartness—Why it is Evil and How to Exorcise it, EuroForth ’98.