home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / nethack-3.1 / sys / unix / cpp3.shr / cpp5.c next >
Encoding:
C/C++ Source or Header  |  1993-01-26  |  21.2 KB  |  909 lines

  1. /*
  2.  *                C P P 5 . C
  3.  *        E x p r e s s i o n   E v a l u a t i o n
  4.  *
  5.  * Edit History
  6.  * 31-Aug-84    MM    USENET net.sources release
  7.  * 04-Oct-84    MM    __LINE__ and __FILE__ must call ungetstring()
  8.  *            so they work correctly with token concatenation.
  9.  *            Added string formal recognition.
  10.  * 25-Oct-84    MM    "Short-circuit" evaluate #if's so that we
  11.  *            don't print unnecessary error messages for
  12.  *            #if !defined(FOO) && FOO != 0 && 10 / FOO ...
  13.  * 31-Oct-84    ado/MM    Added token concatenation
  14.  *  6-Nov-84    MM    Split from #define stuff, added sizeof stuff
  15.  * 19-Nov-84    ado    #if error returns TRUE for (sigh) compatibility
  16.  */
  17.  
  18. #include    <stdio.h>
  19. #include    <ctype.h>
  20. #include    "cppdef.h"
  21. #include    "cpp.h"
  22.  
  23. /*
  24.  * Evaluate an #if expression.
  25.  */
  26.  
  27. static char    *opname[] = {        /* For debug and error messages    */
  28. "end of expression", "val", "id",
  29.   "+",   "-",  "*",  "/",  "%",
  30.   "<<", ">>",  "&",  "|",  "^",
  31.   "==", "!=",  "<", "<=", ">=",  ">",
  32.   "&&", "||",  "?",  ":",  ",",
  33.   "unary +", "unary -", "~", "!",  "(",  ")", "(none)",
  34. };
  35.  
  36. /*
  37.  * opdope[] has the operator precedence:
  38.  *     Bits
  39.  *      7    Unused (so the value is always positive)
  40.  *    6-2    Precedence (000x .. 017x)
  41.  *    1-0    Binary op. flags:
  42.  *        01    The binop flag should be set/cleared when this op is seen.
  43.  *        10    The new value of the binop flag.
  44.  * Note:  Expected, New binop
  45.  * constant    0    1    Binop, end, or ) should follow constants
  46.  * End of line    1    0    End may not be preceeded by an operator
  47.  * binary    1    0    Binary op follows a value, value follows.
  48.  * unary    0    0    Unary op doesn't follow a value, value follows
  49.  *   (        0    0    Doesn't follow value, value or unop follows
  50.  *   )        1    1    Follows value.  Op follows.
  51.  */
  52.  
  53. static char    opdope[OP_MAX] = {
  54.   0001,                    /* End of expression        */
  55.   0002,                    /* Digit            */
  56.   0000,                    /* Letter (identifier)        */
  57.   0141, 0141, 0151, 0151, 0151,        /* ADD, SUB, MUL, DIV, MOD    */
  58.   0131, 0131, 0101, 0071, 0071,        /* ASL, ASR, AND,  OR, XOR    */
  59.   0111, 0111, 0121, 0121, 0121,    0121,    /*  EQ,  NE,  LT,  LE,  GE,  GT    */
  60.   0061, 0051, 0041, 0041, 0031,        /* ANA, ORO, QUE, COL, CMA    */
  61. /*
  62.  * Unary op's follow
  63.  */
  64.   0160, 0160, 0160, 0160,        /* NEG, PLU, COM, NOT        */
  65.   0170, 0013, 0023,            /* LPA, RPA, END        */
  66. };
  67. /*
  68.  * OP_QUE and OP_RPA have alternate precedences:
  69.  */
  70. #define    OP_RPA_PREC    0013
  71. #define OP_QUE_PREC    0034
  72.  
  73. /*
  74.  * S_ANDOR and S_QUEST signal "short-circuit" boolean evaluation, so that
  75.  *    #if FOO != 0 && 10 / FOO ...
  76.  * doesn't generate an error message.  They are stored in optab.skip.
  77.  */
  78. #define S_ANDOR        2
  79. #define S_QUEST        1
  80.  
  81. typedef struct optab {
  82.     char    op;            /* Operator            */
  83.     char    prec;            /* Its precedence        */
  84.     char    skip;            /* Short-circuit: TRUE to skip    */
  85. } OPTAB;
  86. static int    evalue;            /* Current value from evallex()    */
  87.  
  88. #ifdef    nomacargs
  89. FILE_LOCAL int
  90. isbinary(op)
  91. register int    op;
  92. {
  93.     return (op >= FIRST_BINOP && op <= LAST_BINOP);
  94. }
  95.  
  96. FILE_LOCAL int
  97. isunary(op)
  98. register int    op;
  99. {
  100.     return (op >= FIRST_UNOP && op <= LAST_UNOP);
  101. }
  102. #else
  103. #define    isbinary(op)    (op >= FIRST_BINOP && op <= LAST_BINOP)
  104. #define    isunary(op)    (op >= FIRST_UNOP  && op <= LAST_UNOP)
  105. #endif
  106.  
  107. /*
  108.  * The following definitions are used to specify basic variable sizes.
  109.  */
  110.  
  111. #ifndef    S_CHAR
  112. #define    S_CHAR        (sizeof (char))
  113. #endif
  114. #ifndef    S_SINT
  115. #define    S_SINT        (sizeof (short int))
  116. #endif
  117. #ifndef    S_INT
  118. #define    S_INT        (sizeof (int))
  119. #endif
  120. #ifndef    S_LINT
  121. #define    S_LINT        (sizeof (long int))
  122. #endif
  123. #ifndef    S_FLOAT
  124. #define    S_FLOAT        (sizeof (float))
  125. #endif
  126. #ifndef    S_DOUBLE
  127. #define    S_DOUBLE    (sizeof (double))
  128. #endif
  129. #ifndef    S_PCHAR
  130. #define    S_PCHAR        (sizeof (char *))
  131. #endif
  132. #ifndef    S_PSINT
  133. #define    S_PSINT        (sizeof (short int *))
  134. #endif
  135. #ifndef    S_PINT
  136. #define    S_PINT        (sizeof (int *))
  137. #endif
  138. #ifndef    S_PLINT
  139. #define    S_PLINT        (sizeof (long int *))
  140. #endif
  141. #ifndef    S_PFLOAT
  142. #define    S_PFLOAT    (sizeof (float *))
  143. #endif
  144. #ifndef    S_PDOUBLE
  145. #define    S_PDOUBLE    (sizeof (double *))
  146. #endif
  147. #ifndef    S_PFPTR
  148. #define S_PFPTR        (sizeof (int (*)()))
  149. #endif
  150.  
  151. typedef struct types {
  152.     short    type;            /* This is the bit if        */
  153.     char    *name;            /* this is the token word    */
  154. } TYPES;
  155.  
  156. static TYPES basic_types[] = {
  157.     { T_CHAR,    "char",        },
  158.     { T_INT,    "int",        },
  159.     { T_FLOAT,    "float",    },
  160.     { T_DOUBLE,    "double",    },
  161.     { T_SHORT,    "short",    },
  162.     { T_LONG,    "long",        },
  163.     { T_SIGNED,    "signed",    },
  164.     { T_UNSIGNED,    "unsigned",    },
  165.     { 0,        NULL,        },    /* Signal end        */
  166. };
  167.  
  168. /*
  169.  * Test_table[] is used to test for illegal combinations.
  170.  */
  171. static short test_table[] = {
  172.     T_FLOAT | T_DOUBLE | T_LONG | T_SHORT,
  173.     T_FLOAT | T_DOUBLE | T_CHAR | T_INT,
  174.     T_FLOAT | T_DOUBLE | T_SIGNED | T_UNSIGNED,
  175.     T_LONG  | T_SHORT  | T_CHAR,
  176.     0                        /* end marker    */
  177. };
  178.  
  179. /*
  180.  * The order of this table is important -- it is also referenced by
  181.  * the command line processor to allow run-time overriding of the
  182.  * built-in size values.  The order must not be changed:
  183.  *    char, short, int, long, float, double (func pointer)
  184.  */
  185. SIZES size_table[] = {
  186.     { T_CHAR,    S_CHAR,        S_PCHAR        },    /* char        */
  187.     { T_SHORT,    S_SINT,        S_PSINT        },    /* short int    */
  188.     { T_INT,    S_INT,        S_PINT        },    /* int        */
  189.     { T_LONG,    S_LINT,        S_PLINT        },    /* long        */
  190.     { T_FLOAT,    S_FLOAT,    S_PFLOAT    },    /* float    */
  191.     { T_DOUBLE,    S_DOUBLE,    S_PDOUBLE    },    /* double    */
  192.     { T_FPTR,    0,        S_PFPTR        },    /* int (*())     */
  193.     { 0,    0,        0        },    /* End of table    */
  194. };
  195.  
  196. int
  197. eval()
  198. /*
  199.  * Evaluate an expression.  Straight-forward operator precedence.
  200.  * This is called from control() on encountering an #if statement.
  201.  * It calls the following routines:
  202.  * evallex    Lexical analyser -- returns the type and value of
  203.  *        the next input token.
  204.  * evaleval    Evaluate the current operator, given the values on
  205.  *        the value stack.  Returns a pointer to the (new)
  206.  *        value stack.
  207.  * For compatiblity with older cpp's, this return returns 1 (TRUE)
  208.  * if a syntax error is detected.
  209.  */
  210. {
  211.     register int    op;        /* Current operator        */
  212.     register int    *valp;        /* -> value vector        */
  213.     register OPTAB    *opp;        /* Operator stack        */
  214.     int        prec;        /* Op precedence        */
  215.     int        binop;        /* Set if binary op. needed    */
  216.     int        op1;        /* Operand from stack        */
  217.     int        skip;        /* For short-circuit testing    */
  218.     int        value[NEXP];    /* Value stack            */
  219.     OPTAB        opstack[NEXP];    /* Operand stack        */
  220.     extern int    *evaleval();    /* Does actual evaluation    */
  221.  
  222.     valp = value;
  223.     opp = opstack;
  224.     opp->op = OP_END;        /* Mark bottom of stack        */
  225.     opp->prec = opdope[OP_END];    /* And its precedence        */
  226.     opp->skip = 0;            /* Not skipping now        */
  227.     binop = 0;
  228. again:    ;
  229. #ifdef    DEBUG_EVAL
  230.     printf("In #if at again: skip = %d, binop = %d, line is: %s",
  231.         opp->skip, binop, infile->bptr);
  232. #endif
  233.     if ((op = evallex(opp->skip)) == OP_SUB && binop == 0)
  234.         op = OP_NEG;            /* Unary minus        */
  235.     else if (op == OP_ADD && binop == 0)
  236.         op = OP_PLU;            /* Unary plus        */
  237.     else if (op == OP_FAIL)
  238.         return (1);                /* Error in evallex    */
  239. #ifdef    DEBUG_EVAL
  240.     printf("op = %s, opdope = %03o, binop = %d, skip = %d\n",
  241.         opname[op], opdope[op], binop, opp->skip);
  242. #endif
  243.     if (op == DIG) {            /* Value?        */
  244.         if (binop != 0) {
  245.         cerror("misplaced constant in #if", NULLST);
  246.         return (1);
  247.         }
  248.         else if (valp >= &value[NEXP-1]) {
  249.         cerror("#if value stack overflow", NULLST);
  250.         return (1);
  251.         }
  252.         else {
  253. #ifdef    DEBUG_EVAL
  254.         printf("pushing %d onto value stack[%d]\n",
  255.             evalue, valp - value);
  256. #endif
  257.         *valp++ = evalue;
  258.         binop = 1;
  259.         }
  260.         goto again;
  261.     }
  262.     else if (op > OP_END) {
  263.         cerror("Illegal #if line", NULLST);
  264.         return (1);
  265.     }
  266.     prec = opdope[op];
  267.     if (binop != (prec & 1)) {
  268.         cerror("Operator %s in incorrect context", opname[op]);
  269.         return (1);
  270.     }
  271.     binop = (prec & 2) >> 1;
  272.     for (;;) {
  273. #ifdef    DEBUG_EVAL
  274.         printf("op %s, prec %d., stacked op %s, prec %d, skip %d\n",
  275.         opname[op], prec, opname[opp->op], opp->prec, opp->skip);
  276. #endif
  277.         if (prec > opp->prec) {
  278.         if (op == OP_LPA)
  279.             prec = OP_RPA_PREC;
  280.         else if (op == OP_QUE)
  281.             prec = OP_QUE_PREC;
  282.         op1 = opp->skip;        /* Save skip for test    */
  283.         /*
  284.          * Push operator onto op. stack.
  285.          */
  286.         opp++;
  287.         if (opp >= &opstack[NEXP]) {
  288.             cerror("expression stack overflow at op \"%s\"",
  289.             opname[op]);
  290.             return (1);
  291.         }
  292.         opp->op = op;
  293.         opp->prec = prec;
  294.         skip = (valp[-1] != 0);        /* Short-circuit tester    */
  295.         /*
  296.          * Do the short-circuit stuff here.  Short-circuiting
  297.          * stops automagically when operators are evaluated.
  298.          */
  299.         if ((op == OP_ANA && !skip)
  300.          || (op == OP_ORO && skip))
  301.             opp->skip = S_ANDOR;    /* And/or skip starts    */
  302.         else if (op == OP_QUE)        /* Start of ?: operator    */
  303.             opp->skip = (op1 & S_ANDOR) | ((!skip) ? S_QUEST : 0);
  304.         else if (op == OP_COL) {    /* : inverts S_QUEST    */
  305.             opp->skip = (op1 & S_ANDOR)
  306.                   | (((op1 & S_QUEST) != 0) ? 0 : S_QUEST);
  307.         }
  308.         else {                /* Other ops leave    */
  309.             opp->skip = op1;        /*  skipping unchanged.    */
  310.         }
  311. #ifdef    DEBUG_EVAL
  312.         printf("stacking %s, valp[-1] == %d at %s",
  313.             opname[op], valp[-1], infile->bptr);
  314.         dumpstack(opstack, opp, value, valp);
  315. #endif
  316.         goto again;
  317.         }
  318.         /*
  319.          * Pop operator from op. stack and evaluate it.
  320.          * End of stack and '(' are specials.
  321.          */
  322.         skip = opp->skip;            /* Remember skip value    */
  323.         switch ((op1 = opp->op)) {        /* Look at stacked op    */
  324.         case OP_END:            /* Stack end marker    */
  325.         if (op == OP_EOE)
  326.             return (valp[-1]);        /* Finished ok.        */
  327.         goto again;            /* Read another op.    */
  328.  
  329.         case OP_LPA:            /* ( on stack        */
  330.         if (op != OP_RPA) {        /* Matches ) on input    */
  331.             cerror("unbalanced paren's, op is \"%s\"", opname[op]);
  332.             return (1);
  333.         }
  334.         opp--;                /* Unstack it        */
  335.         /* goto again;            -- Fall through        */
  336.  
  337.         case OP_QUE:
  338.         goto again;            /* Evaluate true expr.    */
  339.  
  340.         case OP_COL:            /* : on stack.        */
  341.         opp--;                /* Unstack :        */
  342.         if (opp->op != OP_QUE) {    /* Matches ? on stack?    */
  343.             cerror("Misplaced '?' or ':', previous operator is %s",
  344.             opname[opp->op]);
  345.             return (1);
  346.         }
  347.         /*
  348.          * Evaluate op1.
  349.          */
  350.         default:                /* Others:        */
  351.         opp--;                /* Unstack the operator    */
  352. #ifdef    DEBUG_EVAL
  353.         printf("Stack before evaluation of %s\n", opname[op1]);
  354.         dumpstack(opstack, opp, value, valp);
  355. #endif
  356.         valp = evaleval(valp, op1, skip);
  357. #ifdef    DEBUG_EVAL
  358.         printf("Stack after evaluation\n");
  359.         dumpstack(opstack, opp, value, valp);
  360. #endif
  361.         }                    /* op1 switch end    */
  362.     }                    /* Stack unwind loop    */
  363. }
  364.  
  365. FILE_LOCAL int
  366. evallex(skip)
  367. int        skip;        /* TRUE if short-circuit evaluation    */
  368. /*
  369.  * Return next eval operator or value.  Called from eval().  It
  370.  * calls a special-purpose routines for 'char' strings and
  371.  * numeric values:
  372.  * evalchar    called to evaluate 'x'
  373.  * evalnum    called to evaluate numbers.
  374.  */
  375. {
  376.     register int    c, c1, t;
  377.  
  378. again:  do {                    /* Collect the token    */
  379.         c = skipws();
  380.         if ((c = macroid(c)) == EOF_CHAR || c == '\n') {
  381.         unget();
  382.         return (OP_EOE);        /* End of expression    */
  383.         }
  384.     } while ((t = type[c]) == LET && catenate());
  385.     if (t == INV) {                /* Total nonsense    */
  386.         if (!skip) {
  387.         if (isascii(c) && isprint(c))
  388.             cierror("illegal character '%c' in #if", c);
  389.         else
  390.             cierror("illegal character (%d decimal) in #if", c);
  391.         }
  392.         return (OP_FAIL);
  393.     }
  394.     else if (t == QUO) {            /* ' or "        */
  395.         if (c == '\'') {            /* Character constant    */
  396.         evalue = evalchar(skip);    /* Somewhat messy    */
  397. #ifdef    DEBUG_EVAL
  398.         printf("evalchar returns %d.\n", evalue);
  399. #endif
  400.         return (DIG);            /* Return a value    */
  401.         }
  402.         cerror("Can't use a string in an #if", NULLST);
  403.         return (OP_FAIL);
  404.     }
  405.     else if (t == LET) {            /* ID must be a macro    */
  406.         if (streq(token, "defined")) {    /* Or defined name    */
  407.         c1 = c = skipws();
  408.         if (c == '(')            /* Allow defined(name)    */
  409.             c = skipws();
  410.         if (type[c] == LET) {
  411.             evalue = (lookid(c) != NULL);
  412.             if (c1 != '('        /* Need to balance    */
  413.              || skipws() == ')')    /* Did we balance?    */
  414.             return (DIG);        /* Parsed ok        */
  415.         }
  416.         cerror("Bad #if ... defined() syntax", NULLST);
  417.         return (OP_FAIL);
  418.         }
  419.         else if (streq(token, "sizeof"))    /* New sizeof hackery    */
  420.         return (dosizeof());        /* Gets own routine    */
  421.         /*
  422.          * The Draft ANSI C Standard says that an undefined symbol
  423.          * in an #if has the value zero.  We are a bit pickier,
  424.          * warning except where the programmer was careful to write
  425.          *         #if defined(foo) ? foo : 0
  426.          */
  427. #ifdef VERBOSE
  428.          if (!skip)
  429.           cwarn("undefined symbol \"%s\" in #if, 0 used", token);
  430. #endif
  431.         evalue = 0;
  432.         return (DIG);
  433.     }
  434.     else if (t == DIG) {            /* Numbers are harder    */
  435.         evalue = evalnum(c);
  436. #ifdef    DEBUG_EVAL
  437.         printf("evalnum returns %d.\n", evalue);
  438. #endif
  439.     }
  440.     else if (strchr("!=<>&|\\", c) != NULL) {
  441.         /*
  442.          * Process a possible multi-byte lexeme.
  443.          */
  444.         c1 = cget();            /* Peek at next char    */
  445.         switch (c) {
  446.         case '!':
  447.         if (c1 == '=')
  448.             return (OP_NE);
  449.         break;
  450.  
  451.         case '=':
  452.         if (c1 != '=') {        /* Can't say a=b in #if    */
  453.             unget();
  454.             cerror("= not allowed in #if", NULLST);
  455.             return (OP_FAIL);
  456.         }
  457.         return (OP_EQ);
  458.  
  459.         case '>':
  460.         case '<':
  461.         if (c1 == c)
  462.             return ((c == '<') ? OP_ASL : OP_ASR);
  463.         else if (c1 == '=')
  464.             return ((c == '<') ? OP_LE  : OP_GE);
  465.         break;
  466.  
  467.         case '|':
  468.         case '&':
  469.         if (c1 == c)
  470.             return ((c == '|') ? OP_ORO : OP_ANA);
  471.         break;
  472.  
  473.         case '\\':
  474.         if (c1 == '\n')            /* Multi-line if    */
  475.             goto again;
  476.         cerror("Unexpected \\ in #if", NULLST);
  477.         return (OP_FAIL);
  478.         }
  479.         unget();
  480.     }
  481.     return (t);
  482. }
  483.  
  484. FILE_LOCAL int
  485. dosizeof()
  486. /*
  487.  * Process the sizeof (basic type) operation in an #if string.
  488.  * Sets evalue to the size and returns
  489.  *    DIG        success
  490.  *    OP_FAIL        bad parse or something.
  491.  */
  492. {
  493.     register int    c;
  494.     register TYPES    *tp;
  495.     register SIZES    *sizp;
  496.     register short    *testp;
  497.     short        typecode;
  498.  
  499.     if ((c = skipws()) != '(')
  500.         goto nogood;
  501.     /*
  502.      * Scan off the tokens.
  503.      */
  504.     typecode = 0;
  505.     while ((c = skipws())) {
  506.         if ((c = macroid(c)) == EOF_CHAR || c == '\n')
  507.         goto nogood;            /* End of line is a bug    */
  508.         else if (c == '(') {        /* thing (*)() func ptr    */
  509.         if (skipws() == '*'
  510.          && skipws() == ')') {        /* We found (*)        */
  511.             if (skipws() != '(')    /* Let () be optional    */
  512.             unget();
  513.             else if (skipws() != ')')
  514.             goto nogood;
  515.             typecode |= T_FPTR;        /* Function pointer    */
  516.         }
  517.         else {                /* Junk is a bug    */
  518.             goto nogood;
  519.         }
  520.         }
  521.         else if (type[c] != LET)        /* Exit if not a type    */
  522.         break;
  523.         else if (!catenate()) {        /* Maybe combine tokens    */
  524.         /*
  525.          * Look for this unexpandable token in basic_types.
  526.          * The code accepts "int long" as well as "long int"
  527.          * which is a minor bug as bugs go (and one shared with
  528.          * a lot of C compilers).
  529.          */
  530.         for (tp = basic_types; tp->name != NULLST; tp++) {
  531.             if (streq(token, tp->name))
  532.             break;
  533.         }
  534.         if (tp->name == NULLST) {
  535.             cerror("#if sizeof, unknown type \"%s\"", token);
  536.             return (OP_FAIL);
  537.         }
  538.         typecode |= tp->type;        /* Or in the type bit    */
  539.         }
  540.     }
  541.     /*
  542.      * We are at the end of the type scan.  Chew off '*' if necessary.
  543.      */
  544.     if (c == '*') {
  545.         typecode |= T_PTR;
  546.         c = skipws();
  547.     }
  548.     if (c == ')') {                /* Last syntax check    */
  549.         for (testp = test_table; *testp != 0; testp++) {
  550.         if (!bittest(typecode & *testp)) {
  551.             cerror("#if ... sizeof: illegal type combination", NULLST);
  552.             return (OP_FAIL);
  553.         }
  554.         }
  555.         /*
  556.          * We assume that all function pointers are the same size:
  557.          *        sizeof (int (*)()) == sizeof (float (*)())
  558.          * We assume that signed and unsigned don't change the size:
  559.          *        sizeof (signed int) == (sizeof unsigned int)
  560.          */
  561.         if ((typecode & T_FPTR) != 0)    /* Function pointer    */
  562.         typecode = T_FPTR | T_PTR;
  563.         else {                /* Var or var * datum    */
  564.         typecode &= ~(T_SIGNED | T_UNSIGNED);
  565.         if ((typecode & (T_SHORT | T_LONG)) != 0)
  566.             typecode &= ~T_INT;
  567.         }
  568.         if ((typecode & ~T_PTR) == 0) {
  569.         cerror("#if sizeof() error, no type specified", NULLST);
  570.         return (OP_FAIL);
  571.         }
  572.         /*
  573.          * Exactly one bit (and possibly T_PTR) may be set.
  574.          */
  575.         for (sizp = size_table; sizp->bits != 0; sizp++) {
  576.         if ((typecode & ~T_PTR) == sizp->bits) {
  577.             evalue = ((typecode & T_PTR) != 0)
  578.             ? sizp->psize : sizp->size;
  579.             return (DIG);
  580.         }
  581.         }                    /* We shouldn't fail    */
  582.         cierror("#if ... sizeof: bug, unknown type code 0x%x", typecode);
  583.         return (OP_FAIL);
  584.     }
  585.  
  586. nogood:    unget();
  587.     cerror("#if ... sizeof() syntax error", NULLST);
  588.     return (OP_FAIL);
  589. }
  590.  
  591. FILE_LOCAL int
  592. bittest(value)
  593. /*
  594.  * TRUE if value is zero or exactly one bit is set in value.
  595.  */
  596. {
  597. #if (4096 & ~(-4096)) == 0
  598.     return ((value & ~(-value)) == 0);
  599. #else
  600.     /*
  601.      * Do it the hard way (for non 2's complement machines)
  602.      */
  603.     return (value == 0 || value ^ (value - 1) == (value * 2 - 1));
  604. #endif
  605. }
  606.  
  607. FILE_LOCAL int
  608. evalnum(c)
  609. register int    c;
  610. /*
  611.  * Expand number for #if lexical analysis.  Note: evalnum recognizes
  612.  * the unsigned suffix, but only returns a signed int value.
  613.  */
  614. {
  615.     register int    value;
  616.     register int    base;
  617.     register int    c1;
  618.  
  619.     if (c != '0')
  620.         base = 10;
  621.     else if ((c = cget()) == 'x' || c == 'X') {
  622.         base = 16;
  623.         c = cget();
  624.     }
  625.     else base = 8;
  626.     value = 0;
  627.     for (;;) {
  628.         c1 = c;
  629.         if (isascii(c) && isupper(c1))
  630.         c1 = tolower(c1);
  631.         if (c1 >= 'a')
  632.         c1 -= ('a' - 10);
  633.         else c1 -= '0';
  634.         if (c1 < 0 || c1 >= base)
  635.         break;
  636.         value *= base;
  637.         value += c1;
  638.         c = cget();
  639.     }
  640.     if (c == 'u' || c == 'U')    /* Unsigned nonsense        */
  641.         c = cget();
  642.     unget();
  643.     return (value);
  644. }
  645.  
  646. FILE_LOCAL int
  647. evalchar(skip)
  648. int        skip;        /* TRUE if short-circuit evaluation    */
  649. /*
  650.  * Get a character constant
  651.  */
  652. {
  653.     register int    c;
  654.     register int    value;
  655.     register int    count;
  656.  
  657.     instring = TRUE;
  658.     if ((c = cget()) == '\\') {
  659.         switch ((c = cget())) {
  660.         case 'a':                /* New in Standard    */
  661. #if ('a' == '\a' || '\a' == ALERT)
  662.         value = ALERT;            /* Use predefined value    */
  663. #else
  664.         value = '\a';            /* Use compiler's value    */
  665. #endif
  666.         break;
  667.  
  668.         case 'b':
  669.         value = '\b';
  670.         break;
  671.  
  672.         case 'f':
  673.         value = '\f';
  674.         break;
  675.  
  676.         case 'n':
  677.         value = '\n';
  678.         break;
  679.  
  680.         case 'r':
  681.         value = '\r';
  682.         break;
  683.  
  684.         case 't':
  685.         value = '\t';
  686.         break;
  687.  
  688.         case 'v':                /* New in Standard    */
  689. #if ('v' == '\v' || '\v' == VT)
  690.         value = VT;            /* Use predefined value    */
  691. #else
  692.         value = '\v';            /* Use compiler's value    */
  693. #endif
  694.         break;
  695.  
  696.         case 'x':                /* '\xFF'        */
  697.         count = 3;
  698.         value = 0;
  699.         while ((((c = get()) >= '0' && c <= '9')
  700.              || (c >= 'a' && c <= 'f')
  701.              || (c >= 'A' && c <= 'F'))
  702.             && (--count >= 0)) {
  703.             value *= 16;
  704.             value += (c <= '9') ? (c - '0') : ((c & 0xF) + 9);
  705.         }
  706.         unget();
  707.         break;
  708.  
  709.         default:
  710.         if (c >= '0' && c <= '7') {
  711.             count = 3;
  712.             value = 0;
  713.             while (c >= '0' && c <= '7' && --count >= 0) {
  714.             value *= 8;
  715.             value += (c - '0');
  716.             c = get();
  717.             }
  718.             unget();
  719.         }
  720.         else value = c;
  721.         break;
  722.         }
  723.     }
  724.     else if (c == '\'')
  725.         value = 0;
  726.     else value = c;
  727.     /*
  728.      * We warn on multi-byte constants and try to hack
  729.      * (big|little)endian machines.
  730.      */
  731. #if BIG_ENDIAN
  732.     count = 0;
  733. #endif
  734.     while ((c = get()) != '\'' && c != EOF_CHAR && c != '\n') {
  735.         if (!skip)
  736.         ciwarn("multi-byte constant '%c' isn't portable", c);
  737. #if BIG_ENDIAN
  738.         count += BITS_CHAR;
  739.         value += (c << count);
  740. #else
  741.         value <<= BITS_CHAR;
  742.         value += c;
  743. #endif
  744.     }
  745.     instring = FALSE;
  746.     return (value);
  747. }
  748.  
  749. FILE_LOCAL int *
  750. evaleval(valp, op, skip)
  751. register int    *valp;
  752. int        op;
  753. int        skip;        /* TRUE if short-circuit evaluation    */
  754. /*
  755.  * Apply the argument operator to the data on the value stack.
  756.  * One or two values are popped from the value stack and the result
  757.  * is pushed onto the value stack.
  758.  *
  759.  * OP_COL is a special case.
  760.  *
  761.  * evaleval() returns the new pointer to the top of the value stack.
  762.  */
  763. {
  764.     register int    v1, v2;
  765.  
  766.     if (isbinary(op))
  767.         v2 = *--valp;
  768.     v1 = *--valp;
  769. #ifdef    DEBUG_EVAL
  770.     printf("%s op %s", (isbinary(op)) ? "binary" : "unary",
  771.         opname[op]);
  772.     if (isbinary(op))
  773.         printf(", v2 = %d.", v2);
  774.     printf(", v1 = %d.\n", v1);
  775. #endif
  776.     switch (op) {
  777.     case OP_EOE:
  778.          break;
  779.  
  780.     case OP_ADD:
  781.         v1 += v2;
  782.         break;
  783.  
  784.     case OP_SUB:
  785.         v1 -= v2;
  786.         break;
  787.  
  788.     case OP_MUL:
  789.         v1 *= v2;
  790.         break;
  791.  
  792.     case OP_DIV:
  793.     case OP_MOD:
  794.         if (v2 == 0) {
  795.         if (!skip) {
  796.             cwarn("%s by zero in #if, zero result assumed",
  797.             (op == OP_DIV) ? "divide" : "mod");
  798.         }
  799.         v1 = 0;
  800.         }
  801.         else if (op == OP_DIV)
  802.         v1 /= v2;
  803.         else
  804.         v1 %= v2;
  805.         break;
  806.  
  807.     case OP_ASL:
  808.         v1 <<= v2;
  809.         break;
  810.  
  811.     case OP_ASR:
  812.         v1 >>= v2;
  813.         break;
  814.  
  815.     case OP_AND:
  816.         v1 &= v2;
  817.         break;
  818.  
  819.     case OP_OR:
  820.         v1 |= v2;
  821.         break;
  822.  
  823.     case OP_XOR:
  824.         v1 ^= v2;
  825.         break;
  826.  
  827.     case OP_EQ:
  828.         v1 = (v1 == v2);
  829.         break;
  830.  
  831.     case OP_NE:
  832.         v1 = (v1 != v2);
  833.         break;
  834.  
  835.     case OP_LT:
  836.         v1 = (v1 < v2);
  837.         break;
  838.  
  839.     case OP_LE:
  840.         v1 = (v1 <= v2);
  841.         break;
  842.  
  843.     case OP_GE:
  844.         v1 = (v1 >= v2);
  845.         break;
  846.  
  847.     case OP_GT:
  848.         v1 = (v1 > v2);
  849.         break;
  850.  
  851.     case OP_ANA:
  852.         v1 = (v1 && v2);
  853.         break;
  854.  
  855.     case OP_ORO:
  856.         v1 = (v1 || v2);
  857.         break;
  858.  
  859.     case OP_COL:
  860.         /*
  861.          * v1 has the "true" value, v2 the "false" value.
  862.          * The top of the value stack has the test.
  863.          */
  864.         v1 = (*--valp) ? v1 : v2;
  865.         break;
  866.  
  867.     case OP_NEG:
  868.         v1 = (-v1);
  869.         break;
  870.  
  871.     case OP_PLU:
  872.         break;
  873.  
  874.     case OP_COM:
  875.         v1 = ~v1;
  876.         break;
  877.  
  878.     case OP_NOT:
  879.         v1 = !v1;
  880.         break;
  881.  
  882.     default:
  883.         cierror("#if bug, operand = %d.", op);
  884.         v1 = 0;
  885.     }
  886.     *valp++ = v1;
  887.     return (valp);
  888. }
  889.  
  890. #ifdef    DEBUG_EVAL
  891. dumpstack(opstack, opp, value, valp)
  892. OPTAB        opstack[NEXP];    /* Operand stack        */
  893. register OPTAB    *opp;        /* Operator stack        */
  894. int        value[NEXP];    /* Value stack            */
  895. register int    *valp;        /* -> value vector        */
  896. {
  897.     printf("index op prec skip name -- op stack at %s", infile->bptr);
  898.     while (opp > opstack) {
  899.         printf(" [%2d] %2d  %03o    %d %s\n", opp - opstack,
  900.         opp->op, opp->prec, opp->skip, opname[opp->op]);
  901.         opp--;
  902.     }
  903.     while (--valp >= value) {
  904.         printf("value[%d] = %d\n", (valp - value), *valp);
  905.     }
  906. }
  907. #endif
  908.  
  909.