This repository has been archived by the owner on Aug 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
mp_ccalc.pas
1341 lines (1166 loc) · 37.9 KB
/
mp_ccalc.pas
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
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit mp_ccalc;
{Parse and evaluate mp_complex expressions}
interface
{$i STD.INC}
{$X+}
{$ifdef BIT16}
{$N+}
{$endif}
uses
BTypes, mp_types;
{$i mp_conf.inc}
(*************************************************************************
DESCRIPTION : Parse and evaluate mp_complex expressions
REQUIREMENTS : BP7, D1-D7/D9-D10/D12/D17-D18, FPC, VP
EXTERNAL DATA : (mp_types)
MEMORY USAGE : heap
DISPLAY MODE : ---
REFERENCES : [19] T. Norvell: Parsing Expressions by Recursive Descent,
http://www.engr.mun.ca/~theo/Misc/exp_parsing.htm
[20] G. Toal's tutorial pages OperatorPrecedence.html,CompilersOneOhOne.html,
GrahamToalsCompilerDemo.html at http://www.gtoal.com/software/
[21] T.R. Nicely's parser.c in factor1.zip from http://www.trnicely.net
derived from GMP demo pexpr.c (see [15])
Version Date Author Modification
------- -------- ------- ------------------------------------------
0.0.10 12.11.18 W.Ehrhardt First BP version derived from mp_rcalc
0.0.11 12.11.18 we constant I
0.0.12 12.11.18 we _ARG, _CONJ, _RE, _IM
0.0.13 12.11.18 we _LOG10
0.0.14 13.11.18 we check some functions for invalid argument
0.0.15 13.11.18 we _ARCCOTHC
**************************************************************************)
(*-------------------------------------------------------------------------
(C) Copyright 2008-2018 Wolfgang Ehrhardt
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from
the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in
a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
----------------------------------------------------------------------------*)
{#Z+} {Turn off reference for help}
{Parse errors >0, eval errors < 0}
const
Err_Missing_LeftBracket = 1; {Missing "("}
Err_Missing_Comma = 2; {Missing argument separator}
Err_Missing_RightBracket = 3; {Missing ")"}
Err_Unknown_Function = 4; {Unknown function}
Err_Unknown_Element = 5; {Unknown element}
Err_Trailing_Garbage = 6; {Trailing garbage}
Err_Invalid_Number = 7; {Invalid number}
Err_Unknown_Operation = 8; {Unknown operation}
Err_Invalid_HexNumber = 9; {Invalid hex number}
Err_MPERR_Eval = -1; {MP_Err <> MP_OK}
Err_Division_by_zero = -2; {Division by zero}
Err_Invalid_argument = -3; {Invalid arg, e.g. ln(0)}
Err_X_not_init = -4; {Variable X not initialized}
Err_Y_not_init = -5; {Variable Y not initialized}
Err_Z_not_init = -6; {Variable Z not initialized}
Err_Overflow = -8; {Overflow}
{#Z-}
type
TFOperation = (_CONST, _CHS, _ABS, _ADD, _SUB, _MUL, _DIV, _EXPT, _SQRT,
_SQR, _X, _Y, _Z, _I, _ARG, _CONJ, _RE, _IM, _ARCCOSH,
_AGM, _ARCCOS, _ARCSIN, _ARCTAN, _ARCTAN2, _ARCSINH, _ARCTANH,
_COS, _COSH, _EXP, _EXPM1, _LN, _LN1P, _LOG10,
_SIN, _SINH, _TAN, _TANH, _COT, _CSC, _SEC, _COTH, _CSCH, _SECH,
_ARCCOT, _ARCCOTC, _ARCCSC, _ARCSEC, _ARCCOTH, _ARCCOTHC,
_ARCCSCH, _ARCSECH, _NROOT,
_TEST);
{implemented operators, functions, and variables}
type
PFExpr = ^TFExpr; {Expression node pointer}
TFExpr = record {binary tree node}
op: TFOperation; {operation/function/variable}
nn: byte; {number of nodes}
case integer of
0: (Value: mp_complex); {value if op=_CONST}
1: (SNode: PFExpr); {expr = (SNode op) or op(SNode)}
2: (LNode, RNode: PFExpr;) {expr = LNode op RNode or op(LNode,RNode)}
end;
TFEval = record {Evaluation record}
X : mp_complex; {Variable X}
Y : mp_complex; {Variable Y}
Z : mp_complex; {Variable Z}
Res: mp_complex; {Evaluation result}
Err: integer; {Eval error code}
end;
function mpc_parse(psz: pchar8; var e: PFExpr; var Err: integer): pchar8;
{-Parse string psz into expression tree e, if OK Err=0 and result^=#0,}
{ else Err=Err_xx and result points to error position}
procedure mpc_eval(e: PFExpr; var evr: TFEval);
{-Evaluate expression tree e, result in evr}
procedure mpc_clear_expr(var e: PFExpr);
{-Release memory used by e}
procedure mpc_calculate(psz: pchar8; var evr: TFEval; var EPos: integer);
{-Parse and evaluate string psz}
function mpc_calc_errorstr(Err: integer): mp_string;
{-Translate known error codes}
procedure mpc_init_eval(var evr: TFEval);
{-Initialize the mp_complex records of evr}
procedure mpc_clear_eval(var evr: TFEval);
{-Clear the mp_complex records of evr}
implementation
uses
mp_base, mp_numth, mp_real, mp_cmplx;
(* Approx. grammar for expression parser:
<Expr> ::= <Term> '+' <Term>
| <Term> '-' <Term>
| '+'<Term>
| '-'<Term>;
<Term> ::= <Factor> '*' <Factor>
| <Factor> '/' <Factor>
| <Factor>;
<Factor> ::= <Element> '^' <Factor>
| <Element>;
<Element> ::= <Func> | <Var> | <number> | 'i' | 'pi' | '(' <Expr> ')';
<Func> ::= <Ident> '(' <Arglist> ')';
<Var> ::= 'X'..'Z' | 'x'..'z';
<Arglist ::= <Expr> | <Expr> ',' <Expr>;
<Ident> ::= <alpha> {<alpha> | <digit>};
<intnum> ::= <digit> { <digit> };
<expo> ::= 'e' ['+' | '-'] <intnum>
| 'E' ['+' | '-'] <intnum>
<number> ::= ['+' | '-'] [<intnum>] ['.' [<intnum>] <expo>];
<digit> ::= '0'..'9';
<alpha> ::= 'A'..'Z'| 'a'..'z';
*)
type
str10 = string[10];
TFunc = record
op: TFOperation;
arg2: boolean;
name: str10;
end;
type
TOpVar = _X.._Z;
const
MaxFun = 41;
const
FuncTab : array[1..MaxFun] of TFunc = (
(op: _SQRT ; arg2: false; name: 'SQRT'),
(op: _SQR ; arg2: false; name: 'SQR'),
(op: _ABS ; arg2: false; name: 'ABS'),
(op: _ARG ; arg2: false; name: 'ARG'),
(op: _CONJ ; arg2: false; name: 'CONJ'),
(op: _RE ; arg2: false; name: 'RE'),
(op: _IM ; arg2: false; name: 'IM'),
(op: _ARCCOSH ; arg2: false; name: 'ARCCOSH'),
(op: _AGM ; arg2: true ; name: 'AGM'),
(op: _ARCCOS ; arg2: false; name: 'ARCCOS'),
(op: _ARCSIN ; arg2: false; name: 'ARCSIN'),
(op: _ARCTAN ; arg2: false; name: 'ARCTAN'),
(op: _ARCSINH ; arg2: false; name: 'ARCSINH'),
(op: _ARCTANH ; arg2: false; name: 'ARCTANH'),
(op: _COS ; arg2: false; name: 'COS'),
(op: _COSH ; arg2: false; name: 'COSH'),
(op: _EXP ; arg2: false; name: 'EXP'),
(op: _EXPM1 ; arg2: false; name: 'EXPM1'),
(op: _LN ; arg2: false; name: 'LN'),
(op: _LN1P ; arg2: false; name: 'LN1P'),
(op: _LOG10 ; arg2: false; name: 'LOG10'),
(op: _SIN ; arg2: false; name: 'SIN'),
(op: _SINH ; arg2: false; name: 'SINH'),
(op: _TAN ; arg2: false; name: 'TAN'),
(op: _TANH ; arg2: false; name: 'TANH'),
(op: _COT ; arg2: false; name: 'COT'),
(op: _CSC ; arg2: false; name: 'CSC'),
(op: _SEC ; arg2: false; name: 'SEC'),
(op: _COTH ; arg2: false; name: 'COTH'),
(op: _CSCH ; arg2: false; name: 'CSCH'),
(op: _SECH ; arg2: false; name: 'SECH'),
(op: _ARCCOT ; arg2: false; name: 'ARCCOT'),
(op: _ARCCOTC ; arg2: false; name: 'ARCCOTC'),
(op: _ARCCSC ; arg2: false; name: 'ARCCSC'),
(op: _ARCSEC ; arg2: false; name: 'ARCSEC'),
(op: _ARCCOTH ; arg2: false; name: 'ARCCOTH'),
(op: _ARCCOTHC ; arg2: false; name: 'ARCCOTHC'),
(op: _ARCCSCH ; arg2: false; name: 'ARCCSCH'),
(op: _ARCSECH ; arg2: false; name: 'ARCSECH'),
(op: _NROOT ; arg2: true; name: 'NROOT'),
(op: _TEST ; arg2: false; name: 'TEST') {used for tests, development etc}
);
{---------------------------------------------------------------------------}
function SkipWhite(psz: pchar8): pchar8;
{-Skip white space}
begin
while psz^ in [' ',#13,#10,#9] do inc(psz);
SkipWhite := psz;
end;
{---------------------------------------------------------------------------}
procedure mkNode(var r: PFExpr; op: TFOperation; nn: byte; e1, e2: PFExpr);
{-Make a new expression node for (e1 op e2) or (e1 op)}
begin
r := mp_alloc(sizeof(TFExpr));
r^.nn := nn;
r^.op := op;
r^.LNode := e1;
r^.RNode := e2;
end;
{---------------------------------------------------------------------------}
procedure mkNode0c(var r: PFExpr);
{-Make a new node for a constant}
begin
{alloc Value node initialize mp_complex}
r := mp_alloc(sizeof(TFExpr));
r^.op := _CONST;
r^.nn := 0;
mpc_init(r^.Value);
end;
{---------------------------------------------------------------------------}
procedure mkNode0v(var r: PFExpr; const v: TOpVar);
{-Make a new node for a variable v = _X, _Y, or _Z}
begin
{alloc Value node initialize mp_complex}
r := mp_alloc(sizeof(TFExpr));
r^.op := v;
r^.nn := 0;
end;
{---------------------------------------------------------------------------}
procedure mkNode1(var r: PFExpr; op: TFOperation; e: PFExpr);
{-Make a new expression node for r := e op}
begin
mkNode(r,op,1,e,nil);
end;
{---------------------------------------------------------------------------}
procedure mkNode2(var r: PFExpr; op: TFOperation; e1, e2: PFExpr);
{-Make a new expression node for r := e1 op e2}
begin
mkNode(r,op,2,e1,e2);
end;
{---------------------------------------------------------------------------}
function GetIdent(psz: pchar8): mp_string;
{-Gather next identifier, break if not in [A-Z, a-z, 0-9], result is uppercase}
{ first character in must be in [A-Z, a-z]}
var
s: mp_string;
begin
s := '';
if psz^ in ['A'..'Z', 'a'..'z'] then begin
while psz^ in ['A'..'Z', 'a'..'z', '0'..'9'] do begin
s := s+upcase(psz^);
inc(psz);
end;
end;
GetIdent := s;
end;
{---------------------------------------------------------------------------}
function GetFuncIndex(const s: mp_string): integer;
{-Test if s is a known function name, if yes return index else 0}
var
i: integer;
begin
for i:=1 to MaxFun do begin
if FuncTab[i].name=s then begin
GetFuncIndex := i;
exit;
end;
end;
GetFuncIndex := 0;
end;
{---------------------------------------------------------------------------}
function Expr(psz: pchar8; var e: PFExpr; var Err: integer): pchar8; forward;
{-Parse string psz into expression tree}
{---------------------------------------------------------------------------}
{---------------------------------------------------------------------------}
function Func(psz: pchar8; idx: integer; var e: PFExpr; var Err: integer): pchar8;
{-Build function expression, psz points between function name and "("}
var
e1,e2: PFExpr;
const
na: array[boolean] of byte = (1,2);
procedure clear;
{-Clear local PFExpr if error}
begin
if e1<>nil then mpc_clear_expr(e1);
if e2<>nil then mpc_clear_expr(e2);
end;
begin
e1 := nil;
e2 := nil;
e := nil;
Func := psz;
if Err<>0 then exit;
psz := SkipWhite(psz);
if psz^ <> '(' then begin
Func := psz;
Err := Err_Missing_LeftBracket;
exit;
end;
{get first argument}
psz := Expr(psz+1,e1, Err);
Func := psz;
if Err<>0 then begin
clear;
exit;
end;
psz := SkipWhite(psz);
if FuncTab[idx].arg2 then begin
{if two arguments search for ","}
if psz^ <> mp_arg_sep{','} then begin
Func := psz;
Err := Err_Missing_Comma;
clear;
exit;
end;
{evaluate second argument}
psz := Expr(psz+1,e2,Err);
Func := psz;
if Err<>0 then begin
clear;
exit;
end;
psz := SkipWhite(psz);
end
else e2:=nil;
{search for closing ")"}
if psz^ <> ')' then begin
Func := psz;
Err := Err_Missing_RightBracket;
clear;
exit;
end;
inc(psz);
with FuncTab[idx] do mkNode(e, op, na[arg2], e1, e2);
Func := psz;
end;
{---------------------------------------------------------------------------}
function Element(psz: pchar8; var e: PFExpr; var Err: integer): pchar8;
{-Parse an Element}
var
res: PFExpr;
s,sc: pchar8;
lsc: word;
i: integer;
s0: char8;
neg: boolean;
id: str255;
begin
Element := psz;
e := nil;
if Err<>0 then exit;
psz := SkipWhite(psz);
s0 := psz^;
if s0 in ['A'..'Z', 'a'..'z'] then begin
id := GetIdent(psz);
i := GetFuncIndex(id);
if i=0 then begin
if id='X' then begin mkNode0v(e, _X); inc(psz); end
else if id='Y' then begin mkNode0v(e, _Y); inc(psz); end
else if id='Z' then begin mkNode0v(e, _Z); inc(psz); end
else if id='I' then begin
mkNode0c(res);
mpc_seti(res^.Value);
inc(psz,1);
e := res;
end
else if id='PI' then begin
mkNode0c(res);
with res^.Value do begin
mpf_set_pi(re);
mpf_set0(im);
end;
inc(psz,2);
e := res;
end
else if id='LN2' then begin
mkNode0c(res);
with res^.Value do begin
mpf_set_ln2(re);
mpf_set0(im);
end;
inc(psz,3);
e := res;
end
{$ifdef MPC_E1Ln10Tab}
else if id='LN10' then begin
mkNode0c(res);
with res^.Value do begin
mpf_set_ln10(re);
mpf_set0(im);
end;
inc(psz,4);
e := res;
end
else if id='E' then begin
mkNode0c(res);
with res^.Value do begin
mpf_set_exp1(re);
mpf_set0(im);
end;
inc(psz,1);
e := res;
end
{$endif}
else begin
Err := Err_Unknown_Function;
Element := psz;
exit;
end;
end
else begin
inc(psz, length(id));
psz := Func(psz,i,e, Err);
end;
end
else if s0='(' then begin
psz := Expr(psz+1,e,Err);
if Err<>0 then exit;
psz := SkipWhite(psz);
if psz^<>')' then begin
Err := Err_Missing_RightBracket;
Element := psz;
exit;
end;
inc(psz);
end
else if s0 in ['0'..'9','+','-',mp_fract_sep{'.'}] then begin
{get sign}
neg := false;
if psz^ in ['+','-'] then begin
if psz^='-' then neg := true;
inc(psz);
end;
{count decimal characters}
s := psz;
{integer part}
while psz^ in ['0'..'9'] do inc(psz);
if psz^=mp_fract_sep{'.'} then begin
inc(psz);
{fractional part}
while psz^ in ['0'..'9'] do inc(psz);
end;
if (upcase(psz^)='E') and (s<>psz) then begin
{exponent part}
inc(psz);
if psz^ in ['+','-'] then inc(psz);
while psz^ in ['0'..'9'] do inc(psz);
end;
{alloc and move digit string to temp storage}
if s=psz then begin
{empty digit string}
Err := Err_Invalid_Number;
Element := psz;
exit;
end;
lsc := psz-s+1;
sc := mp_getmem(lsc);
move(s^,sc^,lsc-1);
sc[psz-s] := #0;
{Make a constant node}
mkNode0c(res);
{convert digit string to mp_float}
mpf_read_decimal(res^.Value.re,sc);
{apply sign}
if neg then mpf_chs(res^.Value.re, res^.Value.re);
mpf_set0(res^.Value.im);
e := res;
mp_freemem(pointer(sc),lsc);
end
else begin
Err := Err_Unknown_Element;
Element := psz;
exit;
end;
Element := psz;
end;
{---------------------------------------------------------------------------}
function Factor(psz: pchar8; var e: PFExpr; var Err: integer): pchar8;
{-Parse a Factor}
var
t: PFExpr;
begin
e := nil;
if Err<>0 then begin
Factor := psz;
exit;
end;
psz := Element(psz,e,Err);
if Err<>0 then begin
Factor := psz;
exit;
end;
{Look for optional power part of Factor}
psz := SkipWhite(psz);
if psz^='^' then begin
inc(psz);
t := nil;
psz := Factor(psz, t, Err);
if Err=0 then mkNode2(e, _EXPT, e, t)
else if t<>nil then mpc_clear_expr(t);
end;
Factor := psz;
end;
{---------------------------------------------------------------------------}
function Term(psz: pchar8; var e: PFExpr; var Err: integer): pchar8;
{-Parse a Term}
var
t: PFExpr;
begin
e := nil;
if Err<>0 then begin
Term := psz;
exit;
end;
t := nil;
psz := Factor(psz,e, Err);
while Err=0 do begin
psz := SkipWhite(psz);
case upcase(psz[0]) of
'*' : begin
psz := Factor(psz+1, t, Err);
if Err=0 then mkNode2(e, _MUL, e, t);
end;
'/': begin
psz := Factor(psz+1, t, Err);
if Err=0 then mkNode2(e, _DIV, e, t);
end;
else break;
end;
end;
if (Err<>0) and (t<>nil) then mpc_clear_expr(t);
Term := psz;
end;
{---------------------------------------------------------------------------}
function Expr(psz: pchar8; var e: PFExpr; var Err: integer): pchar8;
{-Parse an Expr}
var
t: PFExpr;
c: char8;
begin
e := nil;
if Err<>0 then begin
Expr := psz;
exit;
end;
psz := SkipWhite(psz);
c := psz^;
{Unary prefix operators}
if c='+' then begin
{just skip the +}
psz := Term(psz+1, e, Err)
end
else if c='-' then begin
psz := Term(psz+1, e, Err);
if Err<>0 then begin
Expr := psz;
exit;
end;
mkNode1(e, _CHS, e);
end
else psz := Term(psz, e, Err);
t := nil;
while Err=0 do begin
psz := SkipWhite(psz);
case psz^ of
'+': begin
psz := Term(psz+1, t, Err);
if Err=0 then mkNode2(e, _ADD, e, t);
end;
'-': begin
psz := Term(psz+1, t, Err);
if Err=0 then mkNode2(e, _SUB, e, t);
end;
else break;
end; {case}
end;
if (Err<>0) and (t<>nil) then mpc_clear_expr(t);
Expr := psz;
end;
{---------------------------------------------------------------------------}
function mpc_parse(psz: pchar8; var e: PFExpr; var Err: integer): pchar8;
{-Parse string psz into expression tree e, Err=0 and psz^=#0 if OK,}
{ else Err=Err_xx and result points to error postions}
begin
Err := 0;
psz := Expr(psz, e, Err);
if (Err=0) and (psz^<>#0) then Err := Err_Trailing_Garbage;
mpc_parse := psz;
end;
{---------------------------------------------------------------------------}
{------ The next functions are essentially copies from AMath -------------}
{---------------------------------------------------------------------------}
const
x80000000 = longint($80000000);
type
THexDblA = packed array[0..7] of byte; {Double as array of bytes}
THexSglA = packed array[0..3] of byte; {Single as array of bytes}
THexExtW = packed array[0..4] of word; {Extended as array of word}
THexDblW = packed array[0..3] of word; {Double as array of word}
THexSglW = packed array[0..1] of word; {Single as array of word}
type
TDblRec = packed record {Double as sign, exponent, significand}
lm: longint; {low 32 bit of significand}
hm: longint; {high bits of significand, biased exponent and sign}
end;
const MaxDblHex : THexDblW = ($ffff,$ffff,$ffff,$7fef); {1.797693134862315E+308}
const MaxSglHex : THexSglA = ($ff,$ff,$7f,$7f); {3.4028234E+38}
{---------------------------------------------------------------------------}
function predd(d: double): double;
{-Return next representable double after d in the direction -Inf}
begin
with TDblRec(d) do begin
if THexDblW(d)[3] and $7FF0=$7FF0 then begin
{Inf or Nan}
if (hm and $7FFFFFFF=$7FF00000) and (lm=0) then begin
{d is +- Inf}
if d>0.0 then d := double(MaxDblHex);
end;
end
else begin
{finite number}
if d=0.0 then begin
hm := x80000000;
lm := 1;
end
else if d<0.0 then begin
{d<0: increment significand}
inc(lm);
if lm=0 then inc(hm);
end
else begin
{d>0: decrement significand}
if lm=0 then dec(hm);
dec(lm);
end;
end;
predd := d;
end;
end;
{---------------------------------------------------------------------------}
function preds(s: single): single;
{-Return next representable single after s in the direction -Inf}
var
L: longint absolute s;
begin
if L and $7F800000 = $7F800000 then begin
{Inf or Nan, don't change Nan or -Inf}
if L and $7FFFFF = 0 then begin
{s is +- Inf}
if s>0.0 then s := single(MaxSglHex);
end;
end
else begin
{finite number}
if s=0.0 then L := x80000000
else if s<0.0 then inc(L)
else dec(L);
end;
preds := s;
end;
{---------------------------------------------------------------------------}
function succd(d: double): double;
{-Return next representable double after d in the direction +Inf}
begin
with TDblRec(d) do begin
if THexDblW(d)[3] and $7FF0=$7FF0 then begin
{Inf or Nan}
if (hm and $7FFFFFFF=$7FF00000) and (lm=0) then begin
{d is +- Inf}
if d<0.0 then d := -double(MaxDblHex);
end;
end
else begin
{finite number}
if d=0.0 then begin
hm := 0;
lm := 1;
end
else if d>0.0 then begin
{d>0: increment significand}
inc(lm);
{hm < $7FF00000, so inc(hm) cannot overflow and will give}
{the correct result succd(predd(PosInf_d)) = PosInf_d}
if lm=0 then inc(hm);
end
else begin
{d<0: decrement significand}
if lm=0 then dec(hm);
dec(lm);
end;
end;
succd := d;
end;
end;
{---------------------------------------------------------------------------}
function succs(s: single): single;
{-Return next representable single after s in the direction +Inf}
var
L: longint absolute s;
begin
if L and $7F800000 = $7F800000 then begin
{Inf or Nan, don't change Nan or +Inf}
if L and $7FFFFF = 0 then begin
{s is +- Inf}
if s<0.0 then s := -single(MaxSglHex);
end;
end
else begin
{finite number}
if s=0.0 then L := 1
else if s>0.0 then inc(L)
else dec(L);
end;
succs := s;
end;
{$ifndef EXT64}
{Only implemented for hardware 80-bit extended}
type
TExtRec = packed record {Extended as sign, exponent, significand}
lm: longint; {low 32 bit of significand}
hm: longint; {high 32 bit of significand}
xp: word; {biased exponent and sign }
end;
const MinExtHex : THexExtW = ($0000,$0000,$0000,$8000,$0001); {MinExtended as Hex}
const MaxExtHex : THexExtW = ($ffff,$ffff,$ffff,$ffff,$7ffe); {MaxExtended as Hex}
{---------------------------------------------------------------------------}
function predx(x: extended): extended;
{-Return next representable extended after x in the direction -Inf}
begin
with TExtRec(x) do begin
if xp and $7FFF=$7FFF then begin
{Inf or Nan}
if (hm=x80000000) and (lm=0) then begin
{x is +- Inf}
if x>0.0 then x := extended(MaxExtHex);
end;
predx := x;
exit;
end;
if xp and $7FFF = 0 then begin
{Handle pseudo-denormal: Set exponent to +/- 1, significand is unchanged}
if hm<0 then xp := xp or 1;
end
else if hm>=0 then begin
{don't touch unnormals}
predx := x;
exit;
end;
{finite number}
if x=0 then begin
xp := $8000;
lm := 1;
end
else if xp and $8000 <> 0 then begin
{x<0: increment significand}
inc(lm);
if lm=0 then begin
inc(hm);
if (hm=0) or ((xp=$8000) and (hm=x80000000)) then begin
inc(xp);
hm := hm or x80000000;
if xp=$FFFF then x := -DblNegInf;
end;
end;
end
else begin
{x>0: decrement significand}
if hm<0 then begin
if lm=0 then begin
if hm=x80000000 then begin
dec(xp);
dec(hm);
if xp>0 then hm := hm or x80000000;
end
else dec(hm);
end;
dec(lm);
end
else begin
{denormal}
if lm=0 then dec(hm);
dec(lm);
end;
end;
end;
predx := x;
end;
{---------------------------------------------------------------------------}
function succx(x: extended): extended;
{-Return next representable extended after x in the direction +Inf}
begin
with TExtRec(x) do begin
if xp and $7FFF=$7FFF then begin
{Inf or Nan}
if (hm=x80000000) and (lm=0) then begin
{x is +- Inf}
if x<0.0 then x := -extended(MaxExtHex);
end;
succx := x;
exit;
end;
if xp and $7FFF = 0 then begin
{Handle pseudo-denormal: Set exponent to +/- 1, significand is unchanged}
if hm<0 then xp := xp or 1;
end
else if hm>=0 then begin
{don't touch unnormals}
succx := x;
exit;
end;
{finite number}
if x=0.0 then begin
xp := 0;
lm := 1;
end
else if xp and $8000 = 0 then begin
{x>0: increment significand}
inc(lm);
if lm=0 then begin
inc(hm);
if (hm=0) or ((xp=0) and (hm=x80000000)) then begin
inc(xp);
hm := hm or x80000000;
if xp=$7FFF then x := DblPosInf;;
end;
end;
end
else begin
{x<0: decrement significand}
if lm=0 then begin
if (hm>=0) or (hm=x80000000) then begin
dec(hm);
dec(xp);
if xp and $7FFF > 0 then hm := hm or x80000000;
end
else dec(hm);
end;
dec(lm);
end;
end;
succx := x;
end;
{$endif}
{---------------------------------------------------------------------------}
procedure eval(e: PFExpr; var r: mp_complex; var evr: TFEval);
{-(internal) evaluate expression tree e, result in r}
var
v1,v2: mp_complex;
lr,li,lx: longint;
td: double;
begin
if evr.Err<>0 then exit;
if e^.nn=0 then begin
case e^.op of
_CONST: begin
mpc_copy(e^.Value,r);
if MP_Error<>0 then evr.Err := Err_MPERR_Eval;
end;
_X: begin
if mpc_not_init(evr.X) then evr.Err := Err_X_not_init
else mpc_copy(evr.X,r);
end;
_Y: begin
if mpc_not_init(evr.Y) then evr.Err := Err_Y_not_init
else mpc_copy(evr.Y,r);
end;
_Z: begin
if mpc_not_init(evr.Z) then evr.Err := Err_Z_not_init
else mpc_copy(evr.Z,r);
end;
else evr.Err := Err_Unknown_Operation;
end;
exit;
end;
{always initialize two mp_complex although some ops need only 0 or 1}
mpc_initp2(v1,v2,mpf_get_default_prec);
if MP_Error<>MP_OKAY then begin
evr.Err := Err_MPERR_Eval;
exit;
end;