Search code examples
forth6502

How to define VALUE and TO


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.


Solution

  • 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