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