From 98b0e1189074cbc734c166dc3d514f7eb8a7896b Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Sun, 15 Apr 2012 17:47:14 -0400 Subject: [PATCH] Cleanups --- gmqcc.h | 2 ++ lex.c | 21 +-------------------- parse.c | 20 ++++++++++++++++++++ util.c | 22 ++++++++++++++++++++++ 4 files changed, 45 insertions(+), 20 deletions(-) diff --git a/gmqcc.h b/gmqcc.h index d201c78..665d8c9 100644 --- a/gmqcc.h +++ b/gmqcc.h @@ -116,6 +116,8 @@ int typedef_add (const char *, const char *); void *util_memory_a(unsigned int, unsigned int, const char *); void util_memory_d(void *, unsigned int, const char *); char *util_strdup (const char *); +char *util_strrq (char *); +void util_debug (const char *, const char *, ...); #ifdef NOTRACK # define mem_a(x) malloc(x) diff --git a/lex.c b/lex.c index bedaaa9..560a1ea 100644 --- a/lex.c +++ b/lex.c @@ -344,22 +344,7 @@ void lex_reset(struct lex_file *file) { * recrusion. */ struct lex_file *lex_include(struct lex_file *lex, char *file) { - char *find = (char*)file; - /* - * strip for "" (quotes) .. they might be part of the current - * thing. Or possibly not. - */ - if (*find == '"') { - find++; - file++; - while (*find != '"' && *find != '\0') - find++; - - /* strip end "" (quotes) .. if they're actually there */ - if (*find != '\0') - *find = '\0'; - } - + char *find = util_strrq(file); /* * Dissallow recrusive include: this could easily cause some breakage * and instant OOM. @@ -375,9 +360,5 @@ struct lex_file *lex_include(struct lex_file *lex, char *file) { exit (-1); } - /* must free strdup */ - file --; - mem_d (file); - return lex_open(fp); } diff --git a/parse.c b/parse.c index cc228ed..3cbde9e 100644 --- a/parse.c +++ b/parse.c @@ -35,6 +35,19 @@ typedef struct { } constant; VECTOR_MAKE(constant, compile_constants); +void compile_constant_debug() { + int iter = 0; + for(; iter < compile_constants_elements; iter++) { + constant *c = &compile_constants_data[iter]; + switch(c->type) { + case TYPE_FLOAT: printf("constant: %s FLOAT %f\n", c->name, c->value[0]); break; + case TYPE_VECTOR: printf("constant: %s VECTOR {%f,%f,%f}\n",c->name, c->value[0], c->value[1], c->value[2]); break; + case TYPE_STRING: printf("constant: %s STRING %s\n", c->name, c->string); break; + case TYPE_VOID: printf("constant: %s VOID %s\n", c->name, c->string); break; + } + } +} + /* * Generates a parse tree out of the lexees generated by the lexer. This * is where the tree is built. This is where valid check is performed. @@ -264,6 +277,12 @@ int parse_gen(struct lex_file *file) { error(ERROR_INTERNAL, "Include subsystem failure\n"); exit (-1); } + compile_constants_add((constant) { + .name = "#include", + .type = TYPE_VOID, + .value = {0,0,0}, + .string = copy + }); parse_gen(next); mem_d (copy); lex_close(next); @@ -278,6 +297,7 @@ int parse_gen(struct lex_file *file) { break; } } + compile_constant_debug(); lex_reset(file); return 1; } diff --git a/util.c b/util.c index 76bd448..b6264cc 100644 --- a/util.c +++ b/util.c @@ -81,6 +81,28 @@ char *util_strdup(const char *s) { return ptr; } +/* + * Removed quotes from a string, escapes from \ in string + * as well. This function shouldn't be used to create a + * char array that is later freed (it uses pointer arith) + */ +char *util_strrq(char *s) { + char *dst = s; + char *src = s; + char chr; + while ((chr = *src++) != '\0') { + if (chr == '\\') { + *dst++ = chr; + if ((chr = *src++) == '\0') + break; + *dst++ = chr; + } else if (chr != '"') + *dst++ = chr; + } + *dst = '\0'; + return dst; +} + void util_debug(const char *area, const char *ms, ...) { va_list va; va_start(va, ms); -- 2.39.2