From ca2c128e677d32d9274fa6d85df8f7ce35ef48cf Mon Sep 17 00:00:00 2001 From: Augusto Gunsch Date: Sat, 21 Nov 2020 11:25:30 -0300 Subject: [PATCH] Change translator output format --- .gitattributes | 3 +++ .gitignore | 1 + Makefile | 2 +- main.c | 2 +- parser.c | 4 ++-- translator.c | 29 ++++++++++++++++++++--------- translator.h | 7 ++++++- util.c | 15 +++++++++++++++ util.h | 15 +++++++++++++++ 9 files changed, 64 insertions(+), 14 deletions(-) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..68b6d45 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,3 @@ +*.h linguist-language=C +*.c linguist-language=C +Makefile -linguist-detectable diff --git a/.gitignore b/.gitignore index 4c61528..086aa8a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ vmtranslator +tags diff --git a/Makefile b/Makefile index f2828e7..abd5176 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ FILES = parser.c main.c translator.c util.c INCLUDES = -I. -CFLAGS = -std=c99 -g +CFLAGS = -std=c99 OUTFILE = vmtranslator main: ${FILES} diff --git a/main.c b/main.c index 1078600..3231b04 100644 --- a/main.c +++ b/main.c @@ -246,7 +246,7 @@ int main(int argc, char* argv[]) { translate(t); freeparser(p); - printasmlns(t, output); + printlns(t->output, output); // freeing rest freetranslator(t); diff --git a/parser.c b/parser.c index 00d1c00..94ce1e9 100644 --- a/parser.c +++ b/parser.c @@ -5,7 +5,7 @@ #include #include "parser.h" -void freelns(LINEARRAY* lns) { +void freelnarray(LINEARRAY* lns) { for(int i = 0; i < lns->count; i++) { int tkcount = lns->lns[i]->tokenscount; for(int j = 0; j < tkcount; j++) { @@ -19,7 +19,7 @@ void freelns(LINEARRAY* lns) { } void freeparser(PARSER* p) { - freelns(p->lns); + freelnarray(p->lns); free(p); } diff --git a/translator.c b/translator.c index f97d9d7..3575e85 100644 --- a/translator.c +++ b/translator.c @@ -26,15 +26,11 @@ void freetoclean(TRANSLATOR* t) { void freetranslator(TRANSLATOR* t) { free(t->asmlns->items); free(t->asmlns); + freelns(t->output); freetoclean(t); free(t); } -void printasmlns(TRANSLATOR* t, FILE* stream) { - for(int i = 0; i < t->asmlns->count; i++) - fprintf(stream, "%s\n", t->asmlns->items[i]); -} - char* heapstrtoclean(TRANSLATOR* t, const char* input) { char* newstr = heapstr(input, strlen(input)); pushtoclean(t, newstr); @@ -257,13 +253,21 @@ void checkfun(TRANSLATOR* t, LINE* ln) { } } +void pushln(TRANSLATOR* t, char* content) { + t->curln->content = content; + t->curln->truen = t->lncount; + LINELIST* nextln = (LINELIST*)malloc(sizeof(LINELIST)); + t->curln->next = nextln; + t->lastln = t->curln; + t->curln = nextln; + t->lncount++; +} + void addasm(TRANSLATOR* t, TEMPLATE* tp) { checkasmsize(t, tp->count); - for(int i = 0; i < tp->count; i++) { - t->asmlns->items[t->asmlns->count] = tp->items[i]; - t->asmlns->count++; - } + for(int i = 0; i < tp->count; i++) + pushln(t, tp->items[i]); } void addasmlns(TRANSLATOR* t, LINE* ln, TEMPLATE* tp) { @@ -577,6 +581,8 @@ void translate(TRANSLATOR* t) { fprintf(stderr, "Expected return before end of file; file %s.vm, line %i\n", t->fname, t->lns->count-1); exit(1); } + t->lastln->next = NULL; + free(t->curln); } TRANSLATOR* mktranslator(LINEARRAY* lns, char* fname) { @@ -592,6 +598,11 @@ TRANSLATOR* mktranslator(LINEARRAY* lns, char* fname) { t->toclean->size = sizeof(char*)*(lns->count * 5); t->toclean->items = (char**)malloc(t->toclean->size); + LINELIST* newln = (LINELIST*)malloc(sizeof(LINELIST)); + t->output = newln; + t->curln = newln; + t->lncount = 0; + t->funcount = 0; t->retind = 0; t->cmpind = 0; diff --git a/translator.h b/translator.h index 745e233..6f70417 100644 --- a/translator.h +++ b/translator.h @@ -2,6 +2,7 @@ #define TRANSLATOR_H #include #include "parser.h" +#include "util.h" typedef struct { char** items; @@ -21,10 +22,14 @@ typedef struct { int retind; int cmpind; bool returned; + + LINELIST* output; + LINELIST* curln; + LINELIST* lastln; + int lncount; } TRANSLATOR; void freetranslator(TRANSLATOR* t); -void printasmlns(TRANSLATOR* t, FILE* stream); void translate(TRANSLATOR* t); TRANSLATOR* mktranslator(LINEARRAY* lns, char* fname); #endif diff --git a/util.c b/util.c index 5186e56..c2697fd 100644 --- a/util.c +++ b/util.c @@ -22,3 +22,18 @@ int countplaces(int n) { } return places; } + +void printlns(LINELIST* lns, FILE* stream) { + LINELIST* curln = lns; + while(curln != NULL) { + fprintf(stream, "%s\n", curln->content); + curln = curln->next; + } +} + +void freelns(LINELIST* lns) { + LINELIST* next = lns->next; + free(lns); + if(next != NULL) + freelns(next); +} diff --git a/util.h b/util.h index 3f8cd8c..701e067 100644 --- a/util.h +++ b/util.h @@ -1,2 +1,17 @@ +#ifndef UTIL_H +#define UTIL_H + +#include + char* heapstr(const char* str, int len); int countplaces(int n); + +typedef struct lnls { + char* content; + int truen; + struct lnls* next; +} LINELIST; + +void printlns(LINELIST* lns, FILE* stream); +void freelns(LINELIST* lns); +#endif