Annotation of gforth/kernel/int.fs, revision 1.123
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.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.117 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.121 anton 139: dpl on
1.109 anton 140: over c@ '' = if
141: 1 /string s'>unumber? exit
142: endif
1.121 anton 143: base @ >r 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.120 anton 590: Defer parser1 ( c-addr u -- ... xt)
591: \ "... xt" is the action to be performed by the text-interpretation of c-addr u
592:
593: : parser ( c-addr u -- ... )
594: \ text-interpret the word/number c-addr u, possibly producing a number
595: parser1 execute ;
596:
1.119 anton 597: Defer parse-name ( "name" -- c-addr u ) \ gforth
1.55 anton 598: \G Get the next word from the input buffer
1.119 anton 599: ' (name) IS parse-name
1.77 anton 600:
1.119 anton 601: ' parse-name alias parse-word ( -- c-addr u ) \ gforth-obsolete
602: \G old name for @code{parse-name}
603:
604: ' parse-name alias name ( -- c-addr u ) \ gforth-obsolete
605: \G old name for @code{parse-name}
606:
1.120 anton 607: Defer compiler-notfound1 ( c-addr count -- ... xt )
608: Defer interpreter-notfound1 ( c-addr count -- ... xt )
1.1 pazsan 609:
610: : no.extensions ( addr u -- )
1.42 anton 611: 2drop -&13 throw ;
1.120 anton 612: ' no.extensions IS compiler-notfound1
613: ' no.extensions IS interpreter-notfound1
1.1 pazsan 614:
1.106 anton 615: Defer before-word ( -- ) \ gforth
616: \ called before the text interpreter parses the next word
617: ' noop IS before-word
618:
1.66 anton 619: : interpret1 ( ... -- ... )
1.33 jwilke 620: [ has? backtrace [IF] ]
1.24 anton 621: rp@ backtrace-rp0 !
1.33 jwilke 622: [ [THEN] ]
1.1 pazsan 623: BEGIN
1.106 anton 624: ?stack before-word name dup
1.1 pazsan 625: WHILE
1.120 anton 626: parser1 execute
1.1 pazsan 627: REPEAT
1.66 anton 628: 2drop ;
629:
630: : interpret ( ?? -- ?? ) \ gforth
631: \ interpret/compile the (rest of the) input buffer
632: [ has? backtrace [IF] ]
633: backtrace-rp0 @ >r
634: [ [THEN] ]
635: ['] interpret1 catch
1.65 anton 636: [ has? backtrace [IF] ]
637: r> backtrace-rp0 !
1.66 anton 638: [ [THEN] ]
639: throw ;
1.1 pazsan 640:
641: \ interpreter 30apr92py
642:
643: \ not the most efficient implementations of interpreter and compiler
1.120 anton 644: : interpreter1 ( c-addr u -- ... xt )
1.1 pazsan 645: 2dup find-name dup
646: if
1.120 anton 647: nip nip name>int
1.1 pazsan 648: else
649: drop
650: 2dup 2>r snumber?
651: IF
1.120 anton 652: 2rdrop ['] noop
1.1 pazsan 653: ELSE
1.120 anton 654: 2r> interpreter-notfound1
1.1 pazsan 655: THEN
656: then ;
657:
1.120 anton 658: ' interpreter1 IS parser1
1.1 pazsan 659:
660: \ \ Query Evaluate 07apr93py
661:
662: has? file 0= [IF]
1.12 pazsan 663: : sourceline# ( -- n ) 1 ;
1.61 pazsan 664: [ELSE]
1.64 pazsan 665: has? new-input 0= [IF]
1.58 pazsan 666: Variable #fill-bytes
667: \G number of bytes read via (read-line) by the last refill
1.61 pazsan 668: [THEN]
1.64 pazsan 669: [THEN]
1.58 pazsan 670:
1.64 pazsan 671: has? new-input 0= [IF]
1.1 pazsan 672: : refill ( -- flag ) \ core-ext,block-ext,file-ext
1.29 crook 673: \G Attempt to fill the input buffer from the input source. When
674: \G the input source is the user input device, attempt to receive
675: \G input into the terminal input device. If successful, make the
676: \G result the input buffer, set @code{>IN} to 0 and return true;
677: \G otherwise return false. When the input source is a block, add 1
678: \G to the value of @code{BLK} to make the next block the input
679: \G source and current input buffer, and set @code{>IN} to 0;
680: \G return true if the new value of @code{BLK} is a valid block
681: \G number, false otherwise. When the input source is a text file,
682: \G attempt to read the next line from the file. If successful,
683: \G make the result the current input buffer, set @code{>IN} to 0
684: \G and return true; otherwise, return false. A successful result
685: \G includes receipt of a line containing 0 characters.
1.12 pazsan 686: [ has? file [IF] ]
687: blk @ IF 1 blk +! true 0 >in ! EXIT THEN
688: [ [THEN] ]
689: tib /line
690: [ has? file [IF] ]
691: loadfile @ ?dup
1.59 pazsan 692: IF (read-line) throw #fill-bytes !
1.12 pazsan 693: ELSE
694: [ [THEN] ]
695: sourceline# 0< IF 2drop false EXIT THEN
696: accept true
697: [ has? file [IF] ]
698: THEN
699: 1 loadline +!
700: [ [THEN] ]
701: swap #tib ! 0 >in ! ;
1.1 pazsan 702:
703: : query ( -- ) \ core-ext
1.29 crook 704: \G Make the user input device the input source. Receive input into
705: \G the Terminal Input Buffer. Set @code{>IN} to zero. OBSOLESCENT:
706: \G superceeded by @code{accept}.
1.12 pazsan 707: [ has? file [IF] ]
708: blk off loadfile off
709: [ [THEN] ]
1.64 pazsan 710: refill drop ;
711: [THEN]
1.1 pazsan 712:
713: \ save-mem extend-mem
714:
715: has? os [IF]
716: : save-mem ( addr1 u -- addr2 u ) \ gforth
717: \g copy a memory block into a newly allocated region in the heap
718: swap >r
719: dup allocate throw
720: swap 2dup r> -rot move ;
721:
1.68 anton 722: : free-mem-var ( addr -- )
723: \ addr is the address of a 2variable containing address and size
724: \ of a memory range; frees memory and clears the 2variable.
725: dup 2@ drop dup
726: if ( addr mem-start )
727: free throw
728: 0 0 rot 2!
729: else
730: 2drop
731: then ;
732:
1.1 pazsan 733: : extend-mem ( addr1 u1 u -- addr addr2 u2 )
734: \ extend memory block allocated from the heap by u aus
1.105 anton 735: \ the (possibly reallocated) piece is addr2 u2, the extension is at addr
1.1 pazsan 736: over >r + dup >r resize throw
737: r> over r> + -rot ;
738: [THEN]
739:
740: \ EVALUATE 17may93jaw
741:
1.64 pazsan 742: has? file 0= has? new-input 0= and [IF]
1.1 pazsan 743: : push-file ( -- ) r>
1.12 pazsan 744: tibstack @ >r >tib @ >r #tib @ >r
1.1 pazsan 745: >tib @ tibstack @ = IF r@ tibstack +! THEN
746: tibstack @ >tib ! >in @ >r >r ;
747:
748: : pop-file ( throw-code -- throw-code )
749: r>
1.12 pazsan 750: r> >in ! r> #tib ! r> >tib ! r> tibstack ! >r ;
1.1 pazsan 751: [THEN]
752:
1.64 pazsan 753: has? new-input 0= [IF]
1.29 crook 754: : evaluate ( c-addr u -- ) \ core,block
1.40 crook 755: \G Save the current input source specification. Store @code{-1} in
756: \G @code{source-id} and @code{0} in @code{blk}. Set @code{>IN} to
757: \G @code{0} and make the string @i{c-addr u} the input source
758: \G and input buffer. Interpret. When the parse area is empty,
759: \G restore the input source specification.
1.64 pazsan 760: [ has? file [IF] ]
1.92 anton 761: s" *evaluated string*" loadfilename>r
1.64 pazsan 762: [ [THEN] ]
1.40 crook 763: push-file #tib ! >tib !
1.29 crook 764: >in off
765: [ has? file [IF] ]
766: blk off loadfile off -1 loadline !
767: [ [THEN] ]
768: ['] interpret catch
1.56 anton 769: pop-file
1.64 pazsan 770: [ has? file [IF] ]
1.92 anton 771: r>loadfilename
1.64 pazsan 772: [ [THEN] ]
1.56 anton 773: throw ;
1.64 pazsan 774: [THEN]
1.1 pazsan 775:
776: \ \ Quit 13feb93py
777:
778: Defer 'quit
779:
780: Defer .status
781:
782: : prompt state @ IF ." compiled" EXIT THEN ." ok" ;
783:
1.39 anton 784: : (quit) ( -- )
785: \ exits only through THROW etc.
786: BEGIN
1.98 anton 787: .status
788: ['] cr catch if
1.99 anton 789: >stderr cr ." Can't print to stdout, leaving" cr
1.98 anton 790: \ if stderr does not work either, already DoError causes a hang
791: 2 (bye)
792: endif
1.122 anton 793: refill WHILE
794: interpret prompt
795: REPEAT
796: bye ;
1.1 pazsan 797:
798: ' (quit) IS 'quit
799:
800: \ \ DOERROR (DOERROR) 13jun93jaw
801:
802: 8 Constant max-errors
1.112 pazsan 803: 4 has? file 2 and + Constant /error
1.1 pazsan 804: Variable error-stack 0 error-stack !
1.112 pazsan 805: max-errors /error * cells allot
1.1 pazsan 806: \ format of one cell:
807: \ source ( addr u )
808: \ >in
809: \ line-number
810: \ Loadfilename ( addr u )
811:
1.64 pazsan 812: : error> ( -- addr u >in line# [addr u] )
813: -1 error-stack +!
814: error-stack dup @
1.112 pazsan 815: /error * cells + cell+
816: /error cells bounds DO
1.64 pazsan 817: I @
818: cell +LOOP ;
819: : >error ( addr u >in line# [addr u] -- )
820: error-stack dup @ dup 1+
821: max-errors 1- min error-stack !
1.112 pazsan 822: /error * cells + cell+
823: /error 1- cells bounds swap DO
1.64 pazsan 824: I !
825: -1 cells +LOOP ;
826:
1.1 pazsan 827: : dec. ( n -- ) \ gforth
1.40 crook 828: \G Display @i{n} as a signed decimal number, followed by a space.
829: \ !! not used...
1.1 pazsan 830: base @ decimal swap . base ! ;
831:
1.111 anton 832: : dec.r ( u n -- ) \ gforth
833: \G Display @i{u} as a unsigned decimal number in a field @i{n}
834: \G characters wide.
835: base @ >r decimal .r r> base ! ;
1.23 jwilke 836:
1.1 pazsan 837: : hex. ( u -- ) \ gforth
1.40 crook 838: \G Display @i{u} as an unsigned hex number, prefixed with a "$" and
1.17 crook 839: \G followed by a space.
1.40 crook 840: \ !! not used...
1.33 jwilke 841: [char] $ emit base @ swap hex u. base ! ;
1.1 pazsan 842:
1.94 anton 843: : -trailing ( c_addr u1 -- c_addr u2 ) \ string dash-trailing
844: \G Adjust the string specified by @i{c-addr, u1} to remove all
845: \G trailing spaces. @i{u2} is the length of the modified string.
846: BEGIN
1.102 pazsan 847: dup
1.94 anton 848: WHILE
1.102 pazsan 849: 1- 2dup + c@ bl <>
850: UNTIL 1+ THEN ;
1.94 anton 851:
1.1 pazsan 852: DEFER DOERROR
1.33 jwilke 853:
854: has? backtrace [IF]
1.15 anton 855: Defer dobacktrace ( -- )
856: ' noop IS dobacktrace
1.33 jwilke 857: [THEN]
1.1 pazsan 858:
1.23 jwilke 859: : .error-string ( throw-code -- )
860: dup -2 =
861: IF "error @ ?dup IF count type THEN drop
862: ELSE .error
863: THEN ;
864:
1.111 anton 865: : umin ( u1 u2 -- u )
866: 2dup u>
867: if
868: swap
869: then
870: drop ;
871:
1.112 pazsan 872: Defer mark-start
873: Defer mark-end
874:
875: :noname ." >>>" ; IS mark-start
876: :noname ." <<<" ; IS mark-end
877:
1.111 anton 878: : .error-line ( addr1 u1 n1 -- )
879: \ print error ending at char n1 in line addr1 u1
880: \ should work with UTF-8 (whitespace check looks ok)
881: over umin \ protect against wrong n1
882: swap >r ( addr1 n1 R: u1 )
883: -trailing 1- \ last non-space
884: 0 >r BEGIN \ search for the first non-space
885: 2dup + c@ bl > WHILE
886: r> 1+ >r 1- dup 0< UNTIL THEN 1+
887: ( addr1 n2 r: u1 namelen )
1.112 pazsan 888: 2dup type mark-start
1.111 anton 889: r> -rot r> swap /string ( namelen addr2 u2 )
1.112 pazsan 890: >r swap 2dup type mark-end ( addr2 namelen r: u2 )
1.111 anton 891: r> swap /string type ;
892:
1.64 pazsan 893: : .error-frame ( throwcode addr1 u1 n1 n2 [addr2 u2] -- throwcode )
894: \ addr2 u2: filename of included file - optional
1.23 jwilke 895: \ n2: line number
896: \ n1: error position in input line
897: \ addr1 u1: input line
1.1 pazsan 898: cr error-stack @
1.111 anton 899: IF ( throwcode addr1 u1 n1 n2 [addr2 u2] )
900: [ has? file [IF] ] \ !! unbalanced stack effect
1.64 pazsan 901: ." in file included from "
902: type ." :"
1.111 anton 903: [ [THEN] ] ( throwcode addr1 u1 n1 n2 )
904: 0 dec.r drop 2drop
905: ELSE ( throwcode addr1 u1 n1 n2 [addr2 u2] )
1.64 pazsan 906: [ has? file [IF] ]
907: type ." :"
1.111 anton 908: [ [THEN] ] ( throwcode addr1 u1 n1 n2 )
909: dup 0 dec.r ." : " 4 pick .error-string
910: IF \ if line# non-zero, there is a line
911: cr .error-line
1.57 anton 912: ELSE
913: 2drop drop
914: THEN
1.23 jwilke 915: THEN ;
1.1 pazsan 916:
917: : (DoError) ( throw-code -- )
918: [ has? os [IF] ]
1.8 pazsan 919: >stderr
1.1 pazsan 920: [ [THEN] ]
1.111 anton 921: source >in @ sourceline# [ has? file [IF] ] \ !! unbalanced stack effect
1.64 pazsan 922: sourcefilename
923: [ [THEN] ] .error-frame
1.1 pazsan 924: error-stack @ 0 ?DO
1.64 pazsan 925: error>
1.1 pazsan 926: .error-frame
927: LOOP
1.33 jwilke 928: drop
929: [ has? backtrace [IF] ]
930: dobacktrace
931: [ [THEN] ]
1.8 pazsan 932: normal-dp dpp ! ;
1.1 pazsan 933:
934: ' (DoError) IS DoError
935:
936: : quit ( ?? -- ?? ) \ core
1.27 crook 937: \G Empty the return stack, make the user input device
938: \G the input source, enter interpret state and start
939: \G the text interpreter.
1.64 pazsan 940: rp0 @ rp! handler off clear-tibstack
941: [ has? new-input 0= [IF] ] >tib @ >r [ [THEN] ]
1.1 pazsan 942: BEGIN
943: [ has? compiler [IF] ]
1.104 anton 944: [compile] [
1.1 pazsan 945: [ [THEN] ]
1.104 anton 946: \ stack depths may be arbitrary here
1.1 pazsan 947: ['] 'quit CATCH dup
948: WHILE
1.104 anton 949: <# \ reset hold area, or we may get another error
950: DoError
951: \ stack depths may be arbitrary still (or again), so clear them
952: clearstacks
953: [ has? new-input [IF] ] clear-tibstack
954: [ [ELSE] ] r@ >tib ! r@ tibstack !
955: [ [THEN] ]
1.1 pazsan 956: REPEAT
1.64 pazsan 957: drop [ has? new-input [IF] ] clear-tibstack
958: [ [ELSE] ] r> >tib !
959: [ [THEN] ] ;
1.1 pazsan 960:
961: \ \ Cold Boot 13feb93py
962:
963: : (bootmessage)
1.101 anton 964: ." Gforth " version-string type
1.123 ! anton 965: ." , Copyright (C) 1995-2004,2005 Free Software Foundation, Inc." cr
1.101 anton 966: ." Gforth comes with ABSOLUTELY NO WARRANTY; for details type `license'"
1.1 pazsan 967: [ has? os [IF] ]
968: cr ." Type `bye' to exit"
969: [ [THEN] ] ;
970:
971: defer bootmessage
972: defer process-args
973:
974: ' (bootmessage) IS bootmessage
975:
1.10 anton 976: Defer 'cold ( -- ) \ gforth tick-cold
1.1 pazsan 977: \ hook (deferred word) for things to do right before interpreting the
978: \ command-line arguments
979: ' noop IS 'cold
980:
981:
1.76 jwilke 982: AVariable init8 NIL init8 !
1.1 pazsan 983:
984: : cold ( -- ) \ gforth
1.44 anton 985: [ has? backtrace [IF] ]
986: rp@ backtrace-rp0 !
987: [ [THEN] ]
1.1 pazsan 988: [ has? file [IF] ]
1.78 pazsan 989: os-cold
1.1 pazsan 990: [ [THEN] ]
1.116 anton 991: set-encoding-fixed-width
1.1 pazsan 992: 'cold
993: init8 chainperform
994: [ has? file [IF] ]
1.91 anton 995: s" *the terminal*" loadfilename 2!
1.8 pazsan 996: process-args
1.12 pazsan 997: loadline off
1.1 pazsan 998: [ [THEN] ]
999: bootmessage
1.12 pazsan 1000: quit ;
1.1 pazsan 1001:
1.64 pazsan 1002: has? new-input 0= [IF]
1.5 anton 1003: : clear-tibstack ( -- )
1004: [ has? glocals [IF] ]
1005: lp@ forthstart 7 cells + @ -
1006: [ [ELSE] ]
1007: [ has? os [IF] ]
1.8 pazsan 1008: r0 @ forthstart 6 cells + @ -
1.5 anton 1009: [ [ELSE] ]
1.16 pazsan 1010: sp@ $10 cells +
1.5 anton 1011: [ [THEN] ]
1012: [ [THEN] ]
1013: dup >tib ! tibstack ! #tib off >in off ;
1.64 pazsan 1014: [THEN]
1.5 anton 1015:
1.64 pazsan 1016: : boot ( path n **argv argc -- )
1.1 pazsan 1017: main-task up!
1018: [ has? os [IF] ]
1.78 pazsan 1019: os-boot
1.1 pazsan 1020: [ [THEN] ]
1021: sp@ sp0 !
1.74 pazsan 1022: [ has? peephole [IF] ]
1.87 anton 1023: \ only needed for greedy static superinstruction selection
1024: \ primtable prepare-peephole-table TO peeptable
1.74 pazsan 1025: [ [THEN] ]
1.64 pazsan 1026: [ has? new-input [IF] ]
1027: current-input off
1028: [ [THEN] ]
1.5 anton 1029: clear-tibstack
1.1 pazsan 1030: rp@ rp0 !
1031: [ has? floating [IF] ]
1032: fp@ fp0 !
1033: [ [THEN] ]
1.46 anton 1034: handler off
1.98 anton 1035: ['] cold catch dup -&2049 <> if \ broken pipe?
1036: DoError cr
1037: endif
1.1 pazsan 1038: [ has? os [IF] ]
1.35 anton 1039: 1 (bye) \ !! determin exit code from throw code?
1.1 pazsan 1040: [ [THEN] ]
1041: ;
1042:
1043: has? os [IF]
1044: : bye ( -- ) \ tools-ext
1045: [ has? file [IF] ]
1046: script? 0= IF cr THEN
1047: [ [ELSE] ]
1048: cr
1049: [ [THEN] ]
1050: 0 (bye) ;
1051: [THEN]
1052:
1053: \ **argv may be scanned by the C starter to get some important
1054: \ information, as -display and -geometry for an X client FORTH
1055: \ or space and stackspace overrides
1056:
1057: \ 0 arg contains, however, the name of the program.
1058:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>