Skip to content

Commit

Permalink
Handle basic macro replacement in the preprocessor.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Feb 17, 2009
1 parent 90cd268 commit 38646fe
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions mojoshader_preprocessor.c
Expand Up @@ -1060,6 +1060,28 @@ static void handle_pp_endif(Context *ctx)
} // handle_pp_endif


static int handle_pp_identifier(Context *ctx)
{
IncludeState *state = ctx->include_stack;
const unsigned int len = ((unsigned int) (state->source-state->token));
char *sym = (char *) alloca(len+1);
memcpy(sym, state->token, len);
sym[len] = '\0';

const char *def = find_define(ctx, sym);
if (def == NULL)
return 0; // just send the token through unchanged.

if (!push_source(ctx, state->filename, def, strlen(def), state->line, 0))
{
assert(ctx->out_of_memory);
return 0;
} // if

return 1;
} // handle_pp_identifier


static void unterminated_pp_condition(Context *ctx)
{
IncludeState *state = ctx->include_stack;
Expand Down Expand Up @@ -1192,6 +1214,12 @@ static inline const char *_preprocessor_nexttoken(Preprocessor *_ctx,
continue; // will return at top of loop.
} // else if

else if (token == TOKEN_IDENTIFIER)
{
if (handle_pp_identifier(ctx))
continue; // pushed the include_stack.
} // else if

assert(!skipping);
*_token = token;
*_len = (unsigned int) (state->source - state->token);
Expand Down

0 comments on commit 38646fe

Please sign in to comment.