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