6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Fri Nov 22, 2024 3:40 pm

All times are UTC




Post new topic Reply to topic  [ 14 posts ] 
Author Message
PostPosted: Sat Jan 19, 2019 11:55 pm 
Offline

Joined: Fri May 05, 2017 9:27 pm
Posts: 895
commodorejohn wrote:
it'd be nice if one handler routine could simply :noname up a definition to assign to a selector within the body of the handler itself, but I'm not up enough on low-level Forth monkeying to be certain that that's how this works. If, say, I have:
Code:
: handler-A
    some-code
   :noname
       code-for-another-handler ;
   selector-B !
   ;

If that even parses correctly, is it going to roll up a new copy of the anonymous function every time it's executed? I'm thinking of this in a Lisp-y or Smalltalk-y context where symbols/lists are unique and immutable so there's only ever one copy of a given function, but I know Forth doesn't work that way by nature.

Here is your example using Leo Brodie's DOER/MAKE with real code in place of some-code and code-for-another-handler .
Code:
DOER SELECTOR-B
: HANDLER-A
   .S
   MAKE SELECTOR-B
      ." THIS HAS NO NAME." ;

And here is a log of when it is loaded and executed. The computer's responses are in italics.

4D LOAD OK
SELECTOR-B OK
HANDLER-A STACK EMPTY OK
SELECTOR-B THIS HAS NO NAME. OK

And no, a new version is not compiled each time HANDLER-A is run.

To give you an example of what's possible with DOER/MAKE , here is an example from "Thinking Forth".
Code:
// TODDLER
DOER ANSWER
: RECITAL
   CR ." YOUR DADDY IS STANDING ON THE TABLE.  ASK HIM 'WHY?' "
   MAKE ANSWER  ." TO CHANGE THE LIGHT BULB."
   BEGIN
      MAKE ANSWER  ." BECAUSE IT'S BURNED OUT."
      MAKE ANSWER  ." BECAUSE IT WAS OLD."
      MAKE ANSWER  ." BECAUSE WE PUT IT IN THERE A LONG TIME AGO."
      MAKE ANSWER  ." BECAUSE IT WAS DARK!"
      MAKE ANSWER  ." BECAUSE IT WAS NIGHT TIME!!"
      MAKE ANSWER  ." STOP SAYING WHY?"
      MAKE ANSWER  ." BECAUSE IT'S DRIVING ME CRAZY."
      MAKE ANSWER  ." JUST LET ME CHANGE THIS LIGHT BULB!"
   AGAIN ;
: WHY?   CR ANSWER QUIT ;

And here is a sample run.
Code:
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!
WHY?
BECAUSE IT WAS NIGHT TIME!!
WHY?
STOP SAYING WHY?
WHY?
BECAUSE IT'S DRIVING ME CRAZY.
WHY?
JUST LET ME CHANGE THIS 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!
WHY?
BECAUSE IT WAS NIGHT TIME!!
WHY?
STOP SAYING WHY?
WHY?
BECAUSE IT'S DRIVING ME CRAZY.
WHY?
JUST LET ME CHANGE THIS LIGHT BULB!
WHY?
BECAUSE IT'S BURNED OUT.
WHY?
BECAUSE IT WAS OLD.
WHY?
BECAUSE WE PUT IT IN THERE A LONG TIME AGO.


Cheers,
Jim


Top
 Profile  
Reply with quote  
PostPosted: Sun Jan 20, 2019 1:05 am 
Offline

Joined: Thu Jan 21, 2016 7:33 pm
Posts: 282
Location: Placerville, CA
Hah, brilliant example :D Thanks, that looks like exactly what I was looking for.


Top
 Profile  
Reply with quote  
PostPosted: Sun Jan 20, 2019 9:11 pm 
Offline

Joined: Fri May 05, 2017 9:27 pm
Posts: 895
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:
( 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.]


Last edited by JimBoyd on Wed Jan 23, 2019 9:24 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
PostPosted: Tue Jan 22, 2019 8:41 pm 
Offline

Joined: Sun May 13, 2018 5:49 pm
Posts: 255
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:
( 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:
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.


Top
 Profile  
Reply with quote  
PostPosted: Wed Jan 23, 2019 7:45 pm 
Offline

Joined: Sun May 13, 2018 5:49 pm
Posts: 255
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:
: WHY? CR ANSWER CR QUIT ;

and now it works like:
Code:
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.


Top
 Profile  
Reply with quote  
PostPosted: Wed Jan 23, 2019 9:36 pm 
Offline

Joined: Fri May 05, 2017 9:27 pm
Posts: 895
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!


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: