1
0
mirror of https://github.com/hsoft/collapseos.git synced 2024-11-27 09:48:05 +11:00

Give CASE's default branch access to its input value

ref #97
This commit is contained in:
Virgil Dupras 2020-04-27 20:44:21 -04:00
parent 3b66aefcd1
commit f605e2d85c
5 changed files with 15 additions and 16 deletions

14
blk/042
View File

@ -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.)

View File

@ -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

View File

@ -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

Binary file not shown.

View File

@ -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