VALUE and TO #108
Replies: 6 comments 12 replies
-
Maybe I’m misunderstanding your question (this is really the kind of thing that’s best to discuss synchronously, say on IRC...), but what you’re describing is indeed the correct approach (inside the bounds of ANS), so I’m not sure what it is that I can help you with. As you say, For reference, this kind of thing is known as a “ Aside from these : my-value create , does> @ ;
: my-to ' >body ! ; My recommendation is to now try and make a compilation-only version. What should it do? Obviously it has to be immediate. The tick must stay as is, because it must parse during compilation; the store must be postponed, because it must be performed during execution. I think I’ve said as much as I can without spoiling the whole thing for you, so I’ll hide the complete implementation under a spoiler. Solution: my-value create , does> @ ;
: my-to ' >body state @ if postpone literal postpone ! else ! then ; immediate
0 my-value fred cr fred . ( 0 )
1 my-to fred cr fred . ( 1 )
: tst 2 my-to fred ; cr tst fred . ( 2 ) |
Beta Was this translation helpful? Give feedback.
-
Just several comments-corrections to show a slightly wider picture.
It's not necessary. A number of different implementations for Roughly, these implementations can be separated to the following approaches:
This phrasing can be correct in the approach (1) only. In the approach (2) "it" always does the same. In the approach (3) "it" references the different subroutines in the first and in the second place, and neither of them depends on STATE. |
Beta Was this translation helpful? Give feedback.
-
Here's how I implement it in webForth ( https://github.com/mitra42/webforth )
I use one code word `>BODY` that knows how to find the body or a word from its xt, since my code is token threaded it uses token stored at the XT to determine if it was defined with `CONSTANT`, `VARIABLE`, `USER`, `DEFER` etc
this is code because depending on the type of word it might be looking in Ram or Rom or initialized User variable space.
Then I can build the whole ANS mess of `DEFER`, `TO`, `IS`, `VALUE`, `ACTION-OF` (my least favorite part of ANS on top of that word.
Note that I support ANS's `POSTPONE` I still use `[COMPILE]` and `COMPILE` as its much easier for me to understand the code when I do !
```
: >BODY! \\ ( Store a value in the body of the variable ) >BODY ! ;
: DEFER! >BODY! ; ( to be honest I cant remember why this a seperate word from >BODY! - maybe historical reason )
: '>BODY! STATE @ ( nasty state dependent word ) IF [COMPILE] ['] COMPILE >BODY! ELSE ' >BODY! THEN ;
: IS '>BODY! ; IMMEDIATE
( _USER is a variable that points to the next USER variable available,
$,n is from eForth, builds next dictionary word from a string )
: VALUE TOKEN $,n OVERT HERE tokenValue , _USER @ , _USER ++ >BODY! ;
: TO '>BODY! ; IMMEDIATE
: DEFER@ ( xt -- a; ) >BODY @ ;
: ACTION-OF STATE @ \ https://forth-standard.org/standard/core/ACTION-OF (nasty state dependent word)
IF [COMPILE] ['] COMPILE DEFER@ ELSE ' DEFER@ THEN ; IMMEDIATE
```
\- Mitra
…On 15/10/21 3:30 am, ruv wrote:
Just several comments-corrections to show a slightly wider picture.
|
Beta Was this translation helpful? Give feedback.
-
( A.Haley interpreted VALUEs --- 16sep2021 )
: NOOP ;
VARIABLE #MSG 0 MSG !
CREATE VALUES
‘ @ , ( <value>)
‘ ! , ( TO <value>)
' +! , ( +TO <value>)
' NOOP , ( 'OF <value>)
: VALUE ( n "name" -- )
CREATE ,
DOES> VALUES #MSG @ CELLS + @ EXECUTE
0 #MSG ! ;
: TO ( "name" -- ) 1 #MSG ! ;
: +TO ( "name" -- ) 2 #MSG ! ;
: 'OF ( "name" -- ) 3 #MSG ! ;
\ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pahihu
… On 2021. Oct 14., at 12:56, Andrew Holt ***@***.***> wrote:
Hi,
For my education and amusement I'm trying to figure out how VALUE and TO are implemented.
e.g.
0 value fred
cr fred .
1 to fred
cr fred .
: tst 2 to fred ;
cr tst fred .
So when I run these on gforth this is what I see:
0 value fred ok
cr fred .
0 ok
ok
1 to fred ok
cr fred .
1 ok
ok
: tst 2 to fred ; ok
ok
cr tst fred .
2 ok
So my question is about the behavior of 'to' I'm guessing that it uses state to determine if compiling or interpreting.
If interpreting then it replaces the value assigned to fred
If compiling it places the execution tokens that replace the value.
I've played around with this and can't get it work as expected.
Any advice, tips, examples appreciated.
Thanks.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub <#108>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/ADTEB23H4KGTOTGONGBAZ5TUG2ZPFANCNFSM5F7LPVQQ>.
Triage notifications on the go with GitHub Mobile for iOS <https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675> or Android <https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
|
Beta Was this translation helpful? Give feedback.
-
This how I added VALUE and TO to Camel Forth.
|
Beta Was this translation helpful? Give feedback.
-
For more examples see
T.Rayburn: “METHODS> Object-oriented extensions redux”, FORML 1987 (Vierte Dimension Volume III/Nr.3 Oktober 1987)
P.Bartholdi: “The ‘TO’ solution”, Forth Dimensions Volume 1, Number 4
pahihu
… On 2021. Nov 11., at 14:59, theBF ***@***.***> wrote:
This is very clever. More of an OOP approach with the message interpreter.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub <#108 (reply in thread)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/ADTEB24HCIIDQAMRJPXDJTLULPD23ANCNFSM5F7LPVQQ>.
Triage notifications on the go with GitHub Mobile for iOS <https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675> or Android <https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
|
Beta Was this translation helpful? Give feedback.
-
Hi,
For my education and amusement I'm trying to figure out how VALUE and TO are implemented.
e.g.
So when I run these on gforth this is what I see:
So my question is about the behavior of 'to' I'm guessing that it uses state to determine if compiling or interpreting.
If interpreting then it replaces the value assigned to fred
If compiling it places the execution tokens that replace the value.
I've played around with this and can't get it work as expected.
Any advice, tips, examples appreciated.
Thanks.
Beta Was this translation helpful? Give feedback.
All reactions