olorin@walt.cc.utexas.edu (Dave Weinstein) (08/06/89)
Below is a small module extension to Forth I wrote as part of an upcoming article in _The Computer Journal_. The code is reasonably portable (it assumes that there is some word LAST or LATEST which allows the programmer to get the NFA of the most recently added word). In its present state, importing words takes up header space, and using an imported word causes a performance hit (it does a @ execute). To speed up imported words (while making them nonportable), the word AS can be modified to make its descendent words (AS being a defining word) perform a state test, and then compile themselves into the dictionary if in compile mode (so the descendent words would have to be immediate). --Dave ---------------------------(cut here)------------------------------------- \ Poor Man's Module Package \ written by David Weinstein for _The Computer Journal_ \ copyright David Weinstein 5/30/89 \ \ License: \ The holder of this copy is hereby given full rights \ to use the code for both private and commercial \ purposes, and to pass the code on, so long as the \ authorship of this code is not claimed. \ \ Module Usage: \ \ To define a module: \ \ module <module-name> \ \ ... \ Module Body (Forth Code) \ ... \ end-module \ \ To extract a word from a module: \ \ from <module> import <word> as <destination> \ ... \ import <word> as <destination> \ end-imports \ \ Module Definition Words \ Create a vocabulary and make it the current vocabulary : module ( <name> | -- context current ) context @ current @ vocabulary last @ name> execute definitions ; \ End a module definition : end-module ( context current -- ) current ! context ! ; \ Module Access Words : from ( -- current context ) current @ context @ ; : import ( current context -- current context include-context context cfa ) context @ over ' ; : as ( include-context context cfa -- ) swap context ! ( Swap contexts to avoid REDEFINED errors ) create swap context ! ( Bring back the include context ) , ( And put the cfa in as the argument ) does> @ execute ( Execute imported word at runtime ) ; : end-imports ( current context -- ) context ! current ! ; ------------------------------(cut here)----------------------------- -- Dave Weinstein olorin@walt.cc.utexas.edu (512) 339-4407 Home GEnie: DHWEINSTEIN I am not responsible for the opinions of my employers.