]> git.xonotic.org Git - xonotic/gmqcc.git/commitdiff
Count file lines
authorDale Weiler <killfieldengine@gmail.com>
Wed, 11 Apr 2012 19:53:40 +0000 (15:53 -0400)
committerDale Weiler <killfieldengine@gmail.com>
Wed, 11 Apr 2012 19:53:40 +0000 (15:53 -0400)
gmqcc.h
lex.c
main.c
parse.c
util.c

diff --git a/gmqcc.h b/gmqcc.h
index e95e42860f729b1b92e18484df2039aea09cdcc1..853eb3315e581ff7ea81afbf43e3890931468958 100644 (file)
--- a/gmqcc.h
+++ b/gmqcc.h
@@ -32,6 +32,7 @@ struct lex_file {
        char  peek  [5];
        char  lastok[8192];
        
+       int   line;
        int   last;
        int   current;
        int   length;
diff --git a/lex.c b/lex.c
index 37e914fbf0823fd786ba1d4effe331c284618bf3..4e84bcec7f42ccf10eb6f78b2881f82819b6153c 100644 (file)
--- a/lex.c
+++ b/lex.c
@@ -138,10 +138,16 @@ static int lex_digraph(struct lex_file *file, int first) {
 
 static int lex_getch(struct lex_file *file) {
        int ch = lex_inget(file);
-       if (ch == '?')
-               return lex_trigraph(file);
-       if (ch == '<' || ch == ':' || ch == '%')
-               return lex_digraph (file, ch);
+       
+       switch (ch) {
+               case '?' :
+                       return lex_trigraph(file);
+               case '<' :
+               case ':' :
+               case '%' :
+                       return lex_digraph (file, ch);
+               case '\n': file->line ++;
+       }
                
        return ch;
 }
@@ -150,7 +156,7 @@ static int lex_get(struct lex_file *file) {
        int ch;
        if (!isspace(ch = lex_getch(file)))
                return ch;
-       
+               
        /* skip over all spaces */
        while (isspace(ch) && ch != '\n')
                ch = lex_getch(file);
@@ -258,7 +264,8 @@ static int lex_getsource(struct lex_file *file) {
                case '\'': return lex_skipchr(file);
                case '"':  return lex_skipstr(file);
                case '/':  return lex_skipcmt(file);
-               default:   return ch;
+               default:
+                       return ch;
        }
 }
 
diff --git a/main.c b/main.c
index 00f84110e07e6bbe53d27e53d6bb4e4630f5bef6..5b9828832743bc85e2fabc0116374fd8fb10803c 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--;
        argv++;
diff --git a/parse.c b/parse.c
index 38c33324ccb0246eee0929731e0a123a063cbb50..af6c6229a1dc246a82796909f4dcb3ed11467c7a 100644 (file)
--- a/parse.c
+++ b/parse.c
 #define PARSE_TYPE_DONE     37
 #define PARSE_TYPE_IDENT    38
 
-int parse[PARSE_TYPE_IDENT] = {
-       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
-       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
-       0,0,0,0,0,0,0,0
-};
-
 /*
  * Adds a parse type to the parse tree, this is where all the hard
  * work actually begins.
@@ -89,16 +83,6 @@ int parse[PARSE_TYPE_IDENT] = {
                parsetree             = parsetree->next;                 \
        } while (0)
 
-#define PARSE_TREE_CHK(X,Y,Z)                                        \
-       do { \
-               if(parse[X]) { \
-                       error(ERROR_PARSE, "Expected %c for %c\n", Y, Z); \
-                       exit (1); \
-               } \
-       } while (0)
-       
-#define PARSE_TREE_PUT(X) do { parse[X] = 1; } while (0)
-
 /*
  * This is all the punctuation handled in the parser, these don't
  * need tokens, they're already tokens.
@@ -231,8 +215,6 @@ int parse_tree(struct lex_file *file) {
                                        error(ERROR_PARSE, "Expected `(` on if statement:\n");
                                PARSE_TREE_ADD(PARSE_TYPE_IF);
                                PARSE_TREE_ADD(PARSE_TYPE_LPARTH);
-                               PARSE_TREE_CHK(PARSE_TYPE_LPARTH, ')', '(');
-                               PARSE_TREE_PUT(PARSE_TYPE_LPARTH);
                                break;
                        case TOKEN_ELSE:
                                token = lex_token(file);
@@ -280,17 +262,6 @@ int parse_tree(struct lex_file *file) {
                                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,   {});
@@ -311,9 +282,8 @@ int parse_tree(struct lex_file *file) {
                        case '#':
                                token = lex_token(file); /* skip '#' */
                                while (isspace(token)) {
-                                       if (token == '\n') {
+                                       if (token == '\n')
                                                return error(ERROR_PARSE, "Expected valid preprocessor directive after `#` %s\n");
-                                       }
                                        token = lex_token(file); /* try again */
                                }
                                /*
@@ -321,22 +291,16 @@ int parse_tree(struct lex_file *file) {
                                 * directives so far are #include.
                                 */
                                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.
+                                        * We only suport include " ", not <> like in C (why?)
+                                        * because the latter is silly.
                                         */
                                        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);
+                                       /* we handle lexing at that point now */
+                                       if (token == '\n')
+                                               return error(ERROR_PARSE, "%d: Invalid use of include preprocessor directive: wanted #include \"file.h\"\n", file->line);
                                }
                        
                                /* skip all tokens to end of directive */
@@ -345,18 +309,12 @@ int parse_tree(struct lex_file *file) {
                                break;
                                
                        case '.':
-                               //token = lex_token(file);
                                PARSE_TREE_ADD(PARSE_TYPE_DOT);
                                break;
                        case '(':
-                               //token = lex_token(file);
-                               PARSE_TREE_PUT(PARSE_TYPE_LPARTH);
                                PARSE_TREE_ADD(PARSE_TYPE_LPARTH);
                                break;
                        case ')':
-                               //token = lex_token(file);
-                               parse[PARSE_TYPE_LPARTH] = 0;
-                               PARSE_TREE_PUT(PARSE_TYPE_RPARTH);
                                PARSE_TREE_ADD(PARSE_TYPE_RPARTH);
                                break;
                                
diff --git a/util.c b/util.c
index 5261063f2b0b8684e94ffc4db8df9685fe240e78..a15590791f009719d85c23e47dc3c9ca744636ef 100644 (file)
--- a/util.c
+++ b/util.c
@@ -23,6 +23,7 @@
 #include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
+#include <stdarg.h>
 #include "gmqcc.h"
  
 struct memblock_t {
@@ -79,3 +80,10 @@ char *util_strdup(const char *s) {
        
        return ptr;
 }
+
+void util_debug(const char *ms, ...) {
+       va_list  va;
+       va_start(va, ms);
+       vprintf (ms, va);
+       va_end  (va);
+}