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