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