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