This commit is contained in:
Dale Weiler 2012-04-15 17:47:14 -04:00
parent 29a3c02c35
commit 98b0e11890
4 changed files with 45 additions and 20 deletions

View file

@ -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)

21
lex.c
View file

@ -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);
}

20
parse.c
View file

@ -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;
}

22
util.c
View file

@ -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);