mojoshader_preprocessor.c
changeset 655 0b82fdeebf86
parent 654 9210bd7eb2e2
child 656 ddb1a0b4c443
equal deleted inserted replaced
654:9210bd7eb2e2 655:0b82fdeebf86
    27 #define preprocessor_lexer(s) debug_preprocessor_lexer(s)
    27 #define preprocessor_lexer(s) debug_preprocessor_lexer(s)
    28 #endif
    28 #endif
    29 
    29 
    30 typedef struct DefineHash
    30 typedef struct DefineHash
    31 {
    31 {
    32     MOJOSHADER_preprocessorDefine define;
    32     const char *identifier;
       
    33     const char *definition;
    33     struct DefineHash *next;
    34     struct DefineHash *next;
    34 } DefineHash;
    35 } DefineHash;
    35 
    36 
    36 
    37 
    37 // Simple linked list to cache source filenames, so we don't have to copy
    38 // Simple linked list to cache source filenames, so we don't have to copy
   390     char *definition = NULL;
   391     char *definition = NULL;
   391     const unsigned char hash = hash_define(sym);
   392     const unsigned char hash = hash_define(sym);
   392     DefineHash *bucket = ctx->define_hashtable[hash];
   393     DefineHash *bucket = ctx->define_hashtable[hash];
   393     while (bucket)
   394     while (bucket)
   394     {
   395     {
   395         if (strcmp(bucket->define.identifier, sym) == 0)
   396         if (strcmp(bucket->identifier, sym) == 0)
   396         {
   397         {
   397             failf(ctx, "'%s' already defined", sym);
   398             failf(ctx, "'%s' already defined", sym);
   398             return 0;
   399             return 0;
   399         } // if
   400         } // if
   400         bucket = bucket->next;
   401         bucket = bucket->next;
   410         put_define(ctx, bucket);
   411         put_define(ctx, bucket);
   411         return 0;
   412         return 0;
   412     } // if
   413     } // if
   413 
   414 
   414     if (!copy)
   415     if (!copy)
   415         bucket->define.definition = val;
   416         bucket->definition = val;
   416     else
   417     else
   417     {
   418     {
   418         definition = StrDup(ctx, val);
   419         definition = StrDup(ctx, val);
   419         if (definition == NULL)
   420         if (definition == NULL)
   420         {
   421         {
   421             Free(ctx, identifier);
   422             Free(ctx, identifier);
   422             put_define(ctx, bucket);
   423             put_define(ctx, bucket);
   423             return 0;
   424             return 0;
   424         } // if
   425         } // if
   425         bucket->define.definition = definition;
   426         bucket->definition = definition;
   426     } // if
   427     } // if
   427 
   428 
   428     bucket->define.identifier = identifier;
   429     bucket->identifier = identifier;
   429     bucket->next = ctx->define_hashtable[hash];
   430     bucket->next = ctx->define_hashtable[hash];
   430     ctx->define_hashtable[hash] = bucket;
   431     ctx->define_hashtable[hash] = bucket;
   431     return 1;
   432     return 1;
   432 } // add_define
   433 } // add_define
   433 
   434 
   437     const unsigned char hash = hash_define(sym);
   438     const unsigned char hash = hash_define(sym);
   438     DefineHash *bucket = ctx->define_hashtable[hash];
   439     DefineHash *bucket = ctx->define_hashtable[hash];
   439     DefineHash *prev = NULL;
   440     DefineHash *prev = NULL;
   440     while (bucket)
   441     while (bucket)
   441     {
   442     {
   442         if (strcmp(bucket->define.identifier, sym) == 0)
   443         if (strcmp(bucket->identifier, sym) == 0)
   443         {
   444         {
   444             if (prev == NULL)
   445             if (prev == NULL)
   445                 ctx->define_hashtable[hash] = bucket->next;
   446                 ctx->define_hashtable[hash] = bucket->next;
   446             else
   447             else
   447                 prev->next = bucket->next;
   448                 prev->next = bucket->next;
   448             Free(ctx, (void *) bucket->define.identifier);
   449             Free(ctx, (void *) bucket->identifier);
   449             Free(ctx, (void *) bucket->define.definition);
   450             Free(ctx, (void *) bucket->definition);
   450             put_define(ctx, bucket);
   451             put_define(ctx, bucket);
   451             return 1;
   452             return 1;
   452         } // if
   453         } // if
   453         prev = bucket;
   454         prev = bucket;
   454         bucket = bucket->next;
   455         bucket = bucket->next;
   462 {
   463 {
   463     const unsigned char hash = hash_define(sym);
   464     const unsigned char hash = hash_define(sym);
   464     DefineHash *bucket = ctx->define_hashtable[hash];
   465     DefineHash *bucket = ctx->define_hashtable[hash];
   465     while (bucket)
   466     while (bucket)
   466     {
   467     {
   467         if (strcmp(bucket->define.identifier, sym) == 0)
   468         if (strcmp(bucket->identifier, sym) == 0)
   468             return bucket->define.definition;
   469             return bucket->definition;
   469         bucket = bucket->next;
   470         bucket = bucket->next;
   470     } // while
   471     } // while
   471     return NULL;
   472     return NULL;
   472 } // find_define
   473 } // find_define
   473 
   474 
   480         DefineHash *bucket = ctx->define_hashtable[i];
   481         DefineHash *bucket = ctx->define_hashtable[i];
   481         ctx->define_hashtable[i] = NULL;
   482         ctx->define_hashtable[i] = NULL;
   482         while (bucket)
   483         while (bucket)
   483         {
   484         {
   484             DefineHash *next = bucket->next;
   485             DefineHash *next = bucket->next;
   485             Free(ctx, (void *) bucket->define.identifier);
   486             Free(ctx, (void *) bucket->identifier);
   486             Free(ctx, (void *) bucket->define.definition);
   487             Free(ctx, (void *) bucket->definition);
   487             put_define(ctx, bucket);
   488             put_define(ctx, bucket);
   488             bucket = next;
   489             bucket = next;
   489         } // while
   490         } // while
   490     } // for
   491     } // for
   491 } // find_define
   492 } // find_define