6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sun Nov 10, 2024 9:27 pm

All times are UTC




Post new topic Reply to topic  [ 23 posts ]  Go to page Previous  1, 2
Author Message
PostPosted: Fri Feb 09, 2024 12:36 am 
Offline

Joined: Sat Dec 12, 2015 7:48 pm
Posts: 145
Location: Lake Tahoe
BruceRMcF wrote:
resman wrote:
... I'm a fan of function pointers, so I'll be using this quite a bit.


If you are a fan of function pointers, I hope you have seen mini-oof ("mini-object-oriented-forth): https://bernd-paysan.de/mini-oof.html


That's very cool! Many of my larger PLASMA modules start with a table of function pointers. I'll have to look at this to clean up my interface code.


Top
 Profile  
Reply with quote  
PostPosted: Sun Feb 11, 2024 6:19 pm 
Offline

Joined: Sat Dec 12, 2015 7:48 pm
Posts: 145
Location: Lake Tahoe
Well, darn. Came across an old Forth manual (Apple II Forth 1.7 by John Draper aka Cap'n Crunch) that documented ?EXEC as the word to test if interpreting vs compiling. So [IF][ELSE][THEN] it is. A few quick tests appeared to confirm proper functionality. And I got to use a few things I learned in this thread:
Code:
: [THEN] ; IMMEDIATE ( PLACE HOLDER TO RESUME EXECUTION )
DEFER [ELSE] ( SKIP UNTIL [THEN] IF EXECUTED )
: [IF] ( F -- )
  NOT IF ( SKIP CODE IN BETWEEN [ELSE] OR [THEN] )
    1 >R
    BEGIN
      BL WORD FIND IF
        CASE
          ' [ELSE] OF
            R@ 1 = IF ( RESUME EXECUTING AT MATCHING [ELSE] )
              R> DROP DROP EXIT
            THEN
            ENDOF
          ' [THEN] OF
            R> 1- ?DUP 0= IF ( EXIT AT FINAL [THEN] )
              DROP EXIT
            THEN
            >R
            ENDOF
          [ LATEST ] LITERAL OF ( CHECK FOR NESTED [IF] )
            R> 1+ >R
            ENDOF
        ENDCASE
      ELSE
        DROP
      THEN
    AGAIN
  THEN
; IMMEDIATE
:NONAME ( [ELSE] )
  1 >R
  BEGIN
    BL WORD FIND IF
      CASE
        ' [THEN] OF
          R> 1- ?DUP 0= IF ( EXIT AT FINAL [THEN] )
            DROP EXIT
          THEN
          >R
          ENDOF
        ' [IF] OF ( CHECK FOR NESTED [IF] )
          R> 1+ >R
          ENDOF
      ENDCASE
    ELSE
      DROP
    THEN
  AGAIN
; IMMEDIATE IS [ELSE]


Top
 Profile  
Reply with quote  
PostPosted: Sun Feb 11, 2024 9:51 pm 
Offline

Joined: Sun May 13, 2018 5:49 pm
Posts: 255
I just wanted to point out that forth-standard.org has reference implementations (for the Forth 2012 standard) for many words. You might be interested in their [IF]/[ELSE]/[THEN] implementation as they did it a little differently, but also handle nested [IF]s. Their [IF] is very short and all of the work is done in [ELSE]. [THEN] is an empty word, just like in your implementation.

https://forth-standard.org/standard/tools/BracketIF
https://forth-standard.org/standard/tools/BracketELSE
https://forth-standard.org/standard/tools/BracketTHEN

These are just example reference implementations. You're welcome to solve things your own way, of course - that's part of the fun of using Forth!


Top
 Profile  
Reply with quote  
PostPosted: Sun Feb 11, 2024 11:09 pm 
Offline

Joined: Sat Dec 12, 2015 7:48 pm
Posts: 145
Location: Lake Tahoe
SamCoVT wrote:
I just wanted to point out that forth-standard.org has reference implementations (for the Forth 2012 standard) for many words. You might be interested in their [IF]/[ELSE]/[THEN] implementation as they did it a little differently, but also handle nested [IF]s. Their [IF] is very short and all of the work is done in [ELSE]. [THEN] is an empty word, just like in your implementation.

https://forth-standard.org/standard/tools/BracketIF
https://forth-standard.org/standard/tools/BracketELSE
https://forth-standard.org/standard/tools/BracketTHEN

These are just example reference implementations. You're welcome to solve things your own way, of course - that's part of the fun of using Forth!

Well look at that! This was my first attempt with [ELSE] and I was thinking it looked a LOT like [IF]. Just needed to think about it more. Mine also handles nested [IF]s, I just use the return stack to track the nesting level, I wasn't too sure about other interactions using the data stack. So, with simplified [IF]...

Code:
: [THEN] ; IMMEDIATE ( PLACE HOLDER TO RESUME EXECUTION )
DEFER [ELSE] ( SKIP UNTIL [THEN] IF EXECUTED )
: [IF] ( F -- )
  NOT IF ( SKIP CODE IN BETWEEN [ELSE] OR [THEN] )
    [ELSE]
  THEN
; IMMEDIATE
:NONAME ( [ELSE] )
  1 >R
  BEGIN
    BL WORD FIND IF
      CASE
        ' [ELSE] OF
          R@ 1 = IF ( RESUME EXECUTING AT MATCHING [ELSE] )
            R> DROP DROP EXIT
          THEN
          ENDOF
        ' [THEN] OF
          R> 1- ?DUP 0= IF ( EXIT AT FINAL [THEN] )
            DROP EXIT
          THEN
          >R
          ENDOF
        ' [IF] OF ( CHECK FOR NESTED [IF] )
          R> 1+ >R
          ENDOF
      ENDCASE
    ELSE
      DROP
    THEN
  AGAIN
; IMMEDIATE IS [ELSE]


What I don't understand is why they POSTPONED the [ELSE] in the reference implementation of [IF]. Still one of those things I don't quite get.

[edit] Ah, I think I get it - [ELSE] is IMMEDIATE. Since mine was DEFERed, it doesn't show up as IMMEDIATE until IS, otherwise it would have been executed during compilation of [IF]

I suppose I should also switch over to string compares instead of the dictionary search - no need to DEFER in that case.


Top
 Profile  
Reply with quote  
PostPosted: Mon Feb 12, 2024 1:47 am 
Offline

Joined: Wed Aug 21, 2019 6:10 pm
Posts: 217
resman wrote:
... What I don't understand is why they POSTPONED the [ELSE] in the reference implementation of [IF]. Still one of those things I don't quite get.

[edit] Ah, I think I get it - [ELSE] is IMMEDIATE. Since mine was DEFERed, it doesn't show up as IMMEDIATE until IS, otherwise it would have been executed during compilation of [IF] ...


Exactly ... since their [ELSE] is IMMEDIATE they have to POSTPONE it to compile it into a definition.


Top
 Profile  
Reply with quote  
PostPosted: Tue Feb 13, 2024 10:24 pm 
Offline

Joined: Fri May 05, 2017 9:27 pm
Posts: 894

My Fleet Forth's INTERPRET passes the string returned by WORD to a deferred word I/C ( interpret/compile ) which is normally vectored to (I/C) . This allows me to define [IF] [ELSE] and [THEN] like so.
Code:
:NONAME  ( NL1 ADR -- NL2 )
         ( NL1 ADR -- )
   DUP  5 " [IF]"   TEXT= ABS UNDER+
   DUP  7 " [THEN]" TEXT= UNDER+
        7 " [ELSE]" TEXT= OVER 1 =
   AND +  ?DUP ?EXIT
   ['] (I/C) IS I/C ;
: [THEN] ; IMMEDIATE
: [ELSE]  ( -- )
   1  [ ROT ] LITERAL IS I/C ;
   IMMEDIATE
: [IF]  ( FLAG -- )
   ?EXIT
   [COMPILE] [ELSE] ; IMMEDIATE

Rather than use FIND to find the names in the dictionary, it uses a string compare. TEXT= is a primitive so the string compare is faster than a dictionary search.
The phrase
Code:
   [ ROT ]

in the definition of [ELSE] rotates the address of the nameless word compiled by :NONAME to the top of the stack as a parameter for the immediate word LITERAL which compiles that address as an inline literal.
The word ?EXIT in the definition of [IF] is a primitive. It is equivalent to:
Code:
   IF  EXIT  THEN



Top
 Profile  
Reply with quote  
PostPosted: Thu Feb 15, 2024 4:11 pm 
Offline

Joined: Sat Dec 12, 2015 7:48 pm
Posts: 145
Location: Lake Tahoe
Very clever. I still think like I'm programming in a HLL; need to make the mental shift.


Top
 Profile  
Reply with quote  
PostPosted: Thu Feb 15, 2024 8:04 pm 
Offline

Joined: Wed Aug 21, 2019 6:10 pm
Posts: 217
resman wrote:
Very clever. I still think like I'm programming in a HLL; need to make the mental shift.


Yes, you are programming to create the exact HLL that does the job you need done, so it covers the range from low level programming extending the underlying capabilities through to high level programming.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 23 posts ]  Go to page Previous  1, 2

All times are UTC


Who is online

Users browsing this forum: No registered users and 4 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: