Parcourir la source

Give CASE's default branch access to its input value

ref #97
pull/102/head
Virgil Dupras il y a 4 ans
Parent
révision
f605e2d85c
5 fichiers modifiés avec 15 ajouts et 16 suppressions
  1. +7
    -7
      blk/042
  2. +3
    -3
      blk/427
  3. +2
    -4
      blk/428
  4. BIN
      emul/stage0.bin
  5. +3
    -2
      tests/forth/test_flow.fs

+ 7
- 7
blk/042 Voir le fichier

@@ -1,9 +1,9 @@
Flow

Note about flow words: flow words can only be used in
definitions. In the INTERPRET loop, they don't have the desired
effect because each word from the input stream is executed
immediately. In this context, branching doesn't work.
Note that flow words can only be used in definitions. In the
INTERPRET loop, they don't have the desired effect because each
word from the input stream is executed immediately. In this
context, branching doesn't work.

f IF A ELSE B THEN: if f is true, execute A, if false, execute
B. ELSE is optional.
@@ -11,6 +11,6 @@ BEGIN .. f UNTIL: if f is false, branch to BEGIN.
BEGIN .. AGAIN: Always branch to BEGIN.
x y DO .. LOOP: LOOP increments y. if y != x, branch to DO.
x CASE y OF A ENDOF z OF B ENDOF C ENDCASE: If x == y, execute
A, if x == z, execute B. Otherwise, execute C.
(cont.)
A, if x == z, execute B. Otherwise, execute C. x is dropped
in case of an OF match, *but it is kept if it reaches C*. You
have to consume it to avoid PSP leak. (cont.)

+ 3
- 3
blk/427 Voir le fichier

@@ -1,10 +1,10 @@
( During a CASE, the stack grows by 1 at each ENDOF so that
we can fill all those ENDOF branching addrs. So that we
know when to stop, we put a 0 on PSP. That's our stopgap. )
: CASE 0 COMPILE >R ; IMMEDIATE
: CASE 0 ; IMMEDIATE
: OF
COMPILE I COMPILE =
[COMPILE] IF
COMPILE OVER COMPILE =
[COMPILE] IF COMPILE DROP
; IMMEDIATE
: ENDOF [COMPILE] ELSE ; IMMEDIATE


+ 2
- 4
blk/428 Voir le fichier

@@ -1,11 +1,9 @@
( At this point, we have something like "0 e1 e2 e3 val". We
want top drop val, and then call THEN as long as we don't
want to drop val, and then call THEN as long as we don't
hit 0. )
: ENDCASE
BEGIN
DUP NOT IF
DROP COMPILE R> COMPILE DROP EXIT
THEN
DUP NOT IF DROP EXIT THEN
[COMPILE] THEN
AGAIN
; IMMEDIATE


BIN
emul/stage0.bin Voir le fichier


+ 3
- 2
tests/forth/test_flow.fs Voir le fichier

@@ -3,11 +3,12 @@
'X' OF 42 ENDOF
0x12 OF 43 ENDOF
255 OF 44 ENDOF
45
1+
ENDCASE
;

'X' foo 42 #eq
0x12 foo 43 #eq
255 foo 44 #eq
254 foo 45 #eq
254 foo 255 #eq
'S S0 #eq

Chargement…
Annuler
Enregistrer