Include now works

This commit is contained in:
Dale Weiler 2012-04-14 03:53:20 -04:00
parent ebe77805eb
commit 4e35032a24
6 changed files with 67 additions and 15 deletions

View file

@ -76,10 +76,11 @@ struct lex_file {
#define LEX_STRLIT 1130 #define LEX_STRLIT 1130
#define LEX_IDENT 1131 #define LEX_IDENT 1131
int lex_token(struct lex_file *); int lex_token (struct lex_file *);
void lex_reset(struct lex_file *); void lex_reset (struct lex_file *);
void lex_close(struct lex_file *); void lex_close (struct lex_file *);
struct lex_file *lex_open (FILE *); struct lex_file *lex_include(struct lex_file *, char *);
struct lex_file *lex_open (FILE *);
//=================================================================== //===================================================================
//========================== error.c ================================ //========================== error.c ================================

46
lex.c
View file

@ -58,7 +58,7 @@ void lex_close(struct lex_file *file) {
if (!file) return; if (!file) return;
fclose(file->file); /* may already be closed */ fclose(file->file); /* may already be closed */
mem_d(file); mem_d (file);
} }
static void lex_addch(int ch, struct lex_file *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->peek, 0, sizeof(file->peek ));
memset(file->lastok, 0, sizeof(file->lastok)); 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);
}

11
parse.c
View file

@ -427,6 +427,17 @@ int parse_tree(struct lex_file *file) {
token = lex_token(file); token = lex_token(file);
if (token == '\n') if (token == '\n')
return error(ERROR_PARSE, "%d: Invalid use of include preprocessor directive: wanted #include \"file.h\"\n", file->line-1); 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 */ /* skip all tokens to end of directive */

View file

@ -2,12 +2,4 @@
* all of these includes should work. No matter what the spacing * all of these includes should work. No matter what the spacing
* is, we rely on it. * is, we rely on it.
*/ */
#include "include.h" #include "test/include2.qc"
#include "include.h"
#include "include.h"
#include "include.h"
#include "include.h"
# include "include.h"
# include "include.h"
#include "include.h"
# include "include.h"

View file

@ -1,4 +1,4 @@
float typef = 1; float typef;
vector typev; vector typev;
string types; string types;
entity typee; entity typee;

View file

@ -4,3 +4,7 @@ vector vec3 = { -.0, +.0, +0.1 };
vector vec4 = { 1.1, 2.2, 3.3 }; vector vec4 = { 1.1, 2.2, 3.3 };
vector vec5 = { 2., 3., 4. }; vector vec5 = { 2., 3., 4. };
vector vec6 = { -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.
*/