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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
|
// crm_math_exec.c - CRM114 language math processing
// Copyright 2001-2009 William S. Yerazunis.
// This file is under GPLv3, as described in COPYING.
// include some standard files
#include "crm114_sysincludes.h"
// include any local crm114 configuration file
#include "crm114_config.h"
// include the crm114 data structures file
#include "crm114_structs.h"
// and include the routine declarations file
#include "crm114.h"
static int math_formatter ( double value, char *format, char *buf, long buflen);
//
// strmath - evaluate a string for the mathematical result,
// returning the length of the valid string.
//
long strmath (char *buf, long inlen, long maxlen, long *retstat)
{
long status;
long old_internal_trace;
old_internal_trace = internal_trace;
if (inlen < 0)
{
fatalerror5 ("Bug in caller to strmath() - it makes no sense to",
" have a negative length string! \n", CRM_ENGINE_HERE);
internal_trace = old_internal_trace;
return (0);
};
// Check for first-character control of Algebraic v. RPN
if (buf[0] == 'A')
{
// internal_trace = 1;
memmove (buf, &buf[1], inlen-1);
buf[inlen-1] = '\0';
status = stralmath (buf, inlen-1, maxlen, retstat);
internal_trace = old_internal_trace;
return (status);
}
if (buf[0] == 'R')
{
// Do we want to do selective tracing?
// internal_trace = 1;
memmove (buf, &buf[1], inlen-1);
buf[inlen-1] = '\0';
status = strpnmath (buf, inlen-1, maxlen, retstat);
internal_trace = old_internal_trace;
return (status);
}
// No first-character control, so use q_expansion_mode to control.
if (q_expansion_mode == 0 || q_expansion_mode == 2)
{
return (stralmath (buf, inlen, maxlen, retstat));
}
else
{
return (strpnmath (buf, inlen, maxlen, retstat));
}
}
// strpnmath - do a basic math evaluation of very simple expressions.
//
// This does math, in RPN, on a string, and returns a string value.
//
long strpnmath (char *buf, long inlen, long maxlen, long *retstat)
{
double stack [DEFAULT_MATHSTK_LIMIT]; // the evaluation stack
double sd; // how many 10^n's we've seen since a decimal
long od; // output decimal flag
long ip, op; // in string pointer, out string pointer
long sp; // stack pointer - points to next (vacant) space
long sinc; // stack incrment enable - do we start a new number
long errstat; // error status
char outformat[64]; // output format
long outstringlen;
// start off by initializing things
ip = 0; // in pointer is zero
op = 0; // output pointer is zero
sp = 0; // still at the top of the stack
od = 0; // no decimals seen yet, so no flag to output in decimal
sinc = 0; // no autopush.
outformat[0] = '\0';
// now our number-inputting hacks
stack[sp] = 0.0 ;
sd = 1.0;
// all initialized... let's begin.
if (internal_trace)
fprintf (stderr, "Math on '%s' len %ld retstat %lx \n",
buf, inlen, (long) retstat);
for (ip = 0; ip < inlen; ip++)
{
if (internal_trace)
fprintf (stderr, "ip = %ld, sp = %ld, stack[sp] = %f, ch='%c'\n",
ip, sp, stack[sp], buf[ip]);
if (sp < 0)
{
errstat = nonfatalerror5 ("Stack Underflow in math evaluation",
"", CRM_ENGINE_HERE);
return (0);
};
if (sp >= DEFAULT_MATHSTK_LIMIT)
{
errstat=nonfatalerror5 ("Stack Overflow in math evaluation.\n "
"CRM114 Barbie says 'This math is too hard'.",
buf, CRM_ENGINE_HERE);
return (0);
};
switch (buf[ip])
{
//
// a digit,or maybe a number - big change - we now use strtod
//
case '.':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '-':
case '+':
{
char *frejected;
// handle the case of a minus sign that isn't a unary -.
if (buf[ip] == '-' && !( isdigit (buf[ip+1])))
{
if (sp > 0)
{
sp--;
stack[sp] = stack[sp] - stack[sp+1];
sinc = 1;
}
break;
};
if (buf[ip] == '+' && !( isdigit (buf[ip+1])))
{
if (sp > 0)
{
sp--;
stack[sp] = stack[sp] + stack[sp+1];
sinc = 1;
}
break;
};
// Neither unary +/- so we use strtod to convert
// the string we're looking at to floating point.
sp++;
stack[sp] = strtod ( &buf[ip], &frejected);
if (user_trace)
fprintf (stderr, "got number: %e\n", stack[sp]);
//
// Now, move [ip] over to accomodate characters used.
// (the -1 is because there's an auto-increment in the big
// FOR-loop)
ip = ((unsigned long) frejected) - ((unsigned long) buf ) - 1;
}
break;
//
// and now the standard math operators (except for - and + above)
//
case '*':
{
if (sp > 0)
{
sp--;
stack[sp] = stack[sp] * stack[sp+1];
sinc = 1;
}
};
break;
case '/':
{
if (sp > 0)
{
sp--;
// don't worry about divide-by-zero, we get INF in IEEE.
stack[sp] = stack[sp] / stack[sp+1];
sinc = 1;
}
};
break;
case '%':
{
if (sp > 0)
{
sp--;
stack[sp] = ((long long) stack[sp]) % ((long long)stack[sp+1]);
sinc = 1;
}
};
break;
case '^': // exponentiation - for positive bases, neg base + int exp.
if (sp > 0)
{
sp--;
if (stack[sp] < 0.0
&& ((long long)(stack[sp+1]))/1 != stack[sp+1])
{ stack[sp] = stack[sp] / 0.0; }
else
stack[sp] = pow (stack[sp], stack[sp+1]);
if (internal_trace)
fprintf (stderr, "exp out: %lf\n", stack[sp]);
sinc = 1;
}
break;
case 'v': // logs as BASE v ARG; (NaN on BASE <= 0)
if (sp > 0)
{
sp--;
if (stack[sp] <= 0.0 )
{ stack[sp] = stack[sp] / 0.0 ; }
else
stack[sp] = log (stack[sp+1]) / log (stack[sp]);
sinc = 1;
}
break;
case '=':
{
if (sp > 0)
{
sp--;
if (stack[sp] == stack[sp+1])
{
if (retstat) *retstat = 0;
stack[sp] = 1;
}
else
{
if (retstat) *retstat = 1;
stack[sp] = 0;
};
sinc = 1;
}
};
break;
case '!':
{
if (sp > 0 && buf[ip+1] == '=')
{
ip++; // gobble up the equals sign
sp--;
if (stack[sp] != stack[sp+1])
{
if (retstat) *retstat = 0;
stack[sp] = 1;
}
else
{
if (retstat) *retstat = 1;
stack[sp] = 0;
};
sinc = 1;
}
};
break;
case '>':
{
if (buf[ip+1] == '=')
{
ip++; // gobble up the equals sign too...
if (sp > 0)
{
sp--;
if (stack[sp] >= stack[sp+1])
{
if (retstat) *retstat = 0;
stack[sp] = 1;
}
else
{
if (retstat) *retstat = 1;
stack[sp] = 0;
};
sinc = 1;
}
}
else
{
if (sp > 0)
{
sp--;
if (stack[sp] > stack[sp+1])
{
if (retstat) *retstat = 0;
stack[sp] = 1;
}
else
{
if (retstat) *retstat = 1;
stack[sp] = 0;
};
sinc = 1;
}
};
}
break;
case '<':
{
if (buf[ip+1] == '=')
{
ip++; // gobble up the equals sign
if (sp > 0)
{
sp--;
if (stack[sp] <= stack[sp+1])
{
if (retstat) *retstat = 0;
stack[sp] = 1;
}
else
{
if (retstat) *retstat = 1;
stack[sp] = 0;
};
sinc = 1;
}
}
else
{
if (sp > 0)
{
sp--;
if (stack[sp] < stack[sp+1])
{
if (retstat) *retstat = 0;
stack[sp] = 1;
}
else
{
if (retstat) *retstat = 1;
stack[sp] = 0;
};
sinc = 1;
}
};
};
break;
case 'e':
case 'E':
case 'f':
case 'F':
case 'g':
case 'G':
case 'x':
case 'X':
// User-specified formatting; use the user's
// top-of-stack value as a format.
//
{
if (sp > 0)
{
char tempstring [2048];
tempstring[0] = '\0';
sp--;
// Special case - if the format is an integer, add a ".0"
// to the format string so we get integer output.
if ( buf[ip] == 'x' || buf[ip] == 'X')
{
snprintf (outformat, 63, "%%%g.0ll%c",
stack[sp+1], (short) buf[ip] );
}
else
{
if ( ((long)stack[sp+1]) / 1 == stack[sp+1])
{
snprintf(outformat, 63, "%%%g.0%c", stack[sp+1], buf[ip]);
}
else
{
snprintf(outformat, 63, "%%%g%c", stack[sp+1], buf[ip]);
};
};
if (internal_trace)
fprintf (stderr, "Format string -->%s<-- \n", outformat);
stack[sp+1] = 0;
if (buf[ip] != 'x' && buf[ip] != 'X')
{
snprintf (tempstring, 2047, outformat, stack[sp]);
if (internal_trace)
fprintf (stderr,
"Intermediate result string -->%s<-- \n",
tempstring);
}
else
{
long long intpart ;
intpart = ((long long) stack[sp]) / 1;
snprintf (tempstring, 2047, outformat, intpart);
if (internal_trace)
fprintf (stderr,
"Intermediate hex result string -->%s<-- \n",
tempstring);
};
// And now do the back conversion of the result.
// Note that X formatting (hexadecimal) does NOT do the
// back conversion; the only effect is to store the
// format string for later.
if (buf[ip] != 'x' &&
buf[ip] != 'X')
stack[sp] = strtod (tempstring, NULL);
}
};
break;
case ' ':
case '\n':
case '\t':
//
// a space is just an end-of-number - push the number we're
// seeing.
{
sinc = 1;
};
break;
case '(':
case ')':
// why are you using parenthesis in RPN code??
{
nonfatalerror5 ("It's just silly to use parenthesis in RPN!",
" Perhaps you should check your setups?",
CRM_ENGINE_HERE);
sinc = 1;
};
break;
default:
{
char bogus[4];
bogus[0] = buf[ip];
bogus[1] = '\000';
nonfatalerror5 (" Sorry, but I can't do RPN math on this un-mathy "
"character: ", bogus, CRM_ENGINE_HERE);
sinc = 1;
};
break;
};
};
if (internal_trace)
{
fprintf (stderr,
"Final qexpand state: ip = %ld, sp = %ld, stack[sp] = %f, ch='%c'\n",
ip, sp, stack[sp], buf[ip]);
if (retstat)
fprintf (stderr, "retstat = %ld\n", *retstat);
};
// now the top of stack contains the result of the calculation.
// fprintf it into the output buffer, and we're done.
outstringlen = math_formatter ( stack[sp], outformat, buf, maxlen) ;
return (outstringlen);
}
/////////////////////////////////////////////////////////////////
//
// Here's where we format a floating point number so it's "purty".
// Note that if "format" is NULL, or a null string, we do smart
// formatting on the number itself.
//
//
int math_formatter ( double value, char *format, char *buf, long buflen)
{
long outlen;
// If the user supplied a format, use that.
//
if (format && format[0] != '\0')
{
//
// special case - if the user supplied an x or X-format, that's
// preconversion to integer; use that strlen() does not count
// the null termination.
if (format[strlen(format)-1] == 'x'
|| format[strlen(format)-1] == 'X')
{
long long equiv ;
if (internal_trace)
fprintf (stderr, "Final hex format: %s\n", format );
equiv = value * 1;
outlen = snprintf (buf, buflen, format, equiv);
return (outlen);
};
//
// Nothing so special; use the user format as it is.
if (internal_trace)
fprintf (stderr, "Final format: %s\n", format );
outlen = snprintf (buf, buflen, format, value);
return (outlen);
};
// Nope - we didn't get a preferred formatting, so here's the
// adaptive smart code.
//
// print out 0 as 0
//
if (value == 0.0 )
{
outlen = snprintf (buf, buflen, "0");
goto formatdone;
}
//
// use E notation if bigger than 1 trillion
//
if (value > 1000000000000.0 || value < -1000000000000.0 )
{
outlen = snprintf (buf, buflen, "%.5E", value);
goto formatdone;
}
//
// use E notation if smaller than .01
//
if ( value < 0.01 && value > -0.01 )
{
outlen = snprintf (buf, buflen, "%.5E", value);
goto formatdone;
}
//
// if an integer value, print with 0 precision
//
if (((long)(value*2.0) / 2) == value)
{
outlen = snprintf (buf, buflen, "%.0f", value);
goto formatdone;
}
//
// if none of the above, print with five digits precision
//
outlen = snprintf (buf, buflen, "%.5f", value);
//
//
// one way or another, once we're here, we've sprinted it.
formatdone:
if (internal_trace)
fprintf (stderr, "math_formatter outlen = %ld\n", outlen);
return (outlen);
}
////////////////////////////////////////////////////////////////////
//
// Alternative implementation of the uglyness that is string math.
//
// This version uses two stacks (left arg, op) and a single scalar
// rightarg. Partial computations are kept on the leftarg and op
// stack. The current stack status is held in validstack, and is
// the OR of LEFTVALID, OPVALID, and RIGHTVALID.
//
#define LEFTVALID 0x1
#define OPVALID 0x2
#define RIGHTVALID 0x4
long stralmath (char *buf, long inlen, long maxlen, long *retstat)
{
double leftarg [DEFAULT_MATHSTK_LIMIT] ; // left float arg
long opstack [DEFAULT_MATHSTK_LIMIT]; // operand
double rightarg; // right float arg
long validstack [DEFAULT_MATHSTK_LIMIT]; // validity markers
long sp; // stack pointer
long ip, op; // input and output pointer
long errstat; // error status
char *frejected; // done loc. for a strtod.
char outformat[256]; // how to format our result
long state; // Local copy of state, in case
// retstat is NULL (not used)
// Start off by initializing things
ip = 0;
op = 0;
sp = 0;
outformat[0] = '\0';
state = 0;
// Set up the stacks
//
leftarg [0] = 0.0;
rightarg = 0.0;
opstack [0] = '\0';
validstack [0] = 0;
// initialization done... begin the work.
if (internal_trace)
fprintf (stderr, "Starting Algebraic Math on '%s' (len %ld)\n",
buf, inlen);
for (ip = 0; ip < inlen; ip++)
{
// Debugging trace
if (internal_trace)
fprintf (stderr,
"ip = %ld, sp = %ld, L=%f, Op=%c, R=%f, V=%x next='%c'\n",
ip, sp,
leftarg[sp], (short) opstack[sp],
rightarg, (short) validstack[sp],
buf[ip]);
// Top of the loop- we're a state machine driven by the top of
// the stack's validity.
if (sp >= DEFAULT_MATHSTK_LIMIT)
{
errstat = nonfatalerror5 ("Stack Overflow in math evaluation. ",
"CRM114 Barbie says 'This math is too hard'.",
CRM_ENGINE_HERE);
if (retstat) *retstat = 0;
return (0);
};
switch (validstack[sp])
{
case (0):
// empty top of stack; can accept either number or monadic operator
if (internal_trace)
fprintf (stderr, "stacktop empty\n");
switch (buf[ip])
{
// Monadic operators and numbers
case '-':
case '+':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '.':
case ',': // for those locales that use , not . as decimal
{
if (internal_trace)
fprintf (stderr, "found left numeric\n");
leftarg[sp] = strtod (&buf[ip], &frejected);
if (user_trace)
fprintf (stderr, " Got left arg %e\n", leftarg[sp]);
ip = ((unsigned long) frejected) - ((unsigned long) buf) - 1;
validstack[sp] = LEFTVALID;
};
break;
case '(':
{
if (internal_trace)
fprintf (stderr,
"Open Paren - start new math stack level\n");
sp++;
leftarg[sp] = 0.0;
rightarg = 0.0;
opstack[sp] = 0;
validstack[sp] = 0;
}
break;
// deal with a possible rightarg strtod situation
case ' ':
break;
default:
errstat = nonfatalerror5 ("Math expression makes no sense",
" (need to have a number here).",
CRM_ENGINE_HERE);
if (retstat) *retstat = 0;
return (0);
break;
};
break;
// if left arg is valid; next thing must be an operator;
// however op then op is also valid and should form composite
// operators like '>=' and '!=' (see below).
case (LEFTVALID):
if (internal_trace)
fprintf (stderr, "leftvalid\n");
switch (buf[ip])
{
case '-':
case '+':
case '*':
case '/':
case '%':
case '>':
case '<':
case '=':
case '!':
case '^':
case 'v':
case 'e':
case 'E':
case 'f':
case 'F':
case 'g':
case 'G':
case 'x':
case 'X':
{
if (internal_trace)
fprintf (stderr, "found op\n");
opstack[sp] = ( buf[ip] & 0xFF );
validstack[sp] = LEFTVALID | OPVALID;
// is the next char also an op? If so, gobble it up?
switch (buf[ip+1])
{
case '=':
if (internal_trace)
fprintf (stderr, "two-char operator\n");
opstack[sp] = ((opstack[sp] << 8) | buf[ip+1]);
ip++;
};
};
break;
case ')':
// close paren pops the stack, and returns the left arg
// to "whereever", which might be leftarg stack, or rightarg
if (internal_trace)
fprintf (stderr, "close parenthesis, pop stack down\n");
sp--;
if (validstack[sp] == (LEFTVALID | OPVALID))
{
rightarg = leftarg [sp+1];
validstack[sp] = LEFTVALID | OPVALID | RIGHTVALID;
}
else
{
leftarg[sp] = leftarg [sp+1];
validstack[sp] = LEFTVALID;
};
break;
case ' ':
break;
default:
errstat = nonfatalerror5 ("Math needs an operator in: ",
buf, CRM_ENGINE_HERE);
if (retstat) *retstat = 0;
return (0);
break;
}
break;
case (LEFTVALID | OPVALID):
// left arg and op are both valid; right now we can have
// an enhanced operator (next char is also an op)
if (internal_trace)
fprintf (stderr, "left + opvalid \n");
switch (buf[ip])
{
case '(':
{
if (internal_trace)
fprintf (stderr,
"Open Paren - start new math stack level\n");
sp++;
leftarg[sp] = 0.0;
rightarg = 0.0;
opstack[sp] = 0;
validstack[sp] = 0;
}
break;
// deal with a possible rightarg strtod situation
case '-':
case '+':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '.':
case ',':
{
rightarg = strtod (&buf[ip], &frejected);
if (internal_trace)
fprintf (stderr, " Got right arg %e\n", rightarg);
ip = ((unsigned long) frejected) - ((unsigned long) buf) - 1;
validstack[sp] = validstack[sp] | RIGHTVALID;
};
case ' ':
break;
default:
errstat = nonfatalerror5 ("Math is missing a number in: ",
buf, CRM_ENGINE_HERE);
if (retstat) *retstat = 0;
return (0);
break;
};
};
//////////////////////////////////////////////////
//
// Now if we have a left-op-right situation, and can
// execute the operator right here and now.
//
while (validstack[sp] == (LEFTVALID | OPVALID | RIGHTVALID) )
{
if (internal_trace)
fprintf (stderr, "Executing %c operator\n", (short)opstack[sp]);
switch (opstack[sp])
{
// Math operators
case '+':
leftarg[sp] = leftarg[sp] + rightarg;
break;
case '-':
leftarg[sp] = leftarg[sp] - rightarg;
break;
case '*':
leftarg[sp] = leftarg[sp] * rightarg;
break;
case '/':
leftarg[sp] = leftarg[sp] / rightarg;
break;
case '%':
leftarg[sp] = (long long) leftarg[sp] % (long long) rightarg;
break;
case '^':
// since we don't do complex numbers (yet) handle as NaN
if (leftarg[sp] < 0.0 \
&& ((long long) (rightarg))/1 != rightarg)
{ leftarg[sp] = leftarg[sp] / 0.0;}
else
leftarg[sp] = pow (leftarg[sp], rightarg);
if (internal_trace)
fprintf (stderr, "exp out: %lf\n", leftarg[sp]);
break;
case 'v': // Logarithm BASE v ARG
// Negative bases on logarithms? Not for us! force NaN
if (leftarg[sp] <= 0)
{ leftarg[sp] = leftarg[sp] / 0.0;}
else
leftarg[sp] = log (rightarg) / log (leftarg[sp]);
break;
// Relational operators
case '<':
if (leftarg[sp] < rightarg)
{ leftarg[sp] = 1;
state = 0;}
else
{ leftarg[sp] = 0;
state = 1;};
break;
case '>':
if (leftarg[sp] > rightarg)
{ leftarg[sp] = 1;
state = 0;}
else
{ leftarg[sp] = 0;
state = 1;};
break;
case '=':
if (leftarg[sp] == rightarg)
{ leftarg[sp] = 1;
state = 0; }
else
{ leftarg[sp] = 0;
state = 1;};
break;
case (('<' << 8) + '='):
if (leftarg[sp] <= rightarg)
{ leftarg[sp] = 1;
state = 0;}
else
{ leftarg[sp] = 0;
state = 1;};
break;
case (('>' << 8) + '='):
if (leftarg[sp] >= rightarg)
{ leftarg[sp] = 1;
state = 0;}
else
{ leftarg[sp] = 0;
state = 1;};
break;
case ( ('!' << 8) + '='):
if (leftarg[sp] != rightarg)
{ leftarg[sp] = 1;
state = 0;}
else
{ leftarg[sp] = 0;
state = 1;};
break;
// Formatting operators
case 'e':
case 'E':
case 'f':
case 'F':
case 'g':
case 'G':
case 'x':
case 'X':
{
char tempstring [2048];
if (internal_trace)
fprintf (stderr, "Formatting operator %c \n",
(short)opstack[sp]);
// Do we have a float or an int format?
if (opstack[sp] == 'x' || opstack[sp] == 'X')
{
snprintf (outformat, 255, "%%%g.0ll%c",
rightarg, (short) opstack[sp]);
}
else
{
if (((long) rightarg) / 1 == rightarg)
{
snprintf (outformat, 255, "%%%g.0%c",
rightarg, (short) opstack[sp]);
}
else
{
snprintf (outformat, 255, "%%%g%c",
rightarg, (short)opstack[sp]);
};
};
if (internal_trace)
fprintf (stderr, "Format string -->%s<-- \n", outformat);
// A little more funny business needed for
// hexadecimal print out, because X format
// can't take IEEE floating point as inputs.
if (opstack[sp] != 'x' &&
opstack[sp] != 'X')
{
if (internal_trace)
fprintf (stderr, "Normal convert ");
snprintf (tempstring, 2047, outformat, leftarg[sp] );
leftarg[sp] = strtod (tempstring, NULL);
validstack[sp] = LEFTVALID;
}
else
{
// Note that we actually don't use the
// results of octal conversion; the only
// effect is to set the final format
// string.
long long equiv;
if (internal_trace)
fprintf (stderr, "Oct/Hex Convert ");
equiv = leftarg[sp] + 0.0;
if (internal_trace)
fprintf (stderr, "equiv -->%10lld<-- \n", equiv);
snprintf (tempstring, 2047, outformat, equiv);
};
};
break;
default:
errstat = nonfatalerror5 ("Math operator makes no sense in: ",
buf, CRM_ENGINE_HERE);
if (retstat) *retstat = 0;
return (0);
break;
};
validstack[sp] = LEFTVALID;
};
// Check to see that the stack is still valid.
if (sp < 0)
{
errstat = nonfatalerror5
( "Too many close parenthesis in this math: ",
buf, CRM_ENGINE_HERE);
if (retstat) *retstat = 0;
return (0);
};
};
// We made it all the way through. Now return the math formatter result
if (internal_trace)
fprintf (stderr, "Returning at sp= %ld and value %lf\n", sp, leftarg[sp]);
if (retstat) *retstat = state;
// Check that we made it all the way down the stack
if (sp != 0)
{
errstat = nonfatalerror5
("Not enough close parenthesis in this math: ",
buf, CRM_ENGINE_HERE);
if (retstat) *retstat = 0;
return (0);
}
// All's good, return with a value.
{
long return_length;
return_length = (math_formatter (leftarg[sp], outformat, buf, maxlen ));
outformat [return_length] = '\000';
return (return_length);
};
}
|