mojoshader_preprocessor.c
changeset 701 08e5a289abad
parent 699 c2f50b391d96
child 723 6d58b504cc6e
equal deleted inserted replaced
700:2213a232d2fb 701:08e5a289abad
  1126     Conditional *conditional = get_conditional(ctx);
  1126     Conditional *conditional = get_conditional(ctx);
  1127     assert((conditional != NULL) || (ctx->out_of_memory));
  1127     assert((conditional != NULL) || (ctx->out_of_memory));
  1128     if (conditional == NULL)
  1128     if (conditional == NULL)
  1129         return NULL;
  1129         return NULL;
  1130 
  1130 
  1131     Conditional *prev = state->conditional_stack;
  1131     Conditional *parent = state->conditional_stack;
  1132     int skipping = ((prev != NULL) && (prev->skipping));
  1132     const int found = (find_define(ctx, sym) != NULL);
  1133     if (!skipping)
  1133     const int chosen = (type == TOKEN_PP_IFDEF) ? found : !found;
  1134     {
  1134     const int skipping = ( (((parent) && (parent->skipping))) || (!chosen) );
  1135         const int found = (find_define(ctx, sym) != NULL);
       
  1136         if (type == TOKEN_PP_IFDEF)
       
  1137             skipping = !found;
       
  1138         else
       
  1139             skipping = found;
       
  1140     } // if
       
  1141 
  1135 
  1142     conditional->type = type;
  1136     conditional->type = type;
  1143     conditional->linenum = state->line - 1;
  1137     conditional->linenum = state->line - 1;
  1144     conditional->skipping = skipping;
  1138     conditional->skipping = skipping;
  1145     conditional->chosen = !skipping;
  1139     conditional->chosen = chosen;
  1146     conditional->next = prev;
  1140     conditional->next = parent;
  1147     state->conditional_stack = conditional;
  1141     state->conditional_stack = conditional;
  1148     return conditional;
  1142     return conditional;
  1149 } // _handle_pp_ifdef
  1143 } // _handle_pp_ifdef
  1150 
  1144 
  1151 
  1145 
  1612     Conditional *conditional = get_conditional(ctx);
  1606     Conditional *conditional = get_conditional(ctx);
  1613     assert((conditional != NULL) || (ctx->out_of_memory));
  1607     assert((conditional != NULL) || (ctx->out_of_memory));
  1614     if (conditional == NULL)
  1608     if (conditional == NULL)
  1615         return NULL;
  1609         return NULL;
  1616 
  1610 
  1617     Conditional *prev = state->conditional_stack;
  1611     Conditional *parent = state->conditional_stack;
  1618     int skipping = ((prev != NULL) && (prev->skipping));
  1612     const int chosen = result;
  1619     if (!skipping)
  1613     const int skipping = ( (((parent) && (parent->skipping))) || (!chosen) );
  1620         skipping = !result;
       
  1621 
  1614 
  1622     conditional->type = TOKEN_PP_IF;
  1615     conditional->type = TOKEN_PP_IF;
  1623     conditional->linenum = state->line - 1;
  1616     conditional->linenum = state->line - 1;
  1624     conditional->skipping = skipping;
  1617     conditional->skipping = skipping;
  1625     conditional->chosen = !skipping;
  1618     conditional->chosen = chosen;
  1626     conditional->next = prev;
  1619     conditional->next = parent;
  1627     state->conditional_stack = conditional;
  1620     state->conditional_stack = conditional;
  1628     return conditional;
  1621     return conditional;
  1629 } // handle_pp_if
  1622 } // handle_pp_if
  1630 
  1623 
  1631 
  1624 
  1632 static void handle_pp_elif(Context *ctx)
  1625 static void handle_pp_elif(Context *ctx)
  1633 {
  1626 {
  1634     const int result = reduce_pp_expression(ctx);
  1627     const int rc = reduce_pp_expression(ctx);
  1635     if (result == -1)
  1628     if (rc == -1)
  1636         return;
  1629         return;
  1637 
  1630 
  1638     IncludeState *state = ctx->include_stack;
  1631     IncludeState *state = ctx->include_stack;
  1639     Conditional *cond = state->conditional_stack;
  1632     Conditional *cond = state->conditional_stack;
  1640     if (cond == NULL)
  1633     if (cond == NULL)
  1641         fail(ctx, "#elif without #if");
  1634         fail(ctx, "#elif without #if");
  1642     else if (cond->type == TOKEN_PP_ELSE)
  1635     else if (cond->type == TOKEN_PP_ELSE)
  1643         fail(ctx, "#elif after #else");
  1636         fail(ctx, "#elif after #else");
  1644     else
  1637     else
  1645     {
  1638     {
       
  1639         const Conditional *parent = cond->next;
  1646         cond->type = TOKEN_PP_ELIF;
  1640         cond->type = TOKEN_PP_ELIF;
  1647         cond->skipping = ((cond->chosen) || (!result));
  1641         cond->skipping = (parent && parent->skipping) || cond->chosen || !rc;
  1648         if (!cond->chosen)
  1642         if (!cond->chosen)
  1649             cond->chosen = result;
  1643             cond->chosen = rc;
  1650     } // else
  1644     } // else
  1651 } // handle_pp_elif
  1645 } // handle_pp_elif
  1652 
  1646 
  1653 
  1647 
  1654 static void handle_pp_else(Context *ctx)
  1648 static void handle_pp_else(Context *ctx)
  1662         fail(ctx, "#else without #if");
  1656         fail(ctx, "#else without #if");
  1663     else if (cond->type == TOKEN_PP_ELSE)
  1657     else if (cond->type == TOKEN_PP_ELSE)
  1664         fail(ctx, "#else after #else");
  1658         fail(ctx, "#else after #else");
  1665     else
  1659     else
  1666     {
  1660     {
       
  1661         const Conditional *parent = cond->next;
  1667         cond->type = TOKEN_PP_ELSE;
  1662         cond->type = TOKEN_PP_ELSE;
  1668         cond->skipping = cond->chosen;
  1663         cond->skipping = (parent && parent->skipping) || cond->chosen;
  1669         if (!cond->chosen)
  1664         if (!cond->chosen)
  1670             cond->chosen = 1;
  1665             cond->chosen = 1;
  1671     } // else
  1666     } // else
  1672 } // handle_pp_else
  1667 } // handle_pp_else
  1673 
  1668