less globals

This commit is contained in:
Dale Weiler 2013-04-25 12:08:13 +00:00
parent 785ab7c072
commit 9fee84f250
3 changed files with 39 additions and 26 deletions

45
ftepp.c
View file

@ -65,8 +65,7 @@ typedef struct ftepp_s {
bool output_on;
ppcondition *conditions;
/*ppmacro **macros;*/
ht macros; /* hashtable<string, ppmacro*> */
ht macros; /* hashtable<string, ppmacro*> */
char *output_string;
char *itemname;
@ -198,7 +197,12 @@ char *ftepp_predef_timestamp(lex_file *context) {
return value;
}
const ftepp_predef_t ftepp_predefs[FTEPP_PREDEF_COUNT] = {
typedef struct {
const char *name;
char *(*func)(lex_file *);
} ftepp_predef_t;
static const ftepp_predef_t ftepp_predefs[] = {
{ "__LINE__", &ftepp_predef_line },
{ "__FILE__", &ftepp_predef_file },
{ "__COUNTER__", &ftepp_predef_counter },
@ -210,6 +214,25 @@ const ftepp_predef_t ftepp_predefs[FTEPP_PREDEF_COUNT] = {
{ "__TIME_STAMP__", &ftepp_predef_timestamp }
};
static GMQCC_INLINE int ftepp_predef_index(const char *name) {
/* no hashtable here, we simply check for one to exist the naive way */
int i;
for(i = 0; i < (int)(sizeof(ftepp_predefs)/sizeof(*ftepp_predefs)); i++)
if (!strcmp(ftepp_predefs[i].name, name))
return i;
return -1;
}
bool ftepp_predef_exists(const char *name) {
return ftepp_predef_index(name) != -1;
}
/* singleton because we're allowed */
static GMQCC_INLINE char *(*ftepp_predef(const char *name))(lex_file *context) {
int i = ftepp_predef_index(name);
return (i != -1) ? ftepp_predefs[i].func : NULL;
}
#define ftepp_tokval(f) ((f)->lex->tok.value)
#define ftepp_ctx(f) ((f)->lex->tok.ctx)
@ -1652,7 +1675,6 @@ static bool ftepp_preprocess(ftepp_t *ftepp)
/* predef stuff */
char *expand = NULL;
size_t i;
ftepp->lex->flags.preprocessing = true;
ftepp->lex->flags.mergelines = false;
@ -1673,15 +1695,14 @@ static bool ftepp_preprocess(ftepp_t *ftepp)
case TOKEN_TYPENAME:
/* is it a predef? */
if (OPTS_FLAG(FTEPP_PREDEFS)) {
for (i = 0; i < sizeof(ftepp_predefs) / sizeof (*ftepp_predefs); i++) {
if (!strcmp(ftepp_predefs[i].name, ftepp_tokval(ftepp))) {
expand = ftepp_predefs[i].func(ftepp->lex);
ftepp_out(ftepp, expand, false);
ftepp_next(ftepp); /* skip */
char *(*predef)(lex_file*) = ftepp_predef(ftepp_tokval(ftepp));
if (predef) {
expand = predef(ftepp->lex);
ftepp_out (ftepp, expand, false);
ftepp_next(ftepp);
mem_d(expand); /* free memory */
break;
}
mem_d(expand);
break;
}
}

View file

@ -1011,11 +1011,6 @@ void parser_cleanup (struct parser_s *parser);
struct lex_file_s;
struct ftepp_s;
typedef struct {
const char *name;
char *(*func)(struct lex_file_s *);
} ftepp_predef_t;
/*
* line, file, counter, counter_last, random, random_last, date, time
* time_stamp.
@ -1033,8 +1028,6 @@ void ftepp_flush (struct ftepp_s *ftepp);
void ftepp_add_define (struct ftepp_s *ftepp, const char *source, const char *name);
void ftepp_add_macro (struct ftepp_s *ftepp, const char *name, const char *value);
extern const ftepp_predef_t ftepp_predefs[FTEPP_PREDEF_COUNT];
/*===================================================================*/
/*======================= main.c commandline ========================*/
/*===================================================================*/

View file

@ -1788,6 +1788,9 @@ static ast_expression* parse_vararg(parser_t *parser)
return out;
}
/* not to be exposed */
extern bool ftepp_predef_exists(const char *name);
static bool parse_sya_operand(parser_t *parser, shunt *sy, bool with_labels)
{
if (OPTS_FLAG(TRANSLATABLE_STRINGS) &&
@ -1918,13 +1921,9 @@ static bool parse_sya_operand(parser_t *parser, shunt *sy, bool with_labels)
* i've done this thousands of times already myself. Lets check for
* it in the predef table. And diagnose it better :)
*/
if (!OPTS_FLAG(FTEPP_PREDEFS)) {
for (i = 0; i < sizeof(ftepp_predefs)/sizeof(*ftepp_predefs); i++) {
if (!strcmp(ftepp_predefs[i].name, parser_tokval(parser))) {
parseerror(parser, "unexpected ident: %s (use -fftepp-predef to enable pre-defined macros)", parser_tokval(parser));
return false;
}
}
if (!OPTS_FLAG(FTEPP_PREDEFS) && ftepp_predef_exists(parser_tokval(parser))) {
parseerror(parser, "unexpected ident: %s (use -fftepp-predef to enable pre-defined macros)", parser_tokval(parser));
return false;
}
/*