From 72053e16d23b7e45496596bc31a777aa3344cefa Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Sat, 28 Apr 2012 05:35:32 -0400 Subject: [PATCH] Interface change --- gmqcc.h | 9 +++++---- lex.c | 37 ++++++++++++++++++++++--------------- main.c | 12 ++++++------ parse.c | 2 +- util.c | 12 ++++++------ 5 files changed, 40 insertions(+), 32 deletions(-) diff --git a/gmqcc.h b/gmqcc.h index cbe195a..7199a63 100644 --- a/gmqcc.h +++ b/gmqcc.h @@ -167,8 +167,9 @@ enum { int lex_token (lex_file *); void lex_reset (lex_file *); void lex_close (lex_file *); -lex_file *lex_include(lex_file *, char *); -lex_file *lex_open (FILE *); +void lex_parse (lex_file *); +lex_file *lex_include(lex_file *, const char *); +void lex_init (const char *, lex_file **); //=================================================================== //========================== error.c ================================ @@ -208,8 +209,8 @@ void util_meminfo (); bool util_strupper (const char *); bool util_strdigit (const char *); char *util_strdup (const char *); -char *util_strrq (char *); -char *util_strrnl (char *); +char *util_strrq (const char *); +char *util_strrnl (const char *); char *util_strsws (const char *); char *util_strchp (const char *, const char *); void util_debug (const char *, const char *, ...); diff --git a/lex.c b/lex.c index df75d44..eb18ee1 100644 --- a/lex.c +++ b/lex.c @@ -32,12 +32,17 @@ static const char *const lex_keywords[] = { "for", "typedef" }; -lex_file *lex_open(FILE *fp) { +void lex_init(const char *file, lex_file **set) { lex_file *lex = mem_a(sizeof(lex_file)); - if (!lex || !fp) - return NULL; - - lex->file = fp; + if (!lex) + return; + + lex->file = fopen(file, "r"); + if (!lex->file) { + mem_d(lex); + return; + } + fseek(lex->file, 0, SEEK_END); lex->length = ftell(lex->file); lex->size = lex->length; /* copy, this is never changed */ @@ -46,7 +51,7 @@ lex_file *lex_open(FILE *fp) { lex->line = 0; memset(lex->peek, 0, sizeof(lex->peek)); - return lex; + *set = lex; } void lex_close(lex_file *file) { @@ -333,23 +338,25 @@ void lex_reset(lex_file *file) { memset(file->lastok, 0, sizeof(file->lastok)); } +void lex_parse(lex_file *file) { + if (!file) return; + parse_gen(file); /* run parser */ +} + /* * Include a file into the lexer / parsing process: This really * should check if names are the same to prevent endless include * recrusion. */ -lex_file *lex_include(lex_file *lex, char *file) { +lex_file *lex_include(lex_file *lex, const char *file) { util_strrq(file); if (strncmp(lex->name, file, strlen(lex->name)) == 0) { error(lex, ERROR_LEX, "Source file cannot include itself\n"); exit (-1); } - - FILE *fp = fopen(file, "r"); - if (!fp) { - error(lex, ERROR_LEX, "Include file `%s` doesn't exist\n", file); - exit (-1); - } - - return lex_open(fp); + + lex_file *set = NULL; + lex_init(file, &set); + + return set; } diff --git a/main.c b/main.c index 7c4ae87..475053e 100644 --- a/main.c +++ b/main.c @@ -58,9 +58,10 @@ static const int usage(const char *const app) { } int main(int argc, char **argv) { - size_t itr = 0; - char *app = &argv[0][0]; - FILE *fpp = NULL; + size_t itr = 0; + char *app = &argv[0][0]; + FILE *fpp = NULL; + lex_file *lex = NULL; /* * Parse all command line arguments. This is rather annoying to do @@ -137,9 +138,8 @@ int main(int argc, char **argv) { for (; itr < items_elements; itr++) { switch (items_data[itr].type) { case 0: - fpp = fopen(items_data[itr].name, "r"); - lex_file *lex = lex_open(fpp); - parse_gen(lex); + lex_init (items_data[itr].name, &lex); + lex_parse(lex); lex_close(lex); break; case 1: diff --git a/parse.c b/parse.c index 0acc293..4b4f1e6 100644 --- a/parse.c +++ b/parse.c @@ -263,7 +263,7 @@ int parse_gen(lex_file *file) { return error(file, ERROR_PARSE, "Invalid use of include preprocessor directive: wanted #include \"file.h\"\n"); char *copy = util_strdup(file->lastok); - lex_file *next = lex_include(file, copy); + lex_file *next = lex_include(file, copy); if (!next) { error(file, ERROR_INTERNAL, "Include subsystem failure\n"); diff --git a/util.c b/util.c index 942f661..95736e1 100644 --- a/util.c +++ b/util.c @@ -102,9 +102,9 @@ char *util_strdup(const char *s) { * 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 *util_strrq(const char *s) { + char *dst = (char*)s; + char *src = (char*)s; char chr; while ((chr = *src++) != '\0') { if (chr == '\\') { @@ -141,14 +141,14 @@ char *util_strchp(const char *s, const char *e) { * done pointer wise instead of strlen(), and an array * access. */ -char *util_strrnl(char *src) { +char *util_strrnl(const char *src) { if (!src) return NULL; - char *cpy = src; + char *cpy = (char*)src; while (*cpy && *cpy != '\n') cpy++; *cpy = '\0'; - return src; + return (char*)src; } /* -- 2.39.2