]> git.xonotic.org Git - xonotic/gmqcc.git/commitdiff
Include now works
authorDale Weiler <killfieldengine@gmail.com>
Sat, 14 Apr 2012 07:53:20 +0000 (03:53 -0400)
committerDale Weiler <killfieldengine@gmail.com>
Sat, 14 Apr 2012 07:53:20 +0000 (03:53 -0400)
gmqcc.h
lex.c
parse.c
test/include.qc
test/types.qc
test/vector.qc

diff --git a/gmqcc.h b/gmqcc.h
index e8e3df055fee3c2ea6166f4a907a8bf2ba29a548..409762b6486c9b4a3b2a1b701271e80d41069443 100644 (file)
--- a/gmqcc.h
+++ b/gmqcc.h
@@ -76,10 +76,11 @@ struct lex_file {
 #define LEX_STRLIT     1130
 #define LEX_IDENT      1131
 
-int              lex_token(struct lex_file *);
-void             lex_reset(struct lex_file *);
-void             lex_close(struct lex_file *);
-struct lex_file *lex_open (FILE *);
+int              lex_token  (struct lex_file *);
+void             lex_reset  (struct lex_file *);
+void             lex_close  (struct lex_file *);
+struct lex_file *lex_include(struct lex_file *, char *);
+struct lex_file *lex_open   (FILE *);
 
 //===================================================================
 //========================== error.c ================================
diff --git a/lex.c b/lex.c
index 556f485536934d6a860ddc40197bb5aa6327cb0f..bedaaa9db5e2c839e77a124fddc288fc88528398 100644 (file)
--- a/lex.c
+++ b/lex.c
@@ -58,7 +58,7 @@ void lex_close(struct lex_file *file) {
        if (!file) return;
        
        fclose(file->file); /* may already be closed */
-       mem_d(file);
+       mem_d (file);
 }
 
 static void lex_addch(int ch, struct lex_file *file) {
@@ -337,3 +337,47 @@ void lex_reset(struct lex_file *file) {
        memset(file->peek,   0, sizeof(file->peek  ));
        memset(file->lastok, 0, sizeof(file->lastok));
 }
+
+/*
+ * Include a file into the lexer / parsing process:  This really
+ * should check if names are the same to prevent endless include
+ * 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';
+       }
+       
+       /*
+        * Dissallow recrusive include: this could easily cause some breakage
+        * and instant OOM.
+        */
+       if (strncmp(lex->name, file, strlen(lex->name)) == 0) {
+               error(ERROR_LEX, "%s:%d Source file cannot include itself\n", lex->name, lex->line-1);
+               exit (-1);
+       }
+       
+       FILE *fp = fopen(file, "r");
+       if  (!fp) {
+               error(ERROR_LEX, "%s:%d Include file `%s` doesn't exist\n", lex->name, lex->line, file);
+               exit (-1);
+       }
+       
+       /* must free strdup */
+       file --;
+       mem_d (file);
+       
+       return lex_open(fp);
+}
diff --git a/parse.c b/parse.c
index d2c90630b098ab34aac042bbf503394ecda550d8..caefc205116e5ffa715f5a84f03f335b008f0b29 100644 (file)
--- a/parse.c
+++ b/parse.c
@@ -427,6 +427,17 @@ int parse_tree(struct lex_file *file) {
                                                token = lex_token(file);
                                        if (token == '\n')
                                                return error(ERROR_PARSE, "%d: Invalid use of include preprocessor directive: wanted #include \"file.h\"\n", file->line-1);
+                                               
+                                       char            *copy = util_strdup(file->lastok);
+                                       struct lex_file *next = lex_include(file,   copy);
+                                       
+                                       if (!next) {
+                                               error(ERROR_INTERNAL, "Include subsystem failure\n");
+                                               exit (-1);
+                                       }
+                                       parse_tree(next);
+                                       mem_d     (copy);
+                                       lex_close (next);
                                }
                        
                                /* skip all tokens to end of directive */
index 39aa583ac936b7ecc55fd05b119bc1a7f05410f3..b1e0568d14fa1edb9f08bedeb01a9e1452b8512f 100644 (file)
@@ -2,12 +2,4 @@
  * all of these includes should work.  No matter what the spacing
  * is,  we rely on it.
  */
-#include "include.h"
-#include  "include.h"
- #include "include.h"
- #include  "include.h"
-       #include "include.h"
-#  include "include.h"
-  #            include "include.h"
-#include       "include.h"
-#       include                        "include.h"
+#include "test/include2.qc"
index f88d28123f73bff4b9dc14f54796512df0f6aee0..814ce11ee59f5ae538211dfd27c22a2818fb4c7c 100644 (file)
@@ -1,4 +1,4 @@
-float  typef = 1;
+float  typef;
 vector typev;
 string types;
 entity typee;
index d74e481e93aa243fd273519143cb9545259eb7e9..623552521c3fd44b3b55ed04c7836064dd8ee481 100644 (file)
@@ -4,3 +4,7 @@ vector vec3 = { -.0, +.0, +0.1 };
 vector vec4 = { 1.1, 2.2, 3.3  };
 vector vec5 = { 2., 3., 4.     };
 vector vec6 = { -2., +3., -4.  };
+/*
+ * These are just comments:  Ideally there is still some broken things
+ * for the vector yet.  which sort of sucks.
+ */