From e4e6185d5f73682aeb5858aeec2fb91fd2720120 Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Wed, 11 Apr 2012 15:53:40 -0400 Subject: [PATCH] Count file lines --- gmqcc.h | 1 + lex.c | 19 +++++++++++++------ main.c | 9 ++++----- parse.c | 54 ++++++------------------------------------------------ util.c | 8 ++++++++ 5 files changed, 32 insertions(+), 59 deletions(-) diff --git a/gmqcc.h b/gmqcc.h index e95e428..853eb33 100644 --- 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 37e914f..4e84bce 100644 --- 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 00f8411..5b98288 100644 --- a/main.c +++ b/main.c @@ -20,11 +20,10 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -# include - #include -# include - # include "gmqcc.h" - +#include +#include +#include +#include "gmqcc.h" int main(int argc, char **argv) { argc--; argv++; diff --git a/parse.c b/parse.c index 38c3332..af6c622 100644 --- a/parse.c +++ b/parse.c @@ -71,12 +71,6 @@ #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 5261063..a155907 100644 --- a/util.c +++ b/util.c @@ -23,6 +23,7 @@ #include #include #include +#include #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); +} -- 2.39.2