6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sat Apr 27, 2024 9:38 pm

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Tue Jun 06, 2023 9:43 pm 
Offline
User avatar

Joined: Mon Mar 06, 2023 9:26 am
Posts: 84
Location: UK
This probably isn't that impressive, but hey I'm proud of it.

Code:
: IF IMMEDIATE
    STATE @ IF \ compiling
        0 \ leave flag for THEN
       ' 0BRANCH , \ compile 0BRANCH
       HERE @ \ save location of the offset on the stack
       0 , \ compile a dummy offset
    ELSE \ immediate
        :NONAME \ start compiling an anonymous word
        ' 0BRANCH ,
        HERE @
        0 ,
    THEN
;

: THEN IMMEDIATE
   DUP
   HERE @ SWAP - \ calculate the offset from the address saved on the stack
   SWAP ! \ store the offset in the back-filled location
   
    ?DUP IF \ check flag/xt left by IF
        ' EXIT , \ finish off word
        EXECUTE \ execute it
       
        LATEST @ DUP @ LATEST !
        HERE ! \ make sure it doesn't leak

        [COMPILE] [
    THEN
;


The idea is for IF to store a flag underneath the address of the 0BRANCH, that THEN can look at and act on. It also happens that Jonesforth's :NONAME leaves an execution token on the stack, which conveniently doubles up as my flag.

_________________
probably the youngest person on this forum


Top
 Profile  
Reply with quote  
PostPosted: Tue Jun 06, 2023 10:26 pm 
Offline

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

My Forth's :NONAME leaves an execution token on the stack. It also leaves an address and a security number, just like colon, for semicolon to resolve.
Code:
0 VALUE OLDHERE
: THEN
   ?COMP >RESOLVE  OLDHERE
   IF
      [COMPILE] ;  EXECUTE
      OLDHERE HERE - ALLOT
      0 TO OLDHERE
   THEN ; IMMEDIATE
: IF
   STATE @ 0=
   IF
      HERE TO OLDHERE  :NONAME
   THEN
   COMPILE ?BRANCH >MARK ; IMMEDIATE

This code could be used by a metacompiler. In that case, the IF and THEN in the source for IF and THEN would be the metacompiler versions.
If this is to redefine the original IF and THEN , they still exist, then it can be shortened slightly.
Code:
0 VALUE OLDHERE
: THEN
   [COMPILE] THEN   OLDHERE
   IF
      [COMPILE] ;  EXECUTE
      OLDHERE HERE - ALLOT
      0 TO OLDHERE
   THEN ; IMMEDIATE
: IF
   STATE @ 0=
   IF
      HERE TO OLDHERE  :NONAME
   THEN
   [COMPILE] IF ; IMMEDIATE

While this has been an entertaining exercise, I can't think of a good use for the new versions of IF and ELSE .


Top
 Profile  
Reply with quote  
PostPosted: Tue Jun 06, 2023 10:39 pm 
Offline
User avatar

Joined: Mon Mar 06, 2023 9:26 am
Posts: 84
Location: UK
They're useful if you want conditionals without having to wrap code in a function, which I'll admit is a pretty niche requirement. But hey, I learned something and I made something, so I'd say this has been a positive experience.

For what it's worth, this approach could probably be extended to other control flow structures without too much hassle.

_________________
probably the youngest person on this forum


Top
 Profile  
Reply with quote  
PostPosted: Tue Jun 06, 2023 11:07 pm 
Offline

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

The conditional compiling words [IF] [ELSE] and [THEN] are handy.


Top
 Profile  
Reply with quote  
PostPosted: Wed Jun 07, 2023 1:59 pm 
Offline

Joined: Sun May 13, 2018 5:49 pm
Posts: 247
allisonlastname wrote:
This probably isn't that impressive, but hey I'm proud of it.
You've written more than 20 lines of Forth, and you've modified the language itself to do your bidding. That is impressive and means you are on your way to great (Forth) things. I enjoy seeing how other people modify their Forths, even if I don't plan on doing what they are doing, or would have done it differently.

Tali Forth 2, which is what I use mostly, also doesn't have conditionals that work when interpreting.
The ANS pages for [IF], [THEN], and [ELSE] referenced by JimBoyd give example code and I'm likely to go that route as it doesn't use up dictionary space and Tali doesn't have 0BRANCH.

When looking into this earlier, I also stumbled on Tachyon Forth (I believe the latest version is called TAQOZ) for the Propeller micros. It compiles even when interpreting (similar to :NONAME, but the memory is reclaimed later) and then runs the compiled line. This allows IF/ELSE/THEN (along with other compiling words like loops) to be used even in interpreted mode with the limitation that they have to be on the same "interpreted" line if they are being "interpreted". While I don't plan to do anything like that myself, it is neat to see what others have tried and how it works along with the benefits and disadvantages that their solution has.

Good work, and thanks for sharing.


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

All times are UTC


Who is online

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