-
Notifications
You must be signed in to change notification settings - Fork 0
/
FCALC.C
1400 lines (1153 loc) · 29.2 KB
/
FCALC.C
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
/* FCALC.C -- RPN Calculator
Written March 1991 by Craig A. Finseth
Copyright 1991,2,3,4 by Craig A. Finseth
*/
#include "freyja.h"
#include <math.h>
/* ---------- configuration ---------- */
#define REGCOUNT 20 /* number of user registers */
#define BINSIZE 32 /* max size of a binary in bits */
#define WORKSIZE 20 /* amount of "working input" space */
#define SYS_CALC "%calc%"
/* ---------- commands ---------- */
enum CMDS { CM_ADD, CM_AND, CM_B, CM_CF, CM_CLRG, CM_CLST, CM_CLX,
CM_D, CM_DEFAULT, CM_DIGSEPOFF, CM_DIGSEPON, CM_DIV, CM_ENTER,
CM_FACT, CM_H, CM_HELP, CM_INV, CM_LASTX, CM_MEMVIEW, CM_MOD, CM_MUL,
CM_NEG, CM_NOT, CM_NULL, CM_NUM, CM_O, CM_OR, CM_PCT, CM_PCTCH,
CM_PCTTOT, CM_RADIXC, CM_RADIXD, CM_RCL, CM_RDN, CM_RUP, CM_SF, CM_SQ,
CM_SQRT, CM_STO, CM_SUB, CM_SWAP, CM_SWAPR, CM_WSIZE, CM_WSIZEQ,
CM_XEQ, CM_XOR, CM_XRND, CM_LAST };
/* ---------- command list ---------- */
struct command {
enum CMDS cmd; /* command id */
char *name; /* command name */
char desc[11]; /* command descriptor:
desc[0], command suffix
SP none
'B' buffer name
'L' label
'N' number
'P' oPerator or register
'R' register
desc[1], number of arguments dropped off the stack
'0' zero, '1', one, '2' two, '3' three, '4' four
desc[2345], argument descriptors
SP no argument for this entry
'*' any type
'B' coerce to binary
'R' coerce to real
desc[6], number of results pushed to stack
'0' zero, '1', one, '2' two, '3' three, '4' four
desc[7], last X usage
SP not affected
'L' last x register is updated
desc[8], stack lift
SP disabled
'E' enabled
desc[9], trace info
SP nothing special
R register
S summation
desc[10] NUL */
char *help; /* help string */
};
#define NUMCMDS (sizeof(commands) / sizeof(commands[0]))
/* base commands */
static struct command commands[] = {
{ CM_NUM, "", " 0 1 E ", "enter a number" },
{ CM_PCT, "%", " 2RR 2LE ", "percent" },
{ CM_PCTCH, "%CH", " 2RR 1LE ", "percent change" },
{ CM_PCTTOT, "%TOT", " 2RR 2LE ", "percent of total" },
{ CM_MUL, "*", " 2** 1LE ", "multiply" },
{ CM_ADD, "+", " 2** 1LE ", "add" },
{ CM_SUB, "-", " 2** 1LE ", "subtract" },
{ CM_DIV, "/", " 2RR 1LE ", "divide" },
{ CM_INV, "1/X", " 1R 1LE ", "inverse (use INV)" },
{ CM_RCL, "<", "P0 1 ER", "recall" },
{ CM_STO, ">", "P1* 1 ER", "store" },
{ CM_AND, "AND", " 2BB 1LE ", "bitwise and" },
{ CM_B, "B", " 0 0 E ", "set binary mode" },
{ CM_CF, "CF", "N0 0 E ", "clear flag" },
{ CM_CLRG, "CLRG", " 0 0 E ", "clear registers" },
{ CM_CLST, "CLST", " 0 0 E ", "clear stack" },
{ CM_CLX, "CLX", " 0 0 D ", "clear x" },
{ CM_D, "D", " 0 0 E ", "set decimal mode" },
{ CM_DEFAULT, "DEFAULT"," 0 0 E ", "restore default settings" },
{ CM_DIGSEPOFF, "DIGSEPOFF"," 0 0 E ","set digit sep to not display" },
{ CM_DIGSEPON, "DIGSEPON"," 0 0 E ","set digit sep to display" },
{ CM_ENTER, "ENTER^"," 1* 2 D ", "enter" },
{ CM_FACT, "FACT", " 1R 1LE ", "factorial" },
{ CM_H, "H", " 0 0 E ", "set hexadecimal mode" },
{ CM_HELP, "HELP", " 0 0 E ", "help" },
{ CM_INV, "INV", " 1R 1LE ", "inverse" },
{ CM_LASTX, "L", " 0 1 E ", "recall last x" },
{ CM_LASTX, "LASTX"," 0 1 E ", "recall last x" },
{ CM_MEMVIEW, "MEMVIEW"," 0 0 E ", "view calculator memory" },
{ CM_MOD, "MOD", " 2RR 1LE ", "modulus" },
{ CM_NEG, "NEG", " 1R 1LE ", "negate" },
{ CM_NOT, "NOT", " 1B 1LE ", "bitwise not" },
{ CM_NULL, "NULL", " 0 0 E ", "no op" },
{ CM_O, "O", " 0 0 E ", "set octal mode" },
{ CM_OR, "OR", " 2BB 1LE ", "bitwise or" },
{ CM_RDN, "R", " 0 0 E ", "roll down" },
{ CM_RADIXC, "RADIX,"," 0 0 E ", "set radix mark to ," },
{ CM_RADIXD, "RADIX."," 0 0 E ", "set radix mark to ." },
{ CM_RCL, "RCL", "P0 1 ER", "recall" },
{ CM_RDN, "RDN", " 0 0 E ", "roll down" },
{ CM_RUP, "R^", " 0 0 E ", "roll up" },
{ CM_SWAP, "S", " 2** 2 E ", "swap: x<>y" },
{ CM_SF, "SF", "N0 0 E ", "set flag" },
{ CM_SQRT, "SQRT", " 1R 1LE ", "square root" },
{ CM_STO, "STO", "P1* 1 ER", "store" },
{ CM_STO, "ST", "P1* 1 ER", "store" },
{ CM_ENTER, "T", " 1* 2 D ", "enter" },
{ CM_WSIZE, "WSIZE"," 1B 0 E ", "set word size" },
{ CM_WSIZEQ, "WSIZE?"," 0 1 E ", "get word size" },
{ CM_SWAPR, "X<>", "R1* 1 ER", "swap with" },
{ CM_XEQ, "XEQ", "B0 0 E ", "execute a buffer" },
{ CM_XOR, "XOR", " 2BB 1LE ", "bitwise xor" },
{ CM_XRND, "XRND", " 2BR 1LE ", "round Y to X decimal places" },
{ CM_SQ, "X^2", " 1* 1LE ", "square" },
{ CM_PCTCH, "\\GD%"," 2RR 1LE ", "delta %" } };
/* ---------- flags ---------- */
/* The flag array (part of memory) assumes 16 flags/int. That way,
the indices in this table don't have to be recomputed for different
word sizes. */
enum FLGS { FL_RADIX, FL_DIGGRP, FL_WSIZE, FL_BINMODE, FL_LAST };
struct flag {
enum FLGS flg; /* flag id */
int start; /* starting flag # */
int bits; /* number of bits */
int index; /* flag array index */
int shift; /* flag array element shift */
int mask; /* flag array element mask */
char *desc; /* description */
};
#define NUMFLAGS 7 /* number of ints used to hold flags */
#define MAXFLAG 100 /* largest flag supported */
struct flag flags[] = { /* must be in enum FLGS order */
/* user flags */
{ FL_RADIX, 28, 1, 1, 12, 0x1, "radix mark: 0). 1)," },
{ FL_DIGGRP, 29, 1, 1, 13, 0x1, "digit groupings shown: 0)no 1)yes" },
/* Freyja-local flags */
{ FL_WSIZE, 65, 8, 4, 0,0xFF, "binary integer word size" },
{ FL_BINMODE, 73, 2, 4, 8, 0x3, "binary numbers 0)dec 1)oct 2)bin 3)hex" } };
#define FLV_DEC 0x00
#define FLV_OCT 0x01
#define FLV_BIN 0x02
#define FLV_HEX 0x03
/* ---------- number format ---------- */
struct number {
union {
int b; /* binary integer goes here */
double r; /* real number goes here */
} n;
char type; /* 'B' binary, 'R' real */
};
/* ---------- memory ---------- */
#define NUMSTACK 5
#define NUMREGS (REGCOUNT + NUMSTACK)
#define X (NUMREGS)
#define Y (NUMREGS + 1)
#define Z (NUMREGS + 2)
#define T (NUMREGS + 3)
#define L (NUMREGS + 4)
#define NUMSUM 6
#define SUMX 0
#define SUMX2 1
#define SUMY 2
#define SUMY2 3
#define SUMXY 4
#define SUMN 5
struct memory {
struct number r[NUMREGS + NUMSTACK];
int flags[NUMFLAGS]; /* the flags */
FLAG stack_lift;
};
/* ---------- variables ---------- */
static struct memory m; /* memory */
static char input[WORKSIZE + 1]; /* buffer for holding input */
static char fmt_buf[BINSIZE + 2]; /* buffer for U_Fmt */
static int bin_mask; /* word mask for binary operations */
static char doexit = NUL; /* do we exit? NUL = no, N = yes,
don't insert #, Y = yes, insert # */
static enum CMDS pushedcmd = CM_NULL; /* pushed command: execute first */
static struct command *cmdptr; /* current command */
static int cmdnum; /* numeric argument for command */
static FLAG cmdind; /* was it an indirect? */
static struct number *cmdreg; /* register pointer */
static enum CMDS cmdcmd; /* command for sto/rcl */
static char *cmdrest; /* rest of command string */
/* Arguments for commands. Values have been coerced. */
static struct number x;
static struct number y;
static struct number z;
static struct number t;
void U_Cmd(void);
void U_CmdSetup(char arg, struct number *from, struct number *to);
int U_Dispatch(enum CMDS cmd);
struct command *U_FindCmd(enum CMDS cmd);
int U_FlGet(enum FLGS f);
void U_FlSet(enum FLGS f, int value);
char *U_Fmt(struct number *nptr);
char *U_FmtBin(char *buf, int value, FLAG first);
FLAG U_In(void);
void U_Status(void);
void U_ToBin(struct number *nptr);
void U_ToReal(struct number *nptr);
void U_View(void);
/* ------------------------------------------------------------ */
/* Initialize to default values. */
void
UInit(void)
{
int cnt;
for (cnt = 0; cnt < NUMREGS; cnt++) {
m.r[cnt].type = 'R';
m.r[cnt].n.r = 0.0;
}
for (cnt = 0; cnt < NUMFLAGS; cnt++) {
m.flags[cnt] = 0;
}
U_FlSet(FL_RADIX, (Res_Char(RES_CONF, RES_RADIX) == '.') ? 1 : 0);
U_FlSet(FL_DIGGRP, 1);
U_FlSet(FL_WSIZE, BINSIZE);
bin_mask = ~0;
U_FlSet(FL_BINMODE, FLV_HEX);
m.stack_lift = TRUE;
}
/* ------------------------------------------------------------ */
/* Calculator */
void
UCalc(void)
{
uarg = 0;
for (doexit = NUL; doexit == NUL; ) {
U_In();
if (cmdptr != NULL) U_Cmd();
}
if (doexit == 'Y') BInsStr(U_Fmt(&m.r[X]));
DModeLine();
}
/* ------------------------------------------------------------ */
/* Return the operation's description. */
char *
UDescr(int op)
{
return(commands[op].name);
}
/* ------------------------------------------------------------ */
/* Enter the current number into the calculator. */
void
UEnter(void)
{
char buf[WORKSIZE + 1];
FLAG isafter;
FLAG isfirst = TRUE;
char *cptr = buf;
KEYCODE chr;
WNumMark();
if (isafter = BIsAfterMark(mark)) BMarkSwap(mark);
BMarkToPoint(cwin->point);
while (cptr < &buf[sizeof(buf) - 2] && BIsBeforeMark(mark)) {
chr = BGetCharAdv();
if (isfirst) {
if (chr == '-') *cptr++ = '0';
isfirst = FALSE;
}
if (chr == '-') chr = '~';
*cptr++ = chr;
}
*cptr++ = SP;
BPointToMark(cwin->point);
if (isafter) BMarkSwap(mark);
uarg = 0;
KFromStr(buf, cptr - buf);
UCalc();
}
/* ------------------------------------------------------------ */
/* Return the operation's help text. */
char *
UHelp(int op)
{
return(commands[op].help);
}
/* ------------------------------------------------------------ */
/* Return the number of operators. */
int
UNumOps(void)
{
return(NUMCMDS);
}
/* ------------------------------------------------------------ */
/* Insert a copy of the X register. */
void
UPrintX(void)
{
uarg = 0;
if (isuarg) {
WNumMark();
RRegDelete();
}
BInsStr(U_Fmt(&m.r[X]));
}
/* ------------------------------------------------------------ */
/* Execute the current command. */
void
U_Cmd(void)
{
struct number tmp;
int retval;
int amt;
/* set up args */
U_CmdSetup(cmdptr->desc[2], &m.r[X], &x);
U_CmdSetup(cmdptr->desc[3], &m.r[Y], &y);
U_CmdSetup(cmdptr->desc[4], &m.r[Z], &z);
U_CmdSetup(cmdptr->desc[5], &m.r[T], &t);
/* handle indirect registers */
if (cmdptr->desc[0] == 'R' || cmdptr->desc[0] == 'P') {
if (cmdind) {
tmp = *cmdreg;
U_ToBin(&tmp);
if (tmp.n.b < 0 || tmp.n.b >= REGCOUNT) {
DError(RES_UNKINDREG);
cmdptr = NULL;
return;
}
cmdreg = &m.r[tmp.n.b];
}
}
/* execute */
retval = U_Dispatch(cmdptr->cmd);
if (retval < 0) {
/* lastx */
if (cmdptr->desc[7] == 'L') m.r[L] = m.r[X];
/* drop stack */
if (cmdptr->desc[1] == '0') {
}
else if (cmdptr->desc[1] == '1') {
m.r[X] = m.r[Y];
m.r[Y] = m.r[Z];
m.r[Z] = m.r[T];
}
else if (cmdptr->desc[1] == '2') {
m.r[X] = m.r[Z];
m.r[Y] = m.r[T];
m.r[Z] = m.r[T];
}
else {
m.r[X] = m.r[T];
m.r[Y] = m.r[T];
m.r[Z] = m.r[T];
}
/* save results */
amt = cmdptr->desc[6] - '0';
/* stack lift disabled and new operation lifts the stack */
if (!m.stack_lift && cmdptr->desc[1] < cmdptr->desc[6]) {
amt = cmdptr->desc[6] - cmdptr->desc[1] - 1;
if (amt == 0) m.r[X] = x;
}
if (amt <= 0) {
}
else if (amt == 1) {
m.r[T] = m.r[Z];
m.r[Z] = m.r[Y];
m.r[Y] = m.r[X];
m.r[X] = x;
}
else if (amt == 2) {
m.r[T] = m.r[Y];
m.r[Z] = m.r[X];
m.r[Y] = y;
m.r[X] = x;
}
else if (amt == 3) {
m.r[T] = m.r[X];
m.r[Z] = z;
m.r[Y] = y;
m.r[X] = x;
}
else {
m.r[T] = t;
m.r[Z] = z;
m.r[Y] = y;
m.r[X] = x;
}
/* stack lift */
m.stack_lift = cmdptr->desc[8] == 'E';
}
else DError(retval);
}
/* ------------------------------------------------------------ */
/* Setup the register according to the argument type. */
void
U_CmdSetup(char arg, struct number *from, struct number *to)
{
if (arg != SP) {
*to = *from;
if (arg == 'B') U_ToBin(to);
else if (arg == 'R') U_ToReal(to);
}
}
/* ------------------------------------------------------------ */
/* The command dispatch table. Return -1 on success or a number of an
error message if a failure. */
int
U_Dispatch(enum CMDS cmd)
{
struct number tmp;
struct buffer *bptr;
int cnt;
int num;
char buf[WORKSIZE];
switch (cmd) {
case CM_ADD:
if (x.type == 'B' && y.type == 'B') {
x.n.b += y.n.b;
x.n.b &= bin_mask;
}
else {
U_ToReal(&x);
U_ToReal(&y);
x.type = 'R';
x.n.r += y.n.r;
}
break;
case CM_AND:
x.n.b &= y.n.b;
x.n.b &= bin_mask;
break;
case CM_B:
U_FlSet(FL_BINMODE, FLV_BIN);
break;
case CM_CF:
if (cmdnum < 1 || cmdnum > MAXFLAG) return(RES_CALCFLAG);
num = cmdnum - 1;
m.flags[num >> 4] &= ~(1 << (num & 0xF));
break;
case CM_CLRG:
for (cnt = 0; cnt < REGCOUNT; cnt++) {
m.r[cnt].type = 'R';
m.r[cnt].n.r = 0.0;
}
break;
case CM_CLST:
for (cnt = X; cnt < L; cnt++) {
m.r[cnt].type = 'R';
m.r[cnt].n.r = 0.0;
}
break;
case CM_CLX:
m.r[X].type = 'R';
m.r[X].n.r = 0.0;
break;
case CM_D:
U_FlSet(FL_BINMODE, FLV_DEC);
break;
case CM_DEFAULT:
UInit();
break;
case CM_DIGSEPOFF:
U_FlSet(FL_DIGGRP, 0);
break;
case CM_DIGSEPON:
U_FlSet(FL_DIGGRP, 1);
break;
case CM_DIV:
if (x.n.r == 0.0) return(RES_CALCDIVZERO);
x.n.r = y.n.r / x.n.r;
break;
case CM_ENTER:
y = x;
m.stack_lift = TRUE;
break;
case CM_FACT:
if (x.n.r < 0.0) return(RES_CALCNEG);
for (cnt = x.n.r, x.n.r = 1.0; cnt > 1; cnt--) {
x.n.r *= cnt;
}
break;
case CM_H:
U_FlSet(FL_BINMODE, FLV_HEX);
break;
case CM_HELP:
MMenuH();
DIncrDisplay();
break;
case CM_INV:
if (x.n.r == 0.0) return(RES_CALCDIVZERO);
x.n.r = 1.0 / x.n.r;
break;
case CM_LASTX:
x = m.r[L];
break;
case CM_MEMVIEW:
U_View();
break;
case CM_MOD:
if (x.n.r == 0.0) return(RES_CALCMODZERO);
x.n.r = y.n.r - x.n.r * floor(y.n.r / x.n.r);
break;
case CM_MUL:
if (x.type == 'B' && y.type == 'B') {
x.n.b *= y.n.b;
x.n.b &= bin_mask;
}
else {
U_ToReal(&x);
U_ToReal(&y);
x.type = 'R';
x.n.r *= y.n.r;
}
break;
case CM_NEG:
x.n.r = -x.n.r;
break;
case CM_NOT:
x.n.b = ~x.n.b;
x.n.b &= bin_mask;
break;
case CM_NULL:
break;
case CM_NUM:
break;
case CM_O:
U_FlSet(FL_BINMODE, FLV_OCT);
break;
case CM_OR:
x.n.b |= y.n.b;
x.n.b &= bin_mask;
break;
case CM_PCT:
x.n.r *= y.n.r / 100.0;
break;
case CM_PCTCH:
if (y.n.r == 0.0) return(RES_CALCPCTZERO);
x.n.r = 100.0 * (x.n.r - y.n.r) / y.n.r;
break;
case CM_PCTTOT:
if (y.n.r == 0.0) return(RES_CALCPCTZERO);
x.n.r = 100.0 * x.n.r / y.n.r;
break;
case CM_RADIXC:
U_FlSet(FL_RADIX, 0);
break;
case CM_RADIXD:
U_FlSet(FL_RADIX, 1);
break;
case CM_RCL:
if (cmdnum < 0) {
for (cnt = 0; KIsKey() == 'N'; cnt++) {
xsprintf(buf, " %d ", cnt);
DEcho(buf);
}
KGetChar();
return(NULL);
}
y = x;
x = *cmdreg;
U_Dispatch(cmdcmd);
break;
case CM_RDN:
tmp = m.r[X];
m.r[X] = m.r[Y];
m.r[Y] = m.r[Z];
m.r[Z] = m.r[T];
m.r[T] = tmp;
break;
case CM_RUP:
tmp = m.r[X];
m.r[X] = m.r[T];
m.r[T] = m.r[Z];
m.r[Z] = m.r[Y];
m.r[Y] = tmp;
break;
case CM_SF:
if (cmdnum < 1 || cmdnum > MAXFLAG) return(RES_CALCFLAG);
num = cmdnum - 1;
m.flags[num >> 4] |= 1 << (num & 0xF);
break;
case CM_SQ:
x.n.r *= x.n.r;
break;
case CM_SQRT:
if (x.n.r < 0.0) return(RES_CALCNEG);
x.n.r = sqrt(x.n.r);
break;
case CM_STO:
y = *cmdreg;
U_Dispatch(cmdcmd);
*cmdreg = x;
break;
case CM_SUB:
if (x.type == 'B' && y.type == 'B') {
x.n.b = y.n.b - x.n.b;
x.n.b &= bin_mask;
}
else {
U_ToReal(&x);
U_ToReal(&y);
x.type = 'R';
x.n.r = y.n.r - x.n.r;
}
break;
case CM_SWAP:
tmp = y;
y = x;
x = tmp;
break;
case CM_SWAPR:
tmp = x;
x = *cmdreg;
*cmdreg = tmp;
break;
case CM_WSIZE:
if (x.n.b < 0 || x.n.b > BINSIZE) return(RES_CALCRANGE);
U_FlSet(FL_WSIZE, (int)x.n.b);
if (x.n.b == BINSIZE)
bin_mask = ~0;
else bin_mask = (1 << x.n.b) - 1;
break;
case CM_WSIZEQ:
x.type = 'B';
x.n.b = U_FlGet(FL_WSIZE);
break;
case CM_XEQ:
bptr = BBufFind(cmdrest);
if (bptr == NULL) return(RES_CALCUNKPROG);
KFromBuf(bptr);
break;
case CM_XOR:
x.n.b ^= y.n.b;
x.n.b &= bin_mask;
break;
case CM_XRND:
num = x.n.b;
for (cnt = 0; cnt < num; cnt++) y.n.r *= 10.0;
y.n.r += (y.n.r > 0) ? 0.5 : -0.5;
tmp.n.b = y.n.r;
x.n.r = tmp.n.b;
for (cnt = 0; cnt < num; cnt++) x.n.r /= 10.0;
x.type = 'R';
break;
}
return(-1);
}
/* ------------------------------------------------------------ */
/* Return a pointer to the command structure for the specified
command. */
struct command *
U_FindCmd(enum CMDS cmd)
{
struct command *cptr;
for (cptr = commands; cptr < &commands[NUMCMDS]; cptr++) {
if (cmd == cptr->cmd) return(cptr);
}
return(NULL);
}
/* ------------------------------------------------------------ */
/* Return the value of the specified flag. */
int
U_FlGet(enum FLGS f)
{
struct flag *fptr = &flags[(int)f];
return((m.flags[fptr->index] >> fptr->shift) & fptr->mask);
}
/* ------------------------------------------------------------ */
/* Set the specified flag to the supplied value. */
void
U_FlSet(enum FLGS f, int v)
{
struct flag *fptr = &flags[(int)f];
v = (v & fptr->mask) << fptr->shift;
m.flags[fptr->index] &= ~(fptr->mask << fptr->shift);
m.flags[fptr->index] |= v;
}
/* ------------------------------------------------------------ */
/* Return a pointer to a static buffer that contains a formatted
version of the number. */
char *
U_Fmt(struct number *nptr)
{
int mode = U_FlGet(FL_BINMODE);
int cnt;
FLAG iscomma = U_FlGet(FL_RADIX) == 0;
char *cptr;
char *cptr2;
if (nptr->type == 'B') {
switch (mode) {
case FLV_BIN:
*fmt_buf = '#';
U_FmtBin(&fmt_buf[1], nptr->n.b, TRUE);
strcat(fmt_buf, "b");
break;
case FLV_OCT:
xsprintf(fmt_buf, "#%oo", nptr->n.b);
break;
case FLV_DEC:
xsprintf(fmt_buf, "#%ud", nptr->n.b);
break;
case FLV_HEX:
xsprintf(fmt_buf, "#%xh", nptr->n.b);
break;
}
}
else {
sprintf(fmt_buf, "%.9lg", nptr->n.r);
/* handle comma */
if (iscomma) {
for (cptr = fmt_buf; *cptr != NUL; cptr++) {
if (*cptr == '.') *cptr = ',';
}
}
/* handle digit separator */
if (U_FlGet(FL_DIGGRP) == 1) {
cptr2 = fmt_buf;
if (*cptr2 == '-') cptr2++;
cnt = 0;
for (cptr = cptr2; xisdigit(*cptr); cptr++, cnt++) ;
while (cnt > 3) {
cptr -= 3;
memmove(cptr + 1, cptr, strlen(cptr) + 1);
*cptr = iscomma ? '.' : ',';
cnt -= 3;
}
}
}
return(fmt_buf);
}
/* ------------------------------------------------------------ */
/* Print out a binary integer. */
char *
U_FmtBin(char *buf, int value, FLAG first)
{
if (value >= 2) buf = U_FmtBin(buf, value / 2, FALSE);
if (value == 0 && first)
*buf++ = '0';
else *buf++ = (value % 2) + '0';
*buf = NUL;
return(buf);
}
/* ------------------------------------------------------------ */
/* Input the next command. */
FLAG
U_In(void)
{
KEYCODE chr;
int amt;
int cnt;
int base;
char buf[BIGBUFFSIZE];
char *cptr;
char *cptr2;
FLAG isdone = FALSE;
FLAG iscomma;
cmdptr = NULL;
*input = NUL;
if (pushedcmd != CM_NULL) {
cmdptr = U_FindCmd(pushedcmd);
pushedcmd = CM_NULL;
return;
}
chr = KEYREGEN;
while (!isdone) {
amt = strlen(input);
U_Status();
chr = KGetChar();
if (chr == KEYQUIT) {
MExit();
doexit = 'N';
return;
}
else if (chr < 0) {
continue;
}
if (chr >= 256) { /* function key */
chr -= 256;
switch (chr) {
case 59: /* F1 */
pushedcmd = CM_HELP;
isdone = TRUE;
break;
case 67: /* F9 */
case 38: /* Alt-L */
chr = '-';
goto chs;
/*break;*/
case 93: /* Shift-F10 */
doexit = 'N';
MExit();
return;
/*break;*/
case 48: /* Alt-B */
pushedcmd = CM_LASTX;
isdone = TRUE;
break;
case 46: /* Alt-C */
pushedcmd = CM_SWAP;
isdone = TRUE;
break;
case 50: /* Alt-M */
xstrcpy(input, "RCL");
amt = strlen(input);
break;
case 49: /* Alt-N */
xstrcpy(input, "STO");
amt = strlen(input);
break;
case 47: /* Alt-V */
pushedcmd = CM_RDN;
isdone = TRUE;
break;
case 45: /* Alt-X */
pushedcmd = CM_INV;
isdone = TRUE;
break;
case 44: /* Alt-Z */
pushedcmd = CM_SQRT;
isdone = TRUE;
break;
default:
TBell();
return;
/*break;*/
}
continue;
}