pptoken **output;
} ppmacro;
-typedef struct ftepp_s {
+typedef struct gmqcc_preprocess_s {
lex_file *lex;
int token;
unsigned int errors;
static uint32_t ftepp_predef_randval = 0;
/* __DATE__ */
-char *ftepp_predef_date(lex_file *context) {
+static char *ftepp_predef_date(lex_file *context) {
struct tm *itime = NULL;
time_t rtime;
char *value = (char*)mem_a(82);
}
/* __TIME__ */
-char *ftepp_predef_time(lex_file *context) {
+static char *ftepp_predef_time(lex_file *context) {
struct tm *itime = NULL;
time_t rtime;
char *value = (char*)mem_a(82);
}
/* __LINE__ */
-char *ftepp_predef_line(lex_file *context) {
+static char *ftepp_predef_line(lex_file *context) {
char *value;
util_asprintf(&value, "%d", (int)context->line);
return value;
}
/* __FILE__ */
-char *ftepp_predef_file(lex_file *context) {
+static char *ftepp_predef_file(lex_file *context) {
size_t length = strlen(context->name) + 3; /* two quotes and a terminator */
char *value = (char*)mem_a(length);
util_snprintf(value, length, "\"%s\"", context->name);
return value;
}
/* __COUNTER__ */
-char *ftepp_predef_counter(lex_file *context) {
+static char *ftepp_predef_counter(lex_file *context) {
char *value;
ftepp_predef_countval ++;
util_asprintf(&value, "%u", ftepp_predef_countval);
return value;
}
/* __RANDOM__ */
-char *ftepp_predef_random(lex_file *context) {
+static char *ftepp_predef_random(lex_file *context) {
char *value;
ftepp_predef_randval = (util_rand() % 0xFF) + 1;
util_asprintf(&value, "%u", ftepp_predef_randval);
return value;
}
/* __RANDOM_LAST__ */
-char *ftepp_predef_randomlast(lex_file *context) {
+static char *ftepp_predef_randomlast(lex_file *context) {
char *value;
util_asprintf(&value, "%u", ftepp_predef_randval);
return value;
}
/* __TIMESTAMP__ */
-char *ftepp_predef_timestamp(lex_file *context) {
+static char *ftepp_predef_timestamp(lex_file *context) {
struct stat finfo;
char *find;
char *value;
return retval;
}
-bool ftepp_preprocess_file(ftepp_t *ftepp, const char *filename)
+void gmqcc_preprocess_adddefine(ftepp_t *ftepp, const char *source, const char *name);
+
+bool gmqcc_preprocess_file(ftepp_t *ftepp, const char *filename)
{
ftepp->lex = lex_open(filename);
ftepp->itemname = util_strdup(filename);
return ftepp_preprocess_done(ftepp);
}
-bool ftepp_preprocess_string(ftepp_t *ftepp, const char *name, const char *str)
+bool gmqcc_preprocess_string(ftepp_t *ftepp, const char *name, const char *str)
{
- ftepp->lex = lex_open_string(str, strlen(str), name);
+ ftepp->lex = lex_open_string(str, strlen(str), name);
ftepp->itemname = util_strdup(name);
if (!ftepp->lex) {
con_out("failed to create lexer for string \"%s\"\n", name);
}
-void ftepp_add_macro(ftepp_t *ftepp, const char *name, const char *value) {
+void gmqcc_preprocess_addmacro(ftepp_t *ftepp, const char *name, const char *value) {
char *create = NULL;
/* use saner path for empty macros */
if (!value) {
- ftepp_add_define(ftepp, "__builtin__", name);
+ gmqcc_preprocess_adddefine(ftepp, "__builtin__", name);
return;
}
vec_upload(create, value, strlen(value));
vec_push (create, 0);
- ftepp_preprocess_string(ftepp, "__builtin__", create);
+ gmqcc_preprocess_string(ftepp, "__builtin__", create);
vec_free (create);
}
-ftepp_t *ftepp_create()
+ftepp_t *gmqcc_preprocess_create(void)
{
ftepp_t *ftepp;
char minor[32];
memset(major, 0, sizeof(major));
/* set the right macro based on the selected standard */
- ftepp_add_define(ftepp, NULL, "GMQCC");
+ gmqcc_preprocess_adddefine(ftepp, NULL, "GMQCC");
if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_FTEQCC) {
- ftepp_add_define(ftepp, NULL, "__STD_FTEQCC__");
+ gmqcc_preprocess_adddefine(ftepp, NULL, "__STD_FTEQCC__");
/* 1.00 */
major[0] = '"';
major[1] = '1';
minor[1] = '0';
minor[2] = '"';
} else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_GMQCC) {
- ftepp_add_define(ftepp, NULL, "__STD_GMQCC__");
+ gmqcc_preprocess_adddefine(ftepp, NULL, "__STD_GMQCC__");
util_snprintf(major, 32, "\"%d\"", GMQCC_VERSION_MAJOR);
util_snprintf(minor, 32, "\"%d\"", GMQCC_VERSION_MINOR);
} else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCCX) {
- ftepp_add_define(ftepp, NULL, "__STD_QCCX__");
+ gmqcc_preprocess_adddefine(ftepp, NULL, "__STD_QCCX__");
util_snprintf(major, 32, "\"%d\"", GMQCC_VERSION_MAJOR);
util_snprintf(minor, 32, "\"%d\"", GMQCC_VERSION_MINOR);
} else if (OPTS_OPTION_U32(OPTION_STANDARD) == COMPILER_QCC) {
- ftepp_add_define(ftepp, NULL, "__STD_QCC__");
+ gmqcc_preprocess_adddefine(ftepp, NULL, "__STD_QCC__");
/* 1.0 */
major[0] = '"';
major[1] = '1';
minor[2] = '"';
}
- ftepp_add_macro(ftepp, "__STD_VERSION_MINOR__", minor);
- ftepp_add_macro(ftepp, "__STD_VERSION_MAJOR__", major);
+ gmqcc_preprocess_addmacro(ftepp, "__STD_VERSION_MINOR__", minor);
+ gmqcc_preprocess_addmacro(ftepp, "__STD_VERSION_MAJOR__", major);
/*
* We're going to just make __NULL__ nil, which works for 60% of the
* cases of __NULL_ for fteqcc.
*/
- ftepp_add_macro(ftepp, "__NULL__", "nil");
+ gmqcc_preprocess_addmacro(ftepp, "__NULL__", "nil");
return ftepp;
}
-void ftepp_add_define(ftepp_t *ftepp, const char *source, const char *name)
+void gmqcc_preprocess_adddefine(ftepp_t *ftepp, const char *source, const char *name)
{
ppmacro *macro;
lex_ctx ctx = { "__builtin__", 0 };
util_htset(ftepp->macros, name, macro);
}
-const char *ftepp_get(ftepp_t *ftepp)
-{
+const char *gmqcc_preprocess_get(ftepp_t *ftepp) {
return ftepp->output_string;
}
-void ftepp_flush(ftepp_t *ftepp)
-{
+void gmqcc_preprocess_flush(ftepp_t *ftepp) {
ftepp_flush_do(ftepp);
}
-void ftepp_finish(ftepp_t *ftepp)
-{
+void gmqcc_preprocess_destroy(ftepp_t *ftepp) {
if (!ftepp)
return;
ftepp_delete(ftepp);
extern "C" {
#endif /*! __cplusplus */
+/*
+ * Forward declare everything since these are `private` implementations
+ * that the user shouldn't have information of.
+ */
+struct gmqcc_preprocess_s;
+
/*
* Function: gmqcc_global_setmemory
* Set implementations for dynamic memory manipulation.
void (*free_impl) (void *)
);
+/*
+ * Function: gmqcc_preprocess_create
+ * Creates a preprocessor context
+ *
+ * Returns:
+ * Preprocessor context on success, NULL otherwise
+ */
+struct gmqcc_preprocess_s *gmqcc_preprocess_create(void);
+
+/*
+ * Function: gmqcc_preprocess_file
+ * Preprocesses a file for a given preprocessor context
+ *
+ * Parameters:
+ * pp - Pointer to a preprocessor context
+ * filename - Filename as string to be opened and preprocessed
+ *
+ * Returns:
+ * true on success, false otherwise.
+ */
+bool gmqcc_preprocess_file(
+ struct gmqcc_preprocess_s *pp,
+ const char *filename
+);
+
+/*
+ * Function: gmqcc_preprocess_string
+ * Preprocesses a string for a given preprocessor context
+ *
+ * Parameters:
+ * pp - Pointer to a preprocessor context
+ * name - Name for the given string (used in error reporting)
+ * str - String to be preprocessed
+ *
+ * Returns:
+ * true on success, false otherwise.
+ */
+bool gmqcc_preprocess_string (
+ struct gmqcc_preprocess_s *pp,
+ const char *name,
+ const char *str
+);
+
+/*
+ * Function: gmqcc_preprocess_adddefine
+ * Defines a macro for a given preprocessor context
+ *
+ * Parameters:
+ * pp - Pointer to a preprocessor context
+ * ident - Identifier for the given definition (used in error reporting)
+ * name - Name of macro to define
+ */
+void gmqcc_preprocess_adddefine(
+ struct gmqcc_preprocess_s *pp,
+ const char *ident,
+ const char *name
+);
+
+/*
+ * Function: gmqcc_preprocess_addmacro
+ * Defines a macro with a value for a given preprocessor context
+ *
+ * Parameters:
+ * pp - Pointer to a preprocessor context
+ * name - Name of the macro to define
+ * value - Value of the macro
+ */
+void gmqcc_preprocess_addmacro(
+ struct gmqcc_preprocess_s *pp,
+ const char *name,
+ const char *value
+);
+
+/*
+ * Function: gmqcc_preprocess_get
+ * Get preprocessed data as string
+ *
+ * Parameters:
+ * pp - Pointer to preprocessor context
+ *
+ * Returns:
+ * const string of the preprocessed contents.
+ */
+const char *gmqcc_preprocess_get(
+ struct gmqcc_preprocess_s *pp
+);
+
+/*
+ * Function: gmqcc_preprocess_flush
+ * Flush contents to be preprocessed
+ *
+ * Parameters:
+ * pp - Pointer to preprocessor context
+ */
+void gmqcc_preprocess_flush(
+ struct gmqcc_preprocess_s *pp
+);
+
+/*
+ * Function: gmqcc_preprocess_destroy
+ * Destroy preprocessor context
+ *
+ * Parameters:
+ * pp - Pointer to preprocessor context
+ */
+void gmqcc_preprocess_destroy(
+ struct gmqcc_preprocess_s *pp
+);
#ifdef __cplusplus
}
*/
#include "base.h"
#include "lexer.h"
+#include "gmqcc.h"
#include <time.h>
/* TODO: cleanup this whole file .. it's a fuckign mess */
bool progs_src = false;
FILE *outfile = NULL;
struct parser_s *parser = NULL;
- struct ftepp_s *ftepp = NULL;
+
+ struct gmqcc_preprocess_s *ftepp = NULL;
app_name = argv[0];
con_init ();
}
if (OPTS_OPTION_BOOL(OPTION_PP_ONLY) || OPTS_FLAG(FTEPP)) {
- if (!(ftepp = ftepp_create())) {
+ if (!(ftepp = gmqcc_preprocess_create())) {
con_err("failed to initialize parser\n");
retval = 1;
goto cleanup;
/* add macros */
if (OPTS_OPTION_BOOL(OPTION_PP_ONLY) || OPTS_FLAG(FTEPP)) {
for (itr = 0; itr < vec_size(ppems); itr++) {
- ftepp_add_macro(ftepp, ppems[itr].name, ppems[itr].value);
+ gmqcc_preprocess_addmacro(ftepp, ppems[itr].name, ppems[itr].value);
mem_d(ppems[itr].name);
/* can be null */
if (OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
const char *out;
- if (!ftepp_preprocess_file(ftepp, items[itr].filename)) {
+ if (!gmqcc_preprocess_file(ftepp, items[itr].filename)) {
retval = 1;
goto cleanup;
}
- out = ftepp_get(ftepp);
+ out = gmqcc_preprocess_get(ftepp);
if (out)
fs_file_printf(outfile, "%s", out);
- ftepp_flush(ftepp);
+ gmqcc_preprocess_flush(ftepp);
}
else {
if (OPTS_FLAG(FTEPP)) {
const char *data;
- if (!ftepp_preprocess_file(ftepp, items[itr].filename)) {
+ if (!gmqcc_preprocess_file(ftepp, items[itr].filename)) {
retval = 1;
goto cleanup;
}
- data = ftepp_get(ftepp);
+ data = gmqcc_preprocess_get(ftepp);
if (vec_size(data)) {
if (!parser_compile_string(parser, items[itr].filename, data, vec_size(data))) {
retval = 1;
goto cleanup;
}
}
- ftepp_flush(ftepp);
+ gmqcc_preprocess_flush(ftepp);
}
else {
if (!parser_compile_file(parser, items[itr].filename)) {
}
}
- ftepp_finish(ftepp);
+ gmqcc_preprocess_destroy(ftepp);
ftepp = NULL;
if (!OPTS_OPTION_BOOL(OPTION_PP_ONLY)) {
if (!parser_finish(parser, OPTS_OPTION_STR(OPTION_OUTPUT))) {
cleanup:
if (ftepp)
- ftepp_finish(ftepp);
+ gmqcc_preprocess_destroy(ftepp);
con_close();
vec_free(items);
vec_free(ppems);