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