]> git.xonotic.org Git - xonotic/gmqcc.git/commitdiff
Work in progress preprocessor
authorDale Weiler <killfieldengine@gmail.com>
Mon, 9 Apr 2012 13:36:16 +0000 (09:36 -0400)
committerDale Weiler <killfieldengine@gmail.com>
Mon, 9 Apr 2012 13:36:16 +0000 (09:36 -0400)
Makefile
cpp.c [new file with mode: 0644]
error.c
gmqcc
gmqcc.h
lex.c
main.c
parse.c

index ae46f261f8deeca0e96c1f8b0d6a12e956f057d1..98f79a1d008b8372b9b51f02ef85dbd719066423 100644 (file)
--- 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 (file)
index 0000000..e2f8021
--- /dev/null
+++ b/cpp.c
@@ -0,0 +1,10 @@
+#include <limits.h>
+#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 1bd792ea2e4f2efd9978f456b895925a23d8e0d1..eb770806f8a3e2854928d715380e3f9a25305ff1 100644 (file)
--- 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 f34c8a62d65e4d80880e4e4ec6ad0dde46d5cf60..257c415afc2dd5fd5fef3f972261d33851f2d63c 100755 (executable)
Binary files a/gmqcc and b/gmqcc differ
diff --git a/gmqcc.h b/gmqcc.h
index 091725b16807df9396c2f4a309f69b4b9fdeb580..7fda6fd912ca11879ffdccd0cca3ebf622f2066f 100644 (file)
--- 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 e94252de83f23803ac30b08edaea9d45d78d2d56..9d15301d8d0070da7aec062df4af6e2b5864f83b 100644 (file)
--- 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 22662dbe04a5f4868dd6c7321d45549fca656ef8..248f94f3af32074fd38a757d968dc2a297cd5b63 100644 (file)
--- a/main.c
+++ b/main.c
@@ -23,7 +23,6 @@
 #include <stdlib.h>
 #include <string.h>
 #include <limits.h>
-#include <sys/stat.h>
 #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 7743360fe6f23e68696fba1d2b8aa449a787c891..129bcb197b1da7441d62bdf62ffb2606e7daf3bb 100644 (file)
--- a/parse.c
+++ b/parse.c
 
 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);