/*
* Table-driven evaluation of binary operators.
*/
static Linetype
eval_table(const struct ops *ops, int *valp, const char **cpp)
{
const struct op *op;
const char *cp;
int val;
Linetype lt, rt;
debug(“eval%d”, ops – eval_ops);
cp = *cpp;
lt = ops->inner(ops+1, valp, &cp);
if (lt == LT_ERROR)
return (LT_ERROR);
for (;;) {
cp = skipcomment(cp);
for (op = ops->op; op->str != NULL; op++)
if (strncmp(cp, op->str, strlen(op->str)) == 0)
break;
if (op->str == NULL)
break;
cp += strlen(op->str);
debug(“eval%d %s”, ops – eval_ops, op->str);
rt = ops->inner(ops+1, &val, &cp);
if (rt == LT_ERROR)
return (LT_ERROR);
lt = op->fn(valp, lt, *valp, rt, val);
}
*cpp = cp;
debug(“eval%d = %d”, ops – eval_ops, *valp);
debug(“eval%d lt = %s”, ops – eval_ops, linetype_name[lt]);
return (lt);
}
/*
* Evaluate the expression on a #if or #elif line. If we can work out
* the result we return LT_TRUE or LT_FALSE accordingly, otherwise we
* return just a generic LT_IF.
*/
static Linetype
ifeval(const char **cpp)
{
int ret;
int val = 0;
debug(“eval %s”, *cpp);
constexpr = killconsts ? false : true;
ret = eval_table(eval_ops, &val, cpp);
debug(“eval = %d”, val);
return (constexpr ? LT_IF : ret == LT_ERROR ? LT_IF : ret);
}
/*
* Skip over comments, strings, and character literals and stop at the
* next character position that is not whitespace. Between calls we keep
* the comment state in the global variable incomment, and we also adjust
* the global variable linestate when we see a newline.
* XXX: doesn’t cope with the buffer splitting inside a state transition.
*/
static const char *
skipcomment(const char *cp)
{
if (text || ignoring[depth]) {
for (; isspace((unsigned char)*cp); cp++)
if (*cp == ‘\n’)
linestate = LS_START;
return (cp);
}
while (*cp != ‘\0′)
/* don’t reset to LS_START after a line continuation */
if (strncmp(cp, “\\\r\n”, 3) == 0)
cp += 3;
else if (strncmp(cp, “\\\n”, 2) == 0)
cp += 2;
else switch (incomment) {
case NO_COMMENT:
if (strncmp(cp, “/\\\r\n”, 4) == 0) {
incomment = STARTING_COMMENT;
cp += 4;
} else if (strncmp(cp, “/\\\n”, 3) == 0) {
incomment = STARTING_COMMENT;
cp += 3;
} else if (strncmp(cp, “/*”, 2) == 0) {
incomment = C_COMMENT;
cp += 2;
} else if (strncmp(cp, “//”, 2) == 0) {
incomment = CXX_COMMENT;
cp += 2;
} else if (strncmp(cp, “\’”, 1) == 0) {
incomment = CHAR_LITERAL;
linestate = LS_DIRTY;
cp += 1;
} else if (strncmp(cp, “\”", 1) == 0) {
incomment = STRING_LITERAL;
linestate = LS_DIRTY;
cp += 1;
} else if (strncmp(cp, “\n”, 1) == 0) {
linestate = LS_START;
cp += 1;
} else if (strchr(” \r\t”, *cp) != NULL) {
cp += 1;
} else
return (cp);
continue;
case CXX_COMMENT:
if (strncmp(cp, “\n”, 1) == 0) {
incomment = NO_COMMENT;
linestate = LS_START;
}
cp += 1;
continue;
case CHAR_LITERAL:
case STRING_LITERAL:
if ((incomment == CHAR_LITERAL && cp[0] == ‘\”) ||
(incomment == STRING_LITERAL && cp[0] == ‘\”‘)) {
incomment = NO_COMMENT;
cp += 1;
} else if (cp[0] == ‘\\’) {
if (cp[1] == ‘\0′)
cp += 1;
else
cp += 2;
} else if (strncmp(cp, “\n”, 1) == 0) {
if (incomment == CHAR_LITERAL)
error(“unterminated char literal”);
else
error(“unterminated string literal”);
} else
cp += 1;
continue;
case C_COMMENT:
if (strncmp(cp, “*\\\r\n”, 4) == 0) {
incomment = FINISHING_COMMENT;
cp += 4;
} else if (strncmp(cp, “*\\\n”, 3) == 0) {
incomment = FINISHING_COMMENT;
cp += 3;
} else if (strncmp(cp, “*/”, 2) == 0) {
incomment = NO_COMMENT;
cp += 2;
} else
cp += 1;
continue;
case STARTING_COMMENT:
if (*cp == ‘*’) {
incomment = C_COMMENT;
cp += 1;
} else if (*cp == ‘/’) {
incomment = CXX_COMMENT;
cp += 1;
} else {
incomment = NO_COMMENT;
linestate = LS_DIRTY;
}
continue;
case FINISHING_COMMENT:
if (*cp == ‘/’) {
incomment = NO_COMMENT;
cp += 1;
} else
incomment = C_COMMENT;
continue;
default:
abort(); /* bug */
}
return (cp);
}