Page 1 of 1

Re: Anonymous definitions - some questions

Posted: Sun Jan 20, 2019 9:11 pm
by JimBoyd
As I said, the example is straight out of "Thinking Forth". It's in Appendix B where Leo Brodie explains how DOER/MAKE works and gives implementations for various Forth systems. I don't think any of them were subroutine threaded systems though. I don't know if the Forth you're using is ITC ( indirect threaded ), DTC ( direct threaded ), or STC ( subroutine threaded ), or possibly some other threading technique. I successfully ported DOER/MAKE to my Forth, Fleet Forth for the Commodore 64, a short while ago. It's an ITC Forth. Last night I took a look at Durex Forth for the Commodore 64. It appears to use subroutine threaded code. I managed to port DOER/MAKE to it. Be warned! it has had minimal testing! That said, it should work with other subroutine threaded Forths for the 6502 based processors. Maybe someone using Tali Forth or Liara Forth could test it? :D

Code: Select all

( DOER/MAKE For A Subroutine Threaded Forth)
: NOTHING ;
: DOER
   CREATE ['] NOTHING , DOES> 
      @ 1- >R ;
VARIABLE DMARKER  \ Doer marker
: (MAKE)
   R>  1+  DUP 2+ DUP 3 +
   SWAP 1+ @ >BODY !
   @ ?DUP IF  >R  THEN ;
: MAKE
   STATE @ 0= ABORT" COMPILING ONLY!"
   POSTPONE (MAKE)
   HERE DMARKER ! 0 , ; IMMEDIATE
: ;AND
   POSTPONE EXIT
   HERE 1- DMARKER @ ! ; IMMEDIATE
: UNDO
   ['] NOTHING  ' >BODY ! ;
If you've had time to read "Thinking Forth", you'll notice one ability that's missing. The ability to use MAKE outside a colon definition to create an unnamed section of high level code that the DOER word performs. That ability is not needed in the examples I've shown and it would require knowledge of how a given system implements : ( colon) and ; ( semicolon).

Cheers,
Jim

[Edit: Corrected two typos.]

Re: Anonymous definitions - some questions

Posted: Tue Jan 22, 2019 8:41 pm
by SamCoVT
JimBoyd wrote:
Maybe someone using Tali Forth or Liara Forth could test it? :D
It works on Tali, but I had to put a "T" in POSTPONE in two spots and add a definition for 2+. Here's the version that works on Tali Forth 2:

Code: Select all

( DOER/MAKE For A Subroutine Threaded Forth - Tali Forth version)
: 2+ 2 + ;
: NOTHING ;
: DOER
   CREATE ['] NOTHING , DOES> 
      @ 1- >R ;
VARIABLE DMARKER  \ Doer marker
: (MAKE)
   R>  1+  DUP 2+ DUP 3 +
   SWAP 1+ @ >BODY !
   @ ?DUP IF  >R  THEN ;
: MAKE
   STATE @ 0= ABORT" COMPILING ONLY!"
   POSTPONE (MAKE)
   HERE DMARKER ! 0 , ; IMMEDIATE
: ;AND
   POSTPONE EXIT
   HERE 1- DMARKER @ ! ; IMMEDIATE
: UNDO
   ['] NOTHING  ' >BODY ! ;

Code: Select all

recital
YOUR DADDY IS STANDING ON THE TABLE.  ASK HIM 'WHY?'  ok
why?
TO CHANGE THE LIGHT BULB.why?
BECAUSE IT'S BURNED OUT.why?
BECAUSE IT WAS OLD.why?
BECAUSE WE PUT IT IN THERE A LONG TIME AGO.why?
BECAUSE IT WAS DARK!
One oddity is that the " ok" message was missing after each run, leaving the cursor just after the printed message but accepting new input. It looks like the OK message is similarly missing in the example from Thinking Forth that JimBoyd showed a few posts back. Tali doesn't normally print carriage returns at the end of a string, so the new "why?" that I typed ended up on the end of each message, but the doer/make behavior seems to be working properly.

Re: Anonymous definitions - some questions

Posted: Wed Jan 23, 2019 7:45 pm
by SamCoVT
SamCoVT wrote:
One oddity is that the " ok" message was missing after each run, leaving the cursor just after the printed message but accepting new input. It looks like the OK message is similarly missing in the example from Thinking Forth that JimBoyd showed a few posts back. Tali doesn't normally print carriage returns at the end of a string, so the new "why?" that I typed ended up on the end of each message, but the doer/make behavior seems to be working properly.
The missing " ok" message is because of the QUIT in WHY?. Sneaking an extra CR in there makes it work identically to the Thinking Forth version.

I changed why? to:

Code: Select all

: WHY? CR ANSWER CR QUIT ;
and now it works like:

Code: Select all

recital
YOUR DADDY IS STANDING ON THE TABLE.  ASK HIM 'WHY?'  ok
why?
TO CHANGE THE LIGHT BULB.
why?
BECAUSE IT'S BURNED OUT.
why?
BECAUSE IT WAS OLD.

Re: Anonymous definitions - some questions

Posted: Wed Jan 23, 2019 9:36 pm
by JimBoyd
SamCoVT wrote:
It works on Tali, but I had to put a "T" in POSTPONE in two spots and add a definition for 2+.
Oops. I just fixed my typos. I already had DOER/MAKE working on Fleet Forth, an ITC Forth. I didn't know what Forth commodorejohn was using so I ported it to Durex Forth for the Commodore 64 which appears to be an STC Forth. I got frustrated with the Durex editor, specifically how to get out of insert mode and back to the Forth interpreter, so I entered the code interactively. I copied the working code to a text file by hand and somehow missed the 'T' in POSTPONE in both places!