From f605e2d85c22ec6ac16b59230f132ee54f2400a7 Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Mon, 27 Apr 2020 20:44:21 -0400 Subject: [PATCH] Give CASE's default branch access to its input value ref #97 --- blk/042 | 14 +++++++------- blk/427 | 6 +++--- blk/428 | 6 ++---- emul/stage0.bin | Bin 7310 -> 7291 bytes tests/forth/test_flow.fs | 5 +++-- 5 files changed, 15 insertions(+), 16 deletions(-) diff --git a/blk/042 b/blk/042 index eb03a5a..862ebf3 100644 --- a/blk/042 +++ b/blk/042 @@ -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.) diff --git a/blk/427 b/blk/427 index 776e84e..5e66828 100644 --- a/blk/427 +++ b/blk/427 @@ -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 diff --git a/blk/428 b/blk/428 index cb1e5ad..4351798 100644 --- a/blk/428 +++ b/blk/428 @@ -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 diff --git a/emul/stage0.bin b/emul/stage0.bin index feb5f885a7719fd0b807fe1b4f4e04d36c0c2f07..f3e4f38a34ff4ee443c4211209554699d81cee70 100644 GIT binary patch delta 63 zcmeCP{B5ye1-GoVf~T*qtBa>&h^vB?g1?)Bv%hbEr;n?Gf0%2~>r?0Q8i>G6VtAdq+zncO~&J)hERfvWP#wvKa SO@7H8FnJQM#AZt#CjkK5m=}Kl diff --git a/tests/forth/test_flow.fs b/tests/forth/test_flow.fs index b67a9de..3cce5cb 100644 --- a/tests/forth/test_flow.fs +++ b/tests/forth/test_flow.fs @@ -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