]> git.xonotic.org Git - xonotic/gmqcc.git/commitdiff
Interface change
authorDale Weiler <killfieldengine@gmail.com>
Sat, 28 Apr 2012 09:35:32 +0000 (05:35 -0400)
committerDale Weiler <killfieldengine@gmail.com>
Sat, 28 Apr 2012 09:35:32 +0000 (05:35 -0400)
gmqcc.h
lex.c
main.c
parse.c
util.c

diff --git a/gmqcc.h b/gmqcc.h
index cbe195a77a0dd61504b3dca6f978897011439ab8..7199a63d2b2cc826901a939b657dcb7db7bc5d48 100644 (file)
--- 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 df75d44a6d5af61da255369853f6107d3c05bfd1..eb18ee1e30776676db6718b3c596f135c76735b2 100644 (file)
--- 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 7c4ae875a7e6f662aaad043896927343ceab2a6a..475053e701282687f30434bc851d780a0fe579a3 100644 (file)
--- 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 0acc293cf8b1f2fd1d32b1afda9b8a8e8d48bc13..4b4f1e62a233cd051048a9bd331d23a7134af11f 100644 (file)
--- 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 942f6617dd35a0964352504d2d2429dbb49ae3ca..95736e133f9221b42868d6860144b94c05fa6248 100644 (file)
--- 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;
 }
 
 /*