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