From: Dale Weiler Date: Mon, 9 Apr 2012 13:36:16 +0000 (-0400) Subject: Work in progress preprocessor X-Git-Tag: 0.1-rc1~712 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=1deeb8c9cfeeadd6b8073b38e76257c3c16cdc72;p=xonotic%2Fgmqcc.git Work in progress preprocessor --- diff --git a/Makefile b/Makefile index ae46f26..98f79a1 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CC = gcc CFLAGS = -O3 -Wall -OBJ = main.o lex.o error.o parse.o +OBJ = main.o lex.o error.o parse.o cpp.o %.o: %.c $(CC) -c -o $@ $< $(CFLAGS) diff --git a/cpp.c b/cpp.c new file mode 100644 index 0000000..e2f8021 --- /dev/null +++ b/cpp.c @@ -0,0 +1,10 @@ +#include +#include "gmqcc.h" + +/* + * Returns the next token back to the caller + * which is what we parse for the preprocessor here. + */ +int cpp(struct lex_file *file) { + /* TODO ... */ +} diff --git a/error.c b/error.c index 1bd792e..eb77080 100644 --- a/error.c +++ b/error.c @@ -55,7 +55,8 @@ static const char *const error_list[] = { "Parsing Error:", "Lexing Error:", "Internal Error:", - "Compilation Error:" + "Compilation Error:", + "Preprocessor Error:" }; int error(int status, const char *msg, ...) { diff --git a/gmqcc b/gmqcc index f34c8a6..257c415 100755 Binary files a/gmqcc and b/gmqcc differ diff --git a/gmqcc.h b/gmqcc.h index 091725b..7fda6fd 100644 --- a/gmqcc.h +++ b/gmqcc.h @@ -173,27 +173,24 @@ struct lex_file { #define LEX_CHRLIT 129 #define LEX_STRLIT 130 #define LEX_IDENT 131 -#define LEX_DO 132 -#define LEX_ELSE 133 -#define LEX_IF 134 -#define LEX_WHILE 135 -#define LEX_INCLUDE 136 -#define LEX_DEFINE 137 int lex_token(struct lex_file *); void lex_reset(struct lex_file *); int lex_debug(struct lex_file *); int lex_close(struct lex_file *); -struct lex_file *lex_open (const char *); +struct lex_file *lex_open (FILE *); /* errors */ #define ERROR_LEX (SHRT_MAX+0) #define ERROR_PARSE (SHRT_MAX+1) #define ERROR_INTERNAL (SHRT_MAX+2) #define ERROR_COMPILER (SHRT_MAX+3) +#define ERROR_PREPRO (SHRT_MAX+4) int error(int, const char *, ...); /* parse.c */ int parse(struct lex_file *); +/* cpp.c */ +int cpp (struct lex_file *); #endif diff --git a/lex.c b/lex.c index e94252d..9d15301 100644 --- a/lex.c +++ b/lex.c @@ -39,13 +39,13 @@ static const char *const lex_keywords[] = { "string", "float", "vector", - "entity" + "entity", }; -struct lex_file *lex_open(const char *name) { +struct lex_file *lex_open(FILE *fp) { struct lex_file *lex = mem_a(sizeof(struct lex_file)); if (lex) { - lex->file = fopen(name, "r"); + lex->file = fp; fseek(lex->file, 0, SEEK_END); lex->length = ftell(lex->file); lex->size = lex->length; /* copy, this is never changed */ @@ -244,7 +244,7 @@ static int lex_skipcmt(struct lex_file *file) { lex_addch(ch, file); while ((ch = lex_getch(file)) != '*') { if (ch == EOF) - return error(ERROR_LEX, "malformatted comment"," "); + return error(ERROR_LEX, "malformatted comment", " "); else lex_addch(ch, file); } @@ -276,7 +276,7 @@ int lex_token(struct lex_file *file) { /* valid identifier */ if (ch > 0 && (ch == '_' || isalpha(ch))) { lex_clear(file); - while (ch > 0 && (isalpha(ch) || isdigit(ch) || ch == '_')) { + while (ch > 0 && (isalpha(ch) || ch == '_')) { lex_addch(ch, file); ch = lex_getsource(file); } diff --git a/main.c b/main.c index 22662db..248f94f 100644 --- a/main.c +++ b/main.c @@ -23,7 +23,6 @@ #include #include #include -#include #include "gmqcc.h" int usage(const char *name) { @@ -32,7 +31,6 @@ int usage(const char *name) { } int main(int argc, char **argv) { - struct stat chk; const char *ofile = NULL; const char *ifile = NULL; int i; @@ -58,11 +56,14 @@ int main(int argc, char **argv) { printf("ifile: %s\n", ifile); printf("ofile: %s\n", ofile); - /* we check here for file existance, not in the lexer */ - if (stat(ifile, &chk) != 0) - return error(ERROR_COMPILER, "source file `%s` not found\n", ifile); + /* Open file */ + FILE *fp = fopen(ifile, "r"); + if (!fp) { + fclose(fp); + return error(ERROR_COMPILER, "Source file: %s not found\n", ifile); + } - struct lex_file *lex = lex_open(ifile); + struct lex_file *lex = lex_open(fp); lex_debug(lex); parse (lex); lex_close(lex); diff --git a/parse.c b/parse.c index 7743360..129bcb1 100644 --- a/parse.c +++ b/parse.c @@ -25,7 +25,11 @@ int parse(struct lex_file *file) { int token = 0; - while ((token = lex_token(file)) != ERROR_LEX && file->length >= 0) { + while ((token = lex_token(file)) != ERROR_LEX && \ + token != ERROR_COMPILER && \ + token != ERROR_INTERNAL && \ + token != ERROR_PARSE && \ + token != ERROR_PREPRO && file->length >= 0) { switch (token) { case TOKEN_IF: token = lex_token(file); @@ -35,6 +39,10 @@ int parse(struct lex_file *file) { if (token != '(') error(ERROR_PARSE, "Expected `(` after if\n", ""); break; + + /* TODO: Preprocessor */ + case '#': + token = cpp(file); } } lex_reset(file);