/
ftran202.4th
746 lines (611 loc) · 23.2 KB
/
ftran202.4th
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
\ ******** ANS-compatible FORmula TRANslator ********
\ see ftrandoc.txt for instructions
\ ---------------------------------------------------
\ (c) Copyright 2001 Julian V. Noble. \
\ Permission is granted by the author to \
\ use this software for any application pro- \
\ vided this copyright notice is preserved. \
\ ---------------------------------------------------
\ Revisions:
\
\ 2010-06-02 km; this version is 2.02b for kForth 1.5.x,
\ integrated fp/data stack system; thanks
\ to "Ed" on comp.lang.forth for suggestion on
\ replacement of locals code in recursive and
\ re-entrant words; see ftran-test.4th.
\
\ ===== kForth specific ================================
\
\ ans-words must be included
[undefined] f>string [IF] s" strings" included [THEN] \ strings pkg
[undefined] fsm: [IF] s" fsm2" included [THEN] \ finite state machine
[undefined] char_table: [IF] s" chr_tbl" included [THEN] \ character encoding pkg
[undefined] z@ [IF] s" fsl/complex" included [THEN] \ FSL complex arithmetic pkg
\ ==== end of kForth specific scaffolding ==============
\ program begins here
[undefined] ?exit [IF]
: ?exit ( flag) POSTPONE IF
POSTPONE EXIT
POSTPONE THEN ; IMMEDIATE
[THEN]
[undefined] OFF [IF]
: OFF ( adr -- ) FALSE SWAP ! ;
: ON ( adr -- ) TRUE SWAP ! ;
[THEN]
FORTH-WORDLIST SET-CURRENT \ a precaution
\ raising to integer powers
[undefined] f^2 [IF] : f^2 FDUP F* ; [THEN]
: f^3 FDUP FDUP F* F* ;
: f^4 f^2 f^2 ;
\ increment if true ( ptr f -- ptr+1 | ptr)
: ?inc S" 1 AND + " EVALUATE ; IMMEDIATE
WORDLIST ptr ftran \ create separate wordlist
ftran SET-CURRENT \ for FOR...TRAN... def'ns
GET-ORDER ftran SWAP 1+ SET-ORDER \ make ftran findable
\ -------------------------------------------- string manipulation
: $ends ( c-adr -- end beg) \ convert c-adr to ends
COUNT DUP 0> ( beg n f)
-1 AND + ( beg n-1|0)
OVER + SWAP ; ( end beg)
: ends->count ( end beg -- c-adr u) TUCK - 1+ ;
0 ptr src
0 ptr dst
0 value n
: concat ( src u dst --) \ append u chars from src to dst
\ LOCALS| dst n src |
TO dst TO n TO src
src dst CELL+ dst @ + n CMOVE
n dst @ + dst ! ;
\ ---------------------------------------- end string manipulation
\ ------------------------------------------------ data structures
\ 1. String-pointer stack:
\ 3 cells wide, cell at base_adr holds $ptr
16 CONSTANT max_depth \ this seems enough
\ $ stack space + 1 cell for pointer
CREATE $stack max_depth 3 * CELLS CELL+ ALLOT
\ HERE $stack - 1 CELLS - CONSTANT $max \ max depth (cells)
max_depth 3 * CELLS CONSTANT $max \ max depth (cells)
: $init -3 CELLS $stack ! ; $init
: $ptr ( -- adr offset) $stack DUP @ ;
: $lbound ( offset) 0< ABORT" empty $stack!" ;
: ($pop) ( adr offset -- end beg op)
DUP $lbound \ bounds check
+ CELL+ ( adr[TO$])
DUP >R CELL+ DUP a@ SWAP CELL+ a@ SWAP
R> @ ; ( end beg op)
: $pop ( -- end beg op)
$ptr ( adr offset)
($pop) ( end beg op)
-3 CELLS $stack +! ; \ dec $ptr
: $ubound ( offset) $max > ABORT" $stack too deep!" ;
: $push ( end beg op -- )
3 CELLS $stack +! \ inc $ptr
$ptr ( end beg op adr offset)
DUP $ubound \ bounds check
+ CELL+ DUP >R ( end beg op adr[TO$])
! R> CELL+ 2! ;
\ 2. Null string
\ CREATE bl$ 1 C, BL C,
CREATE bl$ ( 1 C, BL C,) 2 allot? 1 OVER C! BL SWAP 1+ C!
bl$ $ends 2CONSTANT 0null
\ 3. re-vectorable dummy names
DEFER expr \ for indirect recursion
DEFER term
DEFER factor
DEFER .op \ for compilation
DEFER do_id
DEFER try_fp#
DEFER .fp#
DEFER do_@
DEFER do_^
DEFER do_fn
\ 4. place to make output string
CREATE out_pad 512 CHARS CELL+ ALLOT \ long output $
\ -------------------------------------------- end data structures
\ -------------------------------------------------- formula input
CREATE in_pad 256 ALLOT
0 in_pad C!
CREATE formula-string 256 ALLOT
0 ptr formula-ptr
[defined] >IN [IF]
\ Get character from input stream. From Wil Baden's opg .
: get-char ( -- char | 0 for EOL | negative for EOF )
SOURCE ( -- start_of_input #chars)
>IN @ ( -- start_of_input #chars input_ptr)
> IF >IN @ CHARS + C@ 1 >IN +!
ELSE DROP REFILL 0=
THEN ;
: parse_formula ;
[ELSE]
: get-char ( -- char | 0 for EOL | negative for EOF )
formula-string C@
formula-ptr formula-string 1+ -
> IF formula-ptr C@ formula-ptr 1+ TO formula-ptr
ELSE [CHAR] " THEN
;
: parse_formula ( <formula> -- )
[CHAR] " WORD formula-string strcpy
formula-string 1+ TO formula-ptr ;
[THEN]
: +c! ( n c-adr --) \ add n to the char at c-adr
TUCK C@ + SWAP C! ;
: append_char ( c c-adr --) \ append 1 char to a counted string
1 OVER +c! \ increment count
DUP C@ + C! ; \ get new address and store
VARIABLE {}level
: >0,4 {}level @ 0> 4 AND ; ( -- 0 | 4)
: copy ( c --) in_pad append_char ;
: copy&inc ( c --) copy 1 {}level +! ;
: copy&dec ( c --) copy -1 {}level +! ;
: err0 CR ." right } before left {" ABORT ;
: err1 CR ." left { between right }'s" ABORT ;
: err2 CR ." no chars betw. successive {'s or }'s" ABORT ;
: err3 CR ." last char before 1st } must be blank" ABORT ;
: err4 CR ." first char after last { must be blank" ABORT ;
7 4 fsm: put_char ( c col# --)
\ input other | bl | { | }
\ state -----------------------------------------------------------
( 0) || copy >0 || DROP >0 || copy&inc >1 || err0 >5
( 1) || err4 >6 || copy >2 || copy&inc >1 || err3 >6
( 2) || copy >2 || copy >3 || err2 >5 || err3 >6
( 3) || copy >2 || copy >3 || err2 >5 || copy&dec >0,4
( 4) || err3 >6 || err2 >6 || err1 >5 || copy&dec >0,4
( 5) ( abnormal termination w/ error0 or error1 )
( 6) ( abnormal termination w/ error2 or error3 )
;fsm
: [put_char] ( c -- col#) \ char -> col #: in out
1 OVER BL = AND ( -- c n) \ other 0
OVER [CHAR] { = 2 AND + ( -- c n) \ bl 1
SWAP [CHAR] } = 3 AND + ( -- #) \ { 2
; \ } 3
0 VALUE ()level
: count_parens ( c -- c )
DUP [CHAR] ( = 1 AND
OVER [CHAR] ) = -1 AND + ( -- c n)
()level + TO ()level ;
: get_formula
{}level OFF
in_pad OFF
0 >state put_char
parse_formula
BEGIN get-char count_parens
DUP [CHAR] " <>
WHILE DUP 0>
IF DUP [put_char] put_char
ELSE DROP THEN
REPEAT DROP
()level 0<> ABORT" Unbalanced parentheses!" ;
\ ---------------------------------------------- end formula input
\ ---------------------------------------------- conversion tables
: 'dfa ' >BODY ;
128 char_table: [token] \ convert ASCII char to token
\ "other" -> 0
1 'dfa [token] CHAR Z CHAR A install
1 'dfa [token] CHAR z CHAR a install
\ modified January 8th, 2004
1 'dfa [token] CHAR [ + C! \ for address passing
1 'dfa [token] CHAR ] + C! \ for address passing
2 'dfa [token] CHAR E CHAR D install
2 'dfa [token] CHAR e CHAR d install
3 'dfa [token] CHAR 9 CHAR 0 install
4 'dfa [token] CHAR . + C!
5 'dfa [token] CHAR ( + C!
6 'dfa [token] CHAR { + C!
7 'dfa [token] CHAR } + C!
8 'dfa [token] CHAR ) + C!
9 'dfa [token] CHAR + + C!
10 'dfa [token] CHAR - + C!
11 'dfa [token] CHAR * + C!
12 'dfa [token] CHAR / + C!
13 'dfa [token] CHAR ^ + C!
15 'dfa [token] CHAR = + C!
17 'dfa [token] CHAR , + C!
\ ------------------------------------------ end conversion tables
\ -------------------------------------------------- finding stuff
\ terminology: (end,beg) = pointers to substring
\ op = operator token
: skip_name ( end beg --)
DUP C@ [token] 1 3 WITHIN \ 1st char a letter or [ ?
IF BEGIN DUP C@ [token] 1 4 WITHIN \ skip letters or digits
WHILE 1+ REPEAT
ELSE CR ." A proper name must begin with a letter!" ABORT
THEN ;
0 value level
0 value c1
0 value c2
: [skip] ( end beg c1 c2 -- end beg')
0 ( LOCALS| level c2 c1 |)
TO level
TO c2
TO c1
DUP C@ c1 <> ?exit \ 1st char <> c1
BEGIN DUP C@
CASE
c1 OF 1 level + TO level ENDOF
c2 OF -1 level + TO level ENDOF
ENDCASE
1+ ( end beg')
DUP C@ c2 <> \ next char <> c2
level 0> INVERT AND \ and level <= 0
>R 2DUP < R> OR \ or past end of string
UNTIL
;
: skip_{} ( end beg -- end beg') [CHAR] { [CHAR] } [skip] ;
: skip_() ( end beg -- end beg') [CHAR] ( [CHAR] ) [skip] ;
: skip_digits ( adr -- adr') \ skip digits rightward
BEGIN DUP C@ [CHAR] 0 [CHAR] 9 1+ WITHIN
WHILE 1+ REPEAT ;
: skip_dp ( adr -- adr|adr+1) \ skip decimal point
DUP C@ [CHAR] . = ?inc ;
: skip+ ( adr -- adr|adr+1) \ skip + sign
DUP C@ [CHAR] + = ?inc ;
: skip- ( adr -- adr|adr+1) \ skip - sign
DUP C@ [CHAR] - = ?inc ;
: skip_fp# ( adr -- adr') \ skip past a fp#
skip_digits skip_dp skip_digits \ skip mantissa
DUP C@ [token] 2 = \ d,D,e or E ?
IF 1+ ELSE EXIT THEN
skip+ skip- skip_digits ; \ skip exponent
: pass_thru ( end beg -- end beg')
skip- \ ignore leading -
DUP C@ [token] CASE
3 OF skip_fp# ENDOF \ digit
4 OF skip_fp# ENDOF \ dec. pt.
1 OF skip_name \ letter
skip_{}
skip_() ENDOF
2 OF skip_name \ dDeE
skip_{}
skip_() ENDOF
5 OF skip_() ENDOF \ left paren: (
ENDCASE
;
: [op] ( char -- token) \ in out
[token] \ "other" 0
7 - DUP 0> AND 2/ ; \ + or - 1
\ * or / 2
\ ^ 3
\ = 4
\ , 5
: op_find ( end beg c -- adr | 0) \ find exposed operator
[op] >R ( end beg) \ save op token
BEGIN pass_thru \ ignore id's, fp#'s, fn's, (expr)'s
DUP C@ [op] R@ <> \ op not found
>R 2DUP > R> AND \ and not done
WHILE 1+ \ incr. ptr
REPEAT TUCK
\ > AND ( -- adr | 0)
\ recoded line above to preserve type-checking for kForth -- km 9/23/03
<= IF DROP 0 THEN ( -- adr | 0)
R> DROP \ clean up
;
\ ---------------------------------------------- end finding stuff
\ -------------------------------------------------------- parsing
: assign \ assign -> id = expr | id = | expr
$init
out_pad OFF
in_pad $ends 2DUP [CHAR] = op_find ( end beg ptr|0)
?DUP IF 1- TUCK >R [CHAR] = $push \ id = expr
( end) R> 2 + BL $push expr
ELSE OVER C@ [CHAR] = = \ id =
IF SWAP 1- SWAP [CHAR] =
ELSE BL THEN \ expr
$push
THEN
expr
;
0 ptr end
0 ptr beg
0 value op
: <expr> \ expr -> term | term & expr
end >r beg >r op >r
$pop to op to beg to end
end beg [CHAR] + op_find ( ptr | false)
?DUP IF ( ptr) DUP c@ >R \ save op'
\ $stack:
( ptr) end OVER 1+ R> $push \ expr' op'
( ptr) 1- beg op $push \ term op
term RECURSE
ELSE end beg op $push term \ term op
THEN
r> to op r> to beg r> to end
;
0 ptr end
0 ptr beg
0 value op
: <term> \ term -> factor | factor % term
end >r beg >r op >r
$pop to op to beg to end ( LOCALS| op beg end |)
end beg [CHAR] * op_find ( ptr true | false)
?DUP IF ( ptr) DUP c@ >R \ save op'
\ $stack:
0NULL op $push \ null op
end OVER 1+ R> $push \ term' op'
( ptr) 1- beg BL $push \ factor bl
factor RECURSE
ELSE end beg op $push
THEN
factor r> to op r> to beg r> to end ;
\ -------------- auxiliary words for parsing factor --------------
: <do_F@> S" F@ " ;
: <do_z@> S" z@ " ;
0 value op
0 ptr beg
0 ptr end
: <do_id> ( end beg op -- op)
to op to beg to end ( LOCALS| op beg end |)
op [CHAR] = = \ op is =
end beg 0null D= \ $ is 0null
OR INVERT \ true if neither
>R \ defer flag
\ modification for address-passing, January 8th, 2004
beg C@ [CHAR] [ = \ enclosed in [] ?
end C@ [CHAR] ] = AND \
>R \ defer flag
R@ IF beg 1+ TO beg \ remove []
end 1- TO end
THEN
R> INVERT \ not in []
end beg ends->count do_id
R> AND \ not =, and not null$
IF do_@ do_id THEN op
;
: leading-? ( adr -- f)
DUP C@ [CHAR] - = SWAP 1+ C@ [token] 3 <> AND ;
: $fneg S" FNEGATE " ;
: $zneg S" znegate " ;
DEFER neg$ ' $fneg IS neg$
0 value op
0 ptr beg
0 ptr end
: try_id ( op end beg -- f) \ true => $ was an id
to beg to end to op ( LOCALS| beg end op |)
beg skip- C@ [token] 1 3 WITHIN \ begins with letter
beg C@ BL = OR \ or a blank
end C@ [CHAR] ) <> AND \ doesn't end with )
DUP
IF end beg skip- op <do_id> .op \ was an id
beg C@ [CHAR] - =
IF neg$ do_fn THEN
THEN \ wasn't an id
;
: <try_fp#> ( op end beg -- f) \ true => $ was a fp#
ends->count >FLOAT
IF .fp# .op TRUE ELSE DROP FALSE THEN
;
: <try_z#> ( op end beg -- f) \ true => $ was a fp#
ends->count >FLOAT
IF 0e0 .fp# .op TRUE ELSE DROP FALSE THEN
;
: enclosed? ( end beg -- f)
C@ [CHAR] ( = SWAP
C@ [CHAR] ) = AND ;
0 value op
0 ptr beg
0 ptr end
\ try_(expr) is re-entrant due to "factor"
: try_(expr) ( op end beg -- f) \ true => $ was (expr)
end >r beg >r op >r
to beg to end to op ( LOCALS| beg end op |)
end beg enclosed?
IF 0null op $push end 1- beg 1+ BL $push
expr factor TRUE
ELSE FALSE THEN
r> to op r> to beg r> to end
;
: <do_f^> ( n --)
CASE 1 OF S" " ENDOF
2 OF S" f^2 " ENDOF
3 OF S" f^3 " ENDOF
4 OF S" f^4 " ENDOF
ENDCASE do_id
;
: <do_z^> ( n --)
CASE 1 OF S" " ENDOF
2 OF S" z^2 " ENDOF
3 OF S" z^3 " ENDOF
4 OF S" z^4 " ENDOF
ENDCASE do_id
;
: int<5? ( end beg -- n TRUE | FALSE)
ends->count 0 S>D 2SWAP >NUMBER ( d adr 0 | d' adr' n)
0= IF 2DROP DUP 1 5 WITHIN ( n f --)
ELSE 2DROP FALSE THEN ;
0 value op
0 ptr beg
0 ptr end
0 ptr optr
\ try_f1^f2 is re-entrant due to "factor"
: try_f1^f2 ( op end beg -- f) \ true => $ was f^f
optr >r end >r beg >r op >r
0 to optr to beg to end to op ( LOCALS| ptr beg end op |)
end beg skip- [CHAR] ^ op_find TO optr
optr
IF 0null op $push \ push operator
end optr 1+ int<5? \ is f2 an integer < 5
IF optr 1- beg skip- \ parse f1^n
BL $push
factor do_^
ELSE DROP \ clear stack
end optr 1+ [CHAR] ^ $push \ f2
optr 1- beg skip- BL $push \ push f1
factor factor
THEN factor
beg C@ [CHAR] - = IF neg$ do_fn THEN
THEN optr 0<> ( flag)
r> to op r> to beg r> to end r> to optr
;
\ ************************************************************
: func_lib ( xt -- c-adr)
CASE ['] FABS OF C" FABS " ENDOF
['] FACOS OF C" FACOS " ENDOF
['] FACOSH OF C" FACOSH " ENDOF
['] FASIN OF C" FASIN " ENDOF
['] FASINH OF C" FASINH " ENDOF
['] FATAN OF C" FATAN " ENDOF
['] FATAN2 OF C" FATAN2 " ENDOF
['] FATANH OF C" FATANH " ENDOF
['] FCOS OF C" FCOS " ENDOF
['] FCOSH OF C" FCOSH " ENDOF
['] FEXP OF C" FEXP " ENDOF
['] FLN OF C" FLN " ENDOF
['] FMAX OF C" FMAX " ENDOF
['] FMIN OF C" FMIN " ENDOF
['] FSIN OF C" FSIN " ENDOF
['] FSINH OF C" FSINH " ENDOF
['] FTAN OF C" FTAN " ENDOF
['] FSQRT OF C" FSQRT " ENDOF
['] FTANH OF C" FTANH " ENDOF
ENDCASE
;
[undefined] CAPS-FIND [IF]
: lcase? ( char -- flag=true if lower case)
DUP [CHAR] a MAX ( char max[a,c])
SWAP [CHAR] z MIN ( max[a,c] min[a,z])
= ;
: ucase ( c-adr u --) OVER + SWAP
DO I C@ DUP lcase? 32 AND - I C! LOOP ;
\ assumes ASCII character coding
: CAPS-FIND DUP COUNT ucase FIND ;
[THEN]
: Fname ( end beg -- xt TRUE | c-adr FALSE)
\ add leading F to fn.name and look up
>R 1+ R> ( end+1 beg)
1 PAD C! [CHAR] F PAD 1+ C!
PAD 1+ -ROT ( pad+1 end+1 beg)
DO 1+ I C@ OVER C! \ append char to PAD
1 PAD +c! \ incr. count at PAD
LOOP DROP
PAD CAPS-FIND 0<>
;
: list! ( --)
$pop >R \ defer op
2DUP [CHAR] , op_find ( end beg ptr|0) \ -> )comma(
?DUP IF ROT OVER 1+ ( beg ptr end ptr+1)
BL $push ( beg ptr)
1- SWAP BL $push
expr RECURSE
ELSE BL $push expr \ only 1 arg
THEN
R> .op \ emit op
;
0 value op
0 ptr beg
0 ptr end
0 ptr optr
: try_func ( op end beg -- f) \ fn -> id arglist
0 to optr to beg to end to op ( LOCALS| ptr beg end op |)
end beg skip- skip_name ( end beg')
DUP TO optr ( end ptr)
enclosed? DUP \ looks like a function
IF optr 1- beg skip- ( end' beg|beg+1)
Fname \ look up F+fn.name
beg C@ [CHAR] - = >R \ defer possible NEGATE
IF func_lib $ends ( end beg) \ library fn
ELSE DROP
optr 1- beg skip- ( end beg) \ other
THEN op $push \ push function name
end 1- optr 1+ BL $push \ push arg list
list! \ handle arg list
$pop -ROT ends->count do_fn .op
R> IF neg$ do_fn THEN
THEN
;
\ ---------------- end auxiliary words for factor ----------------
0 value op
0 ptr beg
0 ptr end
: <factor> \ factor -> id | fp# | ( expr ) | f^f | function
$pop to op to beg to end ( LOCALS| op beg end |) \ true => success
op end beg try_f1^f2 ?exit
op end beg try_id ?exit
op end beg try_fp# ?exit
op end beg try_(expr) ?exit
op end beg try_func ?exit
." Not a factor!" ABORT
;
\ ---------------------------------------------------- end parsing
\ --------------------------------------------------- output words
: real_op ( op --) [token]
CASE 9 OF S" F+ " ENDOF
10 OF S" F- " ENDOF
11 OF S" F* " ENDOF
12 OF S" F/ " ENDOF
13 OF S" F** " ENDOF
15 OF S" F! " ENDOF
0 OF S" " ENDOF
ENDCASE
do_fn
;
: cmplx_op ( op --) [token]
CASE 9 OF S" z+ " ENDOF
10 OF S" z- " ENDOF
11 OF S" z* " ENDOF
12 OF S" z/ " ENDOF
13 OF S" z^ " ENDOF
15 OF S" z! " ENDOF
0 OF S" " ENDOF
ENDCASE
do_fn
;
' <expr> IS expr \ resolve forward refs
' <term> IS term
' <factor> IS factor
: >out ( c-adr u --) out_pad concat ; \ append to out_pad
FORTH-WORDLIST SET-CURRENT \ definitions to FORTH
[undefined] $ftemp [IF] CREATE $ftemp 32 CHARS ALLOT [THEN]
[defined] REPRESENT [IF]
: f->$ ( f: r --) ( -- c-adr u)
BL $ftemp C!
$ftemp CHAR+ [CHAR] . OVER C! ( $ftemp+1)
CHAR+ PRECISION REPRESENT ( n f1 f2)
INVERT
IF ." Can't convert fp# to string!" ABORT THEN
IF [CHAR] - $ftemp C! THEN ( n)
$ftemp PRECISION 2 + CHARS + ( n adr)
[CHAR] E OVER C! \ add E
CHAR+ ( n adr+1)
SWAP S>D TUCK DABS <# #S ROT SIGN #> ( adr+1 c-adr u)
ROT SWAP DUP >R CMOVE
$ftemp PRECISION 3 + R> + CHARS ( c-adr u)
do_fn
;
[ELSE]
: f->$ ( f -- c-adr u)
14 f>string COUNT do_fn ;
[THEN]
: (f") ( --)
['] real_op IS .op \ redirect
['] <try_fp#> IS try_fp#
['] f->$ IS .fp#
['] >out IS do_id
['] >out IS do_fn
['] <do_f@> IS do_@
['] <do_f^> IS do_^
['] $fneg IS neg$
get_formula assign
out_pad DUP CELL+ SWAP @ ( c-adr u)
; nondeferred
: f" (f") STATE @
IF EVALUATE ELSE CR CR TYPE THEN ; IMMEDIATE nondeferred
: f$" (f") EVALUATE ; nondeferred
: z->$ ( f: x y --) FSWAP f->$ 0null ends->count do_fn f->$ ;
: (zz") ( --) \ can't use z" -- Win32Forth uses it!
['] cmplx_op IS .op \ redirect
['] <try_z#> IS try_fp#
['] z->$ IS .fp#
['] >out IS do_id
['] >out IS do_fn
['] <do_z@> IS do_@
['] <do_z^> IS do_^
['] $zneg IS neg$
get_formula assign
out_pad DUP CELL+ SWAP @ ( c-adr u)
;
: zz" (zz") STATE @
IF EVALUATE ELSE CR CR TYPE THEN
; IMMEDIATE
: zz$" (zz") EVALUATE ;
\ ----------------------------------------------- end output words
GET-ORDER NIP 1- SET-ORDER \ hide ftran definitions
\ ---------------------------------------------------- end program