-
Notifications
You must be signed in to change notification settings - Fork 7
The LAB library mechanism
Let us have a look at the words NIP and TUCK with stack effect ( a b -- b ) and ( a b -- b a b ). Those belong in the same category as SWAP and OVER. If some one presents you a Forth program, chances are that those words are used. They are ISO-standard, even if they are not in the CORE wordset. It is natural to have support for them. You can include them in the Forth kernel. That I don't like. Where is the end? An other solution is to put those two together in a small file niptuck.frt in a special directory. There is no end to this either, and the special directory presents a problem to the installation as well as the user, and the solutions are probably system-dependant. In MS-DOS you would use environment variables, in MS-Windows the registry.
The library mechanism such as used in ciforth has the effect that it is possible to load those two definitions while ciforth needs to know the where abouts of merely one file (the "lab" file). (In the section configuration we will see a system-independant solution for the configuring of ciforth.) In fact it is very simple. The file is subdivided in blocks of 16 lines. The first line of every block is an index line and contains in comment the names of the words that are defined in the block:
1 ( NIP TUCK ) \ AvdH 2003 feb 13 17:00:12
2 ( a b -- b )
3 : NIP SWAP DROP ;
4 ( a b -- b a b )
5 : TUCK 2>R R@ 2R> ;
< 11 empty lines >
The stack comment is pitiful, but we don't want to present just yet the style of comment used in ciforth.
If we want to load a program that uses TUCK , we put the line
WANT TUCK
up front, or in an over-all load-file. The first thing WANT does is look whether TUCK is already present. It may have been loaded already, or TUCK was added to a new version. Then it does nothing. Forth inspects all index-lines and loads the first block where the word TUCK is present in the index line.
What if the definition of TUCK require auxiliary words that comprise 5 blocks? Then we just put TUCK in the header of the next 5 blocks. The effect is that the 5 next blocks are compiled, until finally in the last block TUCK is defined. The fun starts for real, if you realize that in these blocks too WANT can be used. It could be that a word needs to be coded differently for Windows and Linux. This will be treated using the example of passing arguments.
Note that orginally WANT was called REQUIRE
N.B. lab means: "library addressable by block". The blocks themselves are numbered. That can be handy for other things, for example the error messages are present from block 32, to the tune of 16 per block.