iloops.fs 2.7 KB
Newer Older
1 2
\ multi-line loops in the interpeter

3
\ Author: Bernd Paysan
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
\ Copyright (C) 2018 Free Software Foundation, Inc.

\ This file is part of Gforth.

\ Gforth is free software; you can redistribute it and/or
\ modify it under the terms of the GNU General Public License
\ as published by the Free Software Foundation, either version 3
\ of the License, or (at your option) any later version.

\ This program is distributed in the hope that it will be useful,
\ but WITHOUT ANY WARRANTY; without even the implied warranty of
\ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
\ GNU General Public License for more details.

\ You should have received a copy of the GNU General Public License
\ along with this program. If not, see http://www.gnu.org/licenses/.
\ Structs for interpreter                              28nov92py

require string.fs

[IFUNDEF] (i)
    user (i)
[THEN]

$10 stack: input-stack
$20 stack: loop-indices

: >input ( -- )
    { | w^ in } save-input in set-stack in @ input-stack >stack ;
: input-drop ( -- )
    input-stack stack> ?dup-IF  { w^ in } in $free  THEN ;
: input< ( -- )
    input-stack $[]# ?dup-IF
	1- input-stack $[] get-stack restore-input  throw
    THEN ;

: [DO]  ( n-limit n-index -- ) \ gforth bracket-do
    (i) @ loop-indices >stack (i) ! loop-indices >stack >input
; immediate

: [?DO] ( n-limit n-index -- ) \ gforth bracket-question-do
    2dup = IF 2drop postpone [ELSE] ELSE postpone [DO] THEN ;
                                                      immediate

: [+LOOP] ( n -- ) \ gforth bracket-question-plus-loop
    loop-indices stack> >r (i) @ >r
    AHEAD  DO
	input<  r> (i) ! r> loop-indices >stack  EXIT
    BUT  THEN  +LOOP
loop-indices stack> (i) !  input-drop
;                                immediate

: [LOOP] ( -- ) \ gforth bracket-loop
  1 postpone [+LOOP] ;                                 immediate

: [FOR] ( n -- ) \ gforth bracket-for
  0 swap postpone [DO] ;                              immediate

: [NEXT] ( n -- ) \ gforth bracket-next
  -1 postpone [+LOOP] ;                               immediate

: [I] ( -- n ) \ gforth bracket-i
    (i) @ ;
comp: drop (i) @ postpone Literal ;

: [BEGIN] ( -- ) \ gforth bracket-begin
    >input ;                                          immediate

: [UNTIL] ( flag -- ) \ gforth bracket-until
    IF  input-drop  ELSE  input<  THEN ;
                                                      immediate

: [REPEAT]  ( -- ) \ gforth bracket-repeat
    input< ;                                          immediate

' [REPEAT] Alias [AGAIN] ( -- ) \ gforth bracket-again
                                                      immediate

: [WHILE]   ( flag -- ) \ gforth bracket-while
    0= IF   postpone [ELSE] 1 countif +!  THEN ;      immediate