[gforth] / gforth / kernel / Attic / interp.fs  

gforth: gforth/kernel/Attic/interp.fs


1 : anton 1.1 \ definitions needed for interpreter / compiler only
2 :    
3 :     \ here allot , c, A, 17dec92py
4 :    
5 :     : allot ( n -- ) \ core
6 : anton 1.2 dup unused > -8 and throw
7 : anton 1.1 dp +! ;
8 :     : c, ( c -- ) \ core
9 :     here 1 chars allot c! ;
10 :     : , ( x -- ) \ core
11 :     here cell allot ! ;
12 :     : 2, ( w1 w2 -- ) \ gforth
13 :     here 2 cells allot 2! ;
14 :    
15 :     \ : aligned ( addr -- addr' ) \ core
16 :     \ [ cell 1- ] Literal + [ -1 cells ] Literal and ;
17 :     : align ( -- ) \ core
18 :     here dup aligned swap ?DO bl c, LOOP ;
19 :    
20 :     \ : faligned ( addr -- f-addr ) \ float
21 :     \ [ 1 floats 1- ] Literal + [ -1 floats ] Literal and ;
22 :    
23 :     : falign ( -- ) \ float
24 :     here dup faligned swap
25 :     ?DO
26 :     bl c,
27 :     LOOP ;
28 :    
29 :     : maxalign ( -- ) \ float
30 :     here dup maxaligned swap
31 :     ?DO
32 :     bl c,
33 :     LOOP ;
34 :    
35 :     \ the code field is aligned if its body is maxaligned
36 :     ' maxalign Alias cfalign ( -- ) \ gforth
37 :    
38 :     ' , alias A, ( addr -- ) \ gforth
39 :    
40 : jwilke 1.3 ' NOOP ALIAS const
41 :    
42 : anton 1.1 \ name> found 17dec92py
43 :    
44 :     $80 constant alias-mask \ set when the word is not an alias!
45 :     $40 constant immediate-mask
46 :     $20 constant restrict-mask
47 :    
48 :     : ((name>)) ( nfa -- cfa )
49 :     name>string + cfaligned ;
50 :    
51 :     : (name>x) ( nfa -- cfa b )
52 :     \ cfa is an intermediate cfa and b is the flags byte of nfa
53 :     dup ((name>))
54 :     swap cell+ c@ dup alias-mask and 0=
55 :     IF
56 :     swap @ swap
57 :     THEN ;
58 :    
59 :     \ input stream primitives 23feb93py
60 :    
61 :     : tib ( -- c-addr ) \ core-ext
62 :     \ obsolescent
63 :     >tib @ ;
64 :     Defer source ( -- addr count ) \ core
65 :     \ used by dodefer:, must be defer
66 :     : (source) ( -- addr count )
67 :     tib #tib @ ;
68 :     ' (source) IS source
69 :    
70 :     : (word) ( addr1 n1 char -- addr2 n2 )
71 :     dup >r skip 2dup r> scan nip - ;
72 :    
73 :     \ (word) should fold white spaces
74 :     \ this is what (parse-white) does
75 :    
76 :     \ word parse 23feb93py
77 :    
78 :     : parse-word ( char -- addr len ) \ gforth
79 :     source 2dup >r >r >in @ over min /string
80 :     rot dup bl = IF drop (parse-white) ELSE (word) THEN
81 :     2dup + r> - 1+ r> min >in ! ;
82 :     : word ( char -- addr ) \ core
83 :     parse-word here place bl here count + c! here ;
84 :    
85 :     : parse ( char -- addr len ) \ core-ext
86 :     >r source >in @ over min /string over swap r> scan >r
87 :     over - dup r> IF 1+ THEN >in +! ;
88 :    
89 :     \ name 13feb93py
90 :    
91 :     : capitalize ( addr len -- addr len ) \ gforth
92 :     2dup chars chars bounds
93 :     ?DO I c@ toupper I c! 1 chars +LOOP ;
94 : jwilke 1.3
95 :     [IFUNDEF] (name) \ name might be a primitive
96 : anton 1.1 : (name) ( -- c-addr count )
97 :     source 2dup >r >r >in @ /string (parse-white)
98 :     2dup + r> - 1+ r> min >in ! ;
99 :     \ name count ;
100 : jwilke 1.3 [THEN]
101 : anton 1.1
102 :     : name-too-short? ( c-addr u -- c-addr u )
103 :     dup 0= -&16 and throw ;
104 :    
105 :     : name-too-long? ( c-addr u -- c-addr u )
106 :     dup $1F u> -&19 and throw ;
107 :    
108 :     \ Literal 17dec92py
109 :    
110 :     : Literal ( compilation n -- ; run-time -- n ) \ core
111 :     postpone lit , ; immediate restrict
112 :     : ALiteral ( compilation addr -- ; run-time -- addr ) \ gforth
113 :     postpone lit A, ; immediate restrict
114 :    
115 :     : char ( 'char' -- n ) \ core
116 :     bl word char+ c@ ;
117 :     : [char] ( compilation 'char' -- ; run-time -- n )
118 :     char postpone Literal ; immediate restrict
119 :    
120 : jwilke 1.5 \ threading 17mar93py
121 :    
122 :     : cfa, ( code-address -- ) \ gforth cfa-comma
123 :     here
124 :     dup lastcfa !
125 :     0 A, 0 , code-address! ;
126 :     : compile, ( xt -- ) \ core-ext compile-comma
127 :     A, ;
128 :     : !does ( addr -- ) \ gforth store-does
129 :     lastxt does-code! ;
130 :     : (does>) ( R: addr -- )
131 :     r> cfaligned /does-handler + !does ;
132 :     : dodoes, ( -- )
133 :     cfalign here /does-handler allot does-handler! ;
134 :    
135 : anton 1.1 : (compile) ( -- ) \ gforth
136 :     r> dup cell+ >r @ compile, ;
137 :    
138 :     : postpone, ( w xt -- )
139 :     \g Compiles the compilation semantics represented by @var{w xt}.
140 :     dup ['] execute =
141 :     if
142 :     drop compile,
143 :     else
144 :     dup ['] compile, =
145 :     if
146 :     drop POSTPONE (compile) compile,
147 :     else
148 :     swap POSTPONE aliteral compile,
149 :     then
150 :     then ;
151 :    
152 :     : POSTPONE ( "name" -- ) \ core
153 :     \g Compiles the compilation semantics of @var{name}.
154 :     COMP' postpone, ; immediate restrict
155 :    
156 :     : interpret/compile: ( interp-xt comp-xt "name" -- ) \ gforth
157 :     Create immediate swap A, A,
158 :     DOES>
159 :     abort" executed primary cfa of an interpret/compile: word" ;
160 :     \ state @ IF cell+ THEN perform ;
161 :    
162 :     \ number? number 23feb93py
163 :    
164 :     hex
165 : jwilke 1.3 const Create bases 10 , 2 , A , 100 ,
166 :     \ 16 2 10 character
167 : anton 1.1 \ !! this saving and restoring base is an abomination! - anton
168 :     : getbase ( addr u -- addr' u' )
169 :     over c@ [char] $ - dup 4 u<
170 :     IF
171 :     cells bases + @ base ! 1 /string
172 :     ELSE
173 :     drop
174 :     THEN ;
175 :     : s>number ( addr len -- d )
176 :     base @ >r dpl on
177 :     over c@ '- = dup >r
178 :     IF
179 :     1 /string
180 :     THEN
181 :     getbase dpl on 0 0 2swap
182 :     BEGIN
183 :     dup >r >number dup
184 :     WHILE
185 :     dup r> -
186 :     WHILE
187 :     dup dpl ! over c@ [char] . =
188 :     WHILE
189 :     1 /string
190 :     REPEAT THEN
191 :     2drop rdrop dpl off
192 :     ELSE
193 :     2drop rdrop r>
194 :     IF
195 :     dnegate
196 :     THEN
197 :     THEN
198 :     r> base ! ;
199 :    
200 :     : snumber? ( c-addr u -- 0 / n -1 / d 0> )
201 :     s>number dpl @ 0=
202 :     IF
203 :     2drop false EXIT
204 :     THEN
205 :     dpl @ dup 0> 0= IF
206 :     nip
207 :     THEN ;
208 :    
209 :     : number? ( string -- string 0 / n -1 / d 0> )
210 :     dup >r count snumber? dup if
211 :     rdrop
212 :     else
213 :     r> swap
214 :     then ;
215 :    
216 :     : number ( string -- d )
217 :     number? ?dup 0= abort" ?" 0<
218 :     IF
219 :     s>d
220 :     THEN ;
221 :    
222 :     \ interpret 10mar92py
223 :    
224 :     Defer parser
225 :     Defer name ( -- c-addr count ) \ gforth
226 :     \ get the next word from the input buffer
227 :     ' (name) IS name
228 :     Defer compiler-notfound ( c-addr count -- )
229 :     Defer interpreter-notfound ( c-addr count -- )
230 :    
231 :     : no.extensions ( addr u -- )
232 :     2drop -&13 bounce ;
233 :     ' no.extensions IS compiler-notfound
234 :     ' no.extensions IS interpreter-notfound
235 :    
236 :     : compile-only-error ( ... -- )
237 :     -&14 throw ;
238 :    
239 :     : interpret ( ?? -- ?? ) \ gforth
240 :     \ interpret/compile the (rest of the) input buffer
241 :     BEGIN
242 :     ?stack name dup
243 :     WHILE
244 :     parser
245 :     REPEAT
246 :     2drop ;
247 :    
248 :     \ interpreter compiler 30apr92py
249 :    
250 :     \ not the most efficient implementations of interpreter and compiler
251 :     : interpreter ( c-addr u -- )
252 :     2dup find-name dup
253 :     if
254 :     nip nip name>int execute
255 :     else
256 :     drop
257 :     2dup 2>r snumber?
258 :     IF
259 :     2rdrop
260 :     ELSE
261 :     2r> interpreter-notfound
262 :     THEN
263 :     then ;
264 :    
265 :     : compiler ( c-addr u -- )
266 :     2dup find-name dup
267 :     if ( c-addr u nt )
268 :     nip nip name>comp execute
269 :     else
270 :     drop
271 :     2dup snumber? dup
272 :     IF
273 :     0>
274 :     IF
275 :     swap postpone Literal
276 :     THEN
277 :     postpone Literal
278 :     2drop
279 :     ELSE
280 :     drop compiler-notfound
281 :     THEN
282 :     then ;
283 :    
284 :     ' interpreter IS parser
285 :    
286 :     : [ ( -- ) \ core left-bracket
287 :     ['] interpreter IS parser state off ; immediate
288 :     : ] ( -- ) \ core right-bracket
289 :     ['] compiler IS parser state on ;
290 :    
291 :     \ Strings 22feb93py
292 :    
293 :     : ," ( "string"<"> -- ) [char] " parse
294 :     here over char+ allot place align ;
295 :     : SLiteral ( Compilation c-addr1 u ; run-time -- c-addr2 u ) \ string
296 :     postpone (S") here over char+ allot place align ;
297 :     immediate restrict
298 :     : ( ( compilation 'ccc<close-paren>' -- ; run-time -- ) \ core,file paren
299 :     [char] ) parse 2drop ; immediate
300 :    
301 :     : \ ( -- ) \ core-ext backslash
302 :     blk @
303 :     IF
304 :     >in @ c/l / 1+ c/l * >in !
305 :     EXIT
306 :     THEN
307 :     source >in ! drop ; immediate
308 :    
309 :     : \G ( -- ) \ gforth backslash
310 :     POSTPONE \ ; immediate
311 :    
312 :     \ error handling 22feb93py
313 :     \ 'abort thrown out! 11may93jaw
314 :    
315 :     : abort" ( compilation 'ccc"' -- ; run-time f -- ) \ core,exception-ext abort-quote
316 :     postpone (abort") ," ; immediate restrict
317 :    
318 :     \ Header states 23feb93py
319 :    
320 :     : cset ( bmask c-addr -- )
321 :     tuck c@ or swap c! ;
322 :     : creset ( bmask c-addr -- )
323 :     tuck c@ swap invert and swap c! ;
324 :     : ctoggle ( bmask c-addr -- )
325 :     tuck c@ xor swap c! ;
326 :    
327 :     : lastflags ( -- c-addr )
328 :     \ the address of the flags byte in the last header
329 :     \ aborts if the last defined word was headerless
330 :     last @ dup 0= abort" last word was headerless" cell+ ;
331 :    
332 :     : immediate ( -- ) \ core
333 :     immediate-mask lastflags cset ;
334 :     : restrict ( -- ) \ gforth
335 :     restrict-mask lastflags cset ;
336 :     ' restrict alias compile-only ( -- ) \ gforth
337 :    
338 :     \ Header 23feb93py
339 :    
340 :     \ input-stream, nextname and noname are quite ugly (passing
341 :     \ information through global variables), but they are useful for dealing
342 :     \ with existing/independent defining words
343 :    
344 :     defer (header)
345 :     defer header ( -- ) \ gforth
346 :     ' (header) IS header
347 :    
348 :     : string, ( c-addr u -- ) \ gforth
349 :     \G puts down string as cstring
350 :     dup c, here swap chars dup allot move ;
351 :    
352 :     : header, ( c-addr u -- ) \ gforth
353 :     name-too-long?
354 :     align here last !
355 :     current @ 1 or A, \ link field; before revealing, it contains the
356 :     \ tagged reveal-into wordlist
357 :     string, cfalign
358 :     alias-mask lastflags cset ;
359 :    
360 :     : input-stream-header ( "name" -- )
361 :     name name-too-short? header, ;
362 :     : input-stream ( -- ) \ general
363 :     \G switches back to getting the name from the input stream ;
364 :     ['] input-stream-header IS (header) ;
365 :    
366 :     ' input-stream-header IS (header)
367 :    
368 :     \ !! make that a 2variable
369 :     create nextname-buffer 32 chars allot
370 :    
371 :     : nextname-header ( -- )
372 :     nextname-buffer count header,
373 :     input-stream ;
374 :    
375 :     \ the next name is given in the string
376 :     : nextname ( c-addr u -- ) \ gforth
377 :     name-too-long?
378 :     nextname-buffer c! ( c-addr )
379 :     nextname-buffer count move
380 :     ['] nextname-header IS (header) ;
381 :    
382 :     : noname-header ( -- )
383 :     0 last ! cfalign
384 :     input-stream ;
385 :    
386 :     : noname ( -- ) \ gforth
387 :     \ the next defined word remains anonymous. The xt of that word is given by lastxt
388 :     ['] noname-header IS (header) ;
389 :    
390 :     : lastxt ( -- xt ) \ gforth
391 :     \ xt is the execution token of the last word defined. The main purpose of this word is to get the xt of words defined using noname
392 :     lastcfa @ ;
393 :    
394 :     : Alias ( cfa "name" -- ) \ gforth
395 :     Header reveal
396 :     alias-mask lastflags creset
397 :     dup A, lastcfa ! ;
398 :    
399 :     : name>string ( nt -- addr count ) \ gforth name-to-string
400 :     \g @var{addr count} is the name of the word represented by @var{nt}.
401 :     cell+ count $1F and ;
402 :    
403 : jwilke 1.3 : head>string
404 :     cell+ count $1F and ;
405 :    
406 :    
407 :     const Create ??? 0 , 3 c, char ? c, char ? c, char ? c,
408 :     \ ??? is used by dovar:, must be created/:dovar
409 :    
410 :     : >head ( cfa -- nt ) \ gforth to-name
411 : anton 1.1 $21 cell do
412 :     dup i - count $9F and + cfaligned over alias-mask + = if
413 :     i - cell - unloop exit
414 :     then
415 :     cell +loop
416 :     drop ??? ( wouldn't 0 be better? ) ;
417 :    
418 : jwilke 1.3 ' >head ALIAS >name
419 :    
420 :     : body> 0 >body - ;
421 :    
422 : anton 1.1 doer? :dovar [IF]
423 :     : Create ( "name" -- ) \ core
424 :     Header reveal dovar: cfa, ;
425 :     [ELSE]
426 :     : Create ( "name" -- ) \ core
427 :     Header reveal here lastcfa ! 0 A, 0 , DOES> ;
428 :     [THEN]
429 :    
430 :     \ Create Variable User Constant 17mar93py
431 :    
432 :     : Variable ( "name" -- ) \ core
433 :     Create 0 , ;
434 :     : AVariable ( "name" -- ) \ gforth
435 :     Create 0 A, ;
436 :     : 2Variable ( "name" -- ) \ double
437 :     create 0 , 0 , ;
438 :    
439 :     : uallot ( n -- ) udp @ swap udp +! ;
440 :    
441 :     doer? :douser [IF]
442 :     : User ( "name" -- ) \ gforth
443 :     Header reveal douser: cfa, cell uallot , ;
444 :     : AUser ( "name" -- ) \ gforth
445 :     User ;
446 :     [ELSE]
447 : jwilke 1.3 : User Create cell uallot , DOES> @ up @ + ;
448 : anton 1.1 : AUser User ;
449 :     [THEN]
450 :    
451 :     doer? :docon [IF]
452 :     : (Constant) Header reveal docon: cfa, ;
453 :     [ELSE]
454 :     : (Constant) Create DOES> @ ;
455 :     [THEN]
456 :     : Constant ( w "name" -- ) \ core
457 :     \G Defines constant @var{name}
458 :     \G
459 :     \G @var{name} execution: @var{-- w}
460 :     (Constant) , ;
461 :     : AConstant ( addr "name" -- ) \ gforth
462 :     (Constant) A, ;
463 :     : Value ( w "name" -- ) \ core-ext
464 :     (Constant) , ;
465 :    
466 :     : 2Constant ( w1 w2 "name" -- ) \ double
467 :     Create ( w1 w2 "name" -- )
468 :     2,
469 :     DOES> ( -- w1 w2 )
470 :     2@ ;
471 :    
472 :     doer? :dofield [IF]
473 :     : (Field) Header reveal dofield: cfa, ;
474 :     [ELSE]
475 :     : (Field) Create DOES> @ + ;
476 :     [THEN]
477 :     \ IS Defer What's Defers TO 24feb93py
478 :    
479 :     doer? :dodefer [IF]
480 :     : Defer ( "name" -- ) \ gforth
481 :     \ !! shouldn't it be initialized with abort or something similar?
482 :     Header Reveal dodefer: cfa,
483 :     ['] noop A, ;
484 :     [ELSE]
485 :     : Defer ( "name" -- ) \ gforth
486 :     Create ['] noop A,
487 :     DOES> @ execute ;
488 :     [THEN]
489 :    
490 :     : Defers ( "name" -- ) \ gforth
491 :     ' >body @ compile, ; immediate
492 :    
493 :     \ : ; 24feb93py
494 :    
495 :     defer :-hook ( sys1 -- sys2 )
496 :     defer ;-hook ( sys2 -- sys1 )
497 :    
498 :     : : ( "name" -- colon-sys ) \ core colon
499 :     Header docol: cfa, defstart ] :-hook ;
500 :     : ; ( compilation colon-sys -- ; run-time nest-sys ) \ core semicolon
501 :     ;-hook ?struc postpone exit reveal postpone [ ; immediate restrict
502 :    
503 :     : :noname ( -- xt colon-sys ) \ core-ext colon-no-name
504 :     0 last !
505 :     cfalign here docol: cfa, 0 ] :-hook ;
506 :    
507 :     \ Search list handling 23feb93py
508 :    
509 :     : last? ( -- false / nfa nfa )
510 :     last @ ?dup ;
511 :     : (reveal) ( nt wid -- )
512 :     ( wid>wordlist-id ) dup >r
513 :     @ over ( name>link ) !
514 :     r> ! ;
515 :    
516 :     \ object oriented search list 17mar93py
517 :    
518 :     \ word list structure:
519 :    
520 :     struct
521 : anton 1.4 cell% field find-method \ xt: ( c_addr u wid -- nt )
522 :     cell% field reveal-method \ xt: ( nt wid -- ) \ used by dofield:, must be field
523 :     cell% field rehash-method \ xt: ( wid -- ) \ re-initializes a "search-data" (hashtables)
524 :     cell% field hash-method \ xt: ( wid -- ) \ initializes ""
525 : anton 1.1 \ \ !! what else
526 :     end-struct wordlist-map-struct
527 :    
528 :     struct
529 : anton 1.4 cell% field wordlist-id \ not the same as wid; representation depends on implementation
530 :     cell% field wordlist-map \ pointer to a wordlist-map-struct
531 :     cell% field wordlist-link \ link field to other wordlists
532 :     cell% field wordlist-extend \ points to wordlist extensions (eg hashtables)
533 : anton 1.1 end-struct wordlist-struct
534 :    
535 :     : f83find ( addr len wordlist -- nt / false )
536 :     ( wid>wordlist-id ) @ (f83find) ;
537 :    
538 : jwilke 1.3 : initvoc ( wid -- )
539 :     dup wordlist-map @ hash-method perform ;
540 :    
541 : anton 1.1 \ Search list table: find reveal
542 :     Create f83search ( -- wordlist-map )
543 : jwilke 1.3 ' f83find A, ' (reveal) A, ' drop A, ' drop A,
544 :    
545 :     here NIL A, G f83search T A, NIL A, NIL A,
546 :     AValue forth-wordlist \ variable, will be redefined by search.fs
547 :    
548 :     AVariable lookup forth-wordlist lookup !
549 :     \ !! last is user and lookup?! jaw
550 :     AVariable current ( -- addr ) \ gforth
551 :     AVariable voclink forth-wordlist wordlist-link voclink !
552 :     lookup AValue context
553 : anton 1.1
554 : jwilke 1.3 forth-wordlist current !
555 : anton 1.1
556 :     \ higher level parts of find
557 :    
558 : anton 1.4 struct
559 :     >body
560 :     cell% field interpret/compile-int
561 :     cell% field interpret/compile-comp
562 : anton 1.1 end-struct interpret/compile-struct
563 :    
564 :     : (cfa>int) ( cfa -- xt )
565 :     dup interpret/compile?
566 :     if
567 :     interpret/compile-int @
568 :     then ;
569 :    
570 :     : (x>int) ( cfa b -- xt )
571 :     \ get interpretation semantics of name
572 :     restrict-mask and
573 :     if
574 :     drop ['] compile-only-error
575 :     else
576 :     (cfa>int)
577 :     then ;
578 :    
579 :     : name>int ( nt -- xt ) \ gforth
580 :     \G @var{xt} represents the interpretation semantics of the word
581 :     \G @var{nt}. Produces @code{' compile-only-error} if
582 :     \G @var{nt} is compile-only.
583 :     (name>x) (x>int) ;
584 :    
585 :     : name?int ( nt -- xt ) \ gforth
586 :     \G Like name>int, but throws an error if compile-only.
587 :     (name>x) restrict-mask and
588 :     if
589 :     compile-only-error \ does not return
590 :     then
591 :     (cfa>int) ;
592 :    
593 :     : name>comp ( nt -- w xt ) \ gforth
594 :     \G @var{w xt} is the compilation token for the word @var{nt}.
595 :     (name>x) >r dup interpret/compile?
596 :     if
597 :     interpret/compile-comp @
598 :     then
599 :     r> immediate-mask and if
600 :     ['] execute
601 :     else
602 :     ['] compile,
603 :     then ;
604 :    
605 :     : (search-wordlist) ( addr count wid -- nt / false )
606 :     dup wordlist-map @ find-method perform ;
607 :    
608 :     : flag-sign ( f -- 1|-1 )
609 :     \ true becomes 1, false -1
610 :     0= 2* 1+ ;
611 :    
612 :     : (name>intn) ( nfa -- xt +-1 )
613 :     (name>x) tuck (x>int) ( b xt )
614 :     swap immediate-mask and flag-sign ;
615 :    
616 :     : search-wordlist ( addr count wid -- 0 / xt +-1 ) \ search
617 :     \ xt is the interpretation semantics
618 :     (search-wordlist) dup if
619 :     (name>intn)
620 :     then ;
621 :    
622 :     : find-name ( c-addr u -- nt/0 ) \ gforth
623 :     \g Find the name @var{c-addr u} in the current search
624 :     \g order. Return its nt, if found, otherwise 0.
625 :     lookup @ (search-wordlist) ;
626 :    
627 :     : sfind ( c-addr u -- 0 / xt +-1 ) \ gforth-obsolete
628 :     find-name dup
629 :     if ( nt )
630 :     state @
631 :     if
632 :     name>comp ['] execute = flag-sign
633 :     else
634 :     (name>intn)
635 :     then
636 :     then ;
637 :    
638 :     : find ( c-addr -- xt +-1 / c-addr 0 ) \ core,search
639 :     dup count sfind dup
640 :     if
641 :     rot drop
642 :     then ;
643 :    
644 :     : (') ( "name" -- nt ) \ gforth
645 :     name find-name dup 0=
646 :     IF
647 :     drop -&13 bounce
648 :     THEN ;
649 :    
650 :     : [(')] ( compilation "name" -- ; run-time -- nt ) \ gforth bracket-paren-tick
651 :     (') postpone ALiteral ; immediate restrict
652 :    
653 :     : ' ( "name" -- xt ) \ core tick
654 :     \g @var{xt} represents @var{name}'s interpretation
655 :     \g semantics. Performs @code{-14 throw} if the word has no
656 :     \g interpretation semantics.
657 :     (') name?int ;
658 :     : ['] ( compilation. "name" -- ; run-time. -- xt ) \ core bracket-tick
659 :     \g @var{xt} represents @var{name}'s interpretation
660 :     \g semantics. Performs @code{-14 throw} if the word has no
661 :     \g interpretation semantics.
662 :     ' postpone ALiteral ; immediate restrict
663 :    
664 :     : COMP' ( "name" -- w xt ) \ gforth c-tick
665 :     \g @var{w xt} represents @var{name}'s compilation semantics.
666 :     (') name>comp ;
667 :     : [COMP'] ( compilation "name" -- ; run-time -- w xt ) \ gforth bracket-comp-tick
668 :     \g @var{w xt} represents @var{name}'s compilation semantics.
669 :     COMP' swap POSTPONE Aliteral POSTPONE ALiteral ; immediate restrict
670 :    
671 :     \ reveal words
672 :    
673 :     Variable warnings ( -- addr ) \ gforth
674 :     G -1 warnings T !
675 :    
676 :     : check-shadow ( addr count wid -- )
677 :     \G prints a warning if the string is already present in the wordlist
678 :     >r 2dup 2dup r> (search-wordlist) warnings @ and ?dup if
679 :     ." redefined " name>string 2dup type
680 :     compare 0<> if
681 :     ." with " type
682 :     else
683 :     2drop
684 :     then
685 :     space space EXIT
686 :     then
687 :     2drop 2drop ;
688 :    
689 :     : reveal ( -- ) \ gforth
690 :     last?
691 :     if \ the last word has a header
692 :     dup ( name>link ) @ 1 and
693 :     if \ it is still hidden
694 :     dup ( name>link ) @ 1 xor ( nt wid )
695 :     2dup >r name>string r> check-shadow ( nt wid )
696 :     dup wordlist-map @ reveal-method perform
697 :     then
698 :     then ;
699 :    
700 :     : rehash ( wid -- )
701 :     dup wordlist-map @ rehash-method perform ;
702 :    
703 :     \ Query 07apr93py
704 :    
705 :     has-files 0= [IF]
706 :     : sourceline# ( -- n ) loadline @ ;
707 :     [THEN]
708 :    
709 :     : refill ( -- flag ) \ core-ext,block-ext,file-ext
710 :     blk @ IF 1 blk +! true 0 >in ! EXIT THEN
711 :     tib /line
712 :     [ has-files [IF] ]
713 :     loadfile @ ?dup
714 :     IF read-line throw
715 :     ELSE
716 :     [ [THEN] ]
717 :     sourceline# 0< IF 2drop false EXIT THEN
718 :     accept true
719 :     [ has-files [IF] ]
720 :     THEN
721 :     [ [THEN] ]
722 :     1 loadline +!
723 :     swap #tib ! 0 >in ! ;
724 :    
725 :     : query ( -- ) \ core-ext
726 :     \G obsolescent
727 :     blk off loadfile off
728 :     tib /line accept #tib ! 0 >in ! ;
729 :    
730 :     \ save-mem extend-mem
731 :    
732 :     has-os [IF]
733 :     : save-mem ( addr1 u -- addr2 u ) \ gforth
734 :     \g copy a memory block into a newly allocated region in the heap
735 :     swap >r
736 :     dup allocate throw
737 :     swap 2dup r> -rot move ;
738 :    
739 :     : extend-mem ( addr1 u1 u -- addr addr2 u2 )
740 :     \ extend memory block allocated from the heap by u aus
741 :     \ the (possibly reallocated piece is addr2 u2, the extension is at addr
742 :     over >r + dup >r resize throw
743 :     r> over r> + -rot ;
744 :     [THEN]
745 :    
746 :     \ RECURSE 17may93jaw
747 :    
748 :     : recurse ( compilation -- ; run-time ?? -- ?? ) \ core
749 : anton 1.2 \g calls the current definition.
750 : anton 1.1 lastxt compile, ; immediate restrict
751 : anton 1.2 ' reveal alias recursive ( compilation -- ; run-time -- ) \ gforth
752 :     \g makes the current definition visible, enabling it to call itself
753 :     \g recursively.
754 :     immediate restrict
755 : anton 1.1
756 :     \ EVALUATE 17may93jaw
757 :    
758 :     has-files 0= [IF]
759 :     : push-file ( -- ) r>
760 :     sourceline# >r tibstack @ >r >tib @ >r #tib @ >r
761 :     >tib @ tibstack @ = IF r@ tibstack +! THEN
762 :     tibstack @ >tib ! >in @ >r >r ;
763 :    
764 :     : pop-file ( throw-code -- throw-code )
765 :     r>
766 :     r> >in ! r> #tib ! r> >tib ! r> tibstack ! r> loadline ! >r ;
767 :     [THEN]
768 :    
769 :     : evaluate ( c-addr len -- ) \ core,block
770 :     push-file #tib ! >tib !
771 :     >in off blk off loadfile off -1 loadline !
772 :     ['] interpret catch
773 :     pop-file throw ;
774 :    
775 :     : abort ( ?? -- ?? ) \ core,exception-ext
776 :     -1 throw ;
777 :    
778 :     \+ environment? true ENV" CORE"
779 :     \ core wordset is now complete!
780 :    
781 :     \ Quit 13feb93py
782 :    
783 :     Defer 'quit
784 :     Defer .status
785 :     : prompt state @ IF ." compiled" EXIT THEN ." ok" ;
786 :     : (Query) ( -- )
787 :     loadfile off blk off refill drop ;
788 :     : (quit) BEGIN .status cr (query) interpret prompt AGAIN ;
789 :     ' (quit) IS 'quit
790 :    
791 :     \ DOERROR (DOERROR) 13jun93jaw
792 :    
793 :     8 Constant max-errors
794 :     Variable error-stack 0 error-stack !
795 :     max-errors 6 * cells allot
796 :     \ format of one cell:
797 :     \ source ( addr u )
798 :     \ >in
799 :     \ line-number
800 :     \ Loadfilename ( addr u )
801 :    
802 :     : dec. ( n -- ) \ gforth
803 :     \ print value in decimal representation
804 :     base @ decimal swap . base ! ;
805 :    
806 :     : hex. ( u -- ) \ gforth
807 :     \ print value as unsigned hex number
808 :     '$ emit base @ swap hex u. base ! ;
809 :    
810 :     : typewhite ( addr u -- ) \ gforth
811 :     \ like type, but white space is printed instead of the characters
812 :     bounds ?do
813 :     i c@ #tab = if \ check for tab
814 :     #tab
815 :     else
816 :     bl
817 :     then
818 :     emit
819 :     loop ;
820 :    
821 :     DEFER DOERROR
822 :    
823 :     : .error-frame ( addr1 u1 n1 n2 addr2 u2 -- )
824 :     cr error-stack @
825 :     IF
826 :     ." in file included from "
827 :     type ." :" dec. drop 2drop
828 :     ELSE
829 :     type ." :" dec.
830 :     cr dup 2over type cr drop
831 :     nip -trailing 1- ( line-start index2 )
832 :     0 >r BEGIN
833 :     2dup + c@ bl > WHILE
834 :     r> 1+ >r 1- dup 0< UNTIL THEN 1+
835 :     ( line-start index1 )
836 :     typewhite
837 :     r> 1 max 0 ?do \ we want at least one "^", even if the length is 0
838 :     [char] ^ emit
839 :     loop
840 :     THEN
841 :     ;
842 :    
843 :     : (DoError) ( throw-code -- )
844 :     [ has-os [IF] ]
845 :     outfile-id dup flush-file drop >r
846 :     stderr to outfile-id
847 :     [ [THEN] ]
848 :     sourceline# IF
849 :     source >in @ sourceline# 0 0 .error-frame
850 :     THEN
851 :     error-stack @ 0 ?DO
852 :     -1 error-stack +!
853 :     error-stack dup @ 6 * cells + cell+
854 :     6 cells bounds DO
855 :     I @
856 :     cell +LOOP
857 :     .error-frame
858 :     LOOP
859 :     dup -2 =
860 :     IF
861 :     "error @ ?dup
862 :     IF
863 :     cr count type
864 :     THEN
865 :     drop
866 :     ELSE
867 :     .error
868 :     THEN
869 :     normal-dp dpp !
870 :     [ has-os [IF] ] r> to outfile-id [ [THEN] ]
871 :     ;
872 :    
873 :     ' (DoError) IS DoError
874 :    
875 :     : quit ( ?? -- ?? ) \ core
876 : jwilke 1.3 rp0 @ rp! handler off >tib @ >r
877 : anton 1.1 BEGIN
878 :     postpone [
879 :     ['] 'quit CATCH dup
880 :     WHILE
881 :     DoError r@ >tib ! r@ tibstack !
882 :     REPEAT
883 :     drop r> >tib ! ;
884 :    
885 :     \ Cold 13feb93py
886 :    
887 :     \ : .name ( name -- ) name>string type space ;
888 :     \ : words listwords @
889 :     \ BEGIN @ dup WHILE dup .name REPEAT drop ;
890 :    
891 :     : (bootmessage)
892 :     ." GForth " version-string type
893 :     ." , Copyright (C) 1994-1997 Free Software Foundation, Inc." cr
894 :     ." GForth comes with ABSOLUTELY NO WARRANTY; for details type `license'"
895 :     [ has-os [IF] ]
896 :     cr ." Type `bye' to exit"
897 :     [ [THEN] ] ;
898 :    
899 :     defer bootmessage
900 : jwilke 1.5 defer process-args
901 : anton 1.1
902 :     ' (bootmessage) IS bootmessage
903 :    
904 : jwilke 1.3 Defer 'cold
905 :     \ hook (deferred word) for things to do right before interpreting the
906 :     \ command-line arguments
907 :     ' noop IS 'cold
908 :    
909 :     include chains.fs
910 :    
911 :     Variable init8
912 :    
913 : anton 1.1 : cold ( -- ) \ gforth
914 :     [ has-files [IF] ]
915 :     pathstring 2@ fpath only-path
916 :     init-included-files
917 :     [ [THEN] ]
918 :     'cold
919 : jwilke 1.3 init8 chainperform
920 : anton 1.1 [ has-files [IF] ]
921 :     ['] process-args catch ?dup
922 :     IF
923 :     dup >r DoError cr r> negate (bye)
924 :     THEN
925 :     argc @ 1 >
926 :     IF \ there may be some unfinished line, so let's finish it
927 :     cr
928 :     THEN
929 :     [ [THEN] ]
930 :     bootmessage
931 :     loadline off quit ;
932 :    
933 :     : boot ( path **argv argc -- )
934 :     main-task up!
935 :     [ has-os [IF] ]
936 :     stdout TO outfile-id
937 :     [ [THEN] ]
938 :     [ has-files [IF] ]
939 :     argc ! argv ! pathstring 2!
940 :     [ [THEN] ]
941 : jwilke 1.3 sp@ sp0 !
942 : anton 1.1 [ has-locals [IF] ]
943 :     lp@ forthstart 7 cells + @ -
944 :     [ [ELSE] ]
945 :     [ has-os [IF] ]
946 :     sp@ $1040 +
947 :     [ [ELSE] ]
948 :     sp@ $40 +
949 :     [ [THEN] ]
950 :     [ [THEN] ]
951 :     dup >tib ! tibstack ! #tib off >in off
952 : jwilke 1.3 rp@ rp0 !
953 : anton 1.1 [ has-floats [IF] ]
954 : jwilke 1.3 fp@ fp0 !
955 : anton 1.1 [ [THEN] ]
956 :     ['] cold catch DoError
957 :     [ has-os [IF] ]
958 :     bye
959 :     [ [THEN] ]
960 :     ;
961 :    
962 :     has-os [IF]
963 :     : bye ( -- ) \ tools-ext
964 :     [ has-files [IF] ]
965 :     script? 0= IF cr THEN
966 :     [ [ELSE] ]
967 :     cr
968 :     [ [THEN] ]
969 :     0 (bye) ;
970 :     [THEN]
971 :    
972 :     \ **argv may be scanned by the C starter to get some important
973 :     \ information, as -display and -geometry for an X client FORTH
974 :     \ or space and stackspace overrides
975 :    
976 :     \ 0 arg contains, however, the name of the program.

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help