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