Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | \ ========== Copyright Header Begin ========================================== |
2 | \ | |
3 | \ Hypervisor Software File: interp.fth | |
4 | \ | |
5 | \ Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. | |
6 | \ | |
7 | \ - Do no alter or remove copyright notices | |
8 | \ | |
9 | \ - Redistribution and use of this software in source and binary forms, with | |
10 | \ or without modification, are permitted provided that the following | |
11 | \ conditions are met: | |
12 | \ | |
13 | \ - Redistribution of source code must retain the above copyright notice, | |
14 | \ this list of conditions and the following disclaimer. | |
15 | \ | |
16 | \ - Redistribution in binary form must reproduce the above copyright notice, | |
17 | \ this list of conditions and the following disclaimer in the | |
18 | \ documentation and/or other materials provided with the distribution. | |
19 | \ | |
20 | \ Neither the name of Sun Microsystems, Inc. or the names of contributors | |
21 | \ may be used to endorse or promote products derived from this software | |
22 | \ without specific prior written permission. | |
23 | \ | |
24 | \ This software is provided "AS IS," without a warranty of any kind. | |
25 | \ ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, | |
26 | \ INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A | |
27 | \ PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN | |
28 | \ MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR | |
29 | \ ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR | |
30 | \ DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN | |
31 | \ OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR | |
32 | \ FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE | |
33 | \ DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, | |
34 | \ ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF | |
35 | \ SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. | |
36 | \ | |
37 | \ You acknowledge that this software is not designed, licensed or | |
38 | \ intended for use in the design, construction, operation or maintenance of | |
39 | \ any nuclear facility. | |
40 | \ | |
41 | \ ========== Copyright Header End ============================================ | |
42 | id: @(#)interp.fth 2.19 03/12/08 13:22:06 | |
43 | purpose: | |
44 | copyright: Copyright 1990-2003 Sun Microsystems, Inc. All Rights Reserved | |
45 | copyright: Copyright 1985-1990 Bradley Forthware | |
46 | copyright: Use is subject to license terms. | |
47 | ||
48 | \ The Text Interpreter | |
49 | ||
50 | \ Input stream parsing | |
51 | ||
52 | \ Error reporting | |
53 | defer mark-error ' noop is mark-error | |
54 | defer show-error ' noop is show-error | |
55 | : where ( -- ) mark-error show-error ; | |
56 | ||
57 | : lose ( -- ) true ( -13) abort" Undefined word encountered " ; | |
58 | ||
59 | \ Number parsing | |
60 | hex | |
61 | : >number ( ud1 c-addr1 u1 -- ud2 c-addr2 u2 ) | |
62 | \ convert double number, leaving address of first unconverted byte | |
63 | begin dup while ( ud adr len ) | |
64 | over c@ base @ digit ( ud adr len digit true | char false ) | |
65 | 0= if drop exit then ( ud adr len digit ) | |
66 | >r 2swap r> ( adr len ud digit ) | |
67 | swap base @ um* drop ( adr len ud.low digit ud.high' ) | |
68 | rot base @ um* d+ ( adr len ud' ) | |
69 | 2swap 1 /string ( ud' adr len ) | |
70 | repeat ( ud' adr len ) | |
71 | ; | |
72 | : numdelim? ( char -- flag ) dup ascii . = swap ascii , = or ; | |
73 | : $dnumber? ( adr len -- [ n .. ] #cells ) | |
74 | dup 0= if ( adr 0 ) nip exit then | |
75 | 0 0 2swap ( ud $ ) | |
76 | over c@ ascii - = ( ud $ neg? ) | |
77 | dup >r negate /string ( ud $' ) ( r: neg? ) | |
78 | ||
79 | \ Convert groups of digits possibly separated by periods or commas | |
80 | begin >number dup 1 > while ( ud' $' ) | |
81 | over c@ numdelim? 0= if ( ud' $' ) | |
82 | 2drop r> 3drop 0 exit ( ud' $' ) | |
83 | then ( ud' $' ) | |
84 | 1 /string ( ud' $' ) | |
85 | repeat ( ud' $' ) | |
86 | ||
87 | if ( ud adr ) | |
88 | \ Do not accept a trailing comma, thus preventing, | |
89 | \ for example, "c," from being interpreted as a number | |
90 | c@ ascii . = if ( ud ) | |
91 | true ( ud dbl? ) | |
92 | else ( ud ) | |
93 | r> 3drop 0 exit | |
94 | then ( ud dbl? ) | |
95 | else ( ud adr ) | |
96 | drop false ( ud dbl? ) | |
97 | then ( ud dbl? ) | |
98 | ||
99 | over or if ( ud ) | |
100 | r> if dnegate then 2 | |
101 | else | |
102 | drop r> if negate then 1 | |
103 | then | |
104 | ; | |
105 | ||
106 | defer do-defined ( cfa -1 | cfa 1 -- ?? ) | |
107 | defer $do-undefined ( adr len -- ) | |
108 | ||
109 | headers | |
110 | defer do-literal | |
111 | : (do-literal) ( n 1 | d 2 -- n | d | ) | |
112 | state @ if | |
113 | 2 = if [compile] dliteral else [compile] literal then | |
114 | else | |
115 | drop | |
116 | then | |
117 | ; | |
118 | ' (do-literal) is do-literal | |
119 | defer $handle-literal? ( adr len -- handled? ) | |
120 | : ($handle-literal?) ( adr len -- handled? ) | |
121 | $dnumber? dup if do-literal true then | |
122 | ; | |
123 | ' ($handle-literal?) is $handle-literal? | |
124 | ||
125 | headers | |
126 | : $compile ( adr len -- ?? ) | |
127 | 2dup 2>r ( adr len ) ( r: adr len ) | |
128 | $find dup if ( xt +-1 ) | |
129 | 2r> 2drop do-defined ( ) | |
130 | else ( adr' len' 0 ) | |
131 | 3drop ( ) | |
132 | 2r@ $handle-literal? 0= if ( ) | |
133 | 2r@ $do-undefined ( ) | |
134 | then | |
135 | 2r> 2drop | |
136 | then | |
137 | ; | |
138 | headerless | |
139 | : interpret-do-defined ( cfa -1 | cfa 1 -- ?? ) drop execute ; | |
140 | : compile-do-defined ( cfa -1 | cfa 1 -- ) | |
141 | 0> if execute \ if immediate | |
142 | else compile, \ if not immediate | |
143 | then | |
144 | ; | |
145 | headers | |
146 | : .not-found ( adr len -- ) (compile-time-error) where type ." ?" cr ; | |
147 | headerless | |
148 | \ Abort after an undefined word in interpret state | |
149 | : $interpret-do-undefined ( adr len -- ) | |
150 | (compile-time-error) mark-error set-abort-message d# -13 throw | |
151 | ; | |
152 | \ Compile a surrogate for an undefined word in compile state | |
153 | : $compile-do-undefined ( adr len -- ) .not-found compile lose ; | |
154 | ||
155 | headers | |
156 | defer [ immediate | |
157 | headerless | |
158 | : ([) ( -- ) | |
159 | ['] interpret-do-defined is do-defined | |
160 | ['] $interpret-do-undefined is $do-undefined | |
161 | state off | |
162 | ; | |
163 | ' ([) is [ | |
164 | ||
165 | headers | |
166 | defer ] | |
167 | headerless | |
168 | : (]) ( -- ) | |
169 | ['] compile-do-defined is do-defined | |
170 | ['] $compile-do-undefined is $do-undefined | |
171 | state on | |
172 | ; | |
173 | ' (]) is ] | |
174 | ||
175 | headers | |
176 | \ Run-time error checking | |
177 | : ?stack ( ?? -- ) | |
178 | sp@ sp0 @ swap u< ( -4 ) abort" Stack Underflow" | |
179 | sp@ sp0 @ ps-size - u< ( -3 ) abort" Stack Overflow" | |
180 | ; | |
181 | ||
182 | defer ?permitted ' noop is ?permitted | |
183 | ||
184 | defer interpret | |
185 | : (interpret ( -- ) | |
186 | begin | |
187 | \ ?stack | |
188 | parse-word dup | |
189 | while | |
190 | ?permitted | |
191 | $compile | |
192 | repeat | |
193 | 2drop | |
194 | ; | |
195 | ' (interpret is interpret | |
196 | ||
197 | \ Ensure that the cursor in on an empty line. | |
198 | : ??cr ( -- ) #out @ if cr then ; | |
199 | ||
200 | \ This hack is for users of window systems. If you pick up with the | |
201 | \ mouse an entire previous command line, including the prompt, then | |
202 | \ paste it into the current line, Forth will ignore the prompt. | |
203 | : ok ( -- ) ; | |
204 | ||
205 | defer status ( -- ) ' noop is status | |
206 | ||
207 | ||
208 | \ A hook for automatic pagination | |
209 | ||
210 | defer mark-output ( -- ) ' noop is mark-output | |
211 | ||
212 | ||
213 | \ Prompts the user for another line of input. Executed only if the input | |
214 | \ stream is coming from a terminal. | |
215 | ||
216 | defer (ok) ( -- ) | |
217 | : "ok" ." ok " ; | |
218 | ' "ok" is (ok) | |
219 | ||
220 | defer reset-page | |
221 | ' noop is reset-page | |
222 | : do-prompt ( -- ) reset-page prompt ; |