mojoshader_preprocessor.c
changeset 639 1b549be9ab59
parent 638 e910619c61f4
child 640 813a139a4a5a
equal deleted inserted replaced
638:e910619c61f4 639:1b549be9ab59
   854     state->report_whitespace = 0;
   854     state->report_whitespace = 0;
   855     ctx->isfail = 1;
   855     ctx->isfail = 1;
   856 } // handle_pp_error
   856 } // handle_pp_error
   857 
   857 
   858 
   858 
       
   859 static void handle_pp_define(Context *ctx)
       
   860 {
       
   861     IncludeState *state = ctx->include_stack;
       
   862 
       
   863     if (preprocessor_internal_lexer(state) != TOKEN_IDENTIFIER)
       
   864     {
       
   865         fail(ctx, "Macro names must be indentifiers");
       
   866         return;
       
   867     } // if
       
   868 
       
   869     MOJOSHADER_malloc m = ctx->malloc;
       
   870     void *d = ctx->malloc_data;
       
   871     const char space = ' ';
       
   872     unsigned int len = ((unsigned int) (state->source-state->token));
       
   873     char *sym = (char *) alloca(len);
       
   874     memcpy(sym, state->token, len-1);
       
   875     sym[len-1] = '\0';
       
   876 
       
   877     Buffer buffer;
       
   878     init_buffer(&buffer);
       
   879 
       
   880     int done = 0;
       
   881     const char *source = NULL;
       
   882     unsigned int bytes_left = 0;
       
   883     state->report_whitespace = 1;
       
   884     while ((!done) && (!ctx->out_of_memory))
       
   885     {
       
   886         bytes_left = state->bytes_left;
       
   887         source = state->source;
       
   888         const Token token = preprocessor_internal_lexer(state);
       
   889         switch (token)
       
   890         {
       
   891             case TOKEN_INCOMPLETE_COMMENT:
       
   892             case TOKEN_EOI:
       
   893                 state->bytes_left = bytes_left;
       
   894                 state->source = source;  // move back so we catch this later.
       
   895                 // fall through!
       
   896             case ((Token) '\n'):
       
   897                 done = 1;
       
   898                 break;
       
   899 
       
   900             case ((Token) ' '):  // may not actually point to ' '.
       
   901                 if (!add_to_buffer(&buffer, &space, 1, m, d))
       
   902                     ctx->out_of_memory = 1;
       
   903                 break;
       
   904 
       
   905             default:
       
   906                 len = ((unsigned int) (state->source-state->token));
       
   907                 if (!add_to_buffer(&buffer, state->token, len, m, d))
       
   908                     ctx->out_of_memory = 1;
       
   909                 break;
       
   910         } // switch
       
   911     } // while
       
   912     state->report_whitespace = 0;
       
   913 
       
   914     char *definition = NULL;
       
   915     if (!ctx->out_of_memory)
       
   916     {
       
   917         definition = flatten_buffer(&buffer, m, d);
       
   918         ctx->out_of_memory = (definition == NULL);
       
   919     } // if
       
   920 
       
   921     free_buffer(&buffer, ctx->free, d);
       
   922 
       
   923     if (ctx->out_of_memory)
       
   924         return;
       
   925 
       
   926     assert(done);
       
   927     add_define(ctx, sym, definition, 0);
       
   928 } // handle_pp_define
       
   929 
       
   930 
   859 static void handle_pp_undef(Context *ctx)
   931 static void handle_pp_undef(Context *ctx)
   860 {
   932 {
   861     IncludeState *state = ctx->include_stack;
   933     IncludeState *state = ctx->include_stack;
   862 
   934 
   863     if (preprocessor_internal_lexer(state) != TOKEN_IDENTIFIER)
   935     if (preprocessor_internal_lexer(state) != TOKEN_IDENTIFIER)
  1030             *_len = 0;
  1102             *_len = 0;
  1031             return NULL;  // we're done!
  1103             return NULL;  // we're done!
  1032         } // if
  1104         } // if
  1033 
  1105 
  1034         // !!! FIXME: todo.
  1106         // !!! FIXME: todo.
  1035         // TOKEN_PP_DEFINE,
       
  1036         // TOKEN_PP_IF,
  1107         // TOKEN_PP_IF,
  1037         // TOKEN_PP_ELIF,
  1108         // TOKEN_PP_ELIF,
  1038 
  1109 
  1039         const Conditional *cond = state->conditional_stack;
  1110         const Conditional *cond = state->conditional_stack;
  1040         const int skipping = ((cond != NULL) && (cond->skipping));
  1111         const int skipping = ((cond != NULL) && (cond->skipping));
  1100         } // else if
  1171         } // else if
  1101 
  1172 
  1102         else if (token == TOKEN_PP_ERROR)
  1173         else if (token == TOKEN_PP_ERROR)
  1103         {
  1174         {
  1104             handle_pp_error(ctx);
  1175             handle_pp_error(ctx);
       
  1176             continue;  // will return at top of loop.
       
  1177         } // else if
       
  1178 
       
  1179         else if (token == TOKEN_PP_DEFINE)
       
  1180         {
       
  1181             handle_pp_define(ctx);
  1105             continue;  // will return at top of loop.
  1182             continue;  // will return at top of loop.
  1106         } // else if
  1183         } // else if
  1107 
  1184 
  1108         else if (token == TOKEN_PP_UNDEF)
  1185         else if (token == TOKEN_PP_UNDEF)
  1109         {
  1186         {