From add3b6629bce008feab69da236fdb1f42c11583c Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Tue, 14 Apr 2020 14:59:01 -0400 Subject: [PATCH] Make DO .. LOOP binary code more compact Only a few bytes saved in forth1.bin, but the DO .. LOOP construct isn't used much yet. It's still significant savings per LOOP call. --- forth/core.fs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/forth/core.fs b/forth/core.fs index a9b44a9..bdf4012 100644 --- a/forth/core.fs +++ b/forth/core.fs @@ -94,12 +94,20 @@ H@ ; IMMEDIATE +( Increase loop counter and returns whether we should loop. ) +: _ + R> ( IP, keep for later ) + R> 1 + ( ip i+1 ) + DUP >R ( ip i ) + I' = ( ip f ) + SWAP >R ( f ) +; + ( One could think that we should have a sub word to avoid all these COMPILE, but we can't because otherwise it messes with the RS ) : LOOP - COMPILE R> 1 LITN COMPILE + COMPILE DUP COMPILE >R - COMPILE I' COMPILE = COMPILE (?br) + COMPILE _ COMPILE (?br) H@ - , COMPILE R> COMPILE DROP COMPILE R> COMPILE DROP ; IMMEDIATE