diff --git a/Makefile b/Makefile index 39b7d39..e0a4639 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ FILES = parser.c main.c INCLUDES = -I. -CFLAGS = -std=c99 -g +CFLAGS = -std=c99 OUTFILE = vmtranslator main: ${FILES} diff --git a/main.c b/main.c index 9ad23e6..3249d52 100644 --- a/main.c +++ b/main.c @@ -16,11 +16,15 @@ int main(int argc, char* argv[]) { return errno; } + // info gathering int lncount, widestln, maxtokens; getinfo(input, &lncount, &widestln, &maxtokens); + + // parsing struct line** lns = parse(input, lncount, widestln, maxtokens); + fclose(input); - printf("lns: %i\n", lncount); + // printing for(int i = 0; i < lncount; i++) { int tkcount = lns[i]->tokenscount; for(int j = 0; j < tkcount; j++) { @@ -29,5 +33,8 @@ int main(int argc, char* argv[]) { printf("\n"); } + // freeing + freelns(lns, lncount); + return 0; } diff --git a/parser.c b/parser.c index 8924163..076c507 100644 --- a/parser.c +++ b/parser.c @@ -4,6 +4,18 @@ #include #include "parser.h" +void freelns(struct line** lns, int lnscount) { + for(int i = 0; i < lnscount; i++) { + int tkcount = lns[i]->tokenscount; + for(int j = 0; j < tkcount; j++) { + free(lns[i]->tokens[j]); + } + free(lns[i]->tokens); + free(lns[i]); + } + free(lns); +} + void gountilbrk (FILE* input) { char c; while(c = fgetc(input), c != -1) { @@ -23,9 +35,13 @@ void getinfo(FILE* input, int* lncount, int* widestln, int* maxtokens) { int maxtoks = 0; short readsmt = 0; while(c = fgetc(input), c != -1) { - if(isspace(c) && readsmt) { - tokens++; - if(c == '\n') { + currsz++; + if(isspace(c)) { + if(readsmt) { + tokens++; + readsmt = 0; + } + if(c == '\n' && tokens > 0) { lns++; if(currsz > widest) widest = currsz; @@ -34,7 +50,6 @@ void getinfo(FILE* input, int* lncount, int* widestln, int* maxtokens) { currsz = 0; tokens = 0; } - readsmt = 0; continue; } @@ -47,7 +62,6 @@ void getinfo(FILE* input, int* lncount, int* widestln, int* maxtokens) { ungetc(nc, input); } - currsz++; readsmt = 1; } rewind(input); diff --git a/parser.h b/parser.h index a72d102..da75d49 100644 --- a/parser.h +++ b/parser.h @@ -8,4 +8,5 @@ struct line { }; void getinfo(FILE* input, int* lncount, int* widestln, int* maxtokens); +void freelns(struct line** lns, int lnscount); struct line** parse(FILE* input, int lncount, int widestln, int maxtokens);