home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / cpm / cpm68k / sdb.lbr / INT.CQ / INT.C
Text File  |  1986-10-31  |  6KB  |  261 lines

  1. /* SDB - boolean expression evaluator */ /* -*-c,save-*- */
  2.  
  3. #include "sdbio.h"
  4.  
  5. /* #define DEBUG    1    /* debug hackery */
  6. #ifdef DEBUG
  7. #define LOCAL
  8. #else
  9. #define LOCAL static
  10. #endif
  11.  
  12.  
  13. LOCAL struct operand *stack[STACKMAX],**sptr;
  14. LOCAL union codecell *cptr;
  15.  
  16. /* db_interpret - interpret a boolean expression */
  17. int db_interpret(slptr)
  18.   register struct sel *slptr;
  19. {
  20.     register struct operand *result;
  21.     register int r;
  22.     register union codecell *tcptr;
  23.  
  24.     /* check for empty where clause */
  25.     if ((cptr = slptr->sl_where) == NULL)
  26.         return (TRUE);
  27.  
  28.     /* setup stack */
  29.     sptr = stack;
  30.  
  31.     /* execute the code */
  32.     do {
  33.     tcptr = cptr;
  34.     cptr++;
  35.     } while ((*(*tcptr).c_operator)());
  36.  
  37.     /* get the result from the top of stack */
  38.     result = *--sptr;
  39.     r = result->o_value.ov_boolean;
  40.     if (result->o_type == TEMP)
  41.         free(result);
  42.  
  43.     /* make sure the stack is empty */
  44.     while (sptr != stack) {
  45.         if ((*sptr)->o_type == TEMP)
  46.             free(*sptr);
  47.         sptr -= 1;
  48.     }
  49.  
  50.     /* return result */
  51.     return (r);
  52. }
  53.  
  54. int db_xstop()
  55. {
  56. #ifdef DEBUG
  57.     printf("*** xstop()\n");
  58. #endif
  59.     return (FALSE);
  60. }
  61.  
  62. int db_xpush()
  63. {
  64. #ifdef DEBUG
  65.     printf("*** xpush(): sptr = %08X, cptr = %08X\n", sptr, cptr);
  66. #endif
  67.     *sptr++ = (*cptr++).c_operand;
  68.     return(TRUE);
  69. }
  70.  
  71. int db_xand()
  72. {
  73.     return (boolean('&'));
  74. }
  75.  
  76. int db_xor()
  77. {
  78.     return (boolean('|'));
  79. }
  80.  
  81. LOCAL int boolean(opr)
  82. {
  83.     register struct operand *lval,*rval,*result;
  84.     register int lv,rv,r;
  85.  
  86.     rval = *--sptr; lval = *--sptr;
  87.     lv = lval->o_value.ov_boolean;
  88.     rv = rval->o_value.ov_boolean;
  89. #ifdef DEBUG
  90.     printf("*** boolean(%c): lval = %08X, rval = %08X, lv = %d, rv = %d\n",
  91.        opr, lval, rval, lv, rv);
  92. #endif
  93.  
  94.     if ((result = malloc(sizeof(struct operand))) == NULL)
  95.         return (db_ferror(INSMEM));
  96.     result->o_type = TEMP;
  97.     switch (opr) {
  98.     case '&':   r = (lv && rv);
  99.                 break;
  100.     case '|':   r = (lv || rv);
  101.                 break;
  102.     }
  103.     result->o_value.ov_boolean = r;
  104.     *sptr++ = result;
  105.     if (lval->o_type == TEMP)
  106.         free(lval);
  107.     if (rval->o_type == TEMP)
  108.         free(rval);
  109.     return (TRUE);
  110. }
  111.  
  112. int db_xnot()
  113. {
  114.     struct operand *val,*result;
  115.  
  116.     val = *--sptr;
  117. #ifdef DEBUG
  118.     printf("*** db_xnot(): vval = %08X\n", val);
  119. #endif
  120.     if ((result = malloc(sizeof(struct operand))) == NULL)
  121.         return (db_ferror(INSMEM));
  122.     result->o_type = TEMP;
  123.     result->o_value.ov_boolean = !val->o_value.ov_boolean;
  124.     *sptr++ = result;
  125.     if (val->o_type == TEMP)
  126.         free(val);
  127.     return (TRUE);
  128. }
  129.  
  130. int db_xlss()
  131. {
  132.     return (compare(LSS));
  133. }
  134.  
  135. int db_xleq()
  136. {
  137.     return (compare(LEQ));
  138. }
  139.  
  140. int db_xeql()
  141. {
  142.     return (compare(EQL));
  143. }
  144.  
  145. int db_xgeq()
  146. {
  147.     return (compare(GEQ));
  148. }
  149.  
  150. int db_xgtr()
  151. {
  152.     return (compare(GTR));
  153. }
  154.  
  155. int db_xneq()
  156. {
  157.     return (compare(NEQ));
  158. }
  159.  
  160. LOCAL int compare(cmp)
  161. {
  162.     struct operand *lval,*rval,*result;
  163.     int i;
  164.  
  165.     rval = *--sptr; lval = *--sptr;
  166. #ifdef DEBUG
  167.     printf("*** compare(%d): lval = %08X, rval = %08X\n",
  168.        cmp, lval, rval);
  169.     printf("***            : lval->o_value.ov_char.ovc_type = %d\n",
  170.        lval->o_value.ov_char.ovc_type);
  171. #endif
  172.     if ((result = malloc(sizeof(struct operand))) == NULL)
  173.         return (db_ferror(INSMEM));
  174.     result->o_type = TEMP;
  175.  
  176.     if (lval->o_value.ov_char.ovc_type == TCHAR)
  177.         i = comp(lval,rval);
  178.     else
  179.         i = ncomp(lval,rval);
  180.  
  181.     switch (cmp) {
  182.     case LSS:   i = (i < 0);
  183.                 break;
  184.     case LEQ:   i = (i <= 0);
  185.                 break;
  186.     case EQL:   i = (i == 0);
  187.                 break;
  188.     case GEQ:   i = (i >= 0);
  189.                 break;
  190.     case GTR:   i = (i > 0);
  191.                 break;
  192.     case NEQ:   i = (i != 0);
  193.                 break;
  194.     }
  195.     result->o_value.ov_boolean = i;
  196.     *sptr++ = result;
  197.     if (lval->o_type == TEMP)
  198.         free(lval);
  199.     if (rval->o_type == TEMP)
  200.         free(rval);
  201.     return (TRUE);
  202. }
  203.  
  204. LOCAL int comp(lval,rval)
  205.   struct operand *lval,*rval;
  206. {
  207.     register char *lptr,*rptr; register int lctr,rctr;
  208.     register int len;
  209.  
  210.     lptr = lval->o_value.ov_char.ovc_string;
  211.     lctr = lval->o_value.ov_char.ovc_length;
  212.     rptr = rval->o_value.ov_char.ovc_string;
  213.     rctr = rval->o_value.ov_char.ovc_length;
  214.  
  215. #ifdef DEBUG
  216.     printf("*** comp(): lptr = %s, lctr = %d, rptr = %s, rctr = %d\n",
  217.        lptr, lctr, rptr, rctr);
  218. #endif
  219.     while (lctr > 0 && (lptr[lctr-1] == 0 || lptr[lctr-1] == ' '))
  220.         lctr--;
  221.     while (rctr > 0 && (rptr[rctr-1] == 0 || rptr[rctr-1] == ' '))
  222.         rctr--;
  223.  
  224.     if (lctr < rctr)
  225.         len = lctr;
  226.     else
  227.         len = rctr;
  228.  
  229.     while ((len--) > 0) {
  230.         if (*lptr++ != *rptr++)
  231.             if (*--lptr < *--rptr)
  232.                 return (-1);
  233.             else
  234.                 return (1);
  235.     }
  236.  
  237.     if (lctr == rctr)
  238.         return (0);
  239.     else if (lctr < rctr)
  240.         return (-1);
  241.     else
  242.         return (1);
  243. }
  244.  
  245. LOCAL int ncomp(lval,rval)
  246.   struct operand *lval,*rval;
  247. {
  248.     char lstr[NUMBERMAX+1],rstr[NUMBERMAX+1];
  249.     int len;
  250.  
  251.     strncpy(lstr,lval->o_value.ov_char.ovc_string,
  252.           (len = lval->o_value.ov_char.ovc_length)); lstr[len] = EOS;
  253.     strncpy(rstr,rval->o_value.ov_char.ovc_string,
  254.           (len = rval->o_value.ov_char.ovc_length)); rstr[len] = EOS;
  255.  
  256. #ifdef DEBUG
  257.     printf("*** ncomp(): lstr = %s, rstr = %s\n", lstr, rstr);
  258. #endif
  259.     return (db_cmp(lstr,rstr));
  260. }
  261.