Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!mit-eddie!uw-beaver!ubc-cs!alberta!aunro!idacom!rob From: rob@idacom.uucp (Rob Chapman) Newsgroups: comp.lang.forth Subject: Libraries for Forth Message-ID: <1990Aug17.071455.29925@idacom.uucp> Date: 17 Aug 90 07:14:55 GMT Organization: IDACOM, a division of Hewlett-Packard Lines: 167 A few notes on my previous posting of ...and zen there were objects. - the word OBJECT would be more appropriate if it was named CLASS. - there is a bug in the definition of INSTANCE which only shows up if an instance is interpreted: : INSTANCE ( object -- ) ( inst -- a ) @+ \ LITERAL CAST ; ^^^^^^^^^^^^^^^ should be: DOES> ( inst -- a ) @+ SWAP CAST \ LITERAL ; LITERAL has two different stack outcomes. If compiling, it consumes an item ( n -- ), if interpreting, it has no affect ( -- ). Thanks to Mitch for this one. Anyway, here's a copy of what was presented at Thursdays' FUG (Forth Users Group). The code is for botForth but could easily be adapted to other Forths. Its based on a Forth which uses a link list for its dictionary structure. Rob Chapman ( FUG excerpt: Aug 14,1990 ) Libraries Forth, without constraint or guidance, can quickly mushroom into a long list of words in a large programming project. These words fall into two categories: tools and application. Tools are words which enrich a programmers environment and allow them to program more efficiently. Tools such as string manipulaters, user interface constructs and decompilers are just a few examples. These tools need to be available when requested and hidden when not needed. By defining a few words, we can create a mechanism to accomplish this end. The following code implements this concept and uses the notion of libraries prevelant in some other languages. Owing to Forth's simplicity yet completeness, only 5 words (also only 5 lines of code) are needed. ( ==== Libraries for botForth: Rob Chapman Aug 14, 1990 ==== ) ( Library structure: | >latest | first entry | dummy name field | ) : LIBRARY ( -- ) DATA HERE 2 + , 0 , E120 , ; : OPEN ( lib -- ) @+ latest @ SWAP ! latest ! ; : ENCLOSE ( lib -- ) latest @ SWAP !+ @ latest ! ; : PREVIOUS ( entry \ head -- prev ) BEGIN 2DUP @ XOR WHILE @ REPEAT NIP ; : CLOSE ( lib -- ) @+ @ SWAP latest PREVIOUS ! ; Notes: : DATA ( -- ) 0 VARIABLE -2 ALLOT ; : @+ ( a -- n \ a+ ) DUP @ SWAP 2 + ; - 16 bit Forth - 'latest' points to the the latest defined word in the word list. - All headers have their link pointer as the first entry. | link | name | body | - E120 is a dummy name field used as the tail of a library link list Usage: LIBRARY TOOLS ( create a library for tools ) TOOLS OPEN ( and open it ) LIBRARY ASSEMBLER ( create an assembler library in the tools library ) ASSEMBLER OPEN . . assembler definitions . ASSEMBLER ENCLOSE ( enclose all the assembler words in the library ) ASSEMBLER OPEN ( open it again for usage ) . . tools . LIBRARY DISASSEMBLER ( create a disassembler library ) DISASSEMBLER OPEN . . disassembler definitions . DISASSEMBLER CLOSE ( enclose all the words in the library ) . . more tools . TOOLS ENCLOSE ( encapsulate everything in the tools library ) In the above example, a tools library is created and inside it, two more libraries are created. When the TOOLS library is closed, the only word that appears in the dictionary is TOOLS. To access the assembler, one must: TOOLS OPEN . This opens the TOOLS library and makes the ASSEMBLER library accessable. Now the assembler library may be opened by: ASSEMBLER OPEN . ASSEMBLER DISASSEMBLER \ | | | \ / | library structure TOOLS | | / FORTH More words may be added to the library by first opening the library, defining the new words and then enclosing the new words. ENCLOSE and CLOSE both close the library, except ENCLOSE adds all the words since it was opened to the library. Libraries and Modules In trying to visualize how libraries fit into the overall scheme of Forth and MODULEs I usually refer to the following picture: \|/ \|/ }libraries within libraries \ | / }libraries Forth / | \ }modules /|\ /|\ }modules within modules Applications are built in the modules while the tools reside in the libraries. When a library is opened, its words are inserted at the top of the Forth word list. This gives them priority in any naming conflicts. By opening libraries in the proper sequence, libraries are effectively stacked into the Forth wordlist. Historical Solutions If we look back in the mists of Forth, (not so far back in ptForth), we come across the words CURRENT and CONTEXT. These could be considered as the first primitive attempts to create this balance of dictionary searching provided by libraries and modules. In figForth, they allowed survival within a tree structured dictionary. The problem with the tree structured dictionary model was that the end words for an application ended up at the bottom of the tree. Using CURRENT and CONTEXT allowed this tree to be searched (CONTEXT) in one way while definitions were added to another part (pointed to by CURRENT). Around Forth-83, a second generation dictionary mechanism was proposed. This threw away the heirarchial dictionary tree provided by earlier efforts and replaced it with a dictionary stack. Definitions were added to the vocabulary which was on top of the stack and the whole stack was searched when looking up words. These are only two of the many different dictionary mechanisms which have been used. ANSI-Forth Wordsets as Libraries The ANSI-Forth standards committee (started in 87,) have categorized a whole bunch of words into what they term the core wordset and other wordsets. The other wordsets are really libraries for Forth much like the ones usually associated with C. They include wordsets for handling files, floating point, strings, double number, blocks, search order, locals, and far memory. Each of these wordsets can have extenstions. By using libraries, it is easy to include all these wordsets in Forth without cluttering it. extsn extsn extsn extsn extsn extsn extsn extsn | | | | | | | | FILES FLOATS STRINGS DOUBLES BLOCKS SEARCHES LOCALS FARS extsn |______|________|________|_______|________|________|______|________| | Forth (CORE) | ...applications... These wordsets or libraries sit behind Forth giving it power and depth if needed, but may be tucked away to keep the core simple when they are not used. Applications can now be built on the other side of Forth where they belong.