From 4e35032a24c315a326715017295da4dee44f5ea6 Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Sat, 14 Apr 2012 03:53:20 -0400 Subject: [PATCH] Include now works --- gmqcc.h | 9 +++++---- lex.c | 46 +++++++++++++++++++++++++++++++++++++++++++++- parse.c | 11 +++++++++++ test/include.qc | 10 +--------- test/types.qc | 2 +- test/vector.qc | 4 ++++ 6 files changed, 67 insertions(+), 15 deletions(-) diff --git a/gmqcc.h b/gmqcc.h index e8e3df0..409762b 100644 --- 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 556f485..bedaaa9 100644 --- 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 d2c9063..caefc20 100644 --- 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 */ diff --git a/test/include.qc b/test/include.qc index 39aa583..b1e0568 100644 --- a/test/include.qc +++ b/test/include.qc @@ -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" diff --git a/test/types.qc b/test/types.qc index f88d281..814ce11 100644 --- a/test/types.qc +++ b/test/types.qc @@ -1,4 +1,4 @@ -float typef = 1; +float typef; vector typev; string types; entity typee; diff --git a/test/vector.qc b/test/vector.qc index d74e481..6235525 100644 --- a/test/vector.qc +++ b/test/vector.qc @@ -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. + */