home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / forth / mac / 40 < prev    next >
Encoding:
Text File  |  1992-12-21  |  4.1 KB  |  126 lines

  1. Newsgroups: comp.lang.forth.mac
  2. Path: sparky!uunet!elroy.jpl.nasa.gov!decwrl!adobe!huxley.mv.us.adobe.com!user
  3. From: hawley@adobe.com (Steve Hawley)
  4. Subject: Re: Stand-alone code segments in Forth
  5. Message-ID: <hawley-191292110139@huxley.mv.us.adobe.com>
  6. Followup-To: comp.lang.forth.mac
  7. Sender: usenet@adobe.com (USENET NEWS)
  8. Organization: Adobe Systems Inc.
  9. References: <BzH7AK.5s7@news.cso.uiuc.edu>
  10. Date: Sat, 19 Dec 1992 19:27:58 GMT
  11. Lines: 113
  12.  
  13. In article <BzH7AK.5s7@news.cso.uiuc.edu>, schalk@symcom.math.uiuc.edu (Ken
  14. Schalk) wrote:
  15. >     I'm new to Forth and enjoying it immensely, but I have a question
  16. > about stand-alone code segments, such as WDEFs and XCMDs.  Let me just
  17. > say that I experienced in other languages (Pascal, C, C++, ...) and
  18. > the Mac Toolbox, and have written both WDEFs and XCMDs in the past.
  19. >     What I'm wondering is, is it possible to build such things with
  20. > any of the Mac Forth incarnations?
  21.  
  22. >     My guess is that it probably hasn't been done, as I think it might
  23. > be kind of painful to turn a Forth dictionary (or some subset thereof)
  24. > into a stand-alone code resource.  However, I think it would be
  25. > wonderful if it were possible.  Having written these things in the
  26. > past, I think the concept of a WDEF in Forth is a beautiful thing.
  27. > (Heck, once you had the basics laid out, you could churn out WDEF
  28. > after WDEF all the time, leading to a distinctive look for your apps,
  29. > albeit a degeneration of the interface standard. :-)
  30.  
  31. Actually, it's not really that hard depending on your architecture.
  32. When I wrote SteveFORTH for the Mac several years ago, my main goal
  33. was to produce a FORTH that had a kernel that was stand-alone and I/O
  34. that was completely abstracted from the language.
  35.  
  36. The way I did this was to write a shell operating system in Think C.
  37. The main() of SteveFORTH looks like this:
  38.  
  39. main()
  40. {    
  41.     MacInits();      /* InitGraf() etc. */
  42.     GetDictionary(); /* load dictionary resource */
  43.     MakeStack();     /* build the parameter stack */
  44.  
  45.     boot();          /* start execution */
  46.     CleanUp();
  47. }
  48.  
  49. Now imagine that we are writing a window definition function.  We could
  50. just
  51. do this:
  52.  
  53. pascal long main(varCode, theWindow, message, param)
  54. short varCode;
  55. WindowPtr theWindow;
  56. short message;
  57. long param;
  58. {
  59.     static short neverBeenCalled;
  60.     long retval;
  61.  
  62.     SetUpRegisters(); /* do all the nasty a4/a5 mucking for globals */
  63.     if (neverBeenCalled) {
  64.         GetDictionary();
  65.         MakeStack();
  66.     }
  67.     Push(varCode);
  68.     PushLong(theWindow);
  69.     PushLong(param);
  70.     Push(message);
  71.  
  72.     boot();
  73.     retval = PopLong();
  74.     CleanUpRegisters();
  75.     return retval;
  76. }
  77.  
  78. In SteveFORTH there was a variable which was the address of the FORTH
  79. function
  80. to call on booting.  By default, it was a routine to make a window and
  81. attach
  82. standard input and output and call the interpretter, but you could easily
  83. just
  84. point it to your WDEF entry point:
  85.  
  86. ok ' myWDEF bootval !
  87.  
  88. and then execute the build application command from SteveFORTH's menu which
  89. will build an application that you can extract the DICT resource and attach
  90. to the application that will contain the above WDEF stub.
  91.  
  92. Here's how boot is defined:
  93.  
  94. boot()
  95. {
  96.     char *a; /* address of FORTH boot function */
  97.  
  98.     /* extract the address and add it to the base of the
  99.      * dictionary.
  100.      */
  101.     a = DStart + *((short *)(DStart + BOOTVAL));
  102.     asm {
  103.         movem.l d0-d7/a0-a6,-(sp) /* save the world */
  104.         move.l a,a0         /* copy the boot vector into A0 (scratch) */
  105.         move.l stack,a6     /* set up parameter stack (and blow away a6) */
  106.         move.l DStart,a3    /* set up dictionary base */
  107.         move.l here,a2      /* load here */
  108.         jsr (a0)            /* call boot function */
  109.         move.l a6,stack     /* save stack */
  110.         move.l a3,DStart    /* save dict base (could've moved!!) */
  111.         move.l a2,here      /* save here */
  112.         movem.l (sp)+,d0-d7/a0-a6 /* restore the world */
  113.     }
  114. }
  115.  
  116.  
  117. Steve Hawley
  118. hawley@adobe.com
  119. --
  120. "Did you know that a cow was *MURDERED* to make that jacket?"
  121. "Yes.    I didn't think there were any witnesses, so I guess I'll have to
  122. kill
  123.  you too." -Jake Johansen
  124.