]> de.git.xonotic.org Git - xonotic/gmqcc.git/commitdiff
Implemented support for #include, but it doesn't add an include to the lexer yet.
authorDale Weiler <killfieldengine@gmail.com>
Wed, 11 Apr 2012 16:13:31 +0000 (12:13 -0400)
committerDale Weiler <killfieldengine@gmail.com>
Wed, 11 Apr 2012 16:13:31 +0000 (12:13 -0400)
code.c
lex.c
main.c
parse.c
test/include.qc [new file with mode: 0644]
typedef.c

diff --git a/code.c b/code.c
index 329730154e9896933f1ff09b48db2af97510d51e..b796b1319937b8a5276cc43263738d18d9eda082 100644 (file)
--- a/code.c
+++ b/code.c
@@ -151,6 +151,7 @@ VECTOR_MAKE(char,                   code_strings   );
 prog_header code_header;
 void code_write() {
        
+       #if 0
        /* Add test program */
        code_strings_add('\0');
        
@@ -188,7 +189,7 @@ void code_write() {
        
        code_functions_add((prog_section_function){0,  0, 0, 0, .name=0, 0, 0, {0}}); /* NULL */
        code_functions_add((prog_section_function){1,  0, 0, 0, .name=1, 0, 0, {0}}); /* m_init */
-       code_functions_add((prog_section_function){-2, 0, 0, 0, .name=8, 0, 0, {0}}); /* print  */
+       code_functions_add((prog_section_function){-4, 0, 0, 0, .name=8, 0, 0, {0}}); /* print  */
        code_functions_add((prog_section_function){0,  0, 0, 0, .name=14+13,        0,0, {0}}); /* m_keydown */
        code_functions_add((prog_section_function){0,  0, 0, 0, .name=14+13+10,     0,0, {0}});
        code_functions_add((prog_section_function){0,  0, 0, 0, .name=14+13+10+7,   0,0, {0}});
@@ -208,8 +209,10 @@ void code_write() {
        code_header.globals    = (prog_section){code_header.functions.offset  + sizeof(prog_section_function) *code_functions_elements,   code_globals_elements    };
        code_header.strings    = (prog_section){code_header.globals.offset    + sizeof(int)                   *code_globals_elements,     code_strings_elements    };
        code_header.entfield   = 0; /* TODO: */
-       
+       #endif
+
 
+       /* write out everything one SHOT! */
        FILE *fp = fopen("program.dat", "wb");
        fwrite(&code_header,         1, sizeof(prog_header), fp);
        fwrite(code_statements_data, 1, sizeof(prog_section_statement)*code_statements_elements, fp);
diff --git a/lex.c b/lex.c
index 3be0045e36d66e1db1c2dade7fce43c1f9cbcb3e..37e914fbf0823fd786ba1d4effe331c284618bf3 100644 (file)
--- a/lex.c
+++ b/lex.c
@@ -302,6 +302,7 @@ int lex_token(struct lex_file *file) {
                if (typedef_find(file->lastok))
                        TEST_TYPE(typedef_find(file->lastok)->name);
                        
+               #undef TEST_TYPE
                return LEX_IDENT;
        }
        return ch;
diff --git a/main.c b/main.c
index 8aa73a0ad4b1ef9f89463cc0a97064f1d57c04b1..00f84110e07e6bbe53d27e53d6bb4e4630f5bef6 100644 (file)
--- a/main.c
+++ b/main.c
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
  */
-#include <stdlib.h>
-#include <string.h>
-#include <limits.h>
-#include "gmqcc.h"
+#      include <stdlib.h>
+       #include <string.h>
+#              include <limits.h>
+  #      include       "gmqcc.h"
 
 int main(int argc, char **argv) {
        argc--;
diff --git a/parse.c b/parse.c
index 2274df94bc4f61627960f55f2f4f00d5e372916b..38c33324ccb0246eee0929731e0a123a063cbb50 100644 (file)
--- a/parse.c
+++ b/parse.c
@@ -23,6 +23,7 @@
 #include <limits.h>
 #include <stdlib.h>
 #include <string.h>
+#include <ctype.h>
 #include "gmqcc.h"
 
 /*
@@ -278,7 +279,18 @@ int parse_tree(struct lex_file *file) {
                        case TOKEN_CONTINUE:
                                PARSE_TREE_ADD(PARSE_TYPE_CONTINUE);
                                break;
-                               
+                       
+                       /*
+                        * DO loops work like so:
+                        * __do_loop_beg:
+                        * IFNOT __do_loop_beg#
+                        *      GOTO __do_loop_end
+                        * INSTR1
+                        * INSTR2
+                        * ......
+                        * GOTO __do_loop_beg
+                        */
+                       
                        case TOKEN_DO:        PARSE_PERFORM(PARSE_TYPE_DO,      {});
                        case TOKEN_WHILE:     PARSE_PERFORM(PARSE_TYPE_WHILE,   {});
                        case TOKEN_BREAK:     PARSE_PERFORM(PARSE_TYPE_BREAK,   {});
@@ -297,14 +309,36 @@ int parse_tree(struct lex_file *file) {
                         * which are higer than the ascii table.)
                         */
                        case '#':
+                               token = lex_token(file); /* skip '#' */
+                               while (isspace(token)) {
+                                       if (token == '\n') {
+                                               return error(ERROR_PARSE, "Expected valid preprocessor directive after `#` %s\n");
+                                       }
+                                       token = lex_token(file); /* try again */
+                               }
                                /*
-                                * Skip the preprocessor for now:  We'll implement our own
-                                * eventually.  For now we need to make sure directives are
-                                * not accidently tokenized.
+                                * If we make it here we found a directive, the supported
+                                * directives so far are #include.
                                 */
-                               token = lex_token(file);
-                               token = lex_token(file);
-                               
+                               if (strncmp(file->lastok, "include", sizeof("include")) == 0) {
+                                       //lex_include("file");
+                                       /*
+                                        * We need to compose a name till we find either a
+                                        * '"',> or a <, (for includes), if we hit a '\n' then
+                                        * clearly the person miswrote the include.
+                                        */
+                                       while (*file->lastok != '"' && token != '\n')
+                                               token = lex_token(file);
+                                               
+                                       if (token == '\n')
+                                               return error(ERROR_PARSE, "Invalid use of include preprocessor directive: wanted #include \"file.h\"\n");
+                                               
+                                       
+                                       //lex_token(file);
+                                       //lex_token(file);
+                                       printf("include: %s\n", file->lastok);
+                               }
+                       
                                /* skip all tokens to end of directive */
                                while (token != '\n')
                                        token = lex_token(file);
diff --git a/test/include.qc b/test/include.qc
new file mode 100644 (file)
index 0000000..39aa583
--- /dev/null
@@ -0,0 +1,13 @@
+/*
+ * 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"
index dcc4e20b377a37a498859cb151d1773621ad1d53..ecd8b8b30d001a86e1d6c206249efa9264a83e4f 100644 (file)
--- a/typedef.c
+++ b/typedef.c
@@ -80,7 +80,7 @@ int typedef_add(const char *from, const char *to) {
            strncmp(from, "void",   sizeof("void"))   == 0) {
                
                typedef_table[hash]       = mem_a(sizeof(typedef_node));
-               typedef_table[hash]->name = strdup(from);
+               typedef_table[hash]->name = util_strdup(from);
                return -100;
        } else {
                /* search the typedefs for it (typedef-a-typedef?) */