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