home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / TOP / USR / SRC / cpp.decus.t.Z / cpp.decus.t / cpp5.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-03-06  |  21.2 KB  |  907 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.         if (!skip)
  428.         cwarn("undefined symbol \"%s\" in #if, 0 used", token);
  429.         evalue = 0;
  430.         return (DIG);
  431.     }
  432.     else if (t == DIG) {            /* Numbers are harder    */
  433.         evalue = evalnum(c);
  434. #ifdef    DEBUG_EVAL
  435.         printf("evalnum returns %d.\n", evalue);
  436. #endif
  437.     }
  438.     else if (strchr("!=<>&|\\", c) != NULL) {
  439.         /*
  440.          * Process a possible multi-byte lexeme.
  441.          */
  442.         c1 = cget();            /* Peek at next char    */
  443.         switch (c) {
  444.         case '!':
  445.         if (c1 == '=')
  446.             return (OP_NE);
  447.         break;
  448.  
  449.         case '=':
  450.         if (c1 != '=') {        /* Can't say a=b in #if    */
  451.             unget();
  452.             cerror("= not allowed in #if", NULLST);
  453.             return (OP_FAIL);
  454.         }
  455.         return (OP_EQ);
  456.  
  457.         case '>':
  458.         case '<':
  459.         if (c1 == c)
  460.             return ((c == '<') ? OP_ASL : OP_ASR);
  461.         else if (c1 == '=')
  462.             return ((c == '<') ? OP_LE  : OP_GE);
  463.         break;
  464.  
  465.         case '|':
  466.         case '&':
  467.         if (c1 == c)
  468.             return ((c == '|') ? OP_ORO : OP_ANA);
  469.         break;
  470.  
  471.         case '\\':
  472.         if (c1 == '\n')            /* Multi-line if    */
  473.             goto again;
  474.         cerror("Unexpected \\ in #if", NULLST);
  475.         return (OP_FAIL);
  476.         }
  477.         unget();
  478.     }
  479.     return (t);
  480. }
  481.  
  482. FILE_LOCAL int
  483. dosizeof()
  484. /*
  485.  * Process the sizeof (basic type) operation in an #if string.
  486.  * Sets evalue to the size and returns
  487.  *    DIG        success
  488.  *    OP_FAIL        bad parse or something.
  489.  */
  490. {
  491.     register int    c;
  492.     register TYPES    *tp;
  493.     register SIZES    *sizp;
  494.     register short    *testp;
  495.     short        typecode;
  496.  
  497.     if ((c = skipws()) != '(')
  498.         goto nogood;
  499.     /*
  500.      * Scan off the tokens.
  501.      */
  502.     typecode = 0;
  503.     while ((c = skipws())) {
  504.         if ((c = macroid(c)) == EOF_CHAR || c == '\n')
  505.         goto nogood;            /* End of line is a bug    */
  506.         else if (c == '(') {        /* thing (*)() func ptr    */
  507.         if (skipws() == '*'
  508.          && skipws() == ')') {        /* We found (*)        */
  509.             if (skipws() != '(')    /* Let () be optional    */
  510.             unget();
  511.             else if (skipws() != ')')
  512.             goto nogood;
  513.             typecode |= T_FPTR;        /* Function pointer    */
  514.         }
  515.         else {                /* Junk is a bug    */
  516.             goto nogood;
  517.         }
  518.         }
  519.         else if (type[c] != LET)        /* Exit if not a type    */
  520.         break;
  521.         else if (!catenate()) {        /* Maybe combine tokens    */
  522.         /*
  523.          * Look for this unexpandable token in basic_types.
  524.          * The code accepts "int long" as well as "long int"
  525.          * which is a minor bug as bugs go (and one shared with
  526.          * a lot of C compilers).
  527.          */
  528.         for (tp = basic_types; tp->name != NULLST; tp++) {
  529.             if (streq(token, tp->name))
  530.             break;
  531.         }
  532.         if (tp->name == NULLST) {
  533.             cerror("#if sizeof, unknown type \"%s\"", token);
  534.             return (OP_FAIL);
  535.         }
  536.         typecode |= tp->type;        /* Or in the type bit    */
  537.         }
  538.     }
  539.     /*
  540.      * We are at the end of the type scan.  Chew off '*' if necessary.
  541.      */
  542.     if (c == '*') {
  543.         typecode |= T_PTR;
  544.         c = skipws();
  545.     }
  546.     if (c == ')') {                /* Last syntax check    */
  547.         for (testp = test_table; *testp != 0; testp++) {
  548.         if (!bittest(typecode & *testp)) {
  549.             cerror("#if ... sizeof: illegal type combination", NULLST);
  550.             return (OP_FAIL);
  551.         }
  552.         }
  553.         /*
  554.          * We assume that all function pointers are the same size:
  555.          *        sizeof (int (*)()) == sizeof (float (*)())
  556.          * We assume that signed and unsigned don't change the size:
  557.          *        sizeof (signed int) == (sizeof unsigned int)
  558.          */
  559.         if ((typecode & T_FPTR) != 0)    /* Function pointer    */
  560.         typecode = T_FPTR | T_PTR;
  561.         else {                /* Var or var * datum    */
  562.         typecode &= ~(T_SIGNED | T_UNSIGNED);
  563.         if ((typecode & (T_SHORT | T_LONG)) != 0)
  564.             typecode &= ~T_INT;
  565.         }
  566.         if ((typecode & ~T_PTR) == 0) {
  567.         cerror("#if sizeof() error, no type specified", NULLST);
  568.         return (OP_FAIL);
  569.         }
  570.         /*
  571.          * Exactly one bit (and possibly T_PTR) may be set.
  572.          */
  573.         for (sizp = size_table; sizp->bits != 0; sizp++) {
  574.         if ((typecode & ~T_PTR) == sizp->bits) {
  575.             evalue = ((typecode & T_PTR) != 0)
  576.             ? sizp->psize : sizp->size;
  577.             return (DIG);
  578.         }
  579.         }                    /* We shouldn't fail    */
  580.         cierror("#if ... sizeof: bug, unknown type code 0x%x", typecode);
  581.         return (OP_FAIL);
  582.     }
  583.  
  584. nogood:    unget();
  585.     cerror("#if ... sizeof() syntax error", NULLST);
  586.     return (OP_FAIL);
  587. }
  588.  
  589. FILE_LOCAL int
  590. bittest(value)
  591. /*
  592.  * TRUE if value is zero or exactly one bit is set in value.
  593.  */
  594. {
  595. #if (4096 & ~(-4096)) == 0
  596.     return ((value & ~(-value)) == 0);
  597. #else
  598.     /*
  599.      * Do it the hard way (for non 2's complement machines)
  600.      */
  601.     return (value == 0 || value ^ (value - 1) == (value * 2 - 1));
  602. #endif
  603. }
  604.  
  605. FILE_LOCAL int
  606. evalnum(c)
  607. register int    c;
  608. /*
  609.  * Expand number for #if lexical analysis.  Note: evalnum recognizes
  610.  * the unsigned suffix, but only returns a signed int value.
  611.  */
  612. {
  613.     register int    value;
  614.     register int    base;
  615.     register int    c1;
  616.  
  617.     if (c != '0')
  618.         base = 10;
  619.     else if ((c = cget()) == 'x' || c == 'X') {
  620.         base = 16;
  621.         c = cget();
  622.     }
  623.     else base = 8;
  624.     value = 0;
  625.     for (;;) {
  626.         c1 = c;
  627.         if (isascii(c) && isupper(c1))
  628.         c1 = tolower(c1);
  629.         if (c1 >= 'a')
  630.         c1 -= ('a' - 10);
  631.         else c1 -= '0';
  632.         if (c1 < 0 || c1 >= base)
  633.         break;
  634.         value *= base;
  635.         value += c1;
  636.         c = cget();
  637.     }
  638.     if (c == 'u' || c == 'U')    /* Unsigned nonsense        */
  639.         c = cget();
  640.     unget();
  641.     return (value);
  642. }
  643.  
  644. FILE_LOCAL int
  645. evalchar(skip)
  646. int        skip;        /* TRUE if short-circuit evaluation    */
  647. /*
  648.  * Get a character constant
  649.  */
  650. {
  651.     register int    c;
  652.     register int    value;
  653.     register int    count;
  654.  
  655.     instring = TRUE;
  656.     if ((c = cget()) == '\\') {
  657.         switch ((c = cget())) {
  658.         case 'a':                /* New in Standard    */
  659. #if ('a' == '\a' || '\a' == ALERT)
  660.         value = ALERT;            /* Use predefined value    */
  661. #else
  662.         value = '\a';            /* Use compiler's value    */
  663. #endif
  664.         break;
  665.  
  666.         case 'b':
  667.         value = '\b';
  668.         break;
  669.  
  670.         case 'f':
  671.         value = '\f';
  672.         break;
  673.  
  674.         case 'n':
  675.         value = '\n';
  676.         break;
  677.  
  678.         case 'r':
  679.         value = '\r';
  680.         break;
  681.  
  682.         case 't':
  683.         value = '\t';
  684.         break;
  685.  
  686.         case 'v':                /* New in Standard    */
  687. #if ('v' == '\v' || '\v' == VT)
  688.         value = VT;            /* Use predefined value    */
  689. #else
  690.         value = '\v';            /* Use compiler's value    */
  691. #endif
  692.         break;
  693.  
  694.         case 'x':                /* '\xFF'        */
  695.         count = 3;
  696.         value = 0;
  697.         while ((((c = get()) >= '0' && c <= '9')
  698.              || (c >= 'a' && c <= 'f')
  699.              || (c >= 'A' && c <= 'F'))
  700.             && (--count >= 0)) {
  701.             value *= 16;
  702.             value += (c <= '9') ? (c - '0') : ((c & 0xF) + 9);
  703.         }
  704.         unget();
  705.         break;
  706.  
  707.         default:
  708.         if (c >= '0' && c <= '7') {
  709.             count = 3;
  710.             value = 0;
  711.             while (c >= '0' && c <= '7' && --count >= 0) {
  712.             value *= 8;
  713.             value += (c - '0');
  714.             c = get();
  715.             }
  716.             unget();
  717.         }
  718.         else value = c;
  719.         break;
  720.         }
  721.     }
  722.     else if (c == '\'')
  723.         value = 0;
  724.     else value = c;
  725.     /*
  726.      * We warn on multi-byte constants and try to hack
  727.      * (big|little)endian machines.
  728.      */
  729. #if BIG_ENDIAN
  730.     count = 0;
  731. #endif
  732.     while ((c = get()) != '\'' && c != EOF_CHAR && c != '\n') {
  733.         if (!skip)
  734.         ciwarn("multi-byte constant '%c' isn't portable", c);
  735. #if BIG_ENDIAN
  736.         count += BITS_CHAR;
  737.         value += (c << count);
  738. #else
  739.         value <<= BITS_CHAR;
  740.         value += c;
  741. #endif
  742.     }
  743.     instring = FALSE;
  744.     return (value);
  745. }
  746.  
  747. FILE_LOCAL int *
  748. evaleval(valp, op, skip)
  749. register int    *valp;
  750. int        op;
  751. int        skip;        /* TRUE if short-circuit evaluation    */
  752. /*
  753.  * Apply the argument operator to the data on the value stack.
  754.  * One or two values are popped from the value stack and the result
  755.  * is pushed onto the value stack.
  756.  *
  757.  * OP_COL is a special case.
  758.  *
  759.  * evaleval() returns the new pointer to the top of the value stack.
  760.  */
  761. {
  762.     register int    v1, v2;
  763.  
  764.     if (isbinary(op))
  765.         v2 = *--valp;
  766.     v1 = *--valp;
  767. #ifdef    DEBUG_EVAL
  768.     printf("%s op %s", (isbinary(op)) ? "binary" : "unary",
  769.         opname[op]);
  770.     if (isbinary(op))
  771.         printf(", v2 = %d.", v2);
  772.     printf(", v1 = %d.\n", v1);
  773. #endif
  774.     switch (op) {
  775.     case OP_EOE:
  776.          break;
  777.  
  778.     case OP_ADD:
  779.         v1 += v2;
  780.         break;
  781.  
  782.     case OP_SUB:
  783.         v1 -= v2;
  784.         break;
  785.  
  786.     case OP_MUL:
  787.         v1 *= v2;
  788.         break;
  789.  
  790.     case OP_DIV:
  791.     case OP_MOD:
  792.         if (v2 == 0) {
  793.         if (!skip) {
  794.             cwarn("%s by zero in #if, zero result assumed",
  795.             (op == OP_DIV) ? "divide" : "mod");
  796.         }
  797.         v1 = 0;
  798.         }
  799.         else if (op == OP_DIV)
  800.         v1 /= v2;
  801.         else
  802.         v1 %= v2;
  803.         break;
  804.  
  805.     case OP_ASL:
  806.         v1 <<= v2;
  807.         break;
  808.  
  809.     case OP_ASR:
  810.         v1 >>= v2;
  811.         break;
  812.  
  813.     case OP_AND:
  814.         v1 &= v2;
  815.         break;
  816.  
  817.     case OP_OR:
  818.         v1 |= v2;
  819.         break;
  820.  
  821.     case OP_XOR:
  822.         v1 ^= v2;
  823.         break;
  824.  
  825.     case OP_EQ:
  826.         v1 = (v1 == v2);
  827.         break;
  828.  
  829.     case OP_NE:
  830.         v1 = (v1 != v2);
  831.         break;
  832.  
  833.     case OP_LT:
  834.         v1 = (v1 < v2);
  835.         break;
  836.  
  837.     case OP_LE:
  838.         v1 = (v1 <= v2);
  839.         break;
  840.  
  841.     case OP_GE:
  842.         v1 = (v1 >= v2);
  843.         break;
  844.  
  845.     case OP_GT:
  846.         v1 = (v1 > v2);
  847.         break;
  848.  
  849.     case OP_ANA:
  850.         v1 = (v1 && v2);
  851.         break;
  852.  
  853.     case OP_ORO:
  854.         v1 = (v1 || v2);
  855.         break;
  856.  
  857.     case OP_COL:
  858.         /*
  859.          * v1 has the "true" value, v2 the "false" value.
  860.          * The top of the value stack has the test.
  861.          */
  862.         v1 = (*--valp) ? v1 : v2;
  863.         break;
  864.  
  865.     case OP_NEG:
  866.         v1 = (-v1);
  867.         break;
  868.  
  869.     case OP_PLU:
  870.         break;
  871.  
  872.     case OP_COM:
  873.         v1 = ~v1;
  874.         break;
  875.  
  876.     case OP_NOT:
  877.         v1 = !v1;
  878.         break;
  879.  
  880.     default:
  881.         cierror("#if bug, operand = %d.", op);
  882.         v1 = 0;
  883.     }
  884.     *valp++ = v1;
  885.     return (valp);
  886. }
  887.  
  888. #ifdef    DEBUG_EVAL
  889. dumpstack(opstack, opp, value, valp)
  890. OPTAB        opstack[NEXP];    /* Operand stack        */
  891. register OPTAB    *opp;        /* Operator stack        */
  892. int        value[NEXP];    /* Value stack            */
  893. register int    *valp;        /* -> value vector        */
  894. {
  895.     printf("index op prec skip name -- op stack at %s", infile->bptr);
  896.     while (opp > opstack) {
  897.         printf(" [%2d] %2d  %03o    %d %s\n", opp - opstack,
  898.         opp->op, opp->prec, opp->skip, opname[opp->op]);
  899.         opp--;
  900.     }
  901.     while (--valp >= value) {
  902.         printf("value[%d] = %d\n", (valp - value), *valp);
  903.     }
  904. }
  905. #endif
  906.  
  907.