forked from august0815/forthos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforth.f
776 lines (673 loc) · 13.4 KB
/
forth.f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
: bl 32 ;
: '\n' 10 ;
: true 1 ;
: false 0 ;
: not 0= ;
: negate 0 swap - ;
: space bl emit ;
: literal immediate
' lit ,
,
;
: ':'
[
char :
]
literal
;
: '(' [ char ( ] literal ;
: ')' [ char ) ] literal ;
: '"' [ char " ] literal ;
: 'a' [ char a ] literal ;
: '0' [ char 0 ] literal ;
: '-' [ char - ] literal ;
: '.' [ char . ] literal ;
: [compile] immediate
wort
find
>cfa
,
;
: recurse immediate
LATEST @
>cfa
,
;
: if immediate
' 0branch ,
here
0 ,
;
: then immediate
dup
here swap -
swap !
;
: else immediate
' branch ,
here
0 ,
swap
dup
here swap -
swap !
;
: begin immediate
here
;
: until immediate
' 0branch ,
here -
,
;
: again immediate
' branch ,
here -
,
;
: while immediate
' 0branch ,
here
0 ,
;
: repeat immediate
' branch ,
swap
here - ,
dup
here swap -
swap !
;
: unless immediate
' not ,
[compile] if
;
: ( immediate
1 begin
key1
dup '(' = if
drop
1+
else
')' = if
1-
then
then
dup 0= until
drop
;
( from now on we can use ( ... ) for comments.) ;
( some more complicated stack examples, showing the stack notation. ) ;
: nip ( x y -- y ) swap drop ;
: tuck ( x y -- y x y ) swap over ;
: pick ( x_u ... x_1 x_0 u -- x_u ... x_1 x_0 x_u )
1+ ( add one because of 'u' on the stack )
4 * ( multiply by the word size )
dsp@ + ( add to the stack pointer )
@ ( and fetch )
;
( standard words for manipulating base. )
: decimal ( -- ) 10 BASE ! ;
: hex ( -- ) 16 BASE ! ;
( with the looping constructs, we can now write spaces, which writes n spaces to stdout. )
: spaces ( n -- )
begin
dup 0> ( while n > 0 )
while
space ( print a space )
1- ( until we count down to 0 )
repeat
drop
;
: case immediate
0
;
: of immediate
' over ,
' = ,
[compile] if
' drop ,
;
: endof immediate
[compile] else
;
: endcase immediate
' drop ,
begin
?dup
while
[compile] then
repeat
;
( this is the underlying recursive definition of u. )
: u. ( u -- )
BASE @ /mod ( width rem quot )
?dup if ( if quotient <> 0 then )
recurse ( print the quotient )
then
( print the remainder )
dup 10 < if
'0' ( decimal digits 0..9 )
else
10 - ( hex and beyond digits a..z )
'a'
then
+
emit
;
(
forth word .s prints the contents of the stack. it doesn't alter the stack.
very useful for debugging.
)
: .s ( -- )
dsp@ ( get current stack pointer )
begin
dup S0 @ <
while
dup @ u. ( print the stack element )
space
4+ ( move up )
repeat
drop
;
( this word returns the width (in characters) of an unsigned number in the current base )
: uwidth ( u -- width )
BASE @ / ( rem quot )
?dup if ( if quotient <> 0 then )
recurse 1+ ( return 1+recursive call )
else
1 ( return 1 )
then
;
: u.r ( u width -- )
swap ( width u )
dup ( width u u )
uwidth ( width u uwidth )
rot ( u uwidth width )
swap - ( u width-uwidth )
( at this point if the requested width is narrower, we'll have a negative number on the stack.
otherwise the number on the stack is the number of spaces to print. but spaces won't print
a negative number of spaces anyway, so it's now safe to call spaces ... )
spaces
( ... and then call the underlying implementation of u. )
u.
;
(
.r prints a signed number, padded to a certain width. we can't just print the sign
and call u.r because we want the sign to be next to the number ('-123' instead of '- 123').
)
: .r ( n width -- )
swap ( width n )
dup 0< if
negate ( width u )
1 ( save a flag to remember that it was negative | width n 1 )
swap ( width 1 u )
rot ( 1 u width )
1- ( 1 u width-1 )
else
0 ( width u 0 )
swap ( width 0 u )
rot ( 0 u width )
then
swap ( flag width u )
dup ( flag width u u )
uwidth ( flag width u uwidth )
rot ( flag u uwidth width )
swap - ( flag u width-uwidth )
spaces ( flag u )
swap ( u flag )
if ( was it negative? print the - character )
'-' emit
then
u.
;
( finally we can define word . in terms of .r, with a trailing space. )
: . 0 .r space ;
( the real u., note the trailing space. )
: u. u. space ;
( ? fetches the integer at an address and prints it. )
: ? ( addr -- ) @ . ;
: ascii 127 and dup 32 < if drop 46 then emit ;
( some more complicated stack examples, showing the stack notation. )
: nip ( x y -- y ) swap drop ;
: tuck ( x y -- y x y ) swap over ;
: pick ( x_u ... x_1 x_0 u -- x_u ... x_1 x_0 x_u )
1+ ( add one because of 'u' on the stack )
4 * ( multiply by the word size )
dsp@ + ( add to the stack pointer )
@ ( and fetch )
;
( c a b within returns true if a <= c and c < b )
( or define without ifs: over - >r - r> u< )
: within
rot ( b c a )
over ( b c a c )
<= if
> if ( b c -- )
true
else
false
then
else
2drop ( b c -- )
false
then
;
: aligned ( addr -- addr )
3 + 3 invert and ( (addr+3) & ~3 )
;
: align here aligned DP ! ;
: c,
DP @ c!
1 DP +!
;
: s" immediate ( -- addr len )
STATE @ if ( compiling? )
' litstring , ( compile litstring )
here ( save the address of the length word on the stack )
0 , ( dummy length - we don't know what it is yet )
begin
key1 ( get next character of the string )
dup '"' <>
while
c, ( copy character )
repeat
drop ( drop the double quote character at the end )
dup ( get the saved address of the length word )
here swap - ( calculate the length )
4- ( subtract 4 (because we measured from the start of the length word) )
swap ! ( and back-fill the length location )
align ( round up to next multiple of 4 bytes for the remaining code )
else ( immediate mode )
here ( get the start address of the temporary space )
begin
key1
dup '"' <>
while
over c! ( save next character )
1+ ( increment address )
repeat
drop ( drop the final " character )
here - ( calculate the length )
here ( push the start address )
swap ( addr len )
then
;
: ." immediate ( -- )
STATE @ if ( compiling? )
[compile] s" ( read the string, and compile litstring, etc. )
' tell , ( compile the final tell )
else
( in immediate mode, just read characters and print them until we get
to the ending double quote. )
begin
key1
dup '"' = if
drop ( drop the double quote character )
exit ( return from this function )
then
emit
again
then
;
: constant
wort
head
DOCOL ,
' lit ,
,
' exit ,
;
: allot ( n -- addr )
DP @ swap
DP +!
;
: cells ( n -- n ) 4 * ;
: variable
1 cells allot
wort head
DOCOL ,
' lit ,
,
' exit ,
;
: value ( n -- )
wort head
DOCOL ,
' lit ,
,
' exit ,
;
: to immediate ( n -- )
wort
find
>dfa
4+
STATE @ if
' lit ,
,
' ! ,
else
!
then
;
( x +to val adds x to val )
: +to immediate
wort
find
>dfa
4+
STATE @ if
' lit ,
,
' +! ,
else
+!
then
;
: id.
4+ ( skip over the link pointer )
dup c@ ( get the flags/length byte )
F_LENMASK and ( mask out the flags - just want the length )
begin
dup 0> ( length > 0? )
while
swap 1+ ( addr len -- len addr+1 )
dup c@ ( len addr -- len addr char | get the next character)
emit ( len addr char -- len addr | and print it)
swap 1- ( len addr -- addr len-1 | subtract one from length )
repeat
2drop ( len addr -- )
;
(
'word word find ?hidden' returns true if 'word' is flagged as hidden.
'word word find ?immediate' returns true if 'word' is flagged as immediate.
)
: ?hidden
4+ ( skip over the link pointer )
c@ ( get the flags/length byte )
F_HIDDEN and ( mask the f_hidden flag and return it (as a truth value) )
;
: ?immediate
4+ ( skip over the link pointer )
c@ ( get the flags/length byte )
F_IMMED and ( mask the f_immed flag and return it (as a truth value) )
;
(
words prints all the words defined in the dictionary, starting with the word defined most recently.
however it doesn't print hidden words.
the implementation simply iterates backwards from latest using the link pointers.
)
: words
LATEST @ ( start at latest dictionary entry )
begin
?dup ( while link pointer is not null )
while
dup ?hidden not if ( ignore hidden words )
dup id. ( but if not hidden, print the word )
space
then
@ ( dereference the link pointer - go to previous word )
repeat
cr
;
: forget
wort find ( find the word, gets the dictionary entry address )
dup @ LATEST ! ( set latest to point to the previous word )
DP ! ( and store here with the dictionary address )
;
: cfa>
LATEST @ ( start at latest dictionary entry )
begin
?dup ( while link pointer is not null )
while
2dup swap ( cfa curr curr cfa )
< if ( current dictionary entry < cfa? )
nip ( leave curr dictionary entry on the stack )
exit
then
@ ( follow link pointer back )
repeat
drop ( restore stack )
0 ( sorry, nothing found )
;
: see
cr
wort
find
here
LATEST @
begin
2 pick
over
<>
while
nip
dup @
repeat
drop
swap
':' emit space dup id. space
dup ?immediate if ." immediate " then
>dfa
begin
2dup >
while
dup @
case
' lit of
4 + dup @
.
endof
' litstring of
[ char s ] literal emit '"' emit space
4 + dup @
swap 4 + swap
2dup tell
'"' emit space
+ aligned
4 -
endof
' 0branch of
." 0branch >"
4 + dup @
.
." < "
endof
' branch of
." branch >"
4 + dup @
.
." < "
endof
' ' of
[ char ' ] literal emit space
4 + dup @
cfa>
id. space
endof
' exit of
2dup
4 +
<> if
." exit "
then
endof
dup
cfa>
id. space
endcase
4 +
repeat
59 emit cr
2drop ( restore stack )
;
: noname
0 0 head
here
DOCOL ,
]
;
: ['] immediate
' lit ,
;
: exception-marker
rdrop
0
;
: catch
dsp@ 4+ >r
' exception-marker 4+
>r
execute
;
: throw ( n -- )
?dup if
rsp@ ( get return stack pointer )
begin
dup R0 4- <
while
dup @ ( get the return stack entry )
' exception-marker 4+ = if ( found the exception-marker on the return stack )
4+ ( skip the exception-marker on the return stack )
rsp! ( restore the return stack pointer )
( restore the parameter stack. )
dup dup dup ( reserve some working space so the stack for this word
doesn't coincide with the part of the stack being restored )
r>
4- ( reserve space on the stack to store n )
swap over ( dsp n dsp )
! ( write n on the stack )
dsp! exit ( restore the parameter stack pointer, immediately exit )
then
4+
repeat
drop
case
0 1- of ( abort )
." aborted" cr
endof
( default case )
." uncaught throw "
dup . cr
endcase
quit
then
;
: abort
0 1- throw
;
: dump
BASE @ rot ( save the current base at the bottom of the stack )
hex ( and switch to hexadecimal mode )
begin
?dup ( while len > 0 )
while
over 8 .r ( print the address )
space
2dup
1- 15 and 1+
begin
?dup
while
swap
dup c@
2 .r space ( print the byte )
1+ swap 1- ( addr len linelen addr -- addr len addr+1 linelen-1 )
repeat
drop ( addr len )
( print the ascii equivalents )
2dup 1- 15 and 1+ ( addr len addr linelen )
begin
?dup ( while linelen > 0)
while
swap ( addr len linelen addr )
dup c@ ( addr len linelen addr byte )
dup 32 128 within if ( 32 <= c < 128? )
emit
else
drop '.' emit
then
1+ swap 1- ( addr len linelen addr -- addr len addr+1 linelen-1 )
repeat
drop ( addr len )
cr
dup 1- 15 and 1+ ( addr len linelen )
tuck ( addr linelen len linelen )
- ( addr linelen len-linelen )
>r + r> ( addr+linelen len-linelen )
repeat
drop ( restore stack )
BASE ! ( restore saved base )
;
: strlen ( str -- len )
dup ( save start address )
begin
dup c@ 0<> ( zero byte found? )
while
1+
repeat
swap - ( calculate the length )
;
: cstring ( addr len -- c-addr )
swap over ( len saddr len )
here swap ( len saddr daddr len )
cmove ( len )
here + ( daddr+len )
0 swap c! ( store terminating nul char )
here ( push start address )
;
: unused ( -- n )
TOPMEM @ ( get end of data segment according to the kernel )
here ( get current position in data segment )
-
4 / ( returns number of cells )
;
: wel
clear cr ." my-forth version 0." 1 .
." adapted from jonesforth version 47" cr
." corrections and additions by richard russell, 19-oct-2009 " cr
." adapted by august0815, 01-feb-2010" cr
unused . ." cells remaining" cr cr
." Type 'words' ... " cr cr
;
4 constant cell
: noop ( -- ) ;
( ----------------------------------------------------------------------- )
( The preceding code is standard Jonesforth (except U. using U/MOD). )
( Below are the BB4Wforth additions and corrections to Jonesforth. )
( These improve ISO compliance and provide access to the BB4W host. )
( (C) Copyright Richard T. Russell 2009, http://www.rtrussell.co.uk/ )
( You may freely adapt this code so long as acknowledgement is made. )
( some words adapted by august0815 , 06.02.2010 )
( STILL TESTING)
;
0 value pnsptr ;
: source TEXT_BUFF @ dup PPTR @ - ;
: >in PPTR @ ;
: alias
wort head wort find >cfa @
dup DOCOL = if ." cannot alias a non-code word" then
,
;
: cell+ ( a-addr1 -- a-addr2 ) 1 cells + ;
: cell- ( a-addr1 -- a-addr2 ) 1 cells - ;
: chars ( n1 -- n2 ) ;
immediate ;
alias (wort) wort ;
alias (find) find ;
alias (key) key1 ;
hide allot ;
hide variable
hide true ;
hide find ;
hide while ;
hide repeat ;
hide constant ;
echoon ;
: allot ( n -- ) here + DP ! ;
: variable ( "<spaces>name" -- ) create 1 cells allot ;
: count ( caddr1 -- caddr2 u ) dup c@ swap 1+ swap ;
immediate ;
echoon ;
wel ;