MAE/6
M. Anton Ertl (anton@mips.complang.tuwien.ac.at)
A library is needed by several parts of the source code, but it should be loaded only once.
If the file specified by c-addr u has beenREQUIRED( i*x c-addr u -- j*x )INCLUDEDorREQUIREDalready, discard c-addr u; otherwise, performINCLUDED.If the same file is
REQUIREDtwice using different names (e.g., through symbolic links), or different files with the same name areREQUIRED(by doing some renaming between the invocations ofREQUIRED), the file will beINCLUDEDthe first time, but not necessarily several times.
It is advisable to make the stack-effect of interpreting the file (i*x -- i*x), such that the stack-effect is the same, whether the file iss" filename" requiredINCLUDEDor not.
The syntax follows the good example ofINCLUDEDin being non-parsing. A parsing version likeNEEDScan be easily defined from it with:Some might say: Why not use the C solution --- put a wrapper like: NEEDS BL WORD COUNT REQUIRED ;around every source file. This is inefficient (the whole file has to be read again, unless the compiler does some pretty sophisticated stuff), requires cooperation from the author of the file (which is problematic, because not the author, but the users of the file have the trouble), and is cumbersome to express in standard Forth (there is no direct equivalent to#ifndef FILE_H #define FILE_H ... #endif#ifdef).
Gforth hasREQUIRED. Many systems, including F-PC, Gforth, and Win32Forth, haveNEEDS, a parsing version ofREQUIRED. I use it extensively, and so do others.
- Michael L. Gassanenko:
- Yes. I do use NEEDS.
I often invent analogs to
#ifdef, namely, if a name is defined, interpret the following line. AFAIK, both F-PC and WIN32FOR have such words, but the only name I can remember is#ifdef/#ifndef(it seems, I myself used this name, because the others were not much meaningful).
- Peter Knaggs:
- Perl introduced a version of
REQUIREDsome time ago that works exactly in this manner. I agree very much with its usefulness, indeed it would allow a standard "library" model. Having said that, it can be defined using standard ANS.