Path: utzoo!utgpu!jarvis.csri.toronto.edu!clyde.concordia.ca!uunet!samsung!aplcen!john From: john@aplcen.apl.jhu.edu (John Hayes) Newsgroups: comp.lang.forth Subject: Re: FORTH Modules: current confusion? Summary: portable Modules Keywords: modules; dictionary structure; compiler mods Message-ID: <4358@aplcen.apl.jhu.edu> Date: 20 Dec 89 17:22:13 GMT References: <5596@sdcc6.ucsd.edu> <6708@tank.uchicago.edu> Reply-To: john@aplcen.apl.jhu.edu (John Hayes) Organization: Johns Hopkins University Lines: 122 In article <6708@tank.uchicago.edu> keith@curry.uchicago.edu (Keith Waclena) writes: >Some background: It's the end of the quarter and I'm about to slip a >little FORTH into a reading course I'm teaching on data encapsulation >in programming languages. The course has followed a historical >progression from block structure through modules, abstract data types >and finally object-oriented programming. We read a paper by Friedman >and Felleisen[1] detailing how one can add modules to Scheme with a >page or so of relatively portable code. Now I thought I'd give my >students a one-page paper by Schorre[2] on how to add modules to FORTH >in 3 short lines of code. Here is a module syntax I have been experimenting with: module> module's-name ( hidden / private definitions goes here ) public: ( public definitions go here ) private: ( more hidden data goes here. public: and private: may be used as many times as necessary or not at all. ) endmodule> module> ... endmodule> encloses a module definition. By default, definitions that occur within the module can not be found outside the module. Definitions made after use of the public: pseudo-label can be found outside the module. private: switches the compiler back to hiding definitions. I have provided two implementations. The first implementation is meant to be portable. It does not rely on dictionary structure but you must have also and previous. The second implementation is written is in ANS Forth. Of course, there is no such thing as ANS Forth yet; the October, 1989 Basis was used. The code exploits a controversial feature of ANS Forth. In ANS Forth, : no longer has any effect on search order (i.e. no current @ context ! ). In my opinion this a cleaner factoring and simplifies some programs, as demonstrated by the two implementations of module>. This change to : is controversial because it changes the way some people use their Forth systems. The purpose of this posting is twofold: to describe an interesting modularization mechanism and to spark discussion on the new : . Any comments? -------------------cut here--------------------------------------------- \ (c) 1989 Johns Hopkins University / Applied Physics Laboratory \ Modules - Create a local name space. Each definition in a module may \ be declared to be visible (i.e. findable) outside the module or only \ visible within the module. \ Usage syntax: \ module> module's-name \ ( hidden / private definitions goes here ) \ public: \ ( public definitions go here ) \ private: \ ( more hidden data goes here. public: and private: may be used as \ many times as necessary or not at all. ) \ endmodule> variable public? : module> \ ( --- ) Start a module definition. Subsequent word \ definitions will be hidden. >in @ vocabulary >in ! \ module is really a vocabulary 0 public? ! \ private definitions by default ' also execute definitions ; \ module is compilation and search : swap-vocabs \ ( --- ) Exchange the first two vocabularies in the search \ order. context @ previous context @ swap context ! also context ! ; : public: \ ( --- ) Subsequent definitions will be visible outside module. public? @ 0= if swap-vocabs definitions 1 public? ! then ; : private: \ ( --- ) Subsequent definitions will be hidden. public? @ if swap-vocabs definitions 0 public? ! then ; : endmodule> \ ( --- ) End a module definition. Restore vocabulary \ environment. private: previous definitions ; -------------------cut here--------------------------------------------- \ (c) 1989 Johns Hopkins University / Applied Physics Laboratory \ Modules - Create a local name space. Each definition in a module may \ be declared to be visible (i.e. findable) outside the module or only \ visible within the module. \ Usage syntax: \ module> module's-name \ ( hidden / private definitions goes here ) \ public: \ ( public definitions go here ) \ private: \ ( more hidden data goes here. public: and private: may be used as \ many times as necessary or not at all. ) \ endmodule> \ Implementation note: for this code to be useful, : cannot change search order. : module> \ ( --- ) Start a module definition. Subsequent word \ definitions will be hidden. >in @ vocabulary >in ! \ module is really a vocabulary ' also execute definitions ; \ module is compilation and search : public: \ ( --- ) Subsequent definitions will be visible outside module. context @ previous definitions \ definitions into outside vocabulary also context ! ; : private: \ ( --- ) Subsequent definitions will be hidden. definitions ; \ sugar : endmodule> \ ( --- ) End a module definition. Restore vocabulary \ environment. previous definitions ; -------------------cut here--------------------------------------------- John R. Hayes Applied Physics Laboratory Johns Hopkins University john@aplcen.apl.jhu.edu