Skip to content

Commit

Permalink
#430 Forth Standard wirds VALUE TO STATE, CREATE VARIABLE refactored …
Browse files Browse the repository at this point in the history
…w/ ENTRY, IS updated
  • Loading branch information
TG9541 committed Jun 3, 2021
1 parent 25c89b1 commit 5e484b5
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 26 deletions.
27 changes: 23 additions & 4 deletions docs/words.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@
; Pop return stack to data stack.
```

```
; doVarPtr ( -- a ) ( TOS STM8: -- Y,Z,N )
```

```
; doVAR ( -- a ) ( TOS STM8: -- Y,Z,N )
; Code for VARIABLE and CREATE.
Expand Down Expand Up @@ -274,7 +278,7 @@

```
; BL ( -- 32 ) ( TOS STM8: -- Y,Z,N )
; Return 32, blank character.
; Return blank character.
```

```
Expand Down Expand Up @@ -841,9 +845,13 @@
```

```
; COMPILE? ( -- n )
; COMPILE? ( -- ) ( TOS STM8: - Y,Z,N )
; 0 if 'EVAL points to $INTERPRETER
```

```
; STATE? ( -- f )
; 0 if 'EVAL points to $INTERPRETER
; GENALIAS COMPIQ "COMPILE?"
```

```
Expand Down Expand Up @@ -1092,8 +1100,14 @@
```

```
; dodoes ( -- )
; dodoes ( -- )
; link action to words created by defining words
; CREATE which must be the first word in a defining word
```

```
; thisvar ( -- a )
; get the next address of caller, return to the caller's caller
```

```
Expand All @@ -1108,6 +1122,11 @@
; GENALIAS YAT "Y@"
```

```
; ENTRY ( -- ; <string> )
; Compile a new dictionary entry with empty code field
```

```
; CREATE ( -- ; <string> )
; Compile a new array
Expand Down
46 changes: 31 additions & 15 deletions forth.asm
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,9 @@ RFROM:


.ifne HAS_CPNVM
; doVARPTR ( - a ) ( TOS STM8: - Y,Z,N )
; doVarPtr ( -- a ) ( TOS STM8: -- Y,Z,N )

HEADFLG DOVARPTR "doVarPtr" COMPO
DOVARPTR:
POPW Y ; get return addr (pfa)
JP YAT
Expand Down Expand Up @@ -2989,14 +2991,23 @@ CR:
DoLitC LF
JP [USREMIT]

; COMPILE? ( -- n )
; COMPILE? ( -- ) ( TOS STM8: - Y,Z,N )
; 0 if 'EVAL points to $INTERPRETER

; GENALIAS COMPIQ "COMPILE?"
COMPIQ:
LDW Y,USREVAL
SUBW Y,#INTER
RET

; STATE? ( -- f )
; 0 if 'EVAL points to $INTERPRETER

; GENALIAS STATEQ "STATE?"
STATEQ:
CALLR COMPIQ
JP YSTOR

; .OK ( -- )
; Display 'ok' while interpreting.

Expand Down Expand Up @@ -3657,14 +3668,21 @@ YAT:
LDW Y,(Y)
JP YSTOR

; ENTRY ( -- ; <string> )
; Compile a new dictionary entry with empty code field

; GENALIAS ENTRY "ENTRY"
ENTRY:
CALL TOKSNAME ; copy token to dictionary
JP OVERT

; CREATE ( -- ; <string> )
; Compile a new array
; without allocating space.

HEADER CREAT "CREATE"
CREAT:
CALL TOKSNAME ; copy token to dictionary
CALL OVERT
CALLR ENTRY
ComLit DOVAR
RET

Expand All @@ -3674,17 +3692,17 @@ CREAT:

HEADER CONST "CONSTANT"
CONST:
CALL COLON
CALLR ENTRY
ComLit DOCON ; compile action code
CALL COMMA ; compile constant
CALL LBRAC
CALL OVERT
; CALL LBRAC
; CALL OVERT
JRA IMMED ; make immediate

; docon ( -- )
; state dependent action code of constant

HEADER DOCON "docon" ; NOALIAS
HEADER DOCON "docon"
DOCON:
POPW Y
CALLR YAT ; R> AT push constant in interpreter mode
Expand All @@ -3701,24 +3719,22 @@ DOCON:

HEADER VARIA "VARIABLE"
VARIA:
CALLR CREAT
CALL ZERO
.ifne HAS_CPNVM
TNZ USRCP
JRPL 1$ ; NVM: allocate space in RAM
DoLitW DOVARPTR ; overwrite call address "DOVAR" with "DOVARPTR"
CALL HERE
CALL CELLM
CALL STORE
CALLR ENTRY
ComLit DOVARPTR
LDW Y,USRVAR
LDW (X),Y ; overwrite ZERO with RAM address for COMMA
DoLitC 2 ; Allocate space for variable in RAM
CALLR ALLOT
JRA 2$
.endif
1$: JP COMMA
1$: CALLR CREAT
2$: JP COMMA
.endif


.ifne HAS_VARIABLE
; ALLOT ( n -- )
; Allocate n bytes to code DICTIONARY.
Expand Down
1 change: 1 addition & 0 deletions inc/defconf.inc
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
UNLINK_CSTOR = 0 ; "C!"
UNLINK_IGET = 0 ; "I"
UNLINK_RFROM = 0 ; "R>"
UNLINK_DOVARPTR = 1 ; "doVarPtr"
UNLINK_DOVAR = 1 ; "doVar"
UNLINK_RAT = 0 ; "R@"
UNLINK_TOR = 0 ; ">R"
Expand Down
11 changes: 4 additions & 7 deletions lib/DEFER
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
#require WIPE
#require :NVM
#require ALIAS
#require COMPILE?

\ STATE ( -- f ) return 0 when in interpreter state
:NVM COMPILE? [ $5A5A , $FF C, ] ;RAM ALIAS STATE
#require STATE?

\ NOOP ( -- ) do nothing
\ DEFER! ( xt1 xt2 -- ) write xt1 at jump address in deferred word xt2
Expand All @@ -19,10 +16,10 @@ NVM
\ use NOP for masking the JP opcode (no ALIAS), initialize with NOOP
: ( NOP JP ) $9DCC , ['] NOOP , POSTPONE [ OVERT ;

: IS ( xt "<space>name" -- ) STATE IF
POSTPONE ['] POSTPONE DEFER!
: IS ( xt "<space>name" -- ) STATE? IF
POSTPONE ['] POSTPONE DEFER! \ Compiler state
ELSE
' DEFER!
' DEFER! \ Interpreter state
THEN ; IMMEDIATE
RAM WIPE

Expand Down
10 changes: 10 additions & 0 deletions lib/STATE
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
\ The Forth-Standard requires a STATE "variable" that is
\ "true when in compilation state and false otherwise"
\ (but "A program shall not directly alter the contents of STATE").

#require STATE?

VARIABLE stateflag \ this implementation is a kludge

: STATE ( -- a-addr )
stateflag STATE? IF -1 ELSE 0 THEN OVER ! ;
46 changes: 46 additions & 0 deletions lib/VALUE
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
\ VALUE ... TO for STM8 eForth TG9541-210603
\ Note: this implementation works according to Forth-Standard: it assumes that
\ the dictionary is writable, which, in NVM, may or may not be the case
\ ------------------------------------------------------------------------------
#require WIPE
#require :NVM
#require ALIAS
#require ENTRY
#require doVarPtr
#require STATE?

\ value! ( xt1 xt2 -- ) write xt1 at body of word at xt2
:NVM 3 + ! ;RAM ALIAS value!

NVM
#require [']

: VALUE ( x "<spaces>name" -- )
ENTRY POSTPONE doVarPtr , ;

: TO ( i * x "<spaces>name" -- ) ( "<spaces>name" -- )
STATE? IF
POSTPONE ['] POSTPONE value! \ Compiler state
ELSE
' value! \ Interpreter state
THEN ; IMMEDIATE
RAM WIPE

\\ Test from forth-standard.org)

#include utils/tester.fs
T{ 111 VALUE v1 -> }T
T{ -999 VALUE v2 -> }T
T{ v1 -> 111 }T
T{ v2 -> -999 }T
T{ 222 TO v1 -> }T
T{ v1 -> 222 }T

T{ : vd1 v1 ; -> }T
T{ vd1 -> 222 }T

T{ : vd2 TO v2 ; -> }T
T{ v2 -> -999 }T
T{ -333 vd2 -> }T
T{ v2 -> -333 }T
T{ v1 -> 222 }T

0 comments on commit 5e484b5

Please sign in to comment.