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