00004850f329a047143c2ac171d7479dd35b7bc6
[calc.git] / parser.c
1 #include <math.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include "alloc.h"
7 #include "argument.h"
8 #include "debug.h"
9 #include "element.h"
10 #include "format.h"
11 #include "program.h"
12 #include "stack.h"
13 #include "storage.h"
14
15 #include "parser.h"
16
17 /* external definition */
18
19 extern void history ();
20
21 /* global variables */
22
23 double answer = 0;
24
25 double anglefactor = 1;
26
27 /* compare codes */
28
29 int codecmp (char *ref, char *str)
30 {
31 int sig;
32
33 while (*ref != '\0') {
34 if (*ref == '\t') {
35 sig = (*str == '.') ? -1 : ((*str >= '0') && (*str <= '9'));
36 } else {
37 sig = *str - *ref;
38 }
39 if (sig != 0) {
40 return (sig > 0) ? 1 : -1;
41 }
42 str++;
43 ref++;
44 }
45
46 return 0;
47 }
48
49 /* subparser function */
50
51 element_t *subparser (element_t **proot, char **pstr, func_t func, int nbops, int prio)
52 {
53 element_t *new = newelement (func, nbops, prio);
54 new->ops[0] = *proot;
55 new->ops[1] = parser (*pstr, pstr, new->prio);
56 if ((new->ops[1] == NULL) || ((new->ops[1] != ERROR_OP) && (new->ops[1]->prio == 9))) {
57 delelement (new->ops[1]);
58 new->ops[1] = ERROR_OP;
59 }
60 if (new->ops[1] == ERROR_OP) {
61 delelement (new);
62 *proot = NULL;
63 return ERROR_OP;
64 }
65 *proot = newelement (Val, 1, 5);
66 (*proot)->ops[0] = new;
67
68 return *proot;
69 }
70
71 /* parser function */
72
73 element_t *parser (char *str, char **next, int prio)
74 {
75 element_t *root = NULL;
76 char *string = str;
77
78 VERBOSE (DEBUG, fprintf (stdout, "Starting parsing\n"));
79
80 /* main loop */
81 while (*str != '\0') {
82 int found = 0;
83 element_t *new = NULL;
84 VERBOSE (INFO, fprintf (stdout, "Processing: %s\n", str));
85
86 /* end without printing */
87
88 if (*str == ';') {
89 if (root) {
90 root->hidden = 1;
91 }
92 break;
93 }
94
95 /* skip spaces and tabs */
96
97 if ((*str == ' ') || (*str == '\t')) {
98 str++;
99 continue;
100 }
101
102 /* check for open brace */
103
104 if (*str == '{') {
105 VERBOSE (DEBUG, fprintf (stdout, "start processing brace\n"));
106 if (root != NULL) {
107 delelement (root);
108 return ERROR_OP;
109 }
110 root = newelement (Code, 0, 5);
111
112 do {
113 new = parser (str + 1, &str, 0);
114 if ((new == NULL) || ((new != ERROR_OP) && (new->prio == 9))) {
115 delelement (new);
116 new = ERROR_OP;
117 }
118 if (new == ERROR_OP) {
119 delelement (root);
120 return ERROR_OP;
121 }
122 element_t *newcode = newelement (Code, root->nbops + 1, 5);
123 for (int i = 0; i < root->nbops; i++) {
124 newcode->ops[i] = root->ops[i];
125 root->ops[i] = NULL;
126 }
127 newcode->ops[root->nbops] = new;
128 delelement (root);
129 root = newcode;
130 } while (*str == ',');
131
132 if (*str != '}') {
133 delelement (root);
134 return ERROR_OP;
135 }
136 str++;
137 VERBOSE (DEBUG, fprintf (stdout, "stop processing brace\n"));
138 continue;
139 }
140
141 /* check for open bracket */
142
143 if (*str == '(') {
144 VERBOSE (DEBUG, fprintf (stdout, "start processing bracket\n"));
145 if (root) {
146 if (root->func == Val) {
147 delelement (root);
148 return ERROR_OP;
149 }
150 do {
151 found = 0;
152 new = parser (str + 1, &str, 0);
153 if ((new == NULL) || ((new != ERROR_OP) && (new->prio == 9))) {
154 delelement (new);
155 new = ERROR_OP;
156 }
157 if ((new == NULL) || (new == ERROR_OP)) {
158 delelement (root);
159 return ERROR_OP;
160 }
161 for (int i = 0; i < root->nbops; i++) {
162 if (root->ops[i] == NULL) {
163 root->ops[i] = new;
164 found = 1;
165 break;
166 }
167 }
168 if (!found) {
169 delelement (new);
170 delelement (root);
171 return ERROR_OP;
172 }
173 } while (*str == ',');
174 } else {
175 root = newelement (Id, 1, 5);
176 new = parser (str + 1, &str, 0);
177 if ((new == NULL) || ((new != ERROR_OP) && (new->prio == 9))) {
178 delelement (new);
179 new = ERROR_OP;
180 }
181 if ((new == NULL) || (new == ERROR_OP) || (*str == ',')) {
182 delelement (new);
183 delelement (root);
184 return ERROR_OP;
185 }
186 root->ops[0] = new;
187 }
188 if (*str != ')') {
189 delelement (root);
190 return ERROR_OP;
191 }
192 str++;
193 VERBOSE (DEBUG, fprintf (stdout, "stop processing bracket\n"));
194 continue;
195 }
196
197 /* check for closing bracket, closing brace or koma */
198
199 if ((*str == ')') || (*str == '}') || (*str == ',')) {
200 if (prio == -9) {
201 delelement (root);
202 return ERROR_OP;
203 }
204 if (next != NULL) {
205 *next = str;
206 }
207 return root;
208 }
209
210 /* look for operators */
211
212 for (int i = 0; i < NB_OPERATORS; i++) {
213 keyword_t *operator = operators + i;
214 if (codecmp (operator->keyword, str) == 0) {
215 VERBOSE (DEBUG, fprintf (stdout, "start processing operator\n"));
216 if ((root) && (root->prio == 9)) {
217 VERBOSE (DEBUG, fprintf (stdout, "terminal function (%d)\n", root->func));
218 delelement (root);
219 return ERROR_OP;
220 } else if (root) {
221 if ((prio) && (prio > operator->prio)) {
222 VERBOSE (DEBUG, fprintf (stdout, "stop because operator priority\n"));
223 *next = str;
224 return root;
225 }
226 str += operator->offset;
227 VERBOSE (INFO, fprintf (stdout, "Oper: %d\n", operator->func));
228 if (subparser (&root, &str, operator->func, operator->nbops, operator->prio) == ERROR_OP) {
229 delelement (root);
230 return ERROR_OP;
231 }
232 } else if (*str == '-') {
233 root = newelement (Sig, 1, 6);
234 } else {
235 return ERROR_OP;
236 }
237 found = 1;
238 VERBOSE (DEBUG, fprintf (stdout, "stop processing operator\n"));
239 break;
240 }
241 }
242 if (found) {
243 continue;
244 }
245
246 /* look for functions */
247
248 for (int i = 0; i < NB_FUNCTIONS; i++) {
249 keyword_t *function = functions + i;
250 if (codecmp (function->keyword, str) == 0) {
251 VERBOSE (DEBUG, fprintf (stdout, "start processing function\n"));
252 if (root == NULL) {
253 VERBOSE (INFO, fprintf (stdout, "Func: %d\n", function->func));
254 root = newelement (function->func, function->nbops, function->prio);
255 } else {
256 delelement (root);
257 return ERROR_OP;
258 }
259 str += function->offset;
260 found = 1;
261 VERBOSE (DEBUG, fprintf (stdout, "stop processing function\n"));
262 break;
263 }
264 }
265 if (found) {
266 continue;
267 }
268
269 /* look for constant */
270
271 for (int i = 0; i < NB_CONSTANTS; i++) {
272 keyword_t *constant = constants + i;
273 if (codecmp (constant->keyword, str) == 0) {
274 VERBOSE (DEBUG, fprintf (stdout, "start processing constant\n"));
275 if (root == NULL) {
276 VERBOSE (INFO, fprintf (stdout, "Const: %d\n", constant->func));
277 root = newelement (constant->func, constant->nbops, constant->prio);
278 } else {
279 delelement (root);
280 return ERROR_OP;
281 }
282 str += constant->offset;
283 found = 1;
284 VERBOSE (DEBUG, fprintf (stdout, "stop processing constant\n"));
285 break;
286 }
287 }
288 if (found) {
289 continue;
290 }
291
292 /* look for number */
293
294 VERBOSE (DEBUG, fprintf (stdout, "start processing value\n"));
295 char *pt;
296 double value = (get_ibase () == 10) ? strtod (str, &pt) : strtol (str, &pt, get_ibase ());
297 VERBOSE (INFO, fprintf (stdout, "Value: %f\n", value));
298 if (str != pt) {
299 if ((root == NULL) || (root->prio == 6)) {
300 new = newelement (Val, 1, 5);
301 new->value = value;
302 if (root == NULL) {
303 root = new;
304 } else {
305 for (int i = 0; i < root->nbops; i++) {
306 if (root->ops[i] == NULL) {
307 root->ops[i] = new;
308 found = 1;
309 break;
310 }
311 }
312 if (!found) {
313 delelement (new);
314 delelement (root);
315 return ERROR_OP;
316 }
317 }
318 str = pt;
319 } else if ((*str == '+') || (*str == '-')) {
320 if ((prio) && (prio > 1)) {
321 VERBOSE (DEBUG, fprintf (stdout, "stop because operator priority\n"));
322 *next = str;
323 return root;
324 }
325 if (subparser (&root, &str, Add, 2, 1) == ERROR_OP) {
326 delelement (root);
327 return ERROR_OP;
328 }
329 } else {
330 delelement (root);
331 return ERROR_OP;
332 }
333 found = 1;
334 }
335 VERBOSE (DEBUG, fprintf (stdout, "stop processing value\n"));
336
337 /* error */
338
339 if (!found) {
340 delelement (root);
341 return ERROR_OP;
342 }
343
344 }
345
346 if (next != NULL) {
347 *next = str;
348 }
349
350 /* save string */
351 if (root != NULL) {
352 root->string = string;
353 }
354
355 return root;
356 }
357
358 /* print element tree */
359
360 void print_element (element_t *root, int level)
361 {
362 char *func = NULL;
363
364 if ((root == NULL) || (root == ERROR_OP)) {
365 return;
366 }
367
368 for (int i = 0; i < level; i++) {
369 printf (" ");
370 }
371
372 switch (root->func) {
373 case Val: func = "Value"; break;
374 case Id: func = "Ident"; break;
375 case Sig: func = "Sign"; break;
376 case Add: func = "Addition"; break;
377 case Sub: func = "Subtraction"; break;
378 case Mul: func = "Multiplication"; break;
379 case Div: func = "Division"; break;
380 case Mod: func = "Modulo"; break;
381 case Pow: func = "Power"; break;
382 case Sqr: func = "Square Root"; break;
383 case Cos: func = "Cosine"; break;
384 case Sin: func = "Sine"; break;
385 case Tan: func = "Tangent"; break;
386 case Acos: func = "Arc Cosine"; break;
387 case Asin: func = "Arc Sine"; break;
388 case Atan: func = "Arc Tangent"; break;
389 case Ln: func = "Logarithm (natural)"; break;
390 case Log: func = "Logarithm (10 base)"; break;
391 case Exp: func = "Exponantial"; break;
392 case Erfc: func = "Complementary Error Function"; break;
393 case Erf: func = "Error Function"; break;
394 case Abs: func = "Absolute value"; break;
395 case Ceil: func = "Ceil value"; break;
396 case Floor: func = "Floor value"; break;
397 case Store: func = "Store"; break;
398 case Recall: func = "Recall"; break;
399 case Inc: func = "Increase"; break;
400 case Dec: func = "Decrease"; break;
401 case Disp: func = "Display"; break;
402 case Memory: func = "Memory"; break;
403 case Clear: func = "Clear"; break;
404 case Quit: func = "Quit"; break;
405 case Help: func = "Help"; break;
406 case History: func = "History"; break;
407 case Ans: func = "Ans"; break;
408 case Pi: func = "Pi"; break;
409 case E: func = "E"; break;
410 case Equal: func = "Equal"; break;
411 case Diff: func = "Different"; break;
412 case Ge: func = "Greater or equal"; break;
413 case Le: func = "Lesser or equal"; break;
414 case Gt: func = "Greater"; break;
415 case Lt: func = "Lesser"; break;
416 case And: func = "And"; break;
417 case Or: func = "Or"; break;
418 case Not: func = "Not"; break;
419 case Cond: func = "Condition"; break;
420 case While: func = "While"; break;
421 case Code: func = "Code"; break;
422 case Print: func = "Print"; break;
423 case Prog: func = "Program"; break;
424 case Arg: func = "Argument"; break;
425 case Call: func = "Call"; break;
426 case List: func = "List"; break;
427 case Edit: func = "Edit"; break;
428 case Del: func = "Del"; break;
429 case Get: func = "Get"; break;
430 case Length: func = "Length"; break;
431 case Pop: func = "Pop"; break;
432 case Push: func = "Push"; break;
433 case Put: func = "Put"; break;
434 case Set: func = "Set"; break;
435 case Show: func = "Show"; break;
436 case Max: func = "Maximum"; break;
437 case Mean: func = "Mean"; break;
438 case Median: func = "Median"; break;
439 case Min: func = "Minimum"; break;
440 case Order: func = "Order"; break;
441 case Prod: func = "Product"; break;
442 case Sum: func = "Sum"; break;
443 case Variance: func = "Variance"; break;
444 case Precision: func = "Precision"; break;
445 case Base: func = "Base"; break;
446 case Deg: func = "Degree"; break;
447 case Grad: func = "Gradian"; break;
448 case Rad: func = "Radian"; break;
449 }
450
451 printf ("Function: %s\n", func);
452
453 if ((root->func == Val) && (root->ops[0] == NULL)) {
454 for (int i = 0; i < level; i++) {
455 printf (" ");
456 }
457 printf ("value: %f\n", root->value);
458 } else {
459 for (int i = 0; i < root->nbops; i++) {
460 print_element (root->ops[i], level + 1);
461 }
462 }
463 }
464
465 /* While do function */
466
467 double while_do (element_t *cond, element_t *action)
468 {
469 double ret = 0;
470 element_t *temp = NULL;
471
472 VERBOSE (DEBUG, fprintf (stdout, "starting while loop\n"));
473 while (1) {
474 VERBOSE (DEBUG, fprintf (stdout, "loop...\n"));
475
476 temp = dupelement (cond);
477 double test = evaluate_element (temp, 0);
478 delelement (temp);
479 if (!test) {
480 break;
481 }
482 if (action) {
483 temp = dupelement (action);
484 ret = evaluate_element (temp, 0);
485 delelement (temp);
486 }
487 }
488
489 VERBOSE (DEBUG, fprintf (stdout, "ending while loop\n"));
490
491 return ret;
492 }
493
494 /* program function */
495
496 double execute_code (element_t **prog, int nbcalls)
497 {
498 double ret = 0;
499 for (int i = 0; i < nbcalls; i++) {
500 ret = evaluate_element (prog[i], 0);
501 }
502 return ret;
503 }
504
505 /* quit function */
506
507 void quit (void)
508 {
509 printf ("bye\n");
510 exit (0);
511 }
512
513 /* help message */
514
515 void help (void)
516 {
517 printf ("calc is a simple calculator\n\n");
518 printf ("arithmetic op.:");
519 printf (" + - * / %% ^\n");
520 printf ("comparison op.:");
521 printf (" == != >= <= > <\n");
522 printf ("logical op.:");
523 printf (" & | !\n");
524 printf ("mathematic func.:");
525 printf (" exp ln log pow sqrt\n");
526 printf ("trigonometric func.:");
527 printf (" acos asin atan cos sin tan\n");
528 printf ("error functions:");
529 printf (" erf erfc\n");
530 printf ("miscellaneous func.:");
531 printf (" abs ceil floor\n");
532 printf ("storage func.:");
533 printf (" clear dec disp inc mem rcl sto\n");
534 printf ("control flow prim.:");
535 printf (" cond print while {} ;\n");
536 printf ("program management:");
537 printf (" arg call del edit ls prog\n");
538 printf ("stack management:");
539 printf (" get len pop push put set show\n");
540 printf ("stack func.:");
541 printf (" max mean med min ord prod sum var\n");
542 printf ("control management:");
543 printf (" base format help hist quit\n");
544 printf ("angle management:");
545 printf (" deg grad rad\n");
546 printf ("constants:");
547 printf (" ans e pi\n");
548 }
549
550 /* format function */
551
552 int format (int precision)
553 {
554 if (precision > 0) {
555 set_precision (precision);
556 set_format ();
557 } else if (precision != -1) {
558 VERBOSE (WARNING, fprintf (stdout, "error incorrect precision (%d)\n", precision));
559 return 0;
560 }
561 return get_precision ();
562 }
563
564 /* base function */
565
566 void base (int in, int out)
567 {
568 if ((in > 0) && (in < 37)) {
569 set_base (in, in);
570 if ((out > 0) && (out < 37)) {
571 set_base (in, out);
572 } else if (out != - 1) {
573 VERBOSE (WARNING, fprintf (stdout, "error incorrect output base (%d)\n", out));
574 }
575 } else if (in != -1 ) {
576 VERBOSE (WARNING, fprintf (stdout, "error incorrect input base (%d)\n", in));
577 } else {
578 printf ("base (I/O): %s\n", show_base ());
579 }
580 }
581
582 /* evaluate element tree */
583
584 #define MASK_SUB 0x1
585 #define MASK_DIV 0x2
586
587 double evaluate_element (element_t *root, char mask)
588 {
589 double op0 = 0, op1 = 0;
590 char nextmask = mask;
591 int i, nb;
592
593 if ((root == NULL) || (root == ERROR_OP)) {
594 VERBOSE (WARNING, fprintf (stdout, "error while evaluating\n"));
595 return 0;
596 }
597
598 /* mask to manage sub operator sub and div */
599 switch (root->func) {
600 case Add:
601 nextmask &= ~MASK_SUB;
602 nextmask &= ~MASK_DIV;
603 break;
604 case Sub:
605 nextmask |= MASK_SUB;
606 nextmask &= ~MASK_DIV;
607 break;
608 case Mul:
609 nextmask &= ~MASK_DIV;
610 break;
611 case Div:
612 nextmask |= MASK_DIV;
613 break;
614 default:
615 nextmask = mask;
616 }
617
618 switch (root->func) {
619 case Val:
620 case Sig:
621 op0 = (root->ops[0]) ? evaluate_element (root->ops[0], nextmask) : root->value;
622 break;
623 case Add:
624 case Sub:
625 case Mul:
626 case Div:
627 case Mod:
628 case Pow:
629 case Equal:
630 case Diff:
631 case Ge:
632 case Le:
633 case Gt:
634 case Lt:
635 case And:
636 case Or:
637 if (root->ops[1]) {
638 op1 = evaluate_element (root->ops[1], nextmask);
639 } else {
640 VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[1])\n"));
641 return 0;
642 }
643 /* fallthrough */
644 case Id:
645 case Sqr:
646 case Cos:
647 case Sin:
648 case Tan:
649 case Acos:
650 case Asin:
651 case Atan:
652 case Ln:
653 case Log:
654 case Exp:
655 case Erfc:
656 case Erf:
657 case Abs:
658 case Ceil:
659 case Floor:
660 case Recall:
661 case Inc:
662 case Dec:
663 case Not:
664 case Cond:
665 case Prog:
666 case Arg:
667 case Call:
668 case Edit:
669 case Del:
670 case Get:
671 if (root->ops[0]) {
672 op0 = evaluate_element (root->ops[0], 0);
673 } else {
674 VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[0])\n"));
675 return 0;
676 }
677 break;
678 case Disp:
679 case Clear:
680 case Quit:
681 case Help:
682 case History:
683 case Ans:
684 case Pi:
685 case E:
686 case Code:
687 case List:
688 case Length:
689 case Pop:
690 case Set:
691 case Show:
692 case Median:
693 case Order:
694 case Prod:
695 case Sum:
696 case Deg:
697 case Grad:
698 case Rad:
699 break;
700 case While:
701 if (root->ops[0] == NULL) {
702 VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[0])\n"));
703 return 0;
704 }
705 break;
706 case Memory:
707 if (root->ops[0] == NULL) {
708 op0 = -1;
709 } else {
710 op0 = (int)evaluate_element (root->ops[0], 0);
711 if (op0 < 0) {
712 VERBOSE (WARNING, fprintf (stdout, "error incorrect memory size (%d)\n", (int)op0));
713 return 0;
714 }
715 }
716 break;
717 case Push:
718 case Print:
719 op0 = (root->ops[0]) ? evaluate_element (root->ops[0], 0) : answer;
720 break;
721 case Store:
722 case Put:
723 if (root->ops[0]) {
724 op0 = evaluate_element (root->ops[0], 0);
725 } else {
726 VERBOSE (WARNING, fprintf (stdout, "error while evaluating (op[0])\n"));
727 return 0;
728 }
729 op1 = (root->ops[1]) ? evaluate_element (root->ops[1], 0) : answer;
730 break;
731 case Max:
732 case Mean:
733 case Min:
734 case Variance:
735 if (root->ops[0]) {
736 op0 = evaluate_element (root->ops[0], 0);
737 op1 = (root->ops[1]) ? evaluate_element (root->ops[1], 0) : answer;
738 }
739 break;
740 case Precision:
741 op0 = (root->ops[0]) ? evaluate_element (root->ops[0], 0) : -1;
742 break;
743 case Base:
744 op0 = (root->ops[0]) ? evaluate_element (root->ops[0], 0) : -1;
745 op1 = (root->ops[1]) ? evaluate_element (root->ops[1], 0) : -1;
746 break;
747 }
748
749 switch (root->func) {
750 case Val: return op0;
751 case Id: return op0;
752 case Sig: return -op0;
753 case Add: return ((mask & MASK_SUB) == 0) ? op0 + op1 : op0 - op1;
754 case Sub: return ((mask & MASK_SUB) == 0) ? op0 - op1 : op0 + op1;
755 case Mul: return ((mask & MASK_DIV) == 0) ? op0 * op1 : op0 / op1;
756 case Div: return ((mask & MASK_DIV) == 0) ? op0 / op1 : op0 * op1;
757 case Mod: return fmod (op0, op1);
758 case Pow: return pow (op0, op1);
759 case Sqr: return sqrt (op0);
760 case Cos: return cos (op0 / anglefactor);
761 case Sin: return sin (op0 / anglefactor);
762 case Tan: return tan (op0 / anglefactor);
763 case Acos: return acos (op0) * anglefactor;
764 case Asin: return asin (op0) * anglefactor;
765 case Atan: return atan (op0) * anglefactor;
766 case Ln: return log (op0);
767 case Log: return log10 (op0);
768 case Exp: return exp (op0);
769 case Erfc: return erfc (op0);
770 case Erf: return erf (op0);
771 case Abs: return fabs (op0);
772 case Ceil: return ceil (op0);
773 case Floor: return floor (op0);
774 case Store: return store ((int)op0, op1);
775 case Recall: return recall ((int)op0);
776 case Inc: return increase ((int)op0);
777 case Dec: return decrease ((int)op0);
778 case Disp: display (); break;
779 case Memory: return memory ((root->ops[0]) ? (int)op0 : -1);
780 case Clear: clear (); break;
781 case Quit: quit (); break;
782 case Help: help (); break;
783 case History: history (); break;
784 case Ans: return answer;
785 case Pi: return M_PI;
786 case E: return M_E;
787 case Equal: return op0 == op1;
788 case Diff: return op0 != op1;
789 case Ge: return op0 >= op1;
790 case Le: return op0 <= op1;
791 case Gt: return op0 > op1;
792 case Lt: return op0 < op1;
793 case And: return (op0 != 0) && (op1 != 0);
794 case Or: return (op0 != 0) || (op1 != 0);
795 case Not: return (op0 == 0);
796 case Cond:
797 if ((op0) && (root->ops[1])) {
798 return evaluate_element (root->ops[1], 0);
799 } else if ((!op0) && (root->ops[2])) {
800 return evaluate_element (root->ops[2], 0);
801 } else {
802 return 0;
803 }
804 case While: return while_do (root->ops[0], root->ops[1]);
805 case Code: return execute_code (root->ops, root->nbops);
806 case Print: return print (op0);
807 case Prog:
808 prog ((int)op0, root->ops[1]);
809 savestring ((int)op0, root->string);
810 break;
811 case Arg: return arg ((int)op0);
812 case Call:
813 for (i = 1, nb = 0; i < root->nbops; i++) {
814 if (root->ops[i]) {
815 nb++;
816 }
817 }
818 return call ((int)op0, nb, root->ops + 1);
819 case List: list (); break;
820 case Edit: edit ((int)op0); break;
821 case Del: del ((int)op0); break;
822 case Get: return get ((int)op0);
823 case Length: return length ();
824 case Pop: return pop ();
825 case Push: return push (op0);
826 case Put: return put ((int)op0, op1);
827 case Set:
828 for (i = 0, nb =0; i < root->nbops; i++) {
829 if (root->ops[i]) {
830 nb++;
831 }
832 }
833 return set (nb, root->ops);
834 case Show: show (); break;
835 case Max:
836 if (root->ops[0]) {
837 return op0 > op1 ? op0 : op1;
838 }
839 return max ();
840 case Mean:
841 if (root->ops[0]) {
842 return (op0 + op1) / 2;
843 }
844 return mean ();
845 case Median: return median ();
846 case Min:
847 if (root->ops[0]) {
848 return op0 < op1 ? op0 : op1;
849 }
850 return min ();
851 case Order: order (); break;
852 case Prod: return prod ();
853 case Sum: return sum ();
854 case Variance:
855 if (root->ops[0]) {
856 double m = (op0 + op1) / 2;
857 op0 -= m;
858 op1 -= m;
859 return op0 * op0 + op1 * op1;
860 }
861 return variance ();
862 case Precision:
863 return format ((int)op0);
864 case Base:
865 base ((int)op0, (int)op1);
866 break;
867 case Deg:
868 anglefactor = 180 / M_PI;
869 break;
870 case Grad:
871 anglefactor = 200 / M_PI;
872 break;
873 case Rad:
874 anglefactor = 1;
875 break;
876 }
877
878 return 0;
879 }
880
881 /* vim: set ts=4 sw=4 et: */