Skip to content

Commit

Permalink
Forth: make DO LOOP +LOOP precompiled words
Browse files Browse the repository at this point in the history
and hide *DO and *LOOP (noheader words)
  • Loading branch information
adumont committed Jan 1, 2022
1 parent 56c95db commit f44b960
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions forth.s
Original file line number Diff line number Diff line change
Expand Up @@ -1445,7 +1445,14 @@ defword "LITSTR",,
@skip:
JMP DEX2_NEXT

defword "STAR_DO","*DO",
defword "DO",,IMMEDIATE_FLAG
; : DO LIT, *DO HERE ; IMMEDIATE
JMP do_COLON
.ADDR do_COMPILE, do_STAR_DO
.ADDR do_HERE
.ADDR do_SEMI

noheader "STAR_DO"
; ( end start -- )
; Used by DO
; We don't use >R as it would mean being a colon word, and that would
Expand All @@ -1467,7 +1474,7 @@ defword "STAR_DO","*DO",
; drop end
JMP do_DROP

defword "STAR_LOOP","*LOOP",
noheader "STAR_LOOP"
; ( INC -- )
; Used by LOOP, +LOOP, takes the increment on the stack

Expand Down Expand Up @@ -1541,6 +1548,21 @@ defword "STAR_LOOP","*LOOP",
; now we call JUMP
JMP do_JUMP

defword "LOOP",,IMMEDIATE_FLAG
; : LOOP LIT, 1 LIT, *LOOP , ; IMMEDIATE
JMP do_COLON
.ADDR do_COMPILE, do_PUSH1
.ADDR do_JUMP, call_from_do_LOOP ; we fallback into +LOOP
; it's a small penalty at compile time, but code is more compact in ROM

defword "PLUS_LOOP","+LOOP",IMMEDIATE_FLAG
; : +LOOP LIT, *LOOP , ; IMMEDIATE " ;
JMP do_COLON
call_from_do_LOOP:
.ADDR do_COMPILE, do_STAR_LOOP
.ADDR do_COMMA
.ADDR do_SEMI

defword "LEAVE",,
; ( -- )
; Leaves out of a DO-LOOP on the next *LOOP
Expand Down Expand Up @@ -2602,9 +2624,9 @@ BOOT_PRG:
; .BYTE " : T 6 BEGIN 1 - DUP WHILE DUP . REPEAT DROP ; T "

; DO LOOP
.BYTE " : DO LIT, *DO HERE ; IMMEDIATE " ;
.BYTE " : LOOP LIT, 1 LIT, *LOOP , ; IMMEDIATE " ;
.BYTE " : +LOOP LIT, *LOOP , ; IMMEDIATE " ;
; .BYTE " : DO LIT, *DO HERE ; IMMEDIATE " ;
; .BYTE " : LOOP LIT, 1 LIT, *LOOP , ; IMMEDIATE " ;
; .BYTE " : +LOOP LIT, *LOOP , ; IMMEDIATE " ;
.BYTE " : LEAVE R> DROP R@ >R ; " ;

; Test DO-LOOP
Expand Down

0 comments on commit f44b960

Please sign in to comment.