forward.fs 3.05 KB
Newer Older
1 2
\ forward definitions

3
\ Authors: Bernd Paysan, Anton Ertl
Anton Ertl's avatar
Anton Ertl committed
4
\ Copyright (C) 2016,2017,2018,2019 Free Software Foundation, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

\ 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/.

21 22 23 24 25 26 27
\ This implementation relies on return address manipulation and
\ specific threaded-code properties

\ note that there are related words with the same name here and in
\ forward1.fs, but they behave differently.

s" unresolved forward definition" exception constant unresolved-forward
28
s" forward must be resolved with :" exception constant forward-needs-:
29 30 31 32 33 34 35 36 37 38 39 40

: unresolved-forward-error ( -- )
    unresolved-forward throw ;

: unfixed-forward ( ... -- ... )
    \ the compiled code for a forward child branches to this word,
    \ which patches the call to the forward word; if the word has not
    \ been resolved, this produces an error after patching the call to
    \ report an error.
    r@ cell- {: target :}
    target @ cell- @ dup >body target !
    execute-exit ;
41 42

: forward ( "name" -- )
43 44 45 46
    \g Defines a forward reference to a colon definition.  Defining a
    \g colon definition with the same name in the same wordlist
    \g resolves the forward references.  Use @code{.unresolved} to
    \g check whether any forwards are unresolved.
47 48 49
    defer ['] unresolved-forward-error lastxt defer!
    ['] branch peephole-compile, ['] unfixed-forward >body ,
    [: ['] call peephole-compile, >body cell+ , ;] set-optimizer ;
50

51 52 53
: is-forward? ( xt -- f )
    \ f is true if xt is an unresolved forward definition
    dup >code-address dodefer: = if
54
        >body @ ['] unresolved-forward-error = exit then
55
    drop false ;
56 57 58

: auto-resolve ( addr u wid -- )
    \G auto-resolve the forward reference in check-shadow
59
    dup 2over rot find-name-in dup if
60 61
	dup is-forward? if
	    latestxt >code-address docol: <> forward-needs-: and throw
62 63
            latestxt swap defer! 2drop drop exit then then
    drop defers check-shadow ;
64 65 66 67 68

' auto-resolve is check-shadow

: .unresolved ( -- )
    \G print all unresolved forward references
69
    [: [:   replace-sourceview >r dup name>view to replace-sourceview
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
	    dup is-forward? [: dup .name ." is unresolved" cr ;] ?warning
	    r> to replace-sourceview
            drop true ;] swap traverse-wordlist ;] map-vocs ;

\ testing
0 [if]
    forward forward1
    see forward1
    ' forward1 is-forward? cr .
    : forward2 forward1 ;
    ' forward2 5 cells dump
    ' forward2 catch cr .
    ' forward1 6 cells
    : forward1 285 ;
    dump
    forward2 .
    ' forward2 5 cells dump
    .s
[then]