[gforth] / gforth / kernel / require.fs  

gforth: gforth/kernel/require.fs


1 : anton 1.1 \ require.fs
2 :    
3 : anton 1.5 \ Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
4 : anton 1.1
5 :     \ This file is part of Gforth.
6 :    
7 :     \ Gforth is free software; you can redistribute it and/or
8 :     \ modify it under the terms of the GNU General Public License
9 :     \ as published by the Free Software Foundation; either version 2
10 :     \ of the License, or (at your option) any later version.
11 :    
12 :     \ This program is distributed in the hope that it will be useful,
13 :     \ but WITHOUT ANY WARRANTY; without even the implied warranty of
14 :     \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 :     \ GNU General Public License for more details.
16 :    
17 :     \ You should have received a copy of the GNU General Public License
18 :     \ along with this program; if not, write to the Free Software
19 :     \ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 :    
21 :     \ Now: Kernel Module, Reloadable
22 :    
23 :     create included-files 0 , 0 , ( pointer to and count of included files )
24 : jwilke 1.8 \ here ," ./the terminal" dup c@ swap 1 + swap , A, here 2 cells -
25 :     \ ./ is confusing for the search path stuff! There should be never a .
26 :     \ in sourcefilename....
27 :     here ," #terminal#" dup c@ swap 1 + swap , A, here 2 cells -
28 : anton 1.1 create image-included-files 1 , A, ( pointer to and count of included files )
29 :     \ included-files points to ALLOCATEd space, while image-included-files
30 :     \ points to ALLOTed objects, so it survives a save-system
31 :    
32 : crook 1.9 : loadfilename ( -- a-addr ) \ gforth
33 :     \G @i{a-addr} @code{2@@} produces the current file name ( @i{c-addr u} )
34 : anton 1.1 included-files 2@ loadfilename# @ min 2* cells + ;
35 :    
36 :     : sourcefilename ( -- c-addr u ) \ gforth
37 : crook 1.7 \G The name of the source file which is currently the input
38 : anton 1.1 \G source. The result is valid only while the file is being
39 :     \G loaded. If the current input source is no (stream) file, the
40 :     \G result is undefined.
41 :     loadfilename 2@ ;
42 :    
43 :     : sourceline# ( -- u ) \ gforth sourceline-number
44 : crook 1.7 \G The line number of the line that is currently being interpreted
45 : anton 1.1 \G from a (stream) file. The first line has the number 1. If the
46 : crook 1.7 \G current input source is not a (stream) file, the result is
47 : anton 1.1 \G undefined.
48 :     loadline @ ;
49 :    
50 : crook 1.7 : init-included-files ( -- ) \ gforth
51 :     \G Clear the list of earlier included files.
52 : anton 1.1 image-included-files 2@ 2* cells save-mem drop ( addr )
53 :     image-included-files 2@ nip included-files 2! ;
54 :    
55 :     : included? ( c-addr u -- f ) \ gforth
56 : crook 1.7 \G True only if the file @var{c-addr u} is in the list of earlier
57 :     \G included files. If the file has been loaded, it may have been
58 : crook 1.9 \G specified as, say, @file{foo.fs} and found somewhere on the
59 :     \G Forth search path. To return @code{true} from @code{included?},
60 :     \G you must specify the exact path to the file, even if that is
61 :     \G @file{./foo.fs}
62 : anton 1.1 included-files 2@ 0
63 :     ?do ( c-addr u addr )
64 :     dup >r 2@ 2over compare 0=
65 :     if
66 :     2drop rdrop unloop
67 :     true EXIT
68 :     then
69 :     r> cell+ cell+
70 :     loop
71 :     2drop drop false ;
72 :    
73 :     : add-included-file ( c-addr u -- ) \ gforth
74 :     \G add name c-addr u to included-files
75 :     included-files 2@ 2* cells 2 cells extend-mem
76 :     2/ cell / included-files 2!
77 :     2! ;
78 :    
79 :     : included1 ( i*x file-id c-addr u -- j*x ) \ gforth
80 : crook 1.7 \G Include the file file-id with the name given by @var{c-addr u}.
81 : anton 1.1 loadfilename# @ >r
82 :     save-mem add-included-file ( file-id )
83 :     included-files 2@ nip 1- loadfilename# !
84 :     ['] include-file catch
85 :     r> loadfilename# !
86 :     throw ;
87 :    
88 : crook 1.7 : included ( i*x c-addr u -- j*x ) \ file
89 : anton 1.4 \G @code{include-file} the file whose name is given by the string
90 : crook 1.7 \G @var{c-addr u}.
91 : jwilke 1.3 open-fpath-file throw included1 ;
92 : anton 1.1
93 :     : required ( i*x addr u -- j*x ) \ gforth
94 : crook 1.7 \G @code{include-file} the file with the name given by @var{addr
95 :     \G u}, if it is not @code{included} (or @code{required})
96 :     \G already. Currently this works by comparing the name of the file
97 :     \G (with path) against the names of earlier included files.
98 : anton 1.4 \ however, it may be better to fstat the file,
99 :     \ and compare the device and inode. The advantages would be: no
100 :     \ problems with several paths to the same file (e.g., due to
101 :     \ links) and we would catch files included with include-file and
102 :     \ write a require-file.
103 : jwilke 1.3 open-fpath-file throw 2dup included?
104 : anton 1.1 if
105 :     2drop close-file throw
106 :     else
107 :     included1
108 :     then ;
109 :    
110 :     \ INCLUDE 9may93jaw
111 :    
112 : anton 1.4 : include ( ... "file" -- ... ) \ gforth
113 : crook 1.7 \G @code{include-file} the file @var{file}.
114 :     name included ;
115 : anton 1.1
116 : anton 1.4 : require ( ... "file" -- ... ) \ gforth
117 : crook 1.7 \G @code{include-file} @var{file} only if it is not included already.
118 :     name required ;
119 : anton 1.1
120 :     0 [IF]
121 :     : \I
122 :     here
123 :     0 word count
124 :     string,
125 :     needsrcs^ @ ! ;
126 :    
127 :     : .modules
128 :     cr
129 :     needs^ @
130 :     BEGIN dup
131 :     WHILE dup cell+ count type cr
132 :     5 spaces
133 :     dup cell+ count + aligned
134 :     @ dup IF count type ELSE drop THEN cr
135 :     @
136 :     REPEAT
137 :     drop ;
138 :    
139 :     : loadfilename#>str ( n -- adr len )
140 :     \ this converts the filenumber into the string
141 :     loadfilenamecount @ swap -
142 :     needs^ @
143 :     swap 0 ?DO dup 0= IF LEAVE THEN @ LOOP
144 :     dup IF cell+ count ELSE drop s" NOT FOUND" THEN ;
145 :     [THEN]
146 :    
147 :     : loadfilename#>str ( n -- adr len )
148 :     included-files 2@ drop swap 2* cells + 2@ ;
149 :    
150 :     : .modules
151 :     included-files 2@ 2* cells bounds ?DO
152 :     cr I 2@ type 2 cells +LOOP ;
153 :    
154 :     \ contains tools/newrequire.fs
155 : crook 1.9 \ \I $Id: require.fs,v 1.8 1999/05/17 14:55:50 jwilke Exp $
156 : anton 1.1

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help