home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 334_02 / interna2.c < prev    next >
Text File  |  1991-02-05  |  13KB  |  684 lines

  1. /*
  2.  *
  3.  *    G N U P L O T  --  internal.c
  4.  *
  5.  *  Copyright (C) 1986 Colin Kelley, Thomas Williams
  6.  *
  7.  *  You may use this code as you wish if credit is given and this message
  8.  *  is retained.
  9.  *
  10.  *  Please e-mail any useful additions to vu-vlsi!plot so they may be
  11.  *  included in later releases.
  12.  *
  13.  *  This file should be edited with 4-column tabs!  (:set ts=4 sw=4 in vi)
  14.  */
  15.  
  16. #include <math.h>
  17. #include <stdio.h>
  18. #include "plot.h"
  19.  
  20. extern BOOLEAN undefined;
  21. extern struct vt_entry vt[MAX_VALUES];
  22. extern struct udft_entry udft[MAX_UDFS];
  23.  
  24. char *strcpy();
  25.  
  26. struct value *pop(), *complex(), *integer();
  27. double magnitude(), angle(), real();
  28.  
  29. struct value stack[STACK_DEPTH];
  30.  
  31. int s_p = -1;   /* stack pointer */
  32.  
  33. #ifndef MSDOS /* suggested by "J.D. McDonald " <mcdonald@uxe.cso.uiuc.edu> */
  34. /*
  35.  * System V and MSC 4.0 call this when they wants to print an error message.
  36.  * Don't!
  37.  */
  38. matherr()
  39. {
  40.     return (undefined = TRUE);        /* don't print error message */
  41. }
  42. #endif MSDOS
  43.  
  44. reset_stack()
  45. {
  46.     s_p = -1;
  47. }
  48.  
  49.  
  50. check_stack()    /* make sure stack's empty */
  51. {
  52.     if (s_p != -1)
  53.         fprintf(stderr,"\nwarning:  internal error--stack not empty!\n");
  54. }
  55.  
  56.  
  57. struct value *pop(x)
  58. struct value *x;
  59. {
  60.     if (s_p  < 0 )
  61.         int_error("stack underflow",NO_CARET);
  62.     *x = stack[s_p--];
  63.     return(x);
  64. }
  65.  
  66. #define ERR_VAR "undefined variable: "
  67.  
  68. f_push(x)
  69. struct value *x;        /* contains index of value to push; must be integer! */
  70. {
  71. static char err_str[sizeof(ERR_VAR) + MAX_ID_LEN] = ERR_VAR;
  72. register int index;
  73.  
  74.     if (x->type != INT)
  75.         int_error("internal error--non-int passed to f_push!",NO_CARET);
  76.     index = x->v.int_val;
  77.  
  78.     if (vt[index].vt_undef) {     /* undefined */
  79.         (void) strcpy(&err_str[sizeof(ERR_VAR) - 1], vt[index].vt_name);
  80.         int_error(err_str,NO_CARET);
  81.     }
  82.     push(&vt[index].vt_value);
  83. }
  84.  
  85.  
  86. f_pushc(x)
  87. struct value *x;
  88. {
  89.     if (s_p == STACK_DEPTH - 1)
  90.         int_error("stack overflow",NO_CARET);
  91.     stack[++s_p] = *x;
  92. }
  93.  
  94.  
  95. f_pushd(x)
  96. struct value *x;
  97. {
  98.     f_pushc(&udft[x->v.int_val].dummy_value);
  99. }
  100.  
  101.  
  102. #define ERR_FUN "undefined function: "
  103.  
  104. f_call(f_index)  /* execute a udf */
  105. struct value *f_index;
  106. {
  107. static char err_str[sizeof(ERR_FUN) + MAX_ID_LEN] = ERR_FUN;
  108.  
  109.     if (udft[f_index->v.int_val].at.count == 0) { /* undefined */
  110.         (void) strcpy(&err_str[sizeof(ERR_FUN) - 1],
  111.                 udft[f_index->v.int_val].udft_name);
  112.         int_error(err_str,NO_CARET);
  113.     }
  114.     (void) pop(&udft[f_index->v.int_val].dummy_value);
  115.  
  116.     execute_at(&udft[f_index->v.int_val].at);
  117. }
  118.  
  119.  
  120. static int_check(v)
  121. struct value *v;
  122. {
  123.     if (v->type != INT)
  124.         int_error("non-integer passed to boolean operator",NO_CARET);
  125. }
  126.  
  127.  
  128. f_terniary()        /* code for (a) ? b : c */
  129. {
  130. struct value a, b, c;
  131.     (void) pop(&c);    (void) pop(&b);    int_check(pop(&a));
  132.     push((a.v.int_val) ? &b : &c);
  133.             /* I just had to use ? : here! */
  134. }
  135.  
  136.  
  137. f_lnot()
  138. {
  139. struct value a;
  140.     int_check(pop(&a));
  141.     push(integer(&a,!a.v.int_val) );
  142. }
  143.  
  144.  
  145. f_bnot()
  146. {
  147. struct value a;
  148.     int_check(pop(&a));
  149.     push( integer(&a,~a.v.int_val) );
  150. }
  151.  
  152.  
  153. f_lor()
  154. {
  155. struct value a,b;
  156.     int_check(pop(&b));
  157.     int_check(pop(&a));
  158.     push( integer(&a,a.v.int_val || b.v.int_val) );
  159. }
  160.  
  161.  
  162. f_land()
  163. {
  164. struct value a,b;
  165.     int_check(pop(&b));
  166.     int_check(pop(&a));
  167.     push( integer(&a,a.v.int_val && b.v.int_val) );
  168. }
  169.  
  170.  
  171. f_bor()
  172. {
  173. struct value a,b;
  174.     int_check(pop(&b));
  175.     int_check(pop(&a));
  176.     push( integer(&a,a.v.int_val | b.v.int_val) );
  177. }
  178.  
  179.  
  180. f_xor()
  181. {
  182. struct value a,b;
  183.     int_check(pop(&b));
  184.     int_check(pop(&a));
  185.     push( integer(&a,a.v.int_val ^ b.v.int_val) );
  186. }
  187.  
  188.  
  189. f_band()
  190. {
  191. struct value a,b;
  192.     int_check(pop(&b));
  193.     int_check(pop(&a));
  194.     push( integer(&a,a.v.int_val & b.v.int_val) );
  195. }
  196.  
  197.  
  198. f_uminus()
  199. {
  200. struct value a;
  201.     (void) pop(&a);
  202.     switch(a.type) {
  203.         case INT:
  204.             a.v.int_val = -a.v.int_val;
  205.             break;
  206.         case CMPLX:
  207.             a.v.cmplx_val.real =
  208.                 -a.v.cmplx_val.real;
  209.             a.v.cmplx_val.imag =
  210.                 -a.v.cmplx_val.imag;
  211.     }
  212.     push(&a);
  213. }
  214.  
  215.  
  216. f_eq() /* note: floating point equality is rare because of roundoff error! */
  217. {
  218. struct value a, b;
  219.     register int result;
  220.     (void) pop(&b);
  221.     (void) pop(&a);
  222.     switch(a.type) {
  223.         case INT:
  224.             switch (b.type) {
  225.                 case INT:
  226.                     result = (a.v.int_val ==
  227.                         b.v.int_val);
  228.                     break;
  229.                 case CMPLX:
  230.                     result = (a.v.int_val ==
  231.                         b.v.cmplx_val.real &&
  232.                        b.v.cmplx_val.imag == 0.0);
  233.             }
  234.             break;
  235.         case CMPLX:
  236.             switch (b.type) {
  237.                 case INT:
  238.                     result = (b.v.int_val == a.v.cmplx_val.real &&
  239.                        a.v.cmplx_val.imag == 0.0);
  240.                     break;
  241.                 case CMPLX:
  242.                     result = (a.v.cmplx_val.real==
  243.                         b.v.cmplx_val.real &&
  244.                         a.v.cmplx_val.imag==
  245.                         b.v.cmplx_val.imag);
  246.             }
  247.     }
  248.     push(integer(&a,result));
  249. }
  250.  
  251.  
  252. f_ne()
  253. {
  254. struct value a, b;
  255.     register int result;
  256.     (void) pop(&b);
  257.     (void) pop(&a);
  258.     switch(a.type) {
  259.         case INT:
  260.             switch (b.type) {
  261.                 case INT:
  262.                     result = (a.v.int_val !=
  263.                         b.v.int_val);
  264.                     break;
  265.                 case CMPLX:
  266.                     result = (a.v.int_val !=
  267.                         b.v.cmplx_val.real ||
  268.                        b.v.cmplx_val.imag != 0.0);
  269.             }
  270.             break;
  271.         case CMPLX:
  272.             switch (b.type) {
  273.                 case INT:
  274.                     result = (b.v.int_val !=
  275.                         a.v.cmplx_val.real ||
  276.                        a.v.cmplx_val.imag != 0.0);
  277.                     break;
  278.                 case CMPLX:
  279.                     result = (a.v.cmplx_val.real !=
  280.                         b.v.cmplx_val.real ||
  281.                         a.v.cmplx_val.imag !=
  282.                         b.v.cmplx_val.imag);
  283.             }
  284.     }
  285.     push(integer(&a,result));
  286. }
  287.  
  288.  
  289. f_gt()
  290. {
  291. struct value a, b;
  292.     register int result;
  293.     (void) pop(&b);
  294.     (void) pop(&a);
  295.     switch(a.type) {
  296.         case INT:
  297.             switch (b.type) {
  298.                 case INT:
  299.                     result = (a.v.int_val >
  300.                         b.v.int_val);
  301.                     break;
  302.                 case CMPLX:
  303.                     result = (a.v.int_val >
  304.                         b.v.cmplx_val.real);
  305.             }
  306.             break;
  307.         case CMPLX:
  308.             switch (b.type) {
  309.                 case INT:
  310.                     result = (a.v.cmplx_val.real >
  311.                         b.v.int_val);
  312.                     break;
  313.                 case CMPLX:
  314.                     result = (a.v.cmplx_val.real >
  315.                         b.v.cmplx_val.real);
  316.             }
  317.     }
  318.     push(integer(&a,result));
  319. }
  320.  
  321.  
  322. f_lt()
  323. {
  324. struct value a, b;
  325.     register int result;
  326.     (void) pop(&b);
  327.     (void) pop(&a);
  328.     switch(a.type) {
  329.         case INT:
  330.             switch (b.type) {
  331.                 case INT:
  332.                     result = (a.v.int_val <
  333.                         b.v.int_val);
  334.                     break;
  335.                 case CMPLX:
  336.                     result = (a.v.int_val <
  337.                         b.v.cmplx_val.real);
  338.             }
  339.             break;
  340.         case CMPLX:
  341.             switch (b.type) {
  342.                 case INT:
  343.                     result = (a.v.cmplx_val.real <
  344.                         b.v.int_val);
  345.                     break;
  346.                 case CMPLX:
  347.                     result = (a.v.cmplx_val.real <
  348.                         b.v.cmplx_val.real);
  349.             }
  350.     }
  351.     push(integer(&a,result));
  352. }
  353.  
  354.  
  355. f_ge()
  356. {
  357. struct value a, b;
  358.     register int result;
  359.     (void) pop(&b);
  360.     (void) pop(&a);
  361.     switch(a.type) {
  362.         case INT:
  363.             switch (b.type) {
  364.                 case INT:
  365.                     result = (a.v.int_val >=
  366.                         b.v.int_val);
  367.                     break;
  368.                 case CMPLX:
  369.                     result = (a.v.int_val >=
  370.                         b.v.cmplx_val.real);
  371.             }
  372.             break;
  373.         case CMPLX:
  374.             switch (b.type) {
  375.                 case INT:
  376.                     result = (a.v.cmplx_val.real >=
  377.                         b.v.int_val);
  378.                     break;
  379.                 case CMPLX:
  380.                     result = (a.v.cmplx_val.real >=
  381.                         b.v.cmplx_val.real);
  382.             }
  383.     }
  384.     push(integer(&a,result));
  385. }
  386.  
  387.  
  388. f_le()
  389. {
  390. struct value a, b;
  391.     register int result;
  392.     (void) pop(&b);
  393.     (void) pop(&a);
  394.     switch(a.type) {
  395.         case INT:
  396.             switch (b.type) {
  397.                 case INT:
  398.                     result = (a.v.int_val <=
  399.                         b.v.int_val);
  400.                     break;
  401.                 case CMPLX:
  402.                     result = (a.v.int_val <=
  403.                         b.v.cmplx_val.real);
  404.             }
  405.             break;
  406.         case CMPLX:
  407.             switch (b.type) {
  408.                 case INT:
  409.                     result = (a.v.cmplx_val.real <=
  410.                         b.v.int_val);
  411.                     break;
  412.                 case CMPLX:
  413.                     result = (a.v.cmplx_val.real <=
  414.                         b.v.cmplx_val.real);
  415.             }
  416.     }
  417.     push(integer(&a,result));
  418. }
  419.  
  420.  
  421. f_plus()
  422. {
  423. struct value a, b, result;
  424.     (void) pop(&b);
  425.     (void) pop(&a);
  426.     switch(a.type) {
  427.         case INT:
  428.             switch (b.type) {
  429.                 case INT:
  430.                     (void) integer(&result,a.v.int_val +
  431.                         b.v.int_val);
  432.                     break;
  433.                 case CMPLX:
  434.                     (void) complex(&result,a.v.int_val +
  435.                         b.v.cmplx_val.real,
  436.                        b.v.cmplx_val.imag);
  437.             }
  438.             break;
  439.         case CMPLX:
  440.             switch (b.type) {
  441.                 case INT:
  442.                     (void) complex(&result,b.v.int_val +
  443.                         a.v.cmplx_val.real,
  444.                        a.v.cmplx_val.imag);
  445.                     break;
  446.                 case CMPLX:
  447.                     (void) complex(&result,a.v.cmplx_val.real+
  448.                         b.v.cmplx_val.real,
  449.                         a.v.cmplx_val.imag+
  450.                         b.v.cmplx_val.imag);
  451.             }
  452.     }
  453.     push(&result);
  454. }
  455.  
  456.  
  457. f_minus()
  458. {
  459. struct value a, b, result;
  460.     (void) pop(&b);
  461.     (void) pop(&a);        /* now do a - b */
  462.     switch(a.type) {
  463.         case INT:
  464.             switch (b.type) {
  465.                 case INT:
  466.                     (void) integer(&result,a.v.int_val -
  467.                         b.v.int_val);
  468.                     break;
  469.                 case CMPLX:
  470.                     (void) complex(&result,a.v.int_val -
  471.                         b.v.cmplx_val.real,
  472.                        -b.v.cmplx_val.imag);
  473.             }
  474.             break;
  475.         case CMPLX:
  476.             switch (b.type) {
  477.                 case INT:
  478.                     (void) complex(&result,a.v.cmplx_val.real -
  479.                         b.v.int_val,
  480.                         a.v.cmplx_val.imag);
  481.                     break;
  482.                 case CMPLX:
  483.                     (void) complex(&result,a.v.cmplx_val.real-
  484.                         b.v.cmplx_val.real,
  485.                         a.v.cmplx_val.imag-
  486.                         b.v.cmplx_val.imag);
  487.             }
  488.     }
  489.     push(&result);
  490. }
  491.  
  492.  
  493. f_mult()
  494. {
  495. struct value a, b, result;
  496.     (void) pop(&b);
  497.     (void) pop(&a);    /* now do a*b */
  498.  
  499.     switch(a.type) {
  500.         case INT:
  501.             switch (b.type) {
  502.                 case INT:
  503.                     (void) integer(&result,a.v.int_val *
  504.                         b.v.int_val);
  505.                     break;
  506.                 case CMPLX:
  507.                     (void) complex(&result,a.v.int_val *
  508.                         b.v.cmplx_val.real,
  509.                         a.v.int_val *
  510.                         b.v.cmplx_val.imag);
  511.             }
  512.             break;
  513.         case CMPLX:
  514.             switch (b.type) {
  515.                 case INT:
  516.                     (void) complex(&result,b.v.int_val *
  517.                         a.v.cmplx_val.real,
  518.                         b.v.int_val *
  519.                         a.v.cmplx_val.imag);
  520.                     break;
  521.                 case CMPLX:
  522.                     (void) complex(&result,a.v.cmplx_val.real*
  523.                         b.v.cmplx_val.real-
  524.                         a.v.cmplx_val.imag*
  525.                         b.v.cmplx_val.imag,
  526.                         a.v.cmplx_val.real*
  527.                         b.v.cmplx_val.imag+
  528.                         a.v.cmplx_val.imag*
  529.                         b.v.cmplx_val.real);
  530.             }
  531.     }
  532.     push(&result);
  533. }
  534.  
  535.  
  536. f_div()
  537. {
  538. struct value a, b, result;
  539. register double square;
  540.     (void) pop(&b);
  541.     (void) pop(&a);    /* now do a/b */
  542.  
  543.     switch(a.type) {
  544.         case INT:
  545.             switch (b.type) {
  546.                 case INT:
  547.                     if (b.v.int_val)
  548.                       (void) integer(&result,a.v.int_val /
  549.                         b.v.int_val);
  550.                     else {
  551.                       (void) integer(&result,0);
  552.                       undefined = TRUE;
  553.                     }
  554.                     break;
  555.                 case CMPLX:
  556.                     square = b.v.cmplx_val.real*
  557.                         b.v.cmplx_val.real +
  558.                         b.v.cmplx_val.imag*
  559.                         b.v.cmplx_val.imag;
  560.                     if (square)
  561.                         (void) complex(&result,a.v.int_val*
  562.                         b.v.cmplx_val.real/square,
  563.                         -a.v.int_val*
  564.                         b.v.cmplx_val.imag/square);
  565.                     else {
  566.                         (void) complex(&result,0.0,0.0);
  567.                         undefined = TRUE;
  568.                     }
  569.             }
  570.             break;
  571.         case CMPLX:
  572.             switch (b.type) {
  573.                 case INT:
  574.                     if (b.v.int_val)
  575.                       
  576.                       (void) complex(&result,a.v.cmplx_val.real/
  577.                         b.v.int_val,
  578.                         a.v.cmplx_val.imag/
  579.                         b.v.int_val);
  580.                     else {
  581.                         (void) complex(&result,0.0,0.0);
  582.                         undefined = TRUE;
  583.                     }
  584.                     break;
  585.                 case CMPLX:
  586.                     square = b.v.cmplx_val.real*
  587.                         b.v.cmplx_val.real +
  588.                         b.v.cmplx_val.imag*
  589.                         b.v.cmplx_val.imag;
  590.                     if (square)
  591.                     (void) complex(&result,(a.v.cmplx_val.real*
  592.                         b.v.cmplx_val.real+
  593.                         a.v.cmplx_val.imag*
  594.                         b.v.cmplx_val.imag)/square,
  595.                         (a.v.cmplx_val.imag*
  596.                         b.v.cmplx_val.real-
  597.                         a.v.cmplx_val.real*
  598.                         b.v.cmplx_val.imag)/
  599.                             square);
  600.                     else {
  601.                         (void) complex(&result,0.0,0.0);
  602.                         undefined = TRUE;
  603.                     }
  604.             }
  605.     }
  606.     push(&result);
  607. }
  608.  
  609.  
  610. f_mod()
  611. {
  612. struct value a, b;
  613.     (void) pop(&b);
  614.     (void) pop(&a);    /* now do a%b */
  615.  
  616.     if (a.type != INT || b.type != INT)
  617.         int_error("can only mod ints",NO_CARET);
  618.     if (b.v.int_val)
  619.         push(integer(&a,a.v.int_val % b.v.int_val));
  620.     else {
  621.         push(integer(&a,0));
  622.         undefined = TRUE;
  623.     }
  624. }
  625.  
  626.  
  627. f_power()
  628. {
  629. struct value a, b, result;
  630. register int i, t, count;
  631. register double mag, ang;
  632.     (void) pop(&b);
  633.     (void) pop(&a);    /* now find a**b */
  634.  
  635.     switch(a.type) {
  636.         case INT:
  637.             switch (b.type) {
  638.                 case INT:
  639.                     count = abs(b.v.int_val);
  640.                     t = 1;
  641.                     for(i=0; i < count; i++)
  642.                         t *= a.v.int_val;
  643.                     if (b.v.int_val >= 0)
  644.                         (void) integer(&result,t);
  645.                     else
  646.                         (void) complex(&result,1.0/t,0.0);
  647.                     break;
  648.                 case CMPLX:
  649.                     mag =
  650.                       pow(magnitude(&a),fabs(b.v.cmplx_val.real));
  651.                     if (b.v.cmplx_val.real < 0.0)
  652.                         mag = 1.0/mag;
  653.                     ang = angle(&a)*b.v.cmplx_val.real+
  654.                       b.v.cmplx_val.imag;
  655.                     (void) complex(&result,mag*cos(ang),
  656.                         mag*sin(ang));
  657.             }
  658.             break;
  659.         case CMPLX:
  660.             switch (b.type) {
  661.                 case INT:
  662.                     /* not so good, but...! */
  663.                     mag =
  664.                       pow(magnitude(&a),(double)abs(b.v.int_val));
  665.                     if (b.v.int_val < 0)
  666.                         mag = 1.0/mag;
  667.                     ang = angle(&a)*b.v.int_val;
  668.                     (void) complex(&result,mag*cos(ang),
  669.                         mag*sin(ang));
  670.                     break;
  671.                 case CMPLX:
  672.                     mag =
  673.                       pow(magnitude(&a),fabs(b.v.cmplx_val.real));
  674.                     if (b.v.cmplx_val.real < 0.0)
  675.                       mag = 1.0/mag;
  676.                     ang = angle(&a)*b.v.cmplx_val.real+
  677.                       b.v.cmplx_val.imag;
  678.                     (void) complex(&result,mag*cos(ang),
  679.                         mag*sin(ang));
  680.             }
  681.     }
  682.     push(&result);
  683. }
  684.