X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=blobdiff_plain;f=src%2Flib_2_control.4th;h=e565d6bf3d574bb9d72be82eddae9314c77ffa00;hb=f2ccca0d0ef6156118d6f3412671013efd9bc6b6;hp=464a9a4a6588886befd043477fd508be6826d7c6;hpb=9da6dc7c0252fdc18aca602928feab518737cc8f;p=forth.jl.git diff --git a/src/lib_2_control.4th b/src/lib_2_control.4th index 464a9a4..e565d6b 100644 --- a/src/lib_2_control.4th +++ b/src/lib_2_control.4th @@ -1,11 +1,18 @@ \ Flow control +\ ... if/unless ... [else ...] then + : IF IMMEDIATE ['] 0BRANCH , \ compile 0BRANCH HERE \ save location of the offset on the stack 0 , \ compile a dummy offset ; +: UNLESS IMMEDIATE + ['] NOT , \ compile NOT (to reverse the test) + [COMPILE] IF \ continue by calling the normal IF +; + : THEN IMMEDIATE DUP HERE SWAP - \ calculate the offset from the address saved on the stack @@ -22,6 +29,9 @@ SWAP ! ; + +\ begin ... while ... repeat, begin ... until, begin ... repeat + : BEGIN IMMEDIATE HERE \ save location on the stack ; @@ -53,10 +63,8 @@ SWAP ! \ and back-fill it in the original location ; -: UNLESS IMMEDIATE - ['] NOT , \ compile NOT (to reverse the test) - [COMPILE] IF \ continue by calling the normal IF -; + +\ [?]do ... [+]loop : DO IMMEDIATE ['] LIT , -1 , [COMPILE] IF @@ -76,6 +84,8 @@ : J RSP@ 6 - @ ; +: K RSP@ 9 - @ ; + : ?LEAVE IMMEDIATE ['] 0BRANCH , 13 , ['] R> , ['] RDROP , ['] RDROP , @@ -89,6 +99,11 @@ [COMPILE] ?LEAVE ; +\ Clean up return stack +: UNLOOP IMMEDIATE + ['] RDROP , ['] RDROP , ['] RDROP , +; + : +LOOP IMMEDIATE ['] DUP , \ Store copy of increment @@ -123,7 +138,7 @@ ; -\ CASE ------------------------------------------------------------------------ +\ case [of ... endof]+ ... endcase : CASE IMMEDIATE 0 \ push 0 to mark the bottom of the stack