mojoshader_preprocessor.c
changeset 699 c2f50b391d96
parent 698 a6cd16b3e270
child 701 08e5a289abad
equal deleted inserted replaced
698:a6cd16b3e270 699:c2f50b391d96
   458     } // while
   458     } // while
   459     return NULL;
   459     return NULL;
   460 } // find_define
   460 } // find_define
   461 
   461 
   462 
   462 
       
   463 static const Define *find_define_by_token(Context *ctx)
       
   464 {
       
   465     IncludeState *state = ctx->include_stack;
       
   466     assert(state->tokenval == TOKEN_IDENTIFIER);
       
   467     char *sym = (char *) alloca(state->tokenlen+1);
       
   468     memcpy(sym, state->token, state->tokenlen);
       
   469     sym[state->tokenlen] = '\0';
       
   470     return find_define(ctx, sym);
       
   471 } // find_define_by_token
       
   472 
       
   473 
   463 static void put_all_defines(Context *ctx)
   474 static void put_all_defines(Context *ctx)
   464 {
   475 {
   465     int i;
   476     int i;
   466     for (i = 0; i < STATICARRAYLEN(ctx->define_hashtable); i++)
   477     for (i = 0; i < STATICARRAYLEN(ctx->define_hashtable); i++)
   467     {
   478     {
   925     char *sym = (char *) Malloc(ctx, state->tokenlen+1);
   936     char *sym = (char *) Malloc(ctx, state->tokenlen+1);
   926     if (sym == NULL)
   937     if (sym == NULL)
   927         return;
   938         return;
   928     memcpy(sym, state->token, state->tokenlen);
   939     memcpy(sym, state->token, state->tokenlen);
   929     sym[state->tokenlen] = '\0';
   940     sym[state->tokenlen] = '\0';
       
   941 
       
   942     if (strcmp(sym, "defined") == 0)
       
   943     {
       
   944         Free(ctx, sym);
       
   945         fail(ctx, "'defined' cannot be used as a macro name");
       
   946         return;
       
   947     } // if
   930 
   948 
   931     // #define a(b) is different than #define a (b)    :(
   949     // #define a(b) is different than #define a (b)    :(
   932     state->report_whitespace = 1;
   950     state->report_whitespace = 1;
   933     lexer(state);
   951     lexer(state);
   934     state->report_whitespace = 0;
   952     state->report_whitespace = 0;
  1427 
  1445 
  1428             case TOKEN_IDENTIFIER:
  1446             case TOKEN_IDENTIFIER:
  1429                 if (handle_pp_identifier(ctx))
  1447                 if (handle_pp_identifier(ctx))
  1430                     continue;  // go again with new IncludeState.
  1448                     continue;  // go again with new IncludeState.
  1431 
  1449 
       
  1450                 if ( (state->tokenlen == 7) &&
       
  1451                      (memcmp(state->token, "defined", 7) == 0) )
       
  1452                 {
       
  1453                     token = lexer(state);
       
  1454                     const int paren = (token == ((Token) '('));
       
  1455                     if (paren)  // gcc doesn't let us nest parens here, either.
       
  1456                         token = lexer(state);
       
  1457                     if (token != TOKEN_IDENTIFIER)
       
  1458                     {
       
  1459                         fail(ctx, "operator 'defined' requires an identifier");
       
  1460                         return -1;
       
  1461                     } // if
       
  1462                     const int found = (find_define_by_token(ctx) != NULL);
       
  1463 
       
  1464                     if (paren)
       
  1465                     {
       
  1466                         if (lexer(state) != ((Token) ')'))
       
  1467                         {
       
  1468                             fail(ctx, "Unmatched ')'");
       
  1469                             return -1;
       
  1470                         } // if
       
  1471                     } // if
       
  1472 
       
  1473                     ADD_TO_OUTPUT(0, found);
       
  1474                     continue;
       
  1475                 } // if
       
  1476 
  1432                 // can't replace identifier with a number? It becomes zero.
  1477                 // can't replace identifier with a number? It becomes zero.
  1433                 token = TOKEN_INT_LITERAL;
  1478                 token = TOKEN_INT_LITERAL;
  1434                 ADD_TO_OUTPUT(0, 0);
  1479                 ADD_TO_OUTPUT(0, 0);
  1435                 break;
  1480                 break;
  1436 
  1481