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