1: \ definitions needed for interpreter only
2:
3: \ Copyright (C) 1995-2000,2004,2005 Free Software Foundation, Inc.
4:
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., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
20:
21: \ \ Revision-Log
22:
23: \ put in seperate file 14sep97jaw
24:
25: \ \ input stream primitives 23feb93py
26:
27: require ./basics.fs \ bounds decimal hex ...
28: require ./io.fs \ type ...
29: require ./nio.fs \ . <# ...
30: require ./errore.fs \ .error ...
31: require kernel/version.fs \ version-string
32:
33: has? new-input 0= [IF]
34: : tib ( -- c-addr ) \ core-ext t-i-b
35: \G @i{c-addr} is the address of the Terminal Input Buffer.
36: \G OBSOLESCENT: @code{source} superceeds the function of this word.
37: >tib @ ;
38:
39: Defer source ( -- c-addr u ) \ core
40: \ used by dodefer:, must be defer
41: \G @i{c-addr} is the address of the input buffer and @i{u} is the
42: \G number of characters in it.
43:
44: : (source) ( -- c-addr u )
45: tib #tib @ ;
46: ' (source) IS source
47: [THEN]
48:
49: : (word) ( addr1 n1 char -- addr2 n2 )
50: dup >r skip 2dup r> scan nip - ;
51:
52: \ (word) should fold white spaces
53: \ this is what (parse-white) does
54:
55: \ word parse 23feb93py
56:
57: : sword ( char -- addr len ) \ gforth-obsolete s-word
58: \G Parses like @code{word}, but the output is like @code{parse} output.
59: \G @xref{core-idef}.
60: \ this word was called PARSE-WORD until 0.3.0, but Open Firmware and
61: \ dpANS6 A.6.2.2008 have a word with that name that behaves
62: \ differently (like NAME).
63: source 2dup >r >r >in @ over min /string
64: rot dup bl = IF
65: drop (parse-white)
66: ELSE
67: (word)
68: THEN
69: [ has? new-input [IF] ]
70: 2dup input-lexeme!
71: [ [THEN] ]
72: 2dup + r> - 1+ r> min >in ! ;
73:
74: : word ( char "<chars>ccc<char>-- c-addr ) \ core
75: \G Skip leading delimiters. Parse @i{ccc}, delimited by
76: \G @i{char}, in the parse area. @i{c-addr} is the address of a
77: \G transient region containing the parsed string in
78: \G counted-string format. If the parse area was empty or
79: \G contained no characters other than delimiters, the resulting
80: \G string has zero length. A program may replace characters within
81: \G the counted string. OBSOLESCENT: the counted string has a
82: \G trailing space that is not included in its length.
83: sword here place bl here count + c! here ;
84:
85: : parse ( char "ccc<char>" -- c-addr u ) \ core-ext
86: \G Parse @i{ccc}, delimited by @i{char}, in the parse
87: \G area. @i{c-addr u} specifies the parsed string within the
88: \G parse area. If the parse area was empty, @i{u} is 0.
89: >r source >in @ over min /string ( c-addr1 u1 )
90: over swap r> scan >r
91: over - dup r> IF 1+ THEN >in +!
92: [ has? new-input [IF] ]
93: 2dup input-lexeme!
94: [ [THEN] ] ;
95:
96: \ name 13feb93py
97:
98: [IFUNDEF] (name) \ name might be a primitive
99:
100: : (name) ( -- c-addr count ) \ gforth
101: source 2dup >r >r >in @ /string (parse-white)
102: [ has? new-input [IF] ]
103: 2dup input-lexeme!
104: [ [THEN] ]
105: 2dup + r> - 1+ r> min >in ! ;
106: \ name count ;
107: [THEN]
108:
109: : name-too-short? ( c-addr u -- c-addr u )
110: dup 0= -&16 and throw ;
111:
112: : name-too-long? ( c-addr u -- c-addr u )
113: dup lcount-mask u> -&19 and throw ;
114:
115: \ \ Number parsing 23feb93py
116:
117: \ number? number 23feb93py
118:
119: hex
120: const Create bases 0A , 10 , 2 , 0A ,
121: \ 10 16 2 10
122:
123: \ !! protect BASE saving wrapper against exceptions
124: : getbase ( addr u -- addr' u' )
125: 2dup s" 0x" string-prefix? >r
126: 2dup s" 0X" string-prefix? r> or
127: base @ &34 < and if
128: hex 2 /string
129: endif
130: over c@ [char] # - dup 4 u<
131: IF
132: cells bases + @ base ! 1 /string
133: ELSE
134: drop
135: THEN ;
136:
137: : sign? ( addr u -- addr1 u1 flag )
138: over c@ [char] - = dup >r
139: IF
140: 1 /string
141: THEN
142: r> ;
143:
144: : s'>unumber? ( addr u -- ud flag )
145: \ convert string "C" or "C'" to character code
146: dup 0= if
147: false exit
148: endif
149: x@+/string 0 s" '" 2rot string-prefix? ;
150:
151: : s>unumber? ( addr u -- ud flag ) \ gforth
152: \G converts string addr u into ud, flag indicates success
153: dpl on
154: over c@ '' = if
155: 1 /string s'>unumber? exit
156: endif
157: base @ >r getbase
158: 0. 2swap
159: BEGIN ( d addr len )
160: dup >r >number dup
161: WHILE \ there are characters left
162: dup r> -
163: WHILE \ the last >number parsed something
164: dup 1- dpl ! over c@ [char] . =
165: WHILE \ the current char is '.'
166: 1 /string
167: REPEAT THEN \ there are unparseable characters left
168: 2drop false
169: ELSE
170: rdrop 2drop true
171: THEN
172: r> base ! ;
173:
174: \ ouch, this is complicated; there must be a simpler way - anton
175: : s>number? ( addr u -- d f ) \ gforth
176: \G converts string addr u into d, flag indicates success
177: sign? >r
178: s>unumber?
179: 0= IF
180: rdrop false
181: ELSE \ no characters left, all ok
182: r>
183: IF
184: dnegate
185: THEN
186: true
187: THEN ;
188:
189: : s>number ( addr len -- d )
190: \ don't use this, there is no way to tell success
191: s>number? drop ;
192:
193: : snumber? ( c-addr u -- 0 / n -1 / d 0> )
194: s>number? 0=
195: IF
196: 2drop false EXIT
197: THEN
198: dpl @ dup 0< IF
199: nip
200: ELSE
201: 1+
202: THEN ;
203:
204: : number? ( string -- string 0 / n -1 / d 0> )
205: dup >r count snumber? dup if
206: rdrop
207: else
208: r> swap
209: then ;
210:
211: : number ( string -- d )
212: number? ?dup 0= abort" ?" 0<
213: IF
214: s>d
215: THEN ;
216:
217: \ \ Comments ( \ \G
218:
219: : ( ( compilation 'ccc<close-paren>' -- ; run-time -- ) \ thisone- core,file paren
220: \G ** this will not get annotated. The alias in glocals.fs will instead **
221: \G It does not work to use "wordset-" prefix since this file is glossed
222: \G by cross.fs which doesn't have the same functionalty as makedoc.fs
223: [char] ) parse 2drop ; immediate
224:
225: : \ ( compilation 'ccc<newline>' -- ; run-time -- ) \ thisone- core-ext,block-ext backslash
226: \G ** this will not get annotated. The alias in glocals.fs will instead **
227: \G It does not work to use "wordset-" prefix since this file is glossed
228: \G by cross.fs which doesn't have the same functionalty as makedoc.fs
229: [ has? file [IF] ]
230: blk @
231: IF
232: >in @ c/l / 1+ c/l * >in !
233: EXIT
234: THEN
235: [ [THEN] ]
236: source >in ! drop ; immediate
237:
238: : \G ( compilation 'ccc<newline>' -- ; run-time -- ) \ gforth backslash-gee
239: \G Equivalent to @code{\} but used as a tag to annotate definition
240: \G comments into documentation.
241: POSTPONE \ ; immediate
242:
243: has? ec [IF]
244: AVariable forth-wordlist
245: : find-name ( c-addr u -- nt | 0 ) \ gforth
246: \g Find the name @i{c-addr u} in the current search
247: \g order. Return its @i{nt}, if found, otherwise 0.
248: forth-wordlist (f83find) ;
249: [ELSE]
250: \ \ object oriented search list 17mar93py
251:
252: \ word list structure:
253:
254: struct
255: cell% field find-method \ xt: ( c_addr u wid -- nt )
256: cell% field reveal-method \ xt: ( nt wid -- ) \ used by dofield:, must be field
257: cell% field rehash-method \ xt: ( wid -- ) \ re-initializes a "search-data" (hashtables)
258: cell% field hash-method \ xt: ( wid -- ) \ initializes ""
259: \ \ !! what else
260: end-struct wordlist-map-struct
261:
262: struct
263: cell% field wordlist-map \ pointer to a wordlist-map-struct
264: cell% field wordlist-id \ linked list of words (for WORDS etc.)
265: cell% field wordlist-link \ link field to other wordlists
266: cell% field wordlist-extend \ wordlist extensions (eg bucket offset)
267: end-struct wordlist-struct
268:
269: has? f83headerstring [IF]
270: : f83find ( addr len wordlist -- nt / false )
271: wordlist-id @ (f83find) ;
272: [ELSE]
273: : f83find ( addr len wordlist -- nt / false )
274: wordlist-id @ (listlfind) ;
275: [THEN]
276:
277: : initvoc ( wid -- )
278: dup wordlist-map @ hash-method perform ;
279:
280: \ Search list table: find reveal
281: Create f83search ( -- wordlist-map )
282: ' f83find A, ' drop A, ' drop A, ' drop A,
283:
284: here G f83search T A, NIL A, NIL A, NIL A,
285: AValue forth-wordlist \ variable, will be redefined by search.fs
286:
287: AVariable lookup forth-wordlist lookup !
288: \ !! last is user and lookup?! jaw
289: AVariable current ( -- addr ) \ gforth
290: \G @code{Variable} -- holds the @i{wid} of the compilation word list.
291: AVariable voclink forth-wordlist wordlist-link voclink !
292: \ lookup AValue context ( -- addr ) \ gforth
293: Defer context ( -- addr ) \ gforth
294: \G @code{context} @code{@@} is the @i{wid} of the word list at the
295: \G top of the search order.
296:
297: ' lookup is context
298: forth-wordlist current !
299:
300: : (search-wordlist) ( addr count wid -- nt | false )
301: dup wordlist-map @ find-method perform ;
302:
303: : search-wordlist ( c-addr count wid -- 0 | xt +-1 ) \ search
304: \G Search the word list identified by @i{wid} for the definition
305: \G named by the string at @i{c-addr count}. If the definition is
306: \G not found, return 0. If the definition is found return 1 (if
307: \G the definition is immediate) or -1 (if the definition is not
308: \G immediate) together with the @i{xt}. In Gforth, the @i{xt}
309: \G returned represents the interpretation semantics. ANS Forth
310: \G does not specify clearly what @i{xt} represents.
311: (search-wordlist) dup if
312: (name>intn)
313: then ;
314:
315: : find-name ( c-addr u -- nt | 0 ) \ gforth
316: \g Find the name @i{c-addr u} in the current search
317: \g order. Return its @i{nt}, if found, otherwise 0.
318: lookup @ (search-wordlist) ;
319: [THEN]
320:
321: \ \ header, finding, ticks 17dec92py
322:
323: \ The constants are defined as 32 bits, but then erased
324: \ and overwritten by the right ones
325:
326: has? f83headerstring [IF]
327: \ to save space, Gforth EC limits words to 31 characters
328: $80 constant alias-mask
329: $40 constant immediate-mask
330: $20 constant restrict-mask
331: $1f constant lcount-mask
332: [ELSE]
333: $80000000 constant alias-mask
334: 1 bits/char 1 - lshift
335: -1 cells allot bigendian [IF] c, 0 1 cells 1- times
336: [ELSE] 0 1 cells 1- times c, [THEN]
337: $40000000 constant immediate-mask
338: 1 bits/char 2 - lshift
339: -1 cells allot bigendian [IF] c, 0 1 cells 1- times
340: [ELSE] 0 1 cells 1- times c, [THEN]
341: $20000000 constant restrict-mask
342: 1 bits/char 3 - lshift
343: -1 cells allot bigendian [IF] c, 0 1 cells 1- times
344: [ELSE] 0 1 cells 1- times c, [THEN]
345: $1fffffff constant lcount-mask
346: 1 bits/char 3 - lshift 1 -
347: -1 cells allot bigendian [IF] c, -1 1 cells 1- times
348: [ELSE] -1 1 cells 1- times c, [THEN]
349: [THEN]
350:
351: \ higher level parts of find
352:
353: : flag-sign ( f -- 1|-1 )
354: \ true becomes 1, false -1
355: 0= 2* 1+ ;
356:
357: : ticking-compile-only-error ( ... -- )
358: -&2048 throw ;
359:
360: : compile-only-error ( ... -- )
361: -&14 throw ;
362:
363: : (cfa>int) ( cfa -- xt )
364: [ has? compiler [IF] ]
365: dup interpret/compile?
366: if
367: interpret/compile-int @
368: then
369: [ [THEN] ] ;
370:
371: : (x>int) ( cfa w -- xt )
372: \ get interpretation semantics of name
373: restrict-mask and [ has? rom [IF] ] 0= [ [THEN] ]
374: if
375: drop ['] compile-only-error
376: else
377: (cfa>int)
378: then ;
379:
380: has? f83headerstring [IF]
381: : name>string ( nt -- addr count ) \ gforth head-to-string
382: \g @i{addr count} is the name of the word represented by @i{nt}.
383: cell+ count lcount-mask and ;
384:
385: : ((name>)) ( nfa -- cfa )
386: name>string + cfaligned ;
387:
388: : (name>x) ( nfa -- cfa w )
389: \ cfa is an intermediate cfa and w is the flags cell of nfa
390: dup ((name>))
391: swap cell+ c@ dup alias-mask and 0=
392: IF
393: swap @ swap
394: THEN ;
395: [ELSE]
396: : name>string ( nt -- addr count ) \ gforth head-to-string
397: \g @i{addr count} is the name of the word represented by @i{nt}.
398: cell+ dup cell+ swap @ lcount-mask and ;
399:
400: : ((name>)) ( nfa -- cfa )
401: name>string + cfaligned ;
402:
403: : (name>x) ( nfa -- cfa w )
404: \ cfa is an intermediate cfa and w is the flags cell of nfa
405: dup ((name>))
406: swap cell+ @ dup alias-mask and 0=
407: IF
408: swap @ swap
409: THEN ;
410: [THEN]
411:
412: : name>int ( nt -- xt ) \ gforth
413: \G @i{xt} represents the interpretation semantics of the word
414: \G @i{nt}. If @i{nt} has no interpretation semantics (i.e. is
415: \G @code{compile-only}), @i{xt} is the execution token for
416: \G @code{ticking-compile-only-error}, which performs @code{-2048 throw}.
417: (name>x) (x>int) ;
418:
419: : name?int ( nt -- xt ) \ gforth
420: \G Like @code{name>int}, but perform @code{-2048 throw} if @i{nt}
421: \G has no interpretation semantics.
422: (name>x) restrict-mask and [ has? rom [IF] ] 0= [ [THEN] ]
423: if
424: ticking-compile-only-error \ does not return
425: then
426: (cfa>int) ;
427:
428: : (name>comp) ( nt -- w +-1 ) \ gforth
429: \G @i{w xt} is the compilation token for the word @i{nt}.
430: (name>x) >r
431: [ has? compiler [IF] ]
432: dup interpret/compile?
433: if
434: interpret/compile-comp @
435: then
436: [ [THEN] ]
437: r> immediate-mask and [ has? rom [IF] ] 0= [ [THEN] ] flag-sign
438: ;
439:
440: : (name>intn) ( nfa -- xt +-1 )
441: (name>x) tuck (x>int) ( w xt )
442: swap immediate-mask and [ has? rom [IF] ] 0= [ [THEN] ] flag-sign ;
443:
444: const Create ??? 0 , 3 , char ? c, char ? c, char ? c,
445: \ ??? is used by dovar:, must be created/:dovar
446:
447: [IFDEF] forthstart
448: \ if we have a forthstart we can define head? with it
449: \ otherwise leave out the head? check
450:
451: : head? ( addr -- f )
452: \G heuristic check whether addr is a name token; may deliver false
453: \G positives; addr must be a valid address; returns 1 for
454: \G particularly unsafe positives
455: \ we follow the link fields and check for plausibility; two
456: \ iterations should catch most false addresses: on the first
457: \ iteration, we may get an xt, on the second a code address (or
458: \ some code), which is typically not in the dictionary.
459: \ we added a third iteration for working with code and ;code words.
460: 3 0 do
461: dup dup aligned <> if \ protect @ against unaligned accesses
462: drop false unloop exit
463: then
464: dup @ dup
465: if ( addr addr1 )
466: dup rot forthstart within
467: if \ addr1 is outside forthstart..addr, not a head
468: drop false unloop exit
469: then ( addr1 )
470: else \ 0 in the link field, no further checks
471: 2drop 1 unloop exit \ this is very unsure, so return 1
472: then
473: loop
474: \ in dubio pro:
475: drop true ;
476:
477: : >head-noprim ( cfa -- nt ) \ gforth to-head-noprim
478: \ also heuristic
479: dup forthstart - max-name-length @ float+ cell+ min cell max cell ?do ( cfa )
480: dup i - dup @ [ alias-mask lcount-mask or ] literal
481: [ 1 bits/char 3 - lshift 1 - 1 bits/char 1 - lshift or
482: -1 cells allot bigendian [IF] c, -1 1 cells 1- times
483: [ELSE] -1 1 cells 1- times c, [THEN] ]
484: and ( cfa len|alias )
485: swap + cell+ cfaligned over alias-mask + =
486: if ( cfa )
487: dup i - cell - dup head?
488: if
489: nip unloop exit
490: then
491: drop
492: then
493: cell +loop
494: drop ??? ( wouldn't 0 be better? ) ;
495:
496: [ELSE]
497:
498: : >head-noprim ( cfa -- nt ) \ gforth to-head-noprim
499: $25 cell do ( cfa )
500: dup i - dup @ [ alias-mask lcount-mask or ] literal
501: [ 1 bits/char 3 - lshift 1 - 1 bits/char 1 - lshift or
502: -1 cells allot bigendian [IF] c, -1 1 cells 1- times
503: [ELSE] -1 1 cells 1- times c, [THEN] ]
504: and ( cfa len|alias )
505: swap + cell + cfaligned over alias-mask + =
506: if ( cfa ) i - cell - unloop exit
507: then
508: cell +loop
509: drop ??? ( wouldn't 0 be better? ) ;
510:
511: [THEN]
512:
513: cell% 2* 0 0 field >body ( xt -- a_addr ) \ core
514: \G Get the address of the body of the word represented by @i{xt} (the
515: \G address of the word's data field).
516: drop drop
517:
518: cell% -2 * 0 0 field body> ( xt -- a_addr )
519: drop drop
520:
521: has? standardthreading has? compiler and [IF]
522:
523: ' @ alias >code-address ( xt -- c_addr ) \ gforth
524: \G @i{c-addr} is the code address of the word @i{xt}.
525:
526: : >does-code ( xt -- a_addr ) \ gforth
527: \G If @i{xt} is the execution token of a child of a @code{DOES>} word,
528: \G @i{a-addr} is the start of the Forth code after the @code{DOES>};
529: \G Otherwise @i{a-addr} is 0.
530: dup @ dodoes: = if
531: cell+ @
532: else
533: drop 0
534: endif ;
535:
536: has? flash [IF] ' flash! [ELSE] ' ! [THEN]
537: alias code-address! ( c_addr xt -- ) \ gforth
538: \G Create a code field with code address @i{c-addr} at @i{xt}.
539:
540: : does-code! ( a_addr xt -- ) \ gforth
541: \G Create a code field at @i{xt} for a child of a @code{DOES>}-word;
542: \G @i{a-addr} is the start of the Forth code after @code{DOES>}.
543: [ has? flash [IF] ]
544: dodoes: over flash! cell+ flash!
545: [ [ELSE] ]
546: dodoes: over ! cell+ !
547: [ [THEN] ] ;
548:
549: ' drop alias does-handler! ( a_addr -- ) \ gforth
550: \G Create a @code{DOES>}-handler at address @i{a-addr}. Normally,
551: \G @i{a-addr} points just behind a @code{DOES>}.
552:
553: 2 cells constant /does-handler ( -- n ) \ gforth
554: \G The size of a @code{DOES>}-handler (includes possible padding).
555:
556: [THEN]
557:
558: : sfind ( c-addr u -- 0 / xt +-1 ) \ gforth-obsolete
559: find-name dup
560: if ( nt )
561: state @
562: if
563: (name>comp)
564: else
565: (name>intn)
566: then
567: then ;
568:
569: : find ( c-addr -- xt +-1 | c-addr 0 ) \ core,search
570: \G Search all word lists in the current search order for the
571: \G definition named by the counted string at @i{c-addr}. If the
572: \G definition is not found, return 0. If the definition is found
573: \G return 1 (if the definition has non-default compilation
574: \G semantics) or -1 (if the definition has default compilation
575: \G semantics). The @i{xt} returned in interpret state represents
576: \G the interpretation semantics. The @i{xt} returned in compile
577: \G state represented either the compilation semantics (for
578: \G non-default compilation semantics) or the run-time semantics
579: \G that the compilation semantics would @code{compile,} (for
580: \G default compilation semantics). The ANS Forth standard does
581: \G not specify clearly what the returned @i{xt} represents (and
582: \G also talks about immediacy instead of non-default compilation
583: \G semantics), so this word is questionable in portable programs.
584: \G If non-portability is ok, @code{find-name} and friends are
585: \G better (@pxref{Name token}).
586: dup count sfind dup
587: if
588: rot drop
589: then ;
590:
591: \ ticks in interpreter
592:
593: : (') ( "name" -- nt ) \ gforth
594: parse-name name-too-short?
595: find-name dup 0=
596: IF
597: drop -&13 throw
598: THEN ;
599:
600: : ' ( "name" -- xt ) \ core tick
601: \g @i{xt} represents @i{name}'s interpretation
602: \g semantics. Perform @code{-14 throw} if the word has no
603: \g interpretation semantics.
604: (') name?int ;
605:
606: has? compiler 0= [IF] \ interpreter only version of IS and TO
607:
608: : IS ' >body ! ;
609: ' IS Alias TO
610:
611: [THEN]
612:
613: \ \ the interpreter loop mar92py
614:
615: \ interpret 10mar92py
616:
617: Defer parser1 ( c-addr u -- ... xt)
618: \ "... xt" is the action to be performed by the text-interpretation of c-addr u
619:
620: : parser ( c-addr u -- ... )
621: \ text-interpret the word/number c-addr u, possibly producing a number
622: parser1 execute ;
623:
624: has? ec [IF]
625: ' (name) Alias parse-name
626: : no.extensions 2drop -&13 throw ;
627: ' no.extensions Alias compiler-notfound1
628: ' no.extensions Alias interpreter-notfound1
629: [ELSE]
630: Defer parse-name ( "name" -- c-addr u ) \ gforth
631: \G Get the next word from the input buffer
632: ' (name) IS parse-name
633:
634: ' parse-name alias parse-word ( -- c-addr u ) \ gforth-obsolete
635: \G old name for @code{parse-name}
636:
637: ' parse-name alias name ( -- c-addr u ) \ gforth-obsolete
638: \G old name for @code{parse-name}
639:
640: Defer compiler-notfound1 ( c-addr count -- ... xt )
641: Defer interpreter-notfound1 ( c-addr count -- ... xt )
642:
643: : no.extensions ( addr u -- )
644: 2drop -&13 throw ;
645: ' no.extensions IS compiler-notfound1
646: ' no.extensions IS interpreter-notfound1
647:
648: Defer before-word ( -- ) \ gforth
649: \ called before the text interpreter parses the next word
650: ' noop IS before-word
651: [THEN]
652:
653: has? backtrace [IF]
654: : interpret1 ( ... -- ... )
655: rp@ backtrace-rp0 !
656: BEGIN
657: ?stack [ has? EC 0= [IF] ] before-word [ [THEN] ] parse-name dup
658: WHILE
659: parser1 execute
660: REPEAT
661: 2drop ;
662:
663: : interpret ( ?? -- ?? ) \ gforth
664: \ interpret/compile the (rest of the) input buffer
665: backtrace-rp0 @ >r
666: ['] interpret1 catch
667: r> backtrace-rp0 !
668: throw>error ;
669: [ELSE]
670: : interpret ( ... -- ... )
671: BEGIN
672: ?stack [ has? EC 0= [IF] ] before-word [ [THEN] ] parse-name dup
673: WHILE
674: parser1 execute
675: REPEAT
676: 2drop ;
677: [THEN]
678:
679: \ interpreter 30apr92py
680:
681: \ not the most efficient implementations of interpreter and compiler
682: : interpreter1 ( c-addr u -- ... xt )
683: 2dup find-name dup
684: if
685: nip nip name>int
686: else
687: drop
688: 2dup 2>r snumber?
689: IF
690: 2rdrop ['] noop
691: ELSE
692: 2r> interpreter-notfound1
693: THEN
694: then ;
695:
696: ' interpreter1 IS parser1
697:
698: \ \ Query Evaluate 07apr93py
699:
700: has? file 0= [IF]
701: : sourceline# ( -- n ) 1 ;
702: [ELSE]
703: has? new-input 0= [IF]
704: Variable #fill-bytes
705: \G number of bytes read via (read-line) by the last refill
706: [THEN]
707: [THEN]
708:
709: has? new-input 0= [IF]
710: : input-start-line ( -- ) >in off ;
711: : refill ( -- flag ) \ core-ext,block-ext,file-ext
712: \G Attempt to fill the input buffer from the input source. When
713: \G the input source is the user input device, attempt to receive
714: \G input into the terminal input device. If successful, make the
715: \G result the input buffer, set @code{>IN} to 0 and return true;
716: \G otherwise return false. When the input source is a block, add 1
717: \G to the value of @code{BLK} to make the next block the input
718: \G source and current input buffer, and set @code{>IN} to 0;
719: \G return true if the new value of @code{BLK} is a valid block
720: \G number, false otherwise. When the input source is a text file,
721: \G attempt to read the next line from the file. If successful,
722: \G make the result the current input buffer, set @code{>IN} to 0
723: \G and return true; otherwise, return false. A successful result
724: \G includes receipt of a line containing 0 characters.
725: [ has? file [IF] ]
726: blk @ IF 1 blk +! true EXIT THEN
727: [ [THEN] ]
728: tib /line
729: [ has? file [IF] ]
730: loadfile @ ?dup
731: IF (read-line) throw #fill-bytes !
732: ELSE
733: [ [THEN] ]
734: sourceline# 0< IF 2drop false EXIT THEN
735: accept eof @ 0=
736: [ has? file [IF] ]
737: THEN
738: 1 loadline +!
739: [ [THEN] ]
740: swap #tib !
741: input-start-line ;
742:
743: : query ( -- ) \ core-ext
744: \G Make the user input device the input source. Receive input into
745: \G the Terminal Input Buffer. Set @code{>IN} to zero. OBSOLESCENT:
746: \G superceeded by @code{accept}.
747: [ has? file [IF] ]
748: blk off loadfile off
749: [ [THEN] ]
750: refill drop ;
751: [THEN]
752:
753: \ save-mem extend-mem
754:
755: has? os [IF]
756: : save-mem ( addr1 u -- addr2 u ) \ gforth
757: \g copy a memory block into a newly allocated region in the heap
758: swap >r
759: dup allocate throw
760: swap 2dup r> -rot move ;
761:
762: : free-mem-var ( addr -- )
763: \ addr is the address of a 2variable containing address and size
764: \ of a memory range; frees memory and clears the 2variable.
765: dup 2@ drop dup
766: if ( addr mem-start )
767: free throw
768: 0 0 rot 2!
769: else
770: 2drop
771: then ;
772:
773: : extend-mem ( addr1 u1 u -- addr addr2 u2 )
774: \ extend memory block allocated from the heap by u aus
775: \ the (possibly reallocated) piece is addr2 u2, the extension is at addr
776: over >r + dup >r resize throw
777: r> over r> + -rot ;
778: [THEN]
779:
780: \ EVALUATE 17may93jaw
781:
782: has? file 0= has? new-input 0= and [IF]
783: : push-file ( -- ) r>
784: tibstack @ >r >tib @ >r #tib @ >r
785: >tib @ tibstack @ = IF r@ tibstack +! THEN
786: tibstack @ >tib ! >in @ >r >r ;
787:
788: : pop-file ( throw-code -- throw-code )
789: r>
790: r> >in ! r> #tib ! r> >tib ! r> tibstack ! >r ;
791: [THEN]
792:
793: has? new-input 0= [IF]
794: : evaluate ( c-addr u -- ) \ core,block
795: \G Save the current input source specification. Store @code{-1} in
796: \G @code{source-id} and @code{0} in @code{blk}. Set @code{>IN} to
797: \G @code{0} and make the string @i{c-addr u} the input source
798: \G and input buffer. Interpret. When the parse area is empty,
799: \G restore the input source specification.
800: [ has? file [IF] ]
801: s" *evaluated string*" loadfilename>r
802: [ [THEN] ]
803: push-file #tib ! >tib !
804: input-start-line
805: [ has? file [IF] ]
806: blk off loadfile off -1 loadline !
807: [ [THEN] ]
808: ['] interpret catch
809: pop-file
810: [ has? file [IF] ]
811: r>loadfilename
812: [ [THEN] ]
813: throw ;
814: [THEN]
815:
816: \ \ Quit 13feb93py
817:
818: Defer 'quit
819:
820: has? ec 0= [IF]
821: Defer .status
822: [THEN]
823:
824: : prompt state @ IF ." compiled" EXIT THEN ." ok" ;
825:
826: : (quit) ( -- )
827: \ exits only through THROW etc.
828: BEGIN
829: [ has? ec [IF] ] cr [ [ELSE] ]
830: .status ['] cr catch if
831: [ has? OS [IF] ] >stderr [ [THEN] ]
832: cr ." Can't print to stdout, leaving" cr
833: \ if stderr does not work either, already DoError causes a hang
834: 2 (bye)
835: endif [ [THEN] ]
836: refill WHILE
837: interpret prompt
838: REPEAT
839: bye ;
840:
841: ' (quit) IS 'quit
842:
843: \ \ DOERROR (DOERROR) 13jun93jaw
844:
845: has? ec 0= [IF]
846: 8 Constant max-errors
847: 5 has? file 2 and + Constant /error
848: Variable error-stack 0 error-stack !
849: max-errors /error * cells allot
850: \ format of one cell:
851: \ source ( c-addr u )
852: \ last parsed lexeme ( c-addr u )
853: \ line-number
854: \ Loadfilename ( addr u )
855:
856: : error> ( -- c-addr1 u1 c-addr2 u2 line# [addr u] )
857: -1 error-stack +!
858: error-stack dup @
859: /error * cells + cell+
860: /error cells bounds DO
861: I @
862: cell +LOOP ;
863:
864: : >error ( c-addr1 u1 c-addr2 u2 line# [addr u] -- )
865: error-stack dup @ dup 1+
866: max-errors 1- min error-stack !
867: /error * cells + cell+
868: /error 1- cells bounds swap DO
869: I !
870: -1 cells +LOOP ;
871:
872: : input-error-data ( -- c-addr1 u1 c-addr2 u2 line# [addr u] )
873: \ error data for the current input, to be used by >error or .error-frame
874: source input-lexeme 2@ sourceline#
875: [ has? file [IF] ] sourcefilename [ [THEN] ] ;
876:
877: : dec. ( n -- ) \ gforth
878: \G Display @i{n} as a signed decimal number, followed by a space.
879: \ !! not used...
880: base @ decimal swap . base ! ;
881:
882: : dec.r ( u n -- ) \ gforth
883: \G Display @i{u} as a unsigned decimal number in a field @i{n}
884: \G characters wide.
885: base @ >r decimal .r r> base ! ;
886:
887: : hex. ( u -- ) \ gforth
888: \G Display @i{u} as an unsigned hex number, prefixed with a "$" and
889: \G followed by a space.
890: \ !! not used...
891: [char] $ emit base @ swap hex u. base ! ;
892:
893: : -trailing ( c_addr u1 -- c_addr u2 ) \ string dash-trailing
894: \G Adjust the string specified by @i{c-addr, u1} to remove all
895: \G trailing spaces. @i{u2} is the length of the modified string.
896: BEGIN
897: dup
898: WHILE
899: 1- 2dup + c@ bl <>
900: UNTIL 1+ THEN ;
901:
902: DEFER DOERROR
903:
904: has? backtrace [IF]
905: Defer dobacktrace ( -- )
906: ' noop IS dobacktrace
907: [THEN]
908:
909: : .error-string ( throw-code -- )
910: dup -2 =
911: IF "error @ ?dup IF count type THEN drop
912: ELSE .error
913: THEN ;
914:
915: : umin ( u1 u2 -- u )
916: 2dup u>
917: if
918: swap
919: then
920: drop ;
921:
922: Defer mark-start
923: Defer mark-end
924:
925: :noname ." >>>" ; IS mark-start
926: :noname ." <<<" ; IS mark-end
927:
928: : part-type ( addr1 u1 u -- addr2 u2 )
929: \ print first u characters of addr1 u1, addr2 u2 is the rest
930: over umin 2 pick over type /string ;
931:
932: : .error-line ( c-addr1 u1 c-addr2 u2 -- )
933: \ print error in line c-addr1 u1, where the error-causing lexeme
934: \ is c-addr2 u2
935: >r 2 pick - part-type ( c-addr3 u3 R: u2 )
936: mark-start r> part-type mark-end ( c-addr4 u4 )
937: type ;
938:
939: : .error-frame ( throwcode addr1 u1 addr2 u2 n2 [addr3 u3] -- throwcode )
940: \ addr3 u3: filename of included file - optional
941: \ n2: line number
942: \ addr2 u2: parsed lexeme (should be marked as causing the error)
943: \ addr1 u1: input line
944: error-stack @
945: IF ( throwcode addr1 u1 n0 n1 n2 [addr2 u2] )
946: [ has? file [IF] ] \ !! unbalanced stack effect
947: over IF
948: cr ." in file included from "
949: type ." :"
950: 0 dec.r 2drop 2drop
951: ELSE
952: 2drop 2drop 2drop drop
953: THEN
954: [ [THEN] ] ( throwcode addr1 u1 n0 n1 n2 )
955: ELSE ( throwcode addr1 u1 n0 n1 n2 [addr2 u2] )
956: [ has? file [IF] ]
957: cr type ." :"
958: [ [THEN] ] ( throwcode addr1 u1 n0 n1 n2 )
959: dup 0 dec.r ." : " 5 pick .error-string
960: IF \ if line# non-zero, there is a line
961: cr .error-line
962: ELSE
963: 2drop 2drop
964: THEN
965: THEN ;
966:
967: : (DoError) ( throw-code -- )
968: [ has? os [IF] ]
969: >stderr
970: [ [THEN] ]
971: input-error-data .error-frame
972: error-stack @ 0 ?DO
973: error>
974: .error-frame
975: LOOP
976: drop
977: [ has? backtrace [IF] ]
978: dobacktrace
979: [ [THEN] ]
980: normal-dp dpp ! ;
981:
982: ' (DoError) IS DoError
983:
984: [ELSE]
985: : dec. base @ >r decimal . r> base ! ;
986: : DoError ( throw-code -- )
987: cr source drop >in @ type ." <<< "
988: dup -2 = IF "error @ type drop EXIT THEN
989: .error ;
990: [THEN]
991:
992: : quit ( ?? -- ?? ) \ core
993: \G Empty the return stack, make the user input device
994: \G the input source, enter interpret state and start
995: \G the text interpreter.
996: rp0 @ rp! handler off clear-tibstack
997: [ has? new-input 0= [IF] ] >tib @ >r [ [THEN] ]
998: BEGIN
999: [ has? compiler [IF] ]
1000: [compile] [
1001: [ [THEN] ]
1002: \ stack depths may be arbitrary here
1003: ['] 'quit CATCH dup
1004: WHILE
1005: <# \ reset hold area, or we may get another error
1006: DoError
1007: \ stack depths may be arbitrary still (or again), so clear them
1008: clearstacks
1009: [ has? new-input [IF] ] clear-tibstack
1010: [ [ELSE] ] r@ >tib ! r@ tibstack !
1011: [ [THEN] ]
1012: REPEAT
1013: drop [ has? new-input [IF] ] clear-tibstack
1014: [ [ELSE] ] r> >tib !
1015: [ [THEN] ] ;
1016:
1017: \ \ Cold Boot 13feb93py
1018:
1019: : (bootmessage)
1020: ." Gforth " version-string type
1021: ." , Copyright (C) 1995-2006 Free Software Foundation, Inc." cr
1022: ." Gforth comes with ABSOLUTELY NO WARRANTY; for details type `license'"
1023: [ has? os [IF] ]
1024: cr ." Type `bye' to exit"
1025: [ [THEN] ] ;
1026:
1027: defer bootmessage \ gforth
1028: \G Hook (deferred word) executed right after interpreting the OS
1029: \G command-line arguments. Normally prints the Gforth startup
1030: \G message.
1031:
1032: has? file [IF]
1033: defer process-args
1034: [THEN]
1035:
1036: ' (bootmessage) IS bootmessage
1037:
1038: has? ec 0= [IF]
1039: Defer 'cold ( -- ) \ gforth tick-cold
1040: \G Hook (deferred word) for things to do right before interpreting the
1041: \G OS command-line arguments. Normally does some initializations that
1042: \G you also want to perform.
1043: ' noop IS 'cold
1044: [THEN]
1045:
1046: : cold ( -- ) \ gforth
1047: [ has? backtrace [IF] ]
1048: rp@ backtrace-rp0 !
1049: [ [THEN] ]
1050: [ has? file [IF] ]
1051: os-cold
1052: [ [THEN] ]
1053: [ has? ec 0= [IF] ]
1054: set-encoding-fixed-width
1055: 'cold
1056: [ [THEN] ]
1057: [ has? file [IF] ]
1058: process-args
1059: loadline off
1060: [ [THEN] ]
1061: bootmessage
1062: quit ;
1063:
1064: has? new-input 0= [IF]
1065: : clear-tibstack ( -- )
1066: [ has? glocals [IF] ]
1067: lp@ forthstart 7 cells + @ -
1068: [ [ELSE] ]
1069: [ has? os [IF] ]
1070: r0 @ forthstart 6 cells + @ -
1071: [ [ELSE] ]
1072: sp@ cell+
1073: [ [THEN] ]
1074: [ [THEN] ]
1075: dup >tib ! tibstack ! #tib off
1076: input-start-line ;
1077: [THEN]
1078:
1079: : boot ( path n **argv argc -- )
1080: [ has? no-userspace 0= [IF] ]
1081: main-task up!
1082: [ [THEN] ]
1083: [ has? os [IF] ]
1084: os-boot
1085: [ [THEN] ]
1086: [ has? rom [IF] ]
1087: ram-shadow dup @ dup -1 <> >r u> r> and IF
1088: ram-shadow 2@ ELSE
1089: ram-mirror ram-size THEN ram-start swap move
1090: [ [THEN] ]
1091: sp@ sp0 !
1092: [ has? peephole [IF] ]
1093: \ only needed for greedy static superinstruction selection
1094: \ primtable prepare-peephole-table TO peeptable
1095: [ [THEN] ]
1096: [ has? new-input [IF] ]
1097: current-input off
1098: [ [THEN] ]
1099: clear-tibstack
1100: 0 0 includefilename 2!
1101: rp@ rp0 !
1102: [ has? floating [IF] ]
1103: fp@ fp0 !
1104: [ [THEN] ]
1105: [ has? ec 0= [IF] ]
1106: handler off
1107: ['] cold catch dup -&2049 <> if \ broken pipe?
1108: DoError cr
1109: endif
1110: [ [ELSE] ]
1111: cold
1112: [ [THEN] ]
1113: [ has? os [IF] ]
1114: 1 (bye) \ !! determin exit code from throw code?
1115: [ [THEN] ]
1116: ;
1117:
1118: has? os [IF]
1119: : bye ( -- ) \ tools-ext
1120: [ has? file [IF] ]
1121: script? 0= IF cr THEN
1122: [ [ELSE] ]
1123: cr
1124: [ [THEN] ]
1125: 0 (bye) ;
1126: [THEN]
1127:
1128: \ **argv may be scanned by the C starter to get some important
1129: \ information, as -display and -geometry for an X client FORTH
1130: \ or space and stackspace overrides
1131:
1132: \ 0 arg contains, however, the name of the program.
1133:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>