Implemented -D for preprocessor

This commit is contained in:
Dale Weiler 2012-12-17 14:58:40 +00:00
parent eab1050ba4
commit c1c7a93884
3 changed files with 56 additions and 22 deletions

52
ftepp.c
View file

@ -1366,12 +1366,23 @@ bool ftepp_preprocess_string(const char *name, const char *str)
return ftepp_preprocess_done();
}
void ftepp_add_macro(const char *name, const char *value) {
char *create = NULL;
vec_upload(create, "#define ", 8);
vec_upload(create, name, strlen(name));
vec_push (create, ' ');
vec_upload(create, value, strlen(value));
vec_push (create, 0);
ftepp_preprocess_string("__builtin__", create);
vec_free (create);
}
bool ftepp_init()
{
char minor[32];
char major[32];
char *verminor = NULL;
char *vermajor = NULL;
ftepp = ftepp_new();
if (!ftepp)
@ -1385,33 +1396,32 @@ bool ftepp_init()
if (opts.standard == COMPILER_FTEQCC) {
ftepp_add_define(NULL, "__STD_FTEQCC__");
/* 1.00 */
major[0] = '1';
minor[0] = '0';
major[0] = '"';
major[1] = '1';
major[2] = '"';
minor[0] = '"';
minor[1] = '0';
minor[2] = '"';
} else if (opts.standard == COMPILER_GMQCC) {
ftepp_add_define(NULL, "__STD_GMQCC__");
sprintf(major, "%d", GMQCC_VERSION_MAJOR);
sprintf(minor, "%d", GMQCC_VERSION_MINOR);
sprintf(major, "\"%d\"", GMQCC_VERSION_MAJOR);
sprintf(minor, "\"%d\"", GMQCC_VERSION_MINOR);
} else if (opts.standard == COMPILER_QCC) {
ftepp_add_define(NULL, "__STD_QCC__");
/* 1.0 */
major[0] = '1';
minor[0] = '0';
major[0] = '"';
major[1] = '1';
major[2] = '"';
minor[0] = '"';
minor[1] = '0';
minor[2] = '"';
}
vec_upload(verminor, "#define __STD_VERSION_MINOR__ \"", 31);
vec_upload(vermajor, "#define __STD_VERSION_MAJOR__ \"", 31);
vec_upload(verminor, minor, strlen(minor));
vec_upload(vermajor, major, strlen(major));
vec_push (verminor, '"');
vec_push (vermajor, '"');
vec_push (verminor, 0);
vec_push (vermajor, 0);
ftepp_add_macro("__STD_VERSION_MINOR__", minor);
ftepp_add_macro("__STD_VERSION_MAJOR__", major);
ftepp_preprocess_string("__builtin__", verminor);
ftepp_preprocess_string("__builtin__", vermajor);
vec_free(verminor);
vec_free(vermajor);
return true;
}

View file

@ -845,6 +845,7 @@ void ftepp_finish ();
const char *ftepp_get ();
void ftepp_flush ();
void ftepp_add_define (const char *source, const char *name);
void ftepp_add_macro (const char *name, const char *value);
/*===================================================================*/
/*======================= main.c commandline ========================*/

25
main.c
View file

@ -34,8 +34,10 @@ cmd_options opts;
const oper_info *operators = NULL;
size_t operator_count = 0;
typedef struct { char *filename; int type; } argitem;
typedef struct { char *filename; int type; } argitem;
typedef struct { char *name; char *value; } ppitem;
static argitem *items = NULL;
static ppitem *ppems = NULL;
#define TYPE_QC 0
#define TYPE_ASM 1
@ -187,6 +189,7 @@ static bool options_parse(int argc, char **argv) {
while (!argend && argc > 1) {
char *argarg;
argitem item;
ppitem macro;
++argv;
--argc;
@ -294,6 +297,17 @@ static bool options_parse(int argc, char **argv) {
options_setflag("LNO", true);
break;
case 'D':
if (!(argarg = strchr(argv[0] + 2, '='))) {
con_out("missing = in -D\n");
exit(0);
}
*argarg='\0'; /* terminate for name */
macro.name = util_strdup(argarg);
macro.value = util_strdup(argv[0]+2);
vec_push(ppems, macro);
break;
/* handle all -fflags */
case 'f':
util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
@ -587,11 +601,19 @@ int main(int argc, char **argv) {
goto cleanup;
}
}
if (opts.pp_only || OPTS_FLAG(FTEPP)) {
if (!ftepp_init()) {
con_err("failed to initialize parser\n");
retval = 1;
goto cleanup;
} else {
size_t i;
for (i = 0; i < vec_size(ppems); ++i) {
ftepp_add_macro(ppems[i].name, ppems[i].value);
mem_d(ppems[i].name);
mem_d(ppems[i].value);
}
}
}
@ -720,6 +742,7 @@ cleanup:
ftepp_finish();
con_close();
vec_free(items);
vec_free(ppems);
if (!opts.pp_only)
parser_cleanup();