How to define VALUE and TO
Asked Answered
C

3

11

I want to implement the Forth words VALUE and TO on a RPC/8 (an emulated computer in a Minecraft mod). My best attempts get me a set of words that work fine so long as I don't use them while compiling. More sepecificly VALUE works, but TO does not.

: VALUE CREATE , DOES> @ ;
: TO ' 3 + ! ;

I have tried everything I can think of to get it working and my best attempt gets me this:

['] NameOfAValue 3 + !

Note that the processor is not a pure 6502 but a 65EL02, a custom variant of the 65816.

EDIT #1: Somehow I forgot the call to CREATE in value. It should have been there all along. EDIT #2: I also got 3 and + switched around in TO... oops. It should have been the other way all along.

Counterpoison answered 16/2, 2013 at 20:39 Comment(4)
Blimey. A question about FORTH running on an emulated computer architecture on top of an emulated variant of a variant of a 6502 inside a game. I can't answer it, but +1 just for the meta-meta-ness of it!Frontier
@Jonners Minecraft (for the PC) is written in Java so all of that is running in a virtual machine too.Sucrose
@Milo; The code listed here for TO is still wrong Milo. ! is ( a n -) not ( n a -). TO is trying to store a stack underflow to address 3. It needs to be : TO ( n) ' 3 + !Concentrate
@Brian Tiffin; Thanks, I don't know how I missed that. Its a typo, the code on my disk image is the correct way.Counterpoison
C
2

Ok After a lot of trial and error as well as much searching I found something that should work, but because of two bugs in redFORTH, does not.

VALUE

\ Works fine, now to reset the value.
: VALUE \ n <name> --
    CREATE ,
    DOES> @
;

TO

\ Works if not compiling, LITERAL and POSTPONE are broken.
: TO
    TIBWORD FIND 3 +
    STATE @ IF
        POSTPONE LITERAL
        POSTPONE !
    ELSE
        !
    THEN
; IMMEDIATE

Demo of bug in LITERAL

\ fails, very wierd error.
: TESTLIT [ 42 ] LITERAL ;
\ TESTLIT Unknown Token: TESTLIT
\ FORGET TESTLIT Unknown Token: TESTLIT
\ WORDS TESTLIT COLD SORTMATCH ...

Demo of bug in POSTPONE

\ fails, postpone is directly equivelent to [']
: TESTPOST POSTPONE + ; IMMEDIATE
: TEST 2 2 TESTPOST . ;
\ . 1935
\ ' + . 1935

I'm off to file a bug report....

EDIT #1: After some more trial and error and not a little swearing (I'm not good with FORTH) I found a way to make it work.

: TO
    TIBWORD FIND 3 +
    STATE @ IF
        (lit) (lit) , , \ store address
        (lit) ! ,
ELSE
        !
    THEN
; IMMEDIATE
Counterpoison answered 20/2, 2013 at 22:26 Comment(0)
T
4

The simplest solution is

VARIABLE TO-MESSAGE   \ 0 : FROM ,  1 : TO .
0 TO-MESSAGE !        \ needed to initialise the variable
         
: TO 1 TO-MESSAGE ! ;

: VALUE CREATE , DOES> TO-MESSAGE @ IF ! ELSE @ THEN 
    0 TO-MESSAGE ! ; 

It uses only CORE words and is absolutely standard. And it just works in interpret and compile mode, because there is no fishy look ahead in the input stream.

Trevar answered 9/2, 2015 at 22:57 Comment(0)
C
2

Ok After a lot of trial and error as well as much searching I found something that should work, but because of two bugs in redFORTH, does not.

VALUE

\ Works fine, now to reset the value.
: VALUE \ n <name> --
    CREATE ,
    DOES> @
;

TO

\ Works if not compiling, LITERAL and POSTPONE are broken.
: TO
    TIBWORD FIND 3 +
    STATE @ IF
        POSTPONE LITERAL
        POSTPONE !
    ELSE
        !
    THEN
; IMMEDIATE

Demo of bug in LITERAL

\ fails, very wierd error.
: TESTLIT [ 42 ] LITERAL ;
\ TESTLIT Unknown Token: TESTLIT
\ FORGET TESTLIT Unknown Token: TESTLIT
\ WORDS TESTLIT COLD SORTMATCH ...

Demo of bug in POSTPONE

\ fails, postpone is directly equivelent to [']
: TESTPOST POSTPONE + ; IMMEDIATE
: TEST 2 2 TESTPOST . ;
\ . 1935
\ ' + . 1935

I'm off to file a bug report....

EDIT #1: After some more trial and error and not a little swearing (I'm not good with FORTH) I found a way to make it work.

: TO
    TIBWORD FIND 3 +
    STATE @ IF
        (lit) (lit) , , \ store address
        (lit) ! ,
ELSE
        !
    THEN
; IMMEDIATE
Counterpoison answered 20/2, 2013 at 22:26 Comment(0)
C
-1

I'm not sure how your Forth handles interpreting versus compile time, but the definition of TO is trying to store a value to address 3. Seems fishy.

Concentrate answered 20/2, 2013 at 20:1 Comment(4)
3 is the offset from the xt where the value created by CREATE is. I found this out via expermentation, too bad there is no word defined to get this offset in a portable way (in redFORTH, there is in other forths)Counterpoison
Excuse the double post, it took longer than 5 minutes to find the code formatting trick for comments. Backtick, shoulda guessed. That comment above should read... Milo, sorry, but the word order looks wrong. Try ' word 3 + ! What I see in your TO definition is; tick a word, add whatever else was on the stack at the time, store that value to address 3.Concentrate
Follow up on an other answer? That is where comments are for.Trevar
Down voted. This is not an answer, let alone an answer to the question.Trevar

© 2022 - 2024 — McMap. All rights reserved.