Annotation of gforth/kernel.fs, revision 1.10
1.1 pazsan 1: \ kernel.fs GForth kernel 17dec92py
2:
3: \ Copyright (C) 1995 Free Software Foundation, Inc.
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
19: \ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20:
21: \ Idea and implementation: Bernd Paysan (py)
22:
23: HEX
24:
25: \ labels for some code addresses
26:
27: : docon: ( -- addr ) \ gforth
1.3 pazsan 28: \G the code address of a @code{CONSTANT}
1.1 pazsan 29: ['] bl >code-address ;
30:
31: : docol: ( -- addr ) \ gforth
1.3 pazsan 32: \G the code address of a colon definition
1.1 pazsan 33: ['] docon: >code-address ;
34:
35: : dovar: ( -- addr ) \ gforth
1.3 pazsan 36: \G the code address of a @code{CREATE}d word
1.1 pazsan 37: ['] udp >code-address ;
38:
39: : douser: ( -- addr ) \ gforth
1.3 pazsan 40: \G the code address of a @code{USER} variable
1.1 pazsan 41: ['] s0 >code-address ;
42:
43: : dodefer: ( -- addr ) \ gforth
1.3 pazsan 44: \G the code address of a @code{defer}ed word
1.1 pazsan 45: ['] source >code-address ;
46:
47: : dofield: ( -- addr ) \ gforth
1.3 pazsan 48: \G the code address of a @code{field}
1.1 pazsan 49: ['] reveal-method >code-address ;
50:
51: NIL AConstant NIL \ gforth
52:
53: \ Bit string manipulation 06oct92py
54:
55: \ Create bits 80 c, 40 c, 20 c, 10 c, 8 c, 4 c, 2 c, 1 c,
56: \ DOES> ( n -- ) + c@ ;
57:
58: \ : >bit ( addr n -- c-addr mask ) 8 /mod rot + swap bits ;
59: \ : +bit ( addr n -- ) >bit over c@ or swap c! ;
60:
61: \ : relinfo ( -- addr ) forthstart dup @ + !!bug!! ;
62: \ : >rel ( addr -- n ) forthstart - ;
63: \ : relon ( addr -- ) relinfo swap >rel cell / +bit ;
64:
65: \ here allot , c, A, 17dec92py
66:
67: : dp ( -- addr ) \ gforth
68: dpp @ ;
69: : here ( -- here ) \ core
70: dp @ ;
71: : allot ( n -- ) \ core
72: dp +! ;
73: : c, ( c -- ) \ core
74: here 1 chars allot c! ;
75: : , ( x -- ) \ core
76: here cell allot ! ;
77: : 2, ( w1 w2 -- ) \ gforth
78: here 2 cells allot 2! ;
79:
80: \ : aligned ( addr -- addr' ) \ core
81: \ [ cell 1- ] Literal + [ -1 cells ] Literal and ;
82: : align ( -- ) \ core
83: here dup aligned swap ?DO bl c, LOOP ;
84:
85: \ : faligned ( addr -- f-addr ) \ float
86: \ [ 1 floats 1- ] Literal + [ -1 floats ] Literal and ;
87:
88: : falign ( -- ) \ float
89: here dup faligned swap
90: ?DO
91: bl c,
92: LOOP ;
93:
94: \ !! this is machine-dependent, but works on all but the strangest machines
95: ' faligned Alias maxaligned ( addr1 -- addr2 ) \ gforth
96: ' falign Alias maxalign ( -- ) \ gforth
97:
98: \ !! machine-dependent and won't work if "0 >body" <> "0 >body maxaligned"
99: ' maxaligned Alias cfaligned ( addr1 -- addr2 ) \ gforth
100: \ the code field is aligned if its body is maxaligned
101: ' maxalign Alias cfalign ( -- ) \ gforth
102:
103: : chars ( n1 -- n2 ) \ core
104: ; immediate
105:
106:
107: \ : A! ( addr1 addr2 -- ) \ gforth
108: \ dup relon ! ;
109: \ : A, ( addr -- ) \ gforth
110: \ here cell allot A! ;
111: ' ! alias A! ( addr1 addr2 -- ) \ gforth
112: ' , alias A, ( addr -- ) \ gforth
113:
114:
115: \ on off 23feb93py
116:
117: : on ( addr -- ) \ gforth
118: true swap ! ;
119: : off ( addr -- ) \ gforth
120: false swap ! ;
121:
122: \ dabs roll 17may93jaw
123:
124: : dabs ( d1 -- d2 ) \ double
125: dup 0< IF dnegate THEN ;
126:
127: : roll ( x0 x1 .. xn n -- x1 .. xn x0 ) \ core-ext
128: dup 1+ pick >r
129: cells sp@ cell+ dup cell+ rot move drop r> ;
130:
131: \ name> found 17dec92py
132:
133: $80 constant alias-mask \ set when the word is not an alias!
134: $40 constant immediate-mask
135: $20 constant restrict-mask
136:
137: : ((name>)) ( nfa -- cfa )
138: name>string + cfaligned ;
139:
140: : (name>x) ( nfa -- cfa b )
141: \ cfa is an intermediate cfa and b is the flags byte of nfa
142: dup ((name>))
143: swap cell+ c@ dup alias-mask and 0=
144: IF
145: swap @ swap
146: THEN ;
147:
148: \ place bounds 13feb93py
149:
150: : place ( addr len to -- ) \ gforth
151: over >r rot over 1+ r> move c! ;
152: : bounds ( beg count -- end beg ) \ gforth
153: over + swap ;
154:
155: : save-mem ( addr1 u -- addr2 u ) \ gforth
1.4 anton 156: \g copy a memory block into a newly allocated region in the heap
1.1 pazsan 157: swap >r
158: dup allocate throw
159: swap 2dup r> -rot move ;
160:
161: : extend-mem ( addr1 u1 u -- addr addr2 u2 )
162: \ extend memory block allocated from the heap by u aus
163: \ the (possibly reallocated piece is addr2 u2, the extension is at addr
164: over >r + dup >r resize throw
165: r> over r> + -rot ;
166:
167: \ input stream primitives 23feb93py
168:
169: : tib ( -- c-addr ) \ core-ext
170: \ obsolescent
171: >tib @ ;
172: Defer source ( -- addr count ) \ core
173: \ used by dodefer:, must be defer
174: : (source) ( -- addr count )
175: tib #tib @ ;
176: ' (source) IS source
177:
178: \ (word) 22feb93py
179:
180: : scan ( addr1 n1 char -- addr2 n2 ) \ gforth
181: \ skip all characters not equal to char
182: >r
183: BEGIN
184: dup
185: WHILE
186: over c@ r@ <>
187: WHILE
188: 1 /string
189: REPEAT THEN
190: rdrop ;
191: : skip ( addr1 n1 char -- addr2 n2 ) \ gforth
192: \ skip all characters equal to char
193: >r
194: BEGIN
195: dup
196: WHILE
197: over c@ r@ =
198: WHILE
199: 1 /string
200: REPEAT THEN
201: rdrop ;
202:
203: : (word) ( addr1 n1 char -- addr2 n2 )
204: dup >r skip 2dup r> scan nip - ;
205:
206: \ (word) should fold white spaces
207: \ this is what (parse-white) does
208:
209: \ word parse 23feb93py
210:
211: : parse-word ( char -- addr len ) \ gforth
212: source 2dup >r >r >in @ over min /string
213: rot dup bl = IF drop (parse-white) ELSE (word) THEN
214: 2dup + r> - 1+ r> min >in ! ;
215: : word ( char -- addr ) \ core
216: parse-word here place bl here count + c! here ;
217:
218: : parse ( char -- addr len ) \ core-ext
219: >r source >in @ over min /string over swap r> scan >r
220: over - dup r> IF 1+ THEN >in +! ;
221:
222: \ name 13feb93py
223:
224: : capitalize ( addr len -- addr len ) \ gforth
225: 2dup chars chars bounds
226: ?DO I c@ toupper I c! 1 chars +LOOP ;
227: : (name) ( -- c-addr count )
228: source 2dup >r >r >in @ /string (parse-white)
229: 2dup + r> - 1+ r> min >in ! ;
230: \ name count ;
231:
232: : name-too-short? ( c-addr u -- c-addr u )
233: dup 0= -&16 and throw ;
234:
235: : name-too-long? ( c-addr u -- c-addr u )
236: dup $1F u> -&19 and throw ;
237:
238: \ Literal 17dec92py
239:
240: : Literal ( compilation n -- ; run-time -- n ) \ core
241: postpone lit , ; immediate restrict
242: : ALiteral ( compilation addr -- ; run-time -- addr ) \ gforth
243: postpone lit A, ; immediate restrict
244:
245: : char ( 'char' -- n ) \ core
246: bl word char+ c@ ;
247: : [char] ( compilation 'char' -- ; run-time -- n )
248: char postpone Literal ; immediate restrict
249:
250: : (compile) ( -- ) \ gforth
251: r> dup cell+ >r @ compile, ;
252:
1.5 anton 253: : postpone, ( w xt -- )
254: \g Compiles the compilation semantics represented by @var{w xt}.
255: dup ['] execute =
256: if
257: drop compile,
258: else
259: dup ['] compile, =
260: if
261: drop POSTPONE (compile) compile,
262: else
263: swap POSTPONE aliteral compile,
264: then
265: then ;
266:
267: : POSTPONE ( "name" -- ) \ core
268: \g Compiles the compilation semantics of @var{name}.
269: COMP' postpone, ; immediate restrict
1.1 pazsan 270:
1.2 anton 271: : interpret/compile: ( interp-xt comp-xt "name" -- ) \ gforth
1.1 pazsan 272: Create immediate swap A, A,
273: DOES>
274: abort" executed primary cfa of an interpret/compile: word" ;
275: \ state @ IF cell+ THEN perform ;
276:
277: \ Use (compile) for the old behavior of compile!
278:
279: \ digit? 17dec92py
280:
281: : digit? ( char -- digit true/ false ) \ gforth
282: base @ $100 =
283: IF
284: true EXIT
285: THEN
286: toupper [char] 0 - dup 9 u> IF
287: [ 'A '9 1 + - ] literal -
288: dup 9 u<= IF
289: drop false EXIT
290: THEN
291: THEN
292: dup base @ u>= IF
293: drop false EXIT
294: THEN
295: true ;
296:
297: : accumulate ( +d0 addr digit - +d1 addr )
298: swap >r swap base @ um* drop rot base @ um* d+ r> ;
299:
300: : >number ( d addr count -- d addr count ) \ core
301: 0
302: ?DO
303: count digit?
304: WHILE
305: accumulate
306: LOOP
307: 0
308: ELSE
309: 1- I' I -
310: UNLOOP
311: THEN ;
312:
313: \ number? number 23feb93py
314:
315: Create bases 10 , 2 , A , 100 ,
1.3 pazsan 316: \ 16 2 10 character
1.1 pazsan 317: \ !! this saving and restoring base is an abomination! - anton
318: : getbase ( addr u -- addr' u' )
319: over c@ [char] $ - dup 4 u<
320: IF
321: cells bases + @ base ! 1 /string
322: ELSE
323: drop
324: THEN ;
325: : s>number ( addr len -- d )
326: base @ >r dpl on
327: over c@ '- = dup >r
328: IF
329: 1 /string
330: THEN
331: getbase dpl on 0 0 2swap
332: BEGIN
333: dup >r >number dup
334: WHILE
335: dup r> -
336: WHILE
337: dup dpl ! over c@ [char] . =
338: WHILE
339: 1 /string
340: REPEAT THEN
341: 2drop rdrop dpl off
342: ELSE
343: 2drop rdrop r>
344: IF
345: dnegate
346: THEN
347: THEN
348: r> base ! ;
349:
350: : snumber? ( c-addr u -- 0 / n -1 / d 0> )
351: s>number dpl @ 0=
352: IF
353: 2drop false EXIT
354: THEN
355: dpl @ dup 0> 0= IF
356: nip
357: THEN ;
358: : number? ( string -- string 0 / n -1 / d 0> )
359: dup >r count snumber? dup if
360: rdrop
361: else
362: r> swap
363: then ;
364: : s>d ( n -- d ) \ core s-to-d
365: dup 0< ;
366: : number ( string -- d )
367: number? ?dup 0= abort" ?" 0<
368: IF
369: s>d
370: THEN ;
371:
372: \ space spaces ud/mod 21mar93py
373: decimal
374: Create spaces ( u -- ) \ core
375: bl 80 times \ times from target compiler! 11may93jaw
376: DOES> ( u -- )
377: swap
378: 0 max 0 ?DO I' I - &80 min 2dup type +LOOP drop ;
379: Create backspaces
380: 08 80 times \ times from target compiler! 11may93jaw
381: DOES> ( u -- )
382: swap
383: 0 max 0 ?DO I' I - &80 min 2dup type +LOOP drop ;
384: hex
385: : space ( -- ) \ core
386: 1 spaces ;
387:
388: : ud/mod ( ud1 u2 -- urem udquot ) \ gforth
389: >r 0 r@ um/mod r> swap >r
390: um/mod r> ;
391:
392: : pad ( -- addr ) \ core-ext
393: here [ $20 8 2* cells + 2 + cell+ ] Literal + aligned ;
394:
395: \ hold <# #> sign # #s 25jan92py
396:
397: : hold ( char -- ) \ core
398: pad cell - -1 chars over +! @ c! ;
399:
400: : <# ( -- ) \ core less-number-sign
401: pad cell - dup ! ;
402:
403: : #> ( xd -- addr u ) \ core number-sign-greater
404: 2drop pad cell - dup @ tuck - ;
405:
406: : sign ( n -- ) \ core
407: 0< IF [char] - hold THEN ;
408:
409: : # ( ud1 -- ud2 ) \ core number-sign
410: base @ 2 max ud/mod rot 9 over <
411: IF
412: [ char A char 9 - 1- ] Literal +
413: THEN
414: [char] 0 + hold ;
415:
416: : #s ( +d -- 0 0 ) \ core number-sign-s
417: BEGIN
418: # 2dup d0=
419: UNTIL ;
420:
421: \ print numbers 07jun92py
422:
423: : d.r ( d n -- ) \ double d-dot-r
424: >r tuck dabs <# #s rot sign #>
425: r> over - spaces type ;
426:
427: : ud.r ( ud n -- ) \ gforth u-d-dot-r
428: >r <# #s #> r> over - spaces type ;
429:
430: : .r ( n1 n2 -- ) \ core-ext dot-r
431: >r s>d r> d.r ;
432: : u.r ( u n -- ) \ core-ext u-dot-r
433: 0 swap ud.r ;
434:
435: : d. ( d -- ) \ double d-dot
436: 0 d.r space ;
437: : ud. ( ud -- ) \ gforth u-d-dot
438: 0 ud.r space ;
439:
440: : . ( n -- ) \ core dot
441: s>d d. ;
442: : u. ( u -- ) \ core u-dot
443: 0 ud. ;
444:
445: \ catch throw 23feb93py
446: \ bounce 08jun93jaw
447:
448: \ !! allow the user to add rollback actions anton
449: \ !! use a separate exception stack? anton
450:
451: : lp@ ( -- addr ) \ gforth l-p-fetch
452: laddr# [ 0 , ] ;
453:
454: : catch ( x1 .. xn xt -- y1 .. ym 0 / z1 .. zn error ) \ exception
1.3 pazsan 455: sp@ >r
456: fp@ >r
457: lp@ >r
458: handler @ >r
459: rp@ handler !
460: execute
461: r> handler ! rdrop rdrop rdrop 0 ;
1.1 pazsan 462:
463: : throw ( y1 .. ym error/0 -- y1 .. ym / z1 .. zn error ) \ exception
464: ?DUP IF
1.3 pazsan 465: [ here 9 cells ! ] \ entry point for signal handler
466: handler @ dup 0= IF
467: 2 (bye)
468: THEN
469: rp!
1.1 pazsan 470: r> handler !
471: r> lp!
472: r> fp!
1.3 pazsan 473: r> swap >r sp! drop r>
1.1 pazsan 474: THEN ;
475:
476: \ Bouncing is very fine,
477: \ programming without wasting time... jaw
478: : bounce ( y1 .. ym error/0 -- y1 .. ym error / y1 .. ym ) \ gforth
479: \ a throw without data or fp stack restauration
480: ?DUP IF
1.3 pazsan 481: handler @ rp!
482: r> handler !
483: r> lp!
484: rdrop
485: rdrop
1.1 pazsan 486: THEN ;
487:
488: \ ?stack 23feb93py
489:
490: : ?stack ( ?? -- ?? ) \ gforth
491: sp@ s0 @ > IF -4 throw THEN
492: fp@ f0 @ > IF -&45 throw THEN ;
493: \ ?stack should be code -- it touches an empty stack!
494:
495: \ interpret 10mar92py
496:
497: Defer parser
498: Defer name ( -- c-addr count ) \ gforth
499: \ get the next word from the input buffer
500: ' (name) IS name
501: Defer compiler-notfound ( c-addr count -- )
502: Defer interpreter-notfound ( c-addr count -- )
503:
504: : no.extensions ( addr u -- )
505: 2drop -&13 bounce ;
506: ' no.extensions IS compiler-notfound
507: ' no.extensions IS interpreter-notfound
508:
509: : compile-only-error ( ... -- )
510: -&14 throw ;
511:
512: : interpret ( ?? -- ?? ) \ gforth
513: \ interpret/compile the (rest of the) input buffer
514: BEGIN
515: ?stack name dup
516: WHILE
517: parser
518: REPEAT
519: 2drop ;
520:
521: \ interpreter compiler 30apr92py
522:
523: \ not the most efficient implementations of interpreter and compiler
524: : interpreter ( c-addr u -- )
525: 2dup find-name dup
526: if
527: nip nip name>int execute
528: else
529: drop
530: 2dup 2>r snumber?
531: IF
532: 2rdrop
533: ELSE
534: 2r> interpreter-notfound
535: THEN
536: then ;
537:
538: : compiler ( c-addr u -- )
539: 2dup find-name dup
1.4 anton 540: if ( c-addr u nt )
1.1 pazsan 541: nip nip name>comp execute
542: else
543: drop
544: 2dup snumber? dup
545: IF
546: 0>
547: IF
548: swap postpone Literal
549: THEN
550: postpone Literal
551: 2drop
552: ELSE
553: drop compiler-notfound
554: THEN
555: then ;
556:
557: ' interpreter IS parser
558:
559: : [ ( -- ) \ core left-bracket
560: ['] interpreter IS parser state off ; immediate
561: : ] ( -- ) \ core right-bracket
562: ['] compiler IS parser state on ;
563:
564: here 0 , \ just a dummy, the real value of locals-list is patched into it in glocals.fs
565: AConstant locals-list \ acts like a variable that contains
566: \ a linear list of locals names
567:
568:
569: variable dead-code \ true if normal code at "here" would be dead
570: variable backedge-locals
571: \ contains the locals list that BEGIN will assume to be live on
572: \ the back edge if the BEGIN is unreachable from above. Set by
573: \ ASSUME-LIVE, reset by UNREACHABLE.
574:
575: : UNREACHABLE ( -- ) \ gforth
576: \ declares the current point of execution as unreachable
577: dead-code on
578: 0 backedge-locals ! ; immediate
579:
580: : ASSUME-LIVE ( orig -- orig ) \ gforth
581: \ used immediatly before a BEGIN that is not reachable from
582: \ above. causes the BEGIN to assume that the same locals are live
583: \ as at the orig point
584: dup orig?
585: 2 pick backedge-locals ! ; immediate
586:
587: \ Control Flow Stack
588: \ orig, etc. have the following structure:
589: \ type ( defstart, live-orig, dead-orig, dest, do-dest, scopestart) ( TOS )
590: \ address (of the branch or the instruction to be branched to) (second)
591: \ locals-list (valid at address) (third)
592:
593: \ types
594: 0 constant defstart
595: 1 constant live-orig
596: 2 constant dead-orig
597: 3 constant dest \ the loopback branch is always assumed live
598: 4 constant do-dest
599: 5 constant scopestart
600:
601: : def? ( n -- )
602: defstart <> abort" unstructured " ;
603:
604: : orig? ( n -- )
605: dup live-orig <> swap dead-orig <> and abort" expected orig " ;
606:
607: : dest? ( n -- )
608: dest <> abort" expected dest " ;
609:
610: : do-dest? ( n -- )
611: do-dest <> abort" expected do-dest " ;
612:
613: : scope? ( n -- )
614: scopestart <> abort" expected scope " ;
615:
616: : non-orig? ( n -- )
617: dest scopestart 1+ within 0= abort" expected dest, do-dest or scope" ;
618:
619: : cs-item? ( n -- )
620: live-orig scopestart 1+ within 0= abort" expected control flow stack item" ;
621:
622: 3 constant cs-item-size
623:
624: : CS-PICK ( ... u -- ... destu ) \ tools-ext
625: 1+ cs-item-size * 1- >r
626: r@ pick r@ pick r@ pick
627: rdrop
628: dup non-orig? ;
629:
630: : CS-ROLL ( destu/origu .. dest0/orig0 u -- .. dest0/orig0 destu/origu ) \ tools-ext
631: 1+ cs-item-size * 1- >r
632: r@ roll r@ roll r@ roll
633: rdrop
634: dup cs-item? ;
635:
636: : cs-push-part ( -- list addr )
637: locals-list @ here ;
638:
639: : cs-push-orig ( -- orig )
640: cs-push-part dead-code @
641: if
642: dead-orig
643: else
644: live-orig
645: then ;
646:
647: \ Structural Conditionals 12dec92py
648:
649: : ?struc ( flag -- ) abort" unstructured " ;
650: : sys? ( sys -- ) dup 0= ?struc ;
651: : >mark ( -- orig )
652: cs-push-orig 0 , ;
653: : >resolve ( addr -- ) here over - swap ! ;
654: : <resolve ( addr -- ) here - , ;
655:
656: : BUT
657: 1 cs-roll ; immediate restrict
658: : YET
659: 0 cs-pick ; immediate restrict
660:
661: \ Structural Conditionals 12dec92py
662:
663: : AHEAD ( compilation -- orig ; run-time -- ) \ tools-ext
664: POSTPONE branch >mark POSTPONE unreachable ; immediate restrict
665:
666: : IF ( compilation -- orig ; run-time f -- ) \ core
667: POSTPONE ?branch >mark ; immediate restrict
668:
669: : ?DUP-IF ( compilation -- orig ; run-time n -- n| ) \ gforth question-dupe-if
1.3 pazsan 670: \G This is the preferred alternative to the idiom "?DUP IF", since it can be
671: \G better handled by tools like stack checkers. Besides, it's faster.
1.1 pazsan 672: POSTPONE ?dup-?branch >mark ; immediate restrict
673:
674: : ?DUP-0=-IF ( compilation -- orig ; run-time n -- n| ) \ gforth question-dupe-zero-equals-if
675: POSTPONE ?dup-0=-?branch >mark ; immediate restrict
676:
1.3 pazsan 677: Defer then-like ( orig -- addr )
678: : cs>addr ( orig/dest -- addr ) drop nip ;
679: ' cs>addr IS then-like
1.1 pazsan 680:
681: : THEN ( compilation orig -- ; run-time -- ) \ core
682: dup orig? then-like >resolve ; immediate restrict
683:
684: ' THEN alias ENDIF ( compilation orig -- ; run-time -- ) \ gforth
685: immediate restrict
686: \ Same as "THEN". This is what you use if your program will be seen by
687: \ people who have not been brought up with Forth (or who have been
688: \ brought up with fig-Forth).
689:
690: : ELSE ( compilation orig1 -- orig2 ; run-time f -- ) \ core
691: POSTPONE ahead
692: 1 cs-roll
693: POSTPONE then ; immediate restrict
694:
1.3 pazsan 695: Defer begin-like ( -- )
696: ' noop IS begin-like
1.1 pazsan 697:
698: : BEGIN ( compilation -- dest ; run-time -- ) \ core
1.3 pazsan 699: begin-like cs-push-part dest ; immediate restrict
1.1 pazsan 700:
1.3 pazsan 701: Defer again-like ( dest -- addr )
702: ' nip IS again-like
1.1 pazsan 703:
704: : AGAIN ( compilation dest -- ; run-time -- ) \ core-ext
705: dest? again-like POSTPONE branch <resolve ; immediate restrict
706:
1.3 pazsan 707: Defer until-like
708: : until, ( list addr xt1 xt2 -- ) drop compile, <resolve drop ;
709: ' until, IS until-like
1.1 pazsan 710:
711: : UNTIL ( compilation dest -- ; run-time f -- ) \ core
712: dest? ['] ?branch ['] ?branch-lp+!# until-like ; immediate restrict
713:
714: : WHILE ( compilation dest -- orig dest ; run-time f -- ) \ core
715: POSTPONE if
716: 1 cs-roll ; immediate restrict
717:
718: : REPEAT ( compilation orig dest -- ; run-time -- ) \ core
719: POSTPONE again
720: POSTPONE then ; immediate restrict
721:
722: \ counted loops
723:
724: \ leave poses a little problem here
725: \ we have to store more than just the address of the branch, so the
726: \ traditional linked list approach is no longer viable.
727: \ This is solved by storing the information about the leavings in a
728: \ special stack.
729:
730: \ !! remove the fixed size limit. 'Tis not hard.
731: 20 constant leave-stack-size
732: create leave-stack 60 cells allot
733: Avariable leave-sp leave-stack 3 cells + leave-sp !
734:
735: : clear-leave-stack ( -- )
736: leave-stack leave-sp ! ;
737:
738: \ : leave-empty? ( -- f )
739: \ leave-sp @ leave-stack = ;
740:
741: : >leave ( orig -- )
742: \ push on leave-stack
743: leave-sp @
744: dup [ leave-stack 60 cells + ] Aliteral
745: >= abort" leave-stack full"
746: tuck ! cell+
747: tuck ! cell+
748: tuck ! cell+
749: leave-sp ! ;
750:
751: : leave> ( -- orig )
752: \ pop from leave-stack
753: leave-sp @
754: dup leave-stack <= IF
755: drop 0 0 0 EXIT THEN
756: cell - dup @ swap
757: cell - dup @ swap
758: cell - dup @ swap
759: leave-sp ! ;
760:
761: : DONE ( compilation orig -- ; run-time -- ) \ gforth
762: \ !! the original done had ( addr -- )
763: drop >r drop
764: begin
765: leave>
766: over r@ u>=
767: while
768: POSTPONE then
769: repeat
770: >leave rdrop ; immediate restrict
771:
772: : LEAVE ( compilation -- ; run-time loop-sys -- ) \ core
773: POSTPONE ahead
774: >leave ; immediate restrict
775:
776: : ?LEAVE ( compilation -- ; run-time f | f loop-sys -- ) \ gforth question-leave
777: POSTPONE 0= POSTPONE if
778: >leave ; immediate restrict
779:
780: : DO ( compilation -- do-sys ; run-time w1 w2 -- loop-sys ) \ core
781: POSTPONE (do)
782: POSTPONE begin drop do-dest
783: ( 0 0 0 >leave ) ; immediate restrict
784:
785: : ?do-like ( -- do-sys )
786: ( 0 0 0 >leave )
787: >mark >leave
788: POSTPONE begin drop do-dest ;
789:
790: : ?DO ( compilation -- do-sys ; run-time w1 w2 -- | loop-sys ) \ core-ext question-do
791: POSTPONE (?do) ?do-like ; immediate restrict
792:
793: : +DO ( compilation -- do-sys ; run-time n1 n2 -- | loop-sys ) \ gforth plus-do
794: POSTPONE (+do) ?do-like ; immediate restrict
795:
796: : U+DO ( compilation -- do-sys ; run-time u1 u2 -- | loop-sys ) \ gforth u-plus-do
797: POSTPONE (u+do) ?do-like ; immediate restrict
798:
799: : -DO ( compilation -- do-sys ; run-time n1 n2 -- | loop-sys ) \ gforth minus-do
800: POSTPONE (-do) ?do-like ; immediate restrict
801:
802: : U-DO ( compilation -- do-sys ; run-time u1 u2 -- | loop-sys ) \ gforth u-minus-do
803: POSTPONE (u-do) ?do-like ; immediate restrict
804:
805: : FOR ( compilation -- do-sys ; run-time u -- loop-sys ) \ gforth
806: POSTPONE (for)
807: POSTPONE begin drop do-dest
808: ( 0 0 0 >leave ) ; immediate restrict
809:
810: \ LOOP etc. are just like UNTIL
811:
812: : loop-like ( do-sys xt1 xt2 -- )
813: >r >r 0 cs-pick swap cell - swap 1 cs-roll r> r> rot do-dest?
814: until-like POSTPONE done POSTPONE unloop ;
815:
816: : LOOP ( compilation do-sys -- ; run-time loop-sys1 -- | loop-sys2 ) \ core
817: ['] (loop) ['] (loop)-lp+!# loop-like ; immediate restrict
818:
819: : +LOOP ( compilation do-sys -- ; run-time loop-sys1 n -- | loop-sys2 ) \ core plus-loop
820: ['] (+loop) ['] (+loop)-lp+!# loop-like ; immediate restrict
821:
822: \ !! should the compiler warn about +DO..-LOOP?
823: : -LOOP ( compilation do-sys -- ; run-time loop-sys1 u -- | loop-sys2 ) \ gforth minus-loop
824: ['] (-loop) ['] (-loop)-lp+!# loop-like ; immediate restrict
825:
826: \ A symmetric version of "+LOOP". I.e., "-high -low ?DO -inc S+LOOP"
827: \ will iterate as often as "high low ?DO inc S+LOOP". For positive
828: \ increments it behaves like "+LOOP". Use S+LOOP instead of +LOOP for
829: \ negative increments.
830: : S+LOOP ( compilation do-sys -- ; run-time loop-sys1 n -- | loop-sys2 ) \ gforth s-plus-loop
831: ['] (s+loop) ['] (s+loop)-lp+!# loop-like ; immediate restrict
832:
833: : NEXT ( compilation do-sys -- ; run-time loop-sys1 -- | loop-sys2 ) \ gforth
834: ['] (next) ['] (next)-lp+!# loop-like ; immediate restrict
835:
836: \ Structural Conditionals 12dec92py
837:
1.3 pazsan 838: Defer exit-like ( -- )
839: ' noop IS exit-like
840:
1.1 pazsan 841: : EXIT ( compilation -- ; run-time nest-sys -- ) \ core
1.3 pazsan 842: exit-like
1.1 pazsan 843: POSTPONE ;s
844: POSTPONE unreachable ; immediate restrict
845:
846: : ?EXIT ( -- ) ( compilation -- ; run-time nest-sys f -- | nest-sys ) \ gforth
847: POSTPONE if POSTPONE exit POSTPONE then ; immediate restrict
848:
849: \ Strings 22feb93py
850:
851: : ," ( "string"<"> -- ) [char] " parse
852: here over char+ allot place align ;
853: : "lit ( -- addr )
854: r> r> dup count + aligned >r swap >r ;
855: : (.") "lit count type ;
856: : (S") "lit count ;
857: : SLiteral ( Compilation c-addr1 u ; run-time -- c-addr2 u ) \ string
858: postpone (S") here over char+ allot place align ;
859: immediate restrict
860: : ( ( compilation 'ccc<close-paren>' -- ; run-time -- ) \ core,file paren
861: BEGIN
862: >in @ [char] ) parse nip >in @ rot - =
863: WHILE
864: loadfile @ IF
865: refill 0= abort" missing ')' in paren comment"
866: THEN
867: REPEAT ; immediate
868: : \ ( -- ) \ core-ext backslash
869: blk @
870: IF
871: >in @ c/l / 1+ c/l * >in !
872: EXIT
873: THEN
874: source >in ! drop ; immediate
875:
876: : \G ( -- ) \ gforth backslash
877: POSTPONE \ ; immediate
878:
879: \ error handling 22feb93py
880: \ 'abort thrown out! 11may93jaw
881:
882: : (abort")
883: "lit >r
884: IF
885: r> "error ! -2 throw
886: THEN
887: rdrop ;
888: : abort" ( compilation 'ccc"' -- ; run-time f -- ) \ core,exception-ext abort-quote
889: postpone (abort") ," ; immediate restrict
890:
891: \ Header states 23feb93py
892:
893: : cset ( bmask c-addr -- )
894: tuck c@ or swap c! ;
895: : creset ( bmask c-addr -- )
896: tuck c@ swap invert and swap c! ;
897: : ctoggle ( bmask c-addr -- )
898: tuck c@ xor swap c! ;
899:
900: : lastflags ( -- c-addr )
901: \ the address of the flags byte in the last header
902: \ aborts if the last defined word was headerless
903: last @ dup 0= abort" last word was headerless" cell+ ;
904:
1.2 anton 905: : immediate ( -- ) \ core
906: immediate-mask lastflags cset ;
907: : restrict ( -- ) \ gforth
908: restrict-mask lastflags cset ;
909: ' restrict alias compile-only ( -- ) \ gforth
1.1 pazsan 910:
911: \ Header 23feb93py
912:
913: \ input-stream, nextname and noname are quite ugly (passing
914: \ information through global variables), but they are useful for dealing
915: \ with existing/independent defining words
916:
917: defer (header)
918: defer header ( -- ) \ gforth
919: ' (header) IS header
920:
921: : string, ( c-addr u -- ) \ gforth
1.3 pazsan 922: \G puts down string as cstring
1.1 pazsan 923: dup c, here swap chars dup allot move ;
924:
925: : header, ( c-addr u -- ) \ gforth
926: name-too-long?
927: align here last !
928: current @ 1 or A, \ link field; before revealing, it contains the
929: \ tagged reveal-into wordlist
930: string, cfalign
931: alias-mask lastflags cset ;
932:
933: : input-stream-header ( "name" -- )
934: name name-too-short? header, ;
935: : input-stream ( -- ) \ general
1.3 pazsan 936: \G switches back to getting the name from the input stream ;
1.1 pazsan 937: ['] input-stream-header IS (header) ;
938:
939: ' input-stream-header IS (header)
940:
941: \ !! make that a 2variable
942: create nextname-buffer 32 chars allot
943:
944: : nextname-header ( -- )
945: nextname-buffer count header,
946: input-stream ;
947:
948: \ the next name is given in the string
949: : nextname ( c-addr u -- ) \ gforth
950: name-too-long?
951: nextname-buffer c! ( c-addr )
952: nextname-buffer count move
953: ['] nextname-header IS (header) ;
954:
955: : noname-header ( -- )
956: 0 last ! cfalign
957: input-stream ;
958:
959: : noname ( -- ) \ gforth
960: \ the next defined word remains anonymous. The xt of that word is given by lastxt
961: ['] noname-header IS (header) ;
962:
963: : lastxt ( -- xt ) \ gforth
964: \ xt is the execution token of the last word defined. The main purpose of this word is to get the xt of words defined using noname
965: lastcfa @ ;
966:
967: : Alias ( cfa "name" -- ) \ gforth
968: Header reveal
969: alias-mask lastflags creset
970: dup A, lastcfa ! ;
971:
1.4 anton 972: : name>string ( nt -- addr count ) \ gforth name-to-string
973: \g @var{addr count} is the name of the word represented by @var{nt}.
974: cell+ count $1F and ;
1.1 pazsan 975:
976: Create ??? 0 , 3 c, char ? c, char ? c, char ? c,
1.4 anton 977: : >name ( cfa -- nt ) \ gforth to-name
1.1 pazsan 978: $21 cell do
979: dup i - count $9F and + cfaligned over alias-mask + = if
980: i - cell - unloop exit
981: then
982: cell +loop
983: drop ??? ( wouldn't 0 be better? ) ;
984:
985: \ threading 17mar93py
986:
987: : cfa, ( code-address -- ) \ gforth cfa-comma
988: here
989: dup lastcfa !
990: 0 A, 0 , code-address! ;
991: : compile, ( xt -- ) \ core-ext compile-comma
992: A, ;
993: : !does ( addr -- ) \ gforth store-does
994: lastxt does-code! ;
995: : (does>) ( R: addr -- )
996: r> /does-handler + !does ;
997: : dodoes, ( -- )
998: here /does-handler allot does-handler! ;
999:
1000: : Create ( "name" -- ) \ core
1001: Header reveal dovar: cfa, ;
1002:
1003: \ Create Variable User Constant 17mar93py
1004:
1005: : Variable ( "name" -- ) \ core
1006: Create 0 , ;
1007: : AVariable ( "name" -- ) \ gforth
1008: Create 0 A, ;
1009: : 2VARIABLE ( "name" -- ) \ double
1010: create 0 , 0 , ;
1011:
1012: : User ( "name" -- ) \ gforth
1013: Variable ;
1014: : AUser ( "name" -- ) \ gforth
1015: AVariable ;
1016:
1017: : (Constant) Header reveal docon: cfa, ;
1018: : Constant ( w "name" -- ) \ core
1.3 pazsan 1019: \G Defines constant @var{name}
1020: \G
1021: \G @var{name} execution: @var{-- w}
1.1 pazsan 1022: (Constant) , ;
1023: : AConstant ( addr "name" -- ) \ gforth
1024: (Constant) A, ;
1025:
1026: : 2Constant ( w1 w2 "name" -- ) \ double
1027: Create ( w1 w2 "name" -- )
1028: 2,
1029: DOES> ( -- w1 w2 )
1030: 2@ ;
1031:
1032: \ IS Defer What's Defers TO 24feb93py
1033:
1034: : Defer ( "name" -- ) \ gforth
1035: \ !! shouldn't it be initialized with abort or something similar?
1036: Header Reveal dodefer: cfa,
1037: ['] noop A, ;
1038: \ Create ( -- )
1039: \ ['] noop A,
1040: \ DOES> ( ??? )
1041: \ perform ;
1042:
1043: : Defers ( "name" -- ) \ gforth
1044: ' >body @ compile, ; immediate
1045:
1046: \ : ; 24feb93py
1047:
1048: defer :-hook ( sys1 -- sys2 )
1049: defer ;-hook ( sys2 -- sys1 )
1050:
1051: : : ( "name" -- colon-sys ) \ core colon
1052: Header docol: cfa, defstart ] :-hook ;
1053: : ; ( compilation colon-sys -- ; run-time nest-sys ) \ core semicolon
1054: ;-hook ?struc postpone exit reveal postpone [ ; immediate restrict
1055:
1056: : :noname ( -- xt colon-sys ) \ core-ext colon-no-name
1057: 0 last !
1058: cfalign here docol: cfa, 0 ] :-hook ;
1059:
1060: \ Search list handling 23feb93py
1061:
1062: AVariable current ( -- addr ) \ gforth
1063:
1064: : last? ( -- false / nfa nfa )
1065: last @ ?dup ;
1.4 anton 1066: : (reveal) ( nt wid -- )
1.1 pazsan 1067: ( wid>wordlist-id ) dup >r
1068: @ over ( name>link ) !
1069: r> ! ;
1070:
1071: \ object oriented search list 17mar93py
1072:
1073: \ word list structure:
1074:
1075: struct
1.4 anton 1076: 1 cells: field find-method \ xt: ( c_addr u wid -- nt )
1077: 1 cells: field reveal-method \ xt: ( nt wid -- ) \ used by dofield:, must be field
1.1 pazsan 1078: 1 cells: field rehash-method \ xt: ( wid -- )
1079: \ \ !! what else
1080: end-struct wordlist-map-struct
1081:
1082: struct
1083: 1 cells: field wordlist-id \ not the same as wid; representation depends on implementation
1084: 1 cells: field wordlist-map \ pointer to a wordlist-map-struct
1085: 1 cells: field wordlist-link \ link field to other wordlists
1086: 1 cells: field wordlist-extend \ points to wordlist extensions (eg hash)
1087: end-struct wordlist-struct
1088:
1.4 anton 1089: : f83find ( addr len wordlist -- nt / false )
1.1 pazsan 1090: ( wid>wordlist-id ) @ (f83find) ;
1091:
1092: \ Search list table: find reveal
1093: Create f83search ( -- wordlist-map )
1094: ' f83find A, ' (reveal) A, ' drop A,
1095:
1096: Create forth-wordlist NIL A, G f83search T A, NIL A, NIL A,
1097: AVariable lookup G forth-wordlist lookup T !
1098: G forth-wordlist current T !
1099:
1100: \ higher level parts of find
1101:
1102: ( struct )
1103: 0 >body cell
1104: 1 cells: field interpret/compile-int
1105: 1 cells: field interpret/compile-comp
1106: end-struct interpret/compile-struct
1107:
1108: : interpret/compile? ( xt -- flag )
1109: >does-code ['] S" >does-code = ;
1110:
1111: : (cfa>int) ( cfa -- xt )
1112: dup interpret/compile?
1113: if
1114: interpret/compile-int @
1115: then ;
1116:
1117: : (x>int) ( cfa b -- xt )
1118: \ get interpretation semantics of name
1119: restrict-mask and
1120: if
1121: drop ['] compile-only-error
1122: else
1123: (cfa>int)
1124: then ;
1125:
1.4 anton 1126: : name>int ( nt -- xt ) \ gforth
1127: \G @var{xt} represents the interpretation semantics of the word
1128: \G @var{nt}. Produces @code{' compile-only-error} if
1129: \G @var{nt} is compile-only.
1.1 pazsan 1130: (name>x) (x>int) ;
1131:
1.4 anton 1132: : name?int ( nt -- xt ) \ gforth
1133: \G Like name>int, but throws an error if compile-only.
1.1 pazsan 1134: (name>x) restrict-mask and
1135: if
1136: compile-only-error \ does not return
1137: then
1138: (cfa>int) ;
1139:
1.4 anton 1140: : name>comp ( nt -- w xt ) \ gforth
1.10 ! anton 1141: \G @var{w xt} is the compilation token for the word @var{nt}.
1.1 pazsan 1142: (name>x) >r dup interpret/compile?
1143: if
1144: interpret/compile-comp @
1145: then
1146: r> immediate-mask and if
1147: ['] execute
1148: else
1149: ['] compile,
1150: then ;
1151:
1.4 anton 1152: : (search-wordlist) ( addr count wid -- nt / false )
1.1 pazsan 1153: dup wordlist-map @ find-method perform ;
1154:
1155: : flag-sign ( f -- 1|-1 )
1156: \ true becomes 1, false -1
1157: 0= 2* 1+ ;
1158:
1159: : (name>intn) ( nfa -- xt +-1 )
1160: (name>x) tuck (x>int) ( b xt )
1161: swap immediate-mask and flag-sign ;
1162:
1163: : search-wordlist ( addr count wid -- 0 / xt +-1 ) \ search
1164: \ xt is the interpretation semantics
1165: (search-wordlist) dup if
1166: (name>intn)
1167: then ;
1168:
1.4 anton 1169: : find-name ( c-addr u -- nt/0 ) \ gforth
1170: \g Find the name @var{c-addr u} in the current search
1171: \g order. Return its nt, if found, otherwise 0.
1.1 pazsan 1172: lookup @ (search-wordlist) ;
1173:
1174: : sfind ( c-addr u -- 0 / xt +-1 ) \ gforth-obsolete
1175: find-name dup
1.4 anton 1176: if ( nt )
1.1 pazsan 1177: state @
1178: if
1179: name>comp ['] execute = flag-sign
1180: else
1181: (name>intn)
1182: then
1183: then ;
1184:
1185: : find ( c-addr -- xt +-1 / c-addr 0 ) \ core
1186: dup count sfind dup
1187: if
1188: rot drop
1189: then ;
1190:
1.4 anton 1191: : (') ( "name" -- nt ) \ gforth
1.1 pazsan 1192: name find-name dup 0=
1193: IF
1194: drop -&13 bounce
1195: THEN ;
1196:
1.4 anton 1197: : [(')] ( compilation "name" -- ; run-time -- nt ) \ gforth bracket-paren-tick
1.1 pazsan 1198: (') postpone ALiteral ; immediate restrict
1199:
1200: : ' ( "name" -- xt ) \ core tick
1.4 anton 1201: \g @var{xt} represents @var{name}'s interpretation
1202: \g semantics. Performs @code{-14 throw} if the word has no
1203: \g interpretation semantics.
1.1 pazsan 1204: (') name?int ;
1.4 anton 1205: : ['] ( compilation. "name" -- ; run-time. -- xt ) \ core bracket-tick
1206: \g @var{xt} represents @var{name}'s interpretation
1207: \g semantics. Performs @code{-14 throw} if the word has no
1208: \g interpretation semantics.
1.1 pazsan 1209: ' postpone ALiteral ; immediate restrict
1210:
1211: : COMP' ( "name" -- w xt ) \ gforth c-tick
1.4 anton 1212: \g @var{w xt} represents @var{name}'s compilation semantics.
1.1 pazsan 1213: (') name>comp ;
1214: : [COMP'] ( compilation "name" -- ; run-time -- w xt ) \ gforth bracket-comp-tick
1.4 anton 1215: \g @var{w xt} represents @var{name}'s compilation semantics.
1.1 pazsan 1216: COMP' swap POSTPONE Aliteral POSTPONE ALiteral ; immediate restrict
1217:
1218: \ reveal words
1219:
1220: Variable warnings ( -- addr ) \ gforth
1221: G -1 warnings T !
1222:
1223: : check-shadow ( addr count wid -- )
1.3 pazsan 1224: \G prints a warning if the string is already present in the wordlist
1.1 pazsan 1225: >r 2dup 2dup r> (search-wordlist) warnings @ and ?dup if
1226: ." redefined " name>string 2dup type
1227: compare 0<> if
1228: ." with " type
1229: else
1230: 2drop
1231: then
1232: space space EXIT
1233: then
1234: 2drop 2drop ;
1235:
1236: : reveal ( -- ) \ gforth
1237: last?
1238: if \ the last word has a header
1239: dup ( name>link ) @ 1 and
1240: if \ it is still hidden
1.4 anton 1241: dup ( name>link ) @ 1 xor ( nt wid )
1242: 2dup >r name>string r> check-shadow ( nt wid )
1.1 pazsan 1243: dup wordlist-map @ reveal-method perform
1244: then
1245: then ;
1246:
1247: : rehash ( wid -- )
1248: dup wordlist-map @ rehash-method perform ;
1249:
1250: \ Input 13feb93py
1251:
1252: 07 constant #bell ( -- c ) \ gforth
1253: 08 constant #bs ( -- c ) \ gforth
1254: 09 constant #tab ( -- c ) \ gforth
1255: 7F constant #del ( -- c ) \ gforth
1256: 0D constant #cr ( -- c ) \ gforth
1257: \ the newline key code
1258: 0C constant #ff ( -- c ) \ gforth
1259: 0A constant #lf ( -- c ) \ gforth
1260:
1261: : bell #bell emit ;
1262: : cr ( -- ) \ core
1263: \ emit a newline
1264: #lf ( sic! ) emit ;
1265:
1266: \ : backspaces 0 ?DO #bs emit LOOP ;
1267:
1268: : (ins) ( max span addr pos1 key -- max span addr pos2 )
1269: >r 2dup + r@ swap c! r> emit 1+ rot 1+ -rot ;
1270: : (bs) ( max span addr pos1 -- max span addr pos2 flag )
1271: dup IF
1272: #bs emit bl emit #bs emit 1- rot 1- -rot
1273: THEN false ;
1274: : (ret) true space ;
1275:
1276: Create ctrlkeys
1277: ] false false false false false false false false
1278: (bs) false (ret) false false (ret) false false
1279: false false false false false false false false
1280: false false false false false false false false [
1281:
1282: defer insert-char
1283: ' (ins) IS insert-char
1284: defer everychar
1285: ' noop IS everychar
1286:
1287: : decode ( max span addr pos1 key -- max span addr pos2 flag )
1288: everychar
1289: dup #del = IF drop #bs THEN \ del is rubout
1290: dup bl < IF cells ctrlkeys + perform EXIT THEN
1291: >r 2over = IF rdrop bell 0 EXIT THEN
1292: r> insert-char 0 ;
1293:
1294: : accept ( addr len -- len ) \ core
1295: dup 0< IF abs over dup 1 chars - c@ tuck type
1296: \ this allows to edit given strings
1297: ELSE 0 THEN rot over
1298: BEGIN key decode UNTIL
1299: 2drop nip ;
1300:
1301: \ Output 13feb93py
1302:
1303: : (type) ( c-addr u -- ) \ gforth
1304: outfile-id write-file drop \ !! use ?DUP-IF THROW ENDIF instead of DROP ?
1305: ;
1306:
1307: Defer type ( c-addr u -- ) \ core
1308: \ defer type for a output buffer or fast
1309: \ screen write
1310:
1311: ' (type) IS Type
1312:
1313: : (emit) ( c -- ) \ gforth
1314: outfile-id emit-file drop \ !! use ?DUP-IF THROW ENDIF instead of DROP ?
1315: ;
1316:
1317: Defer emit ( c -- ) \ core
1318: ' (Emit) IS Emit
1319:
1320: Defer key ( -- c ) \ core
1321: ' (key) IS key
1322:
1323: \ Query 07apr93py
1324:
1325: : refill ( -- flag ) \ core-ext,block-ext,file-ext
1326: blk @ IF 1 blk +! true 0 >in ! EXIT THEN
1327: tib /line
1328: loadfile @ ?dup
1329: IF read-line throw
1330: ELSE sourceline# 0< IF 2drop false EXIT THEN
1331: accept true
1332: THEN
1333: 1 loadline +!
1334: swap #tib ! 0 >in ! ;
1335:
1.8 anton 1336: : query ( -- ) \ core-ext
1.3 pazsan 1337: \G obsolescent
1.8 anton 1338: tib /line accept #tib ! 0 >in ! ;
1.1 pazsan 1339:
1340: \ File specifiers 11jun93jaw
1341:
1342:
1343: \ 1 c, here char r c, 0 c, 0 c, 0 c, char b c, 0 c,
1344: \ 2 c, here char r c, char + c, 0 c,
1345: \ 2 c, here char w c, char + c, 0 c, align
1346: 4 Constant w/o ( -- fam ) \ file w-o
1347: 2 Constant r/w ( -- fam ) \ file r-w
1348: 0 Constant r/o ( -- fam ) \ file r-o
1349:
1350: \ BIN WRITE-LINE 11jun93jaw
1351:
1352: \ : bin dup 1 chars - c@
1353: \ r/o 4 chars + over - dup >r swap move r> ;
1354:
1355: : bin ( fam1 -- fam2 ) \ file
1356: 1 or ;
1357:
1358: : write-line ( c-addr u fileid -- ior ) \ file
1359: dup >r write-file
1360: ?dup IF
1361: r> drop EXIT
1362: THEN
1.10 ! anton 1363: #lf r> emit-file ;
1.1 pazsan 1364:
1365: \ include-file 07apr93py
1366:
1367: : push-file ( -- ) r>
1368: sourceline# >r loadfile @ >r
1369: blk @ >r tibstack @ >r >tib @ >r #tib @ >r
1370: >tib @ tibstack @ = IF r@ tibstack +! THEN
1371: tibstack @ >tib ! >in @ >r >r ;
1372:
1373: : pop-file ( throw-code -- throw-code )
1374: dup IF
1375: source >in @ sourceline# sourcefilename
1376: error-stack dup @ dup 1+
1377: max-errors 1- min error-stack !
1378: 6 * cells + cell+
1379: 5 cells bounds swap DO
1380: I !
1381: -1 cells +LOOP
1382: THEN
1383: r>
1384: r> >in ! r> #tib ! r> >tib ! r> tibstack ! r> blk !
1385: r> loadfile ! r> loadline ! >r ;
1386:
1387: : read-loop ( i*x -- j*x )
1388: BEGIN refill WHILE interpret REPEAT ;
1389:
1390: : include-file ( i*x fid -- j*x ) \ file
1391: push-file loadfile !
1392: 0 loadline ! blk off ['] read-loop catch
1393: loadfile @ close-file swap 2dup or
1394: pop-file drop throw throw ;
1395:
1396: create pathfilenamebuf 256 chars allot \ !! make this grow on demand
1397:
1398: \ : check-file-prefix ( addr len -- addr' len' flag )
1399: \ dup 0= IF true EXIT THEN
1400: \ over c@ '/ = IF true EXIT THEN
1401: \ over 2 S" ./" compare 0= IF true EXIT THEN
1402: \ over 3 S" ../" compare 0= IF true EXIT THEN
1403: \ over 2 S" ~/" compare 0=
1404: \ IF 1 /string
1405: \ S" HOME" getenv tuck pathfilenamebuf swap move
1406: \ 2dup + >r pathfilenamebuf + swap move
1407: \ pathfilenamebuf r> true
1408: \ ELSE false
1409: \ THEN ;
1410:
1411: : absolut-path? ( addr u -- flag ) \ gforth
1.3 pazsan 1412: \G a path is absolute, if it starts with a / or a ~ (~ expansion),
1413: \G or if it is in the form ./* or ../*, extended regexp: ^[/~]|./|../
1414: \G Pathes simply containing a / are not absolute!
1.1 pazsan 1415: over c@ '/ = >r
1416: over c@ '~ = >r
1417: 2dup 2 min S" ./" compare 0= >r
1418: 3 min S" ../" compare 0=
1419: r> r> r> or or or ;
1420: \ [char] / scan nip 0<> ;
1421:
1422: : open-path-file ( c-addr1 u1 -- file-id c-addr2 u2 ) \ gforth
1.3 pazsan 1423: \G opens a file for reading, searching in the path for it (unless
1424: \G the filename contains a slash); c-addr2 u2 is the full filename
1425: \G (valid until the next call); if the file is not found (or in
1426: \G case of other errors for each try), -38 (non-existant file) is
1427: \G thrown. Opening for other access modes makes little sense, as
1428: \G the path will usually contain dirs that are only readable for
1429: \G the user
1.1 pazsan 1430: \ !! use file-status to determine access mode?
1431: 2dup absolut-path?
1432: if \ the filename contains a slash
1433: 2dup r/o open-file throw ( c-addr1 u1 file-id )
1434: -rot >r pathfilenamebuf r@ cmove ( file-id R: u1 )
1435: pathfilenamebuf r> EXIT
1436: then
1437: pathdirs 2@ 0
1438: \ check-file-prefix 0=
1439: \ IF pathdirs 2@ 0
1440: ?DO ( c-addr1 u1 dirnamep )
1441: dup >r 2@ dup >r pathfilenamebuf swap cmove ( addr u )
1442: 2dup pathfilenamebuf r@ chars + swap cmove ( addr u )
1443: pathfilenamebuf over r> + dup >r r/o open-file 0=
1444: IF ( addr u file-id )
1445: nip nip r> rdrop 0 LEAVE
1446: THEN
1447: rdrop drop r> cell+ cell+
1448: LOOP
1449: \ ELSE 2dup open-file throw -rot THEN
1450: 0<> -&38 and throw ( file-id u2 )
1451: pathfilenamebuf swap ;
1452:
1453: create included-files 0 , 0 , ( pointer to and count of included files )
1454: here ," the terminal" dup c@ swap 1 + swap , A, here 2 cells -
1455: create image-included-files 1 , A, ( pointer to and count of included files )
1456: \ included-files points to ALLOCATEd space, while image-included-files
1457: \ points to ALLOTed objects, so it survives a save-system
1458:
1459: : loadfilename ( -- a-addr )
1.3 pazsan 1460: \G a-addr 2@ produces the current file name ( c-addr u )
1.1 pazsan 1461: included-files 2@ drop loadfilename# @ 2* cells + ;
1462:
1463: : sourcefilename ( -- c-addr u ) \ gforth
1.3 pazsan 1464: \G the name of the source file which is currently the input
1465: \G source. The result is valid only while the file is being
1466: \G loaded. If the current input source is no (stream) file, the
1467: \G result is undefined.
1.1 pazsan 1468: loadfilename 2@ ;
1469:
1470: : sourceline# ( -- u ) \ gforth sourceline-number
1.3 pazsan 1471: \G the line number of the line that is currently being interpreted
1472: \G from a (stream) file. The first line has the number 1. If the
1473: \G current input source is no (stream) file, the result is
1474: \G undefined.
1.1 pazsan 1475: loadline @ ;
1476:
1477: : init-included-files ( -- )
1478: image-included-files 2@ 2* cells save-mem drop ( addr )
1479: image-included-files 2@ nip included-files 2! ;
1480:
1481: : included? ( c-addr u -- f ) \ gforth
1.3 pazsan 1482: \G true, iff filename c-addr u is in included-files
1.1 pazsan 1483: included-files 2@ 0
1484: ?do ( c-addr u addr )
1485: dup >r 2@ 2over compare 0=
1486: if
1487: 2drop rdrop unloop
1488: true EXIT
1489: then
1490: r> cell+ cell+
1491: loop
1492: 2drop drop false ;
1493:
1494: : add-included-file ( c-addr u -- ) \ gforth
1.3 pazsan 1495: \G add name c-addr u to included-files
1.1 pazsan 1496: included-files 2@ 2* cells 2 cells extend-mem
1497: 2/ cell / included-files 2!
1498: 2! ;
1499: \ included-files 2@ tuck 1+ 2* cells resize throw
1500: \ swap 2dup 1+ included-files 2!
1501: \ 2* cells + 2! ;
1502:
1503: : included1 ( i*x file-id c-addr u -- j*x ) \ gforth
1.3 pazsan 1504: \G include the file file-id with the name given by c-addr u
1.1 pazsan 1505: loadfilename# @ >r
1506: save-mem add-included-file ( file-id )
1507: included-files 2@ nip 1- loadfilename# !
1508: ['] include-file catch
1509: r> loadfilename# !
1510: throw ;
1511:
1512: : included ( i*x addr u -- j*x ) \ file
1513: open-path-file included1 ;
1514:
1515: : required ( i*x addr u -- j*x ) \ gforth
1.3 pazsan 1516: \G include the file with the name given by addr u, if it is not
1517: \G included already. Currently this works by comparing the name of
1518: \G the file (with path) against the names of earlier included
1519: \G files; however, it would probably be better to fstat the file,
1520: \G and compare the device and inode. The advantages would be: no
1521: \G problems with several paths to the same file (e.g., due to
1522: \G links) and we would catch files included with include-file and
1523: \G write a require-file.
1.1 pazsan 1524: open-path-file 2dup included?
1525: if
1526: 2drop close-file throw
1527: else
1528: included1
1529: then ;
1530:
1531: \ HEX DECIMAL 2may93jaw
1532:
1533: : decimal ( -- ) \ core
1534: a base ! ;
1535: : hex ( -- ) \ core-ext
1536: 10 base ! ;
1537:
1538: \ DEPTH 9may93jaw
1539:
1540: : depth ( -- +n ) \ core
1541: sp@ s0 @ swap - cell / ;
1542: : clearstack ( ... -- )
1543: s0 @ sp! ;
1544:
1545: \ INCLUDE 9may93jaw
1546:
1547: : include ( "file" -- ) \ gforth
1548: name included ;
1549:
1550: : require ( "file" -- ) \ gforth
1551: name required ;
1552:
1553: \ RECURSE 17may93jaw
1554:
1555: : recurse ( compilation -- ; run-time ?? -- ?? ) \ core
1556: lastxt compile, ; immediate restrict
1557: ' reveal alias recursive ( -- ) \ gforth
1558: immediate
1559:
1560: \ */MOD */ 17may93jaw
1561:
1562: \ !! I think */mod should have the same rounding behaviour as / - anton
1563: : */mod ( n1 n2 n3 -- n4 n5 ) \ core star-slash-mod
1564: >r m* r> sm/rem ;
1565:
1566: : */ ( n1 n2 n3 -- n4 ) \ core star-slash
1567: */mod nip ;
1568:
1569: \ EVALUATE 17may93jaw
1570:
1571: : evaluate ( c-addr len -- ) \ core,block
1572: push-file #tib ! >tib !
1573: >in off blk off loadfile off -1 loadline !
1574: ['] interpret catch
1575: pop-file throw ;
1576:
1577: : abort ( ?? -- ?? ) \ core,exception-ext
1578: -1 throw ;
1579:
1580: \+ environment? true ENV" CORE"
1581: \ core wordset is now complete!
1582:
1583: \ Quit 13feb93py
1584:
1585: Defer 'quit
1586: Defer .status
1587: : prompt state @ IF ." compiled" EXIT THEN ." ok" ;
1.8 anton 1588: : (Query) ( -- )
1589: loadfile off blk off refill drop ;
1590: : (quit) BEGIN .status cr (query) interpret prompt AGAIN ;
1.1 pazsan 1591: ' (quit) IS 'quit
1592:
1593: \ DOERROR (DOERROR) 13jun93jaw
1594:
1595: 8 Constant max-errors
1596: Variable error-stack 0 error-stack !
1597: max-errors 6 * cells allot
1598: \ format of one cell:
1599: \ source ( addr u )
1600: \ >in
1601: \ line-number
1602: \ Loadfilename ( addr u )
1603:
1604: : dec. ( n -- ) \ gforth
1605: \ print value in decimal representation
1606: base @ decimal swap . base ! ;
1607:
1608: : hex. ( u -- ) \ gforth
1609: \ print value as unsigned hex number
1610: '$ emit base @ swap hex u. base ! ;
1611:
1612: : typewhite ( addr u -- ) \ gforth
1613: \ like type, but white space is printed instead of the characters
1614: bounds ?do
1.10 ! anton 1615: i c@ #tab = if \ check for tab
! 1616: #tab
1.1 pazsan 1617: else
1618: bl
1619: then
1620: emit
1621: loop ;
1622:
1623: DEFER DOERROR
1624:
1625: : .error-frame ( addr1 u1 n1 n2 addr2 u2 -- )
1626: cr error-stack @
1627: IF
1628: ." in file included from "
1629: type ." :" dec. drop 2drop
1630: ELSE
1631: type ." :" dec.
1632: cr dup 2over type cr drop
1633: nip -trailing 1- ( line-start index2 )
1634: 0 >r BEGIN
1635: 2dup + c@ bl > WHILE
1636: r> 1+ >r 1- dup 0< UNTIL THEN 1+
1637: ( line-start index1 )
1638: typewhite
1639: r> 1 max 0 ?do \ we want at least one "^", even if the length is 0
1640: [char] ^ emit
1641: loop
1642: THEN
1643: ;
1644:
1645: : (DoError) ( throw-code -- )
1646: sourceline# IF
1647: source >in @ sourceline# 0 0 .error-frame
1648: THEN
1649: error-stack @ 0 ?DO
1650: -1 error-stack +!
1651: error-stack dup @ 6 * cells + cell+
1652: 6 cells bounds DO
1653: I @
1654: cell +LOOP
1655: .error-frame
1656: LOOP
1657: dup -2 =
1658: IF
1659: "error @ ?dup
1660: IF
1661: cr count type
1662: THEN
1663: drop
1664: ELSE
1665: .error
1666: THEN
1667: normal-dp dpp ! ;
1668:
1669: ' (DoError) IS DoError
1670:
1671: : quit ( ?? -- ?? ) \ core
1672: r0 @ rp! handler off >tib @ >r
1673: BEGIN
1674: postpone [
1675: ['] 'quit CATCH dup
1676: WHILE
1677: DoError r@ >tib ! r@ tibstack !
1678: REPEAT
1679: drop r> >tib ! ;
1680:
1681: \ Cold 13feb93py
1682:
1683: \ : .name ( name -- ) name>string type space ;
1684: \ : words listwords @
1685: \ BEGIN @ dup WHILE dup .name REPEAT drop ;
1686:
1687: : cstring>sstring ( cstring -- addr n ) \ gforth cstring-to-sstring
1688: -1 0 scan 0 swap 1+ /string ;
1689: : arg ( n -- addr count ) \ gforth
1690: cells argv @ + @ cstring>sstring ;
1691: : #! postpone \ ; immediate
1692:
1693: Create pathstring 2 cells allot \ string
1694: Create pathdirs 2 cells allot \ dir string array, pointer and count
1695: Variable argv
1696: Variable argc
1697:
1698: 0 Value script? ( -- flag )
1699:
1700: : process-path ( addr1 u1 -- addr2 u2 )
1701: \ addr1 u1 is a path string, addr2 u2 is an array of dir strings
1702: align here >r
1703: BEGIN
1.6 anton 1704: over >r 0 scan
1.1 pazsan 1705: over r> tuck - ( rest-str this-str )
1706: dup
1707: IF
1708: 2dup 1- chars + c@ [char] / <>
1709: IF
1710: 2dup chars + [char] / swap c!
1711: 1+
1712: THEN
1713: 2,
1714: ELSE
1715: 2drop
1716: THEN
1717: dup
1718: WHILE
1719: 1 /string
1720: REPEAT
1721: 2drop
1722: here r> tuck - 2 cells / ;
1723:
1724: : do-option ( addr1 len1 addr2 len2 -- n )
1725: 2swap
1726: 2dup s" -e" compare 0= >r
1727: 2dup s" --evaluate" compare 0= r> or
1728: IF 2drop dup >r ['] evaluate catch
1729: ?dup IF dup >r DoError r> negate (bye) THEN
1730: r> >tib +! 2 EXIT THEN
1731: ." Unknown option: " type cr 2drop 1 ;
1732:
1733: : process-args ( -- )
1734: >tib @ >r
1735: argc @ 1
1736: ?DO
1737: I arg over c@ [char] - <>
1738: IF
1739: required 1
1740: ELSE
1741: I 1+ argc @ = IF s" " ELSE I 1+ arg THEN
1742: do-option
1743: THEN
1744: +LOOP
1745: r> >tib ! ;
1746:
1747: Defer 'cold ' noop IS 'cold
1748:
1749: : cold ( -- ) \ gforth
1750: stdout TO outfile-id
1751: pathstring 2@ process-path pathdirs 2!
1752: init-included-files
1753: 'cold
1754: argc @ 1 >
1755: IF
1756: true to script?
1757: ['] process-args catch ?dup
1758: IF
1759: dup >r DoError cr r> negate (bye)
1760: THEN
1761: cr
1762: THEN
1763: false to script?
1764: ." GForth " version-string type ." , Copyright (C) 1994-1996 Free Software Foundation, Inc." cr
1765: ." GForth comes with ABSOLUTELY NO WARRANTY; for details type `license'" cr
1766: ." Type `bye' to exit"
1767: loadline off quit ;
1768:
1769: : license ( -- ) \ gforth
1770: cr
1771: ." This program is free software; you can redistribute it and/or modify" cr
1772: ." it under the terms of the GNU General Public License as published by" cr
1773: ." the Free Software Foundation; either version 2 of the License, or" cr
1774: ." (at your option) any later version." cr cr
1775:
1776: ." This program is distributed in the hope that it will be useful," cr
1777: ." but WITHOUT ANY WARRANTY; without even the implied warranty of" cr
1778: ." MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the" cr
1779: ." GNU General Public License for more details." cr cr
1780:
1781: ." You should have received a copy of the GNU General Public License" cr
1782: ." along with this program; if not, write to the Free Software" cr
1783: ." Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA." cr ;
1784:
1785: : boot ( path **argv argc -- )
1.7 anton 1786: argc ! argv ! pathstring 2! main-task up!
1.9 anton 1787: sp@ s0 !
1788: lp@ forthstart 7 cells + @ - dup >tib ! tibstack ! #tib off >in off
1789: rp@ r0 !
1790: fp@ f0 !
1791: ['] cold catch DoError
1792: bye ;
1.1 pazsan 1793:
1794: : bye ( -- ) \ tools-ext
1795: script? 0= IF cr THEN 0 (bye) ;
1796:
1797: \ **argv may be scanned by the C starter to get some important
1798: \ information, as -display and -geometry for an X client FORTH
1799: \ or space and stackspace overrides
1800:
1801: \ 0 arg contains, however, the name of the program.
1802:
1803:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>