DPANSA15.HTM (7432B)
1 <HTML><HEAD> 2 <TITLE>DPANS94</TITLE> 3 <link disabled rel="stylesheet" href="mpexc6.css"> 4 <style>@import url(mpexc6.css);</style> 5 </head> 6 7 <BODY> 8 <table width=100%> 9 <tr> 10 <td align=left> 11 <a href=dpansa14.htm><img src=left.gif 12 width=26 height=26 align=ALIGN border=0></a> 13 <a href=dpansa16.htm><img src=right.gif 14 width=26 height=26 align=ALIGN border=0></a> 15 </td> 16 <td align=right> 17 <a href=dpans.htm#toc><img src=up.gif 18 width=26 height=26 align=ALIGN border=0></a> 19 <a name=A.15>Table of Contents</a> 20 </td> 21 </tr> 22 </table> 23 <p> 24 <hr size=4> 25 26 <H2>A.15 The optional Programming-Tools word set</H2> 27 28 These words have been in widespread common use since the earliest Forth 29 systems. 30 31 <P> 32 33 Although there are environmental dependencies intrinsic to programs 34 using an assembler, virtually all Forth systems provide such a 35 capability. Insofar as many Forth programs are intended for real-time 36 applications and are intrinsically non-portable for this reason, the 37 Technical Committee believes that providing a standard window into 38 assemblers is a useful contribution to Forth programmers. 39 40 <P> 41 42 Similarly, the programming aids 43 <a href=dpans15.htm#15.6.1.1280>DUMP</a>, 44 etc., are valuable tools even 45 though their specific formats will differ between CPUs and Forth 46 implementations. These words are primarily intended for use by the 47 programmer, and are rarely invoked in programs. 48 49 <P> 50 51 One of the original aims of Forth was to erase the boundary between 52 <B>user</B> and <B>programmer</B> - to give all possible power to anyone 53 who had occasion to use a computer. Nothing in the above labeling or 54 remarks should be construed to mean that this goal has been abandoned. 55 56 <P> 57 58 <hr> 59 <A name=A.15.6> 60 <H3>A.15.6 Glossary</H3> 61 </a> 62 63 64 65 <hr> 66 <A name=A.15.6.1.0220>A.15.6.1.0220 .S</A> 67 <P> 68 69 .S is a debugging convenience found on almost all Forth systems. It is 70 universally mentioned in Forth texts. 71 72 <P> 73 74 <hr> 75 <A name=A.15.6.1.2194>A.15.6.1.2194 SEE</A> 76 <P> 77 78 SEE acts as an on-line form of documentation of words, allowing modification 79 of words by decompiling and regenerating with appropriate changes. 80 81 <P> 82 83 <hr> 84 <A name=A.15.6.1.2465>A.15.6.1.2465 WORDS</A> 85 <P> 86 87 WORDS is a debugging convenience found on almost all Forth systems. It is 88 universally referred to in Forth texts. 89 90 <P> 91 92 <hr> 93 <A name=A.15.6.2.0470>A.15.6.2.0470 ;CODE</A> 94 <P> 95 96 Typical use: <code>: namex ... <create> ... ;CODE ...</code> 97 98 <P> 99 100 where namex is a defining word, and <create> is 101 <a href=dpans6.htm#6.1.1000>CREATE</a> or any user 102 defined 103 word that calls CREATE. 104 105 <P> 106 107 <hr> 108 <A name=A.15.6.2.0930>A.15.6.2.0930 CODE</A> 109 <P> 110 111 Some Forth systems implement the assembly function by adding an 112 <a href=dpans15.htm#15.6.2.0740>ASSEMBLER</a> 113 word list to the search order, using the text interpreter to 114 parse a postfix assembly language with lexical characteristics similar 115 to Forth source code. Typically, in such systems, assembly ends when a 116 word END-CODE is interpreted. 117 118 <P> 119 120 <hr> 121 <A name=A.15.6.2.1015>A.15.6.2.1015 CS-PICK</A> 122 <P> 123 124 The intent is to reiterate a dest on the control-flow stack so that it can be 125 resolved more than once. For example: 126 127 <PRE> 128 \ Conditionally transfer control to beginning of loop 129 \ This is similar in spirit to C's "continue" statement. 130 131 : ?REPEAT ( dest -- dest ) \ Compilation 132 ( flag -- ) \ Execution 133 0 CS-PICK POSTPONE UNTIL 134 ; IMMEDIATE 135 136 : XX ( -- ) \ Example use of ?REPEAT 137 BEGIN 138 ... 139 flag ?REPEAT ( Go back to BEGIN if flag is false ) 140 ... 141 flag ?REPEAT ( Go back to BEGIN if flag is false ) 142 ... 143 flag UNTIL ( Go back to BEGIN if flag is false ) 144 ... 145 ; 146 </PRE> 147 148 <P> 149 150 <hr> 151 <A name=A.15.6.2.1020>A.15.6.2.1020 CS-ROLL</A> 152 <P> 153 154 The intent is to modify the order in which the origs and dests on the 155 control-flow stack are to be resolved by subsequent control-flow words. For 156 example, 157 <a href=dpans6.htm#6.1.2430>WHILE</a> could be implemented in terms of 158 <a href=dpans6.htm#6.1.1700>IF</a> and CS-ROLL, as follows: 159 160 <PRE> 161 : WHILE ( dest -- orig dest ) POSTPONE IF 1 CS-ROLL ; IMMEDIATE 162 </PRE> 163 <P> 164 165 <hr> 166 <A name=A.15.6.2.1580>A.15.6.2.1580 FORGET</A> 167 <P> 168 169 Typical use: <code>... FORGET name ...</code> 170 171 <P> 172 173 FORGET assumes that all the information needed to restore the dictionary to 174 its previous state is inferable somehow from the forgotten word. While this 175 may be true in simple linear dictionary models, it is difficult to implement 176 in other Forth systems; e.g., those with multiple address spaces. For 177 example, if Forth is embedded in ROM, how does FORGET know how much RAM to 178 recover when an array is forgotten? A general and preferred solution is 179 provided by 180 <a href=dpans6.htm#6.2.1850>MARKER</a>. 181 182 <P> 183 184 <hr> 185 <A name=A.15.6.2.2531>A.15.6.2.2531 [ELSE]</A> 186 <P> 187 188 Typical use: <code>... flag [IF] ... [ELSE] ... [THEN] ...</code> 189 190 <P> 191 192 <hr> 193 <A name=A.15.6.2.2532>A.15.6.2.2532 [IF]</A> 194 <P> 195 196 Typical use: <code>... flag [IF] ... [ELSE] ... [THEN] ...</code> 197 198 <P> 199 200 <hr> 201 <A name=A.15.6.2.2533>A.15.6.2.2533 [THEN]</A> 202 <P> 203 204 Typical use: <code>... flag [IF] ... [ELSE] ... [THEN] ...</code> 205 206 <P> 207 208 Software that runs in several system environments often contains some 209 source code that is environmentally dependent. Conditional compilation - the 210 selective inclusion or exclusion of portions of the source code at 211 compile time - is one technique that is often used to assist in the 212 maintenance of such source code. 213 <P> 214 215 Conditional compilation is sometimes done with <B>smart comments</B> - 216 definitions that either skip or do not skip the remainder of the line 217 based on some test. For example: 218 219 <PRE> 220 \ If 16-Bit? contains TRUE, lines preceded by 16BIT\ 221 \ will be skipped. Otherwise, they will not be skipped. 222 223 VARIABLE 16-BIT? 224 225 : 16BIT\ ( -- ) 16-BIT? @ IF POSTPONE \ THEN ; IMMEDIATE 226 </PRE> 227 228 <P> 229 230 This technique works on a line by line basis, and is good for short, 231 isolated variant code sequences. 232 233 <P> 234 235 More complicated conditional compilation problems suggest a nestable 236 method that can encompass more than one source line at a time. The 237 words included in the ANS Forth optional Programming tools extensions 238 word set are useful for this purpose. The implementation given below 239 works with any input source (keyboard, 240 <a href=dpans6.htm#6.1.1360>EVALUATE</a>, 241 <a href=dpans7.htm#7.6.1.0800>BLOCK</a>, or text file). 242 243 <PRE> 244 : [ELSE] ( -- ) 245 1 BEGIN \ level 246 BEGIN 247 BL WORD COUNT DUP WHILE \ level adr len 248 2DUP S" [IF]" COMPARE 0= 249 IF \ level adr len 250 2DROP 1+ \ level' 251 ELSE \ level adr len 252 2DUP S" [ELSE]" 253 COMPARE 0= IF \ level adr len 254 2DROP 1- DUP IF 1+ THEN \ level' 255 ELSE \ level adr len 256 S" [THEN]" COMPARE 0= IF \ level 257 1- \ level' 258 THEN 259 THEN 260 THEN ?DUP 0= IF EXIT THEN \ level' 261 REPEAT 2DROP \ level 262 REFILL 0= UNTIL \ level 263 DROP 264 ; IMMEDIATE 265 266 : [IF] ( flag -- ) 267 0= IF POSTPONE [ELSE] THEN ; IMMEDIATE 268 269 : [THEN] ( -- ) ; IMMEDIATE 270 </PRE> 271 272 <P> 273 274 <hr> 275 <A href=dpans.htm#toc><IMG src="up.gif" ></A> Table of Contents 276 <BR> 277 <A href=dpansa16.htm><IMG src="right.gif" ></A> 278 Next Section 279 <P> 280 </BODY> 281 </HTML>