Change translator output format

This commit is contained in:
Augusto Gunsch 2020-11-21 11:25:30 -03:00
parent 6262faf28a
commit ca2c128e67
No known key found for this signature in database
GPG Key ID: F7EEFE29825C72DC
9 changed files with 64 additions and 14 deletions

3
.gitattributes vendored Normal file
View File

@ -0,0 +1,3 @@
*.h linguist-language=C
*.c linguist-language=C
Makefile -linguist-detectable

1
.gitignore vendored
View File

@ -1 +1,2 @@
vmtranslator
tags

View File

@ -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}

2
main.c
View File

@ -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);

View File

@ -5,7 +5,7 @@
#include <stdbool.h>
#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);
}

View File

@ -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;

View File

@ -2,6 +2,7 @@
#define TRANSLATOR_H
#include <stdbool.h>
#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

15
util.c
View File

@ -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);
}

15
util.h
View File

@ -1,2 +1,17 @@
#ifndef UTIL_H
#define UTIL_H
#include <stdio.h>
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