Code refactoring

This commit is contained in:
Augusto Gunsch 2020-11-19 08:10:51 -03:00
parent a6667a8f5d
commit 4e532a5cb9
No known key found for this signature in database
GPG Key ID: F7EEFE29825C72DC
7 changed files with 166 additions and 154 deletions

43
main.c
View File

@ -4,23 +4,24 @@
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>
#include <dirent.h> #include <dirent.h>
#include <stdbool.h>
#include "parser.h" #include "parser.h"
#include "translator.h" #include "translator.h"
#include "bootstrap.h" #include "bootstrap.h"
#include "util.h" #include "util.h"
struct TranslationList { typedef struct {
char** files; char** files;
char** fnames; char** fnames;
int filecount; int filecount;
int filessz; int filessz;
char* output; char* output;
}; } TRANSLATIONLIST;
char* getname(char* f, int len) { char* getname(char* f, int len) {
int startind = 0; int startind = 0;
int endind = len - 1; int endind = len - 1;
short readsmt = 0; bool readsmt = false;
for(int i = endind; i >= 0; i--) { for(int i = endind; i >= 0; i--) {
if(f[i] == '/') { if(f[i] == '/') {
@ -34,7 +35,7 @@ char* getname(char* f, int len) {
} }
if(f[i] == '.') if(f[i] == '.')
endind = i-1; endind = i-1;
readsmt = 1; readsmt = true;
} }
int size = sizeof(char)*(endind - startind + 2); int size = sizeof(char)*(endind - startind + 2);
@ -46,7 +47,7 @@ char* getname(char* f, int len) {
char* getfullname(char* f, int len) { char* getfullname(char* f, int len) {
int endind = len - 1; int endind = len - 1;
short readsmt = 0; bool readsmt = false;
for(int i = endind; i >= 0; i--) { for(int i = endind; i >= 0; i--) {
if(f[i] == '/') { if(f[i] == '/') {
@ -58,7 +59,7 @@ char* getfullname(char* f, int len) {
} }
if(f[i] == '.') if(f[i] == '.')
endind = i-1; endind = i-1;
readsmt = 1; readsmt = true;
} }
int size = sizeof(char)*(endind + 2); int size = sizeof(char)*(endind + 2);
@ -67,27 +68,27 @@ char* getfullname(char* f, int len) {
return retstr; return retstr;
} }
short isdotvm(char* f, int extind) { bool isdotvm(char* f, int extind) {
char* extstr = f + (sizeof(char) * extind); char* extstr = f + (sizeof(char) * extind);
return strcmp(extstr, ".vm") == 0; return strcmp(extstr, ".vm") == 0;
} }
short isdir(char* f, int len) { bool isdir(char* f, int len) {
short readsmt = 0; bool readsmt = false;
for(int i = len-1; i >= 0; i--) { for(int i = len-1; i >= 0; i--) {
if(f[i] == '.') if(f[i] == '.')
if(readsmt) if(readsmt)
return 0; return false;
else else
continue; continue;
if(f[i] == '/') if(f[i] == '/')
return 1; return 1;
readsmt = 1; readsmt = true;
} }
return 1; return true;
} }
char* getoutname(char* input, int len, short isdir) { char* getoutname(char* input, int len, bool isdir) {
char* outname; char* outname;
if(isdir) { if(isdir) {
char* name = getname(input, len); char* name = getname(input, len);
@ -106,7 +107,7 @@ char* getoutname(char* input, int len, short isdir) {
return outname; return outname;
} }
void addfile(struct TranslationList* l, char* fullname, char* name) { void addfile(TRANSLATIONLIST* l, char* fullname, char* name) {
int count = l->filecount; int count = l->filecount;
int targsize = (count + 1) * sizeof(char*); int targsize = (count + 1) * sizeof(char*);
@ -122,16 +123,16 @@ void addfile(struct TranslationList* l, char* fullname, char* name) {
l->filecount++; l->filecount++;
} }
struct TranslationList* getfiles(char* input) { TRANSLATIONLIST* getfiles(char* input) {
int filessz = sizeof(char*) * 16; int filessz = sizeof(char*) * 16;
struct TranslationList* filelist = (struct TranslationList*)malloc(sizeof(struct TranslationList)); TRANSLATIONLIST* filelist = (TRANSLATIONLIST*)malloc(sizeof(TRANSLATIONLIST));
filelist->files = (char**)malloc(filessz); filelist->files = (char**)malloc(filessz);
filelist->fnames = (char**)malloc(filessz); filelist->fnames = (char**)malloc(filessz);
filelist->filessz = filessz; filelist->filessz = filessz;
filelist->filecount = 0; filelist->filecount = 0;
int inplen = strlen(input); int inplen = strlen(input);
short isitdir = isdir(input, inplen); bool isitdir = isdir(input, inplen);
if(isitdir) { if(isitdir) {
DIR* dir = opendir(input); DIR* dir = opendir(input);
@ -182,7 +183,7 @@ struct TranslationList* getfiles(char* input) {
return filelist; return filelist;
} }
void freetranslationlist(struct TranslationList* ls) { void freetranslationlist(TRANSLATIONLIST* ls) {
for(int i = 0; i < ls->filecount; i++) { for(int i = 0; i < ls->filecount; i++) {
free(ls->files[i]); free(ls->files[i]);
free(ls->fnames[i]); free(ls->fnames[i]);
@ -199,7 +200,7 @@ int main(int argc, char* argv[]) {
return 1; return 1;
} }
struct TranslationList* ls = getfiles(argv[1]); TRANSLATIONLIST* ls = getfiles(argv[1]);
FILE* output = fopen(ls->output, "w"); FILE* output = fopen(ls->output, "w");
for(int i = 0; i < BOOTSTRAPN; i++) { for(int i = 0; i < BOOTSTRAPN; i++) {
@ -218,11 +219,11 @@ int main(int argc, char* argv[]) {
} }
// parsing // parsing
struct Parser* p = mkparser(input); PARSER* p = mkparser(input);
parse(p); parse(p);
// translating // translating
struct Translator* t = mktranslator(p->lns, fname); TRANSLATOR* t = mktranslator(p->lns, fname);
translate(t); translate(t);
freeparser(p); freeparser(p);

View File

@ -2,9 +2,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h> #include <ctype.h>
#include <string.h> #include <string.h>
#include <stdbool.h>
#include "parser.h" #include "parser.h"
void freelns(struct lnarray* lns) { void freelns(LINEARRAY* lns) {
for(int i = 0; i < lns->count; i++) { for(int i = 0; i < lns->count; i++) {
int tkcount = lns->lns[i]->tokenscount; int tkcount = lns->lns[i]->tokenscount;
for(int j = 0; j < tkcount; j++) { for(int j = 0; j < tkcount; j++) {
@ -17,7 +18,7 @@ void freelns(struct lnarray* lns) {
free(lns); free(lns);
} }
void freeparser(struct Parser* p) { void freeparser(PARSER* p) {
freelns(p->lns); freelns(p->lns);
free(p); free(p);
} }
@ -32,7 +33,7 @@ void gountilbrk (FILE* input) {
} }
} }
void getinfo(struct Parser* p) { void getinfo(PARSER* p) {
p->lns->count = 0; p->lns->count = 0;
p->maxtokens = 0; p->maxtokens = 0;
p->widestln = 0; p->widestln = 0;
@ -74,17 +75,17 @@ void getinfo(struct Parser* p) {
rewind(p->input); rewind(p->input);
} }
struct Parser* mkparser(FILE* input) { PARSER* mkparser(FILE* input) {
struct Parser* p = (struct Parser*)malloc(sizeof(struct Parser)); PARSER* p = (PARSER*)malloc(sizeof(PARSER));
struct lnarray* lns = (struct lnarray*)malloc(sizeof(struct lnarray)); LINEARRAY* lns = (LINEARRAY*)malloc(sizeof(LINEARRAY));
p->input = input; p->input = input;
p->lns = lns; p->lns = lns;
getinfo(p); getinfo(p);
return p; return p;
} }
void parse(struct Parser* p) { void parse(PARSER* p) {
struct line** lns = (struct line**)malloc(sizeof(struct line*)*p->lns->count); LINE** lns = (LINE**)malloc(sizeof(LINE*)*p->lns->count);
p->lns->lns = lns; p->lns->lns = lns;
p->lns->count = 0; p->lns->count = 0;
@ -111,7 +112,7 @@ void parse(struct Parser* p) {
if(c == '\n') { if(c == '\n') {
truelncount++; truelncount++;
if(tokensind > 0) { if(tokensind > 0) {
struct line* newln = (struct line*)malloc(sizeof(struct line)); LINE* newln = (LINE*)malloc(sizeof(LINE));
newln->tokens = (char**)malloc(sizeof(char*)*tokensind); newln->tokens = (char**)malloc(sizeof(char*)*tokensind);
for(int i = 0; i < tokensind; i++) { for(int i = 0; i < tokensind; i++) {
newln->tokens[i] = tokens[i]; newln->tokens[i] = tokens[i];

View File

@ -1,24 +1,24 @@
#ifndef parser #ifndef PARSER_H
#define parser #define PARSER_H
struct line { typedef struct {
char** tokens; char** tokens;
int tokenscount; int tokenscount;
int truen; int truen;
}; } LINE;
struct lnarray { typedef struct {
struct line** lns; LINE** lns;
int count; int count;
}; } LINEARRAY;
struct Parser { typedef struct {
FILE* input; FILE* input;
struct lnarray* lns; LINEARRAY* lns;
int widestln; int widestln;
int maxtokens; int maxtokens;
}; } PARSER;
struct Parser* mkparser(FILE* input); PARSER* mkparser(FILE* input);
void freeparser(struct Parser* p); void freeparser(PARSER* p);
void parse(struct Parser* p); void parse(PARSER* p);
#endif #endif

View File

@ -6,40 +6,42 @@
#include "templates.h" #include "templates.h"
#include "util.h" #include "util.h"
void pushtoclean(struct Translator* t, char* topush) { void pushtoclean(TRANSLATOR* t, char* topush) {
int nextsz = sizeof(char*)*(t->tocleanind+1); int nextsz = sizeof(char*)*(t->toclean->count+1);
if(nextsz >= t->tocleansize) { if(nextsz >= t->toclean->size) {
t->tocleansize = nextsz * 2; t->toclean->size = nextsz * 2;
t->toclean = realloc(t->toclean, t->tocleansize); t->toclean->items = realloc(t->toclean->items, t->toclean->size);
} }
t->toclean[t->tocleanind] = topush; t->toclean->items[t->toclean->count] = topush;
t->tocleanind++; t->toclean->count++;
} }
void freetoclean(struct Translator* t) { void freetoclean(TRANSLATOR* t) {
for(int i = 0; i < t->tocleanind; i++) for(int i = 0; i < t->toclean->count; i++)
free(t->toclean[i]); free(t->toclean->items[i]);
free(t->toclean->items);
free(t->toclean); free(t->toclean);
} }
void freetranslator(struct Translator* t) { void freetranslator(TRANSLATOR* t) {
free(t->asmlns->items);
free(t->asmlns); free(t->asmlns);
freetoclean(t); freetoclean(t);
free(t); free(t);
} }
void printasmlns(struct Translator* t, FILE* stream) { void printasmlns(TRANSLATOR* t, FILE* stream) {
for(int i = 0; i < t->asmind; i++) for(int i = 0; i < t->asmlns->count; i++)
fprintf(stream, "%s\n", t->asmlns[i]); fprintf(stream, "%s\n", t->asmlns->items[i]);
} }
char* heapstrtoclean(struct Translator* t, const char* input) { char* heapstrtoclean(TRANSLATOR* t, const char* input) {
char* newstr = heapstr(input, strlen(input)); char* newstr = heapstr(input, strlen(input));
pushtoclean(t, newstr); pushtoclean(t, newstr);
return newstr; return newstr;
} }
char* switchseg(struct Translator* t, struct line* ln) { char* switchseg(TRANSLATOR* t, LINE* ln) {
char* seg = ln->tokens[1]; char* seg = ln->tokens[1];
if(!strcmp(seg, "local")) if(!strcmp(seg, "local"))
return heapstrtoclean(t, "@LCL"); return heapstrtoclean(t, "@LCL");
@ -53,7 +55,7 @@ char* switchseg(struct Translator* t, struct line* ln) {
exit(1); exit(1);
} }
int lnlen(int* out, struct line* ln) { int lnlen(int* out, LINE* ln) {
int len = 0; int len = 0;
for(int i = 0; i < ln->tokenscount; i++) { for(int i = 0; i < ln->tokenscount; i++) {
int l = strlen(ln->tokens[i]); int l = strlen(ln->tokens[i]);
@ -65,7 +67,7 @@ int lnlen(int* out, struct line* ln) {
// produce comment as follows: // produce comment as follows:
// pop/push segment i // pop/push segment i
char* mkcom(struct Translator* t, struct line* ln) { char* mkcom(TRANSLATOR* t, LINE* ln) {
int lens[ln->tokenscount]; int lens[ln->tokenscount];
int comlen = sizeof(char) * lnlen(lens, ln) + ln->tokenscount + 3; int comlen = sizeof(char) * lnlen(lens, ln) + ln->tokenscount + 3;
char* comment = (char*)malloc(comlen); char* comment = (char*)malloc(comlen);
@ -87,7 +89,7 @@ char* mkcom(struct Translator* t, struct line* ln) {
return comment; return comment;
} }
void checknumber(struct Translator* t, struct line* ln, int indlen, char* name, char* n) { void checknumber(TRANSLATOR* t, LINE* ln, int indlen, char* name, char* n) {
for(int i = 0; i < indlen; i++) for(int i = 0; i < indlen; i++)
if(!isdigit(n[i])) { if(!isdigit(n[i])) {
fprintf(stderr, "Invalid %s '%s'; file %s.vm, line %i\n", t->fname, name, n, ln->truen); fprintf(stderr, "Invalid %s '%s'; file %s.vm, line %i\n", t->fname, name, n, ln->truen);
@ -95,40 +97,26 @@ void checknumber(struct Translator* t, struct line* ln, int indlen, char* name,
} }
} }
void checknargs(struct Translator* t, struct line* ln, int nargslen) { void checknargs(TRANSLATOR* t, LINE* ln, int nargslen) {
checknumber(t, ln, nargslen, "argument number", ln->tokens[2]); checknumber(t, ln, nargslen, "argument number", ln->tokens[2]);
} }
void checknlocals(struct Translator* t, struct line* ln, int nlocalslen) { void checknlocals(TRANSLATOR* t, LINE* ln, int nlocalslen) {
checknumber(t, ln, nlocalslen, "local variable number", ln->tokens[2]); checknumber(t, ln, nlocalslen, "local variable number", ln->tokens[2]);
} }
void checkind(struct Translator* t, struct line* ln, int indlen) { void checkind(TRANSLATOR* t, LINE* ln, int indlen) {
checknumber(t, ln, indlen, "index", ln->tokens[2]); checknumber(t, ln, indlen, "index", ln->tokens[2]);
} }
int countplaces(int n) { void checkinfun(TRANSLATOR* t, LINE* ln) {
int places = 1;
int divisor = 1;
if(n < 0) {
n = -n;
places++;
}
while(n / divisor >= 10) {
places++;
divisor *= 10;
}
return places;
}
void checkinfun(struct Translator* t, struct line* ln) {
if(t->funcount <= 0) { if(t->funcount <= 0) {
fprintf(stderr, "Instruction should be part of a function; file %s.vm, line %i\n", t->fname, ln->truen); fprintf(stderr, "Instruction should be part of a function; file %s.vm, line %i\n", t->fname, ln->truen);
exit(1); exit(1);
} }
} }
char* mkspeciallab(struct Translator* t, struct line* ln, char* suffix, int* ind) { char* mkspeciallab(TRANSLATOR* t, LINE* ln, char* suffix, int* ind) {
checkinfun(t, ln); checkinfun(t, ln);
(*ind)++; (*ind)++;
int sz = (t->lastfunlen + countplaces(*ind) + strlen(suffix) + 3) * sizeof(char); int sz = (t->lastfunlen + countplaces(*ind) + strlen(suffix) + 3) * sizeof(char);
@ -138,15 +126,15 @@ char* mkspeciallab(struct Translator* t, struct line* ln, char* suffix, int* ind
return lab; return lab;
} }
char* mkcmplab(struct Translator* t, struct line* ln) { char* mkcmplab(TRANSLATOR* t, LINE* ln) {
return mkspeciallab(t, ln, "cmp", &(t->cmpind)); return mkspeciallab(t, ln, "cmp", &(t->cmpind));
} }
char* mkretlab(struct Translator* t, struct line* ln) { char* mkretlab(TRANSLATOR* t, LINE* ln) {
return mkspeciallab(t, ln, "ret", &(t->retind)); return mkspeciallab(t, ln, "ret", &(t->retind));
} }
char* mkind(struct Translator* t, struct line* ln, int indlen) { char* mkind(TRANSLATOR* t, LINE* ln, int indlen) {
checkind(t, ln, indlen); checkind(t, ln, indlen);
int newsz = sizeof(char) * (indlen + 2); int newsz = sizeof(char) * (indlen + 2);
char* newind = (char*)malloc(newsz); char* newind = (char*)malloc(newsz);
@ -155,7 +143,7 @@ char* mkind(struct Translator* t, struct line* ln, int indlen) {
return newind; return newind;
} }
char* atlab(struct Translator* t, char* label, int labellen) { char* atlab(TRANSLATOR* t, char* label, int labellen) {
int newsz = sizeof(char) * (labellen + 2); int newsz = sizeof(char) * (labellen + 2);
char* newind = (char*)malloc(newsz); char* newind = (char*)malloc(newsz);
snprintf(newind, newsz, "@%s", label); snprintf(newind, newsz, "@%s", label);
@ -163,7 +151,7 @@ char* atlab(struct Translator* t, char* label, int labellen) {
return newind; return newind;
} }
char* atn(struct Translator* t, int n) { char* atn(TRANSLATOR* t, int n) {
int newsz = sizeof(char) * (countplaces(n) + 2); int newsz = sizeof(char) * (countplaces(n) + 2);
char* newind = (char*)malloc(newsz); char* newind = (char*)malloc(newsz);
snprintf(newind, newsz, "@%i", n); snprintf(newind, newsz, "@%i", n);
@ -171,7 +159,7 @@ char* atn(struct Translator* t, int n) {
return newind; return newind;
} }
char* mklab(struct Translator* t, char* label, int labellen) { char* mklab(TRANSLATOR* t, char* label, int labellen) {
int newsz = sizeof(char) * (labellen + 3); int newsz = sizeof(char) * (labellen + 3);
char* newind = (char*)malloc(newsz); char* newind = (char*)malloc(newsz);
snprintf(newind, newsz, "(%s)", label); snprintf(newind, newsz, "(%s)", label);
@ -179,7 +167,7 @@ char* mklab(struct Translator* t, char* label, int labellen) {
return newind; return newind;
} }
char* mkgotolab(struct Translator* t, struct line* ln) { char* mkgotolab(TRANSLATOR* t, LINE* ln) {
int sz = sizeof(char) * (t->lastfunlen + strlen(ln->tokens[1]) + 3); int sz = sizeof(char) * (t->lastfunlen + strlen(ln->tokens[1]) + 3);
char* lab = (char*)malloc(sz); char* lab = (char*)malloc(sz);
snprintf(lab, sz, "@%s$%s", t->lastfun, ln->tokens[1]); snprintf(lab, sz, "@%s$%s", t->lastfun, ln->tokens[1]);
@ -187,7 +175,7 @@ char* mkgotolab(struct Translator* t, struct line* ln) {
return lab; return lab;
} }
char* mkstatind(struct Translator* t, struct line* ln, int indlen) { char* mkstatind(TRANSLATOR* t, LINE* ln, int indlen) {
checkind(t, ln, indlen); checkind(t, ln, indlen);
int newsz = sizeof(char) * (t->fnamelen + indlen + 3); int newsz = sizeof(char) * (t->fnamelen + indlen + 3);
char* newind = (char*)malloc(newsz); char* newind = (char*)malloc(newsz);
@ -196,7 +184,7 @@ char* mkstatind(struct Translator* t, struct line* ln, int indlen) {
return newind; return newind;
} }
char* mktempind(struct Translator* t, struct line* ln, int indlen) { char* mktempind(TRANSLATOR* t, LINE* ln, int indlen) {
checkind(t, ln, indlen); checkind(t, ln, indlen);
int intind = atoi(ln->tokens[2]); int intind = atoi(ln->tokens[2]);
int newsz = sizeof(char) * (indlen + 3); int newsz = sizeof(char) * (indlen + 3);
@ -206,7 +194,7 @@ char* mktempind(struct Translator* t, struct line* ln, int indlen) {
return newind; return newind;
} }
char* mkpointerind(struct Translator* t, struct line* ln, int indlen) { char* mkpointerind(TRANSLATOR* t, LINE* ln, int indlen) {
if(indlen > 1) { if(indlen > 1) {
fprintf(stderr, "Invalid index '%s'; file %s.vm, line %i\n", t->fname, ln->tokens[2], ln->truen); fprintf(stderr, "Invalid index '%s'; file %s.vm, line %i\n", t->fname, ln->tokens[2], ln->truen);
exit(1); exit(1);
@ -230,15 +218,15 @@ char* mkpointerind(struct Translator* t, struct line* ln, int indlen) {
return newind; return newind;
} }
void checkasmsize(struct Translator* t, int toadd) { void checkasmsize(TRANSLATOR* t, int toadd) {
int targ = sizeof(char*)*(t->asmind+toadd); int targ = sizeof(char*)*(t->asmlns->count+toadd);
if(targ >= t->asmsize) { if(targ >= t->asmlns->size) {
t->asmsize = targ * 2; t->asmlns->size = targ * 2;
t->asmlns = (char**)realloc(t->asmlns, t->asmsize); t->asmlns->items = (char**)realloc(t->asmlns->items, t->asmlns->size);
} }
} }
void checkopamnt(struct Translator* t, int amnt, struct line* ln) { void checkopamnt(TRANSLATOR* t, int amnt, LINE* ln) {
if(ln->tokenscount < 2) { if(ln->tokenscount < 2) {
fprintf(stderr, "Missing memory segment; file %s.vm, line %i\n", t->fname, ln->truen); fprintf(stderr, "Missing memory segment; file %s.vm, line %i\n", t->fname, ln->truen);
exit(1); exit(1);
@ -250,14 +238,14 @@ void checkopamnt(struct Translator* t, int amnt, struct line* ln) {
} }
} }
void checklab(struct Translator* t, struct line* ln) { void checklab(TRANSLATOR* t, LINE* ln) {
if(ln->tokenscount < 2) { if(ln->tokenscount < 2) {
fprintf(stderr, "Expected label; file %s.vm, line %i\n", t->fname, ln->truen); fprintf(stderr, "Expected label; file %s.vm, line %i\n", t->fname, ln->truen);
exit(1); exit(1);
} }
} }
void checkfun(struct Translator* t, struct line* ln) { void checkfun(TRANSLATOR* t, LINE* ln) {
if(ln->tokenscount < 2) { if(ln->tokenscount < 2) {
fprintf(stderr, "Expected function; file %s.vm, line %i\n", t->fname, ln->truen); fprintf(stderr, "Expected function; file %s.vm, line %i\n", t->fname, ln->truen);
exit(1); exit(1);
@ -269,23 +257,23 @@ void checkfun(struct Translator* t, struct line* ln) {
} }
} }
void addasm(struct Translator* t, char** insts, int instcount) { void addasm(TRANSLATOR* t, char** insts, int instcount) {
checkasmsize(t, instcount); checkasmsize(t, instcount);
for(int i = 0; i < instcount; i++) { for(int i = 0; i < instcount; i++) {
t->asmlns[t->asmind] = insts[i]; t->asmlns->items[t->asmlns->count] = insts[i];
t->asmind++; t->asmlns->count++;
} }
} }
void addasmlns(struct Translator* t, struct line* ln, char** insts, int instcount) { void addasmlns(TRANSLATOR* t, LINE* ln, char** insts, int instcount) {
// instruction comment // instruction comment
insts[0] = mkcom(t, ln); insts[0] = mkcom(t, ln);
addasm(t, insts, instcount); addasm(t, insts, instcount);
} }
void startpoppush(struct Translator* t, struct line* ln, int indlen, char** insts) { void startpoppush(TRANSLATOR* t, LINE* ln, int indlen, char** insts) {
// @segment // @segment
insts[1] = switchseg(t, ln); insts[1] = switchseg(t, ln);
@ -296,41 +284,41 @@ void startpoppush(struct Translator* t, struct line* ln, int indlen, char** inst
insts[3] = mkind(t, ln, indlen); insts[3] = mkind(t, ln, indlen);
} }
void pushcons(struct Translator* t, struct line* ln, int indlen) { void pushcons(TRANSLATOR* t, LINE* ln, int indlen) {
// @i // @i
tpushcons[1] = mkind(t, ln, indlen); tpushcons[1] = mkind(t, ln, indlen);
addasmlns(t, ln, tpushcons, TPUSHCONSN); addasmlns(t, ln, tpushcons, TPUSHCONSN);
} }
void pushstat(struct Translator* t, struct line* ln, int indlen) { void pushstat(TRANSLATOR* t, LINE* ln, int indlen) {
// @fname.i // @fname.i
tpushstat[1] = mkstatind(t, ln, indlen); tpushstat[1] = mkstatind(t, ln, indlen);
addasmlns(t, ln, tpushstat, TPUSHSTATN); addasmlns(t, ln, tpushstat, TPUSHSTATN);
} }
void pushtemp(struct Translator* t, struct line* ln, int indlen) { void pushtemp(TRANSLATOR* t, LINE* ln, int indlen) {
// @5+i // @5+i
tpushtemp[1] = mktempind(t, ln, indlen); tpushtemp[1] = mktempind(t, ln, indlen);
addasmlns(t, ln, tpushtemp, TPUSHTEMPN); addasmlns(t, ln, tpushtemp, TPUSHTEMPN);
} }
void pushpointer(struct Translator* t, struct line* ln, int indlen) { void pushpointer(TRANSLATOR* t, LINE* ln, int indlen) {
// @THIS/@THAT // @THIS/@THAT
tpushpointer[1] = mkpointerind(t, ln, indlen); tpushpointer[1] = mkpointerind(t, ln, indlen);
addasmlns(t, ln, tpushpointer, TPUSHPOINTERN); addasmlns(t, ln, tpushpointer, TPUSHPOINTERN);
} }
void push(struct Translator* t, struct line* ln, int indlen) { void push(TRANSLATOR* t, LINE* ln, int indlen) {
startpoppush(t, ln, indlen, tpush); startpoppush(t, ln, indlen, tpush);
addasmlns(t, ln, tpush, TPUSHN); addasmlns(t, ln, tpush, TPUSHN);
} }
void popstat(struct Translator* t, struct line* ln, int indlen) { void popstat(TRANSLATOR* t, LINE* ln, int indlen) {
// @fname.i // @fname.i
tpopstat[TPOPSTATN-2] = mkstatind(t, ln, indlen); tpopstat[TPOPSTATN-2] = mkstatind(t, ln, indlen);
@ -340,7 +328,7 @@ void popstat(struct Translator* t, struct line* ln, int indlen) {
addasmlns(t, ln, tpopstat, TPOPSTATN); addasmlns(t, ln, tpopstat, TPOPSTATN);
} }
void poptemp(struct Translator* t, struct line* ln, int indlen) { void poptemp(TRANSLATOR* t, LINE* ln, int indlen) {
// @5+i // @5+i
tpoptemp[TPOPTEMPN-2] = mktempind(t, ln, indlen); tpoptemp[TPOPTEMPN-2] = mktempind(t, ln, indlen);
@ -350,7 +338,7 @@ void poptemp(struct Translator* t, struct line* ln, int indlen) {
addasmlns(t, ln, tpoptemp, TPOPTEMPN); addasmlns(t, ln, tpoptemp, TPOPTEMPN);
} }
void poppointer(struct Translator* t, struct line* ln, int indlen) { void poppointer(TRANSLATOR* t, LINE* ln, int indlen) {
// @THIS/@THAT // @THIS/@THAT
tpoppointer[TPOPPOINTERN-2] = mkpointerind(t, ln, indlen); tpoppointer[TPOPPOINTERN-2] = mkpointerind(t, ln, indlen);
@ -360,19 +348,19 @@ void poppointer(struct Translator* t, struct line* ln, int indlen) {
addasmlns(t, ln, tpoppointer, TPOPPOINTERN); addasmlns(t, ln, tpoppointer, TPOPPOINTERN);
} }
void pop(struct Translator* t, struct line* ln, int indlen) { void pop(TRANSLATOR* t, LINE* ln, int indlen) {
startpoppush(t, ln, indlen, tpop); startpoppush(t, ln, indlen, tpop);
addasmlns(t, ln, tpop, TPOPN); addasmlns(t, ln, tpop, TPOPN);
} }
void arith(struct Translator* t, struct line* ln, char* op) { void arith(TRANSLATOR* t, LINE* ln, char* op) {
tarith[TARITHN-1] = heapstrtoclean(t, op); tarith[TARITHN-1] = heapstrtoclean(t, op);
addasmlns(t, ln, tarith, TARITHN); addasmlns(t, ln, tarith, TARITHN);
} }
void comp(struct Translator* t, struct line* ln, char* op) { void comp(TRANSLATOR* t, LINE* ln, char* op) {
char* label = mkcmplab(t, ln); char* label = mkcmplab(t, ln);
int labellen = strlen(label); int labellen = strlen(label);
@ -392,7 +380,7 @@ void comp(struct Translator* t, struct line* ln, char* op) {
addasmlns(t, ln, tcomp, TCOMPN); addasmlns(t, ln, tcomp, TCOMPN);
}; };
void label(struct Translator* t, struct line* ln) { void label(TRANSLATOR* t, LINE* ln) {
checklab(t, ln); checklab(t, ln);
// (funcname$label) // (funcname$label)
@ -406,7 +394,7 @@ void label(struct Translator* t, struct line* ln) {
addasmlns(t, ln, tlabel, TLABELN); addasmlns(t, ln, tlabel, TLABELN);
} }
void mygoto(struct Translator* t, struct line* ln) { void mygoto(TRANSLATOR* t, LINE* ln) {
checklab(t, ln); checklab(t, ln);
// @label // @label
@ -415,7 +403,7 @@ void mygoto(struct Translator* t, struct line* ln) {
addasmlns(t, ln, tgoto, TGOTON); addasmlns(t, ln, tgoto, TGOTON);
} }
void ifgoto(struct Translator* t, struct line* ln) { void ifgoto(TRANSLATOR* t, LINE* ln) {
checklab(t, ln); checklab(t, ln);
// @label // @label
@ -424,7 +412,7 @@ void ifgoto(struct Translator* t, struct line* ln) {
addasmlns(t, ln, tifgoto, TIFGOTON); addasmlns(t, ln, tifgoto, TIFGOTON);
} }
int pushframe(struct Translator* t, struct line* ln, char* retlab, int retlablen) { int pushframe(TRANSLATOR* t, LINE* ln, char* retlab, int retlablen) {
tcallstart[1] = atlab(t, retlab, retlablen); tcallstart[1] = atlab(t, retlab, retlablen);
addasmlns(t, ln, tcallstart, TCALLSTARTN); addasmlns(t, ln, tcallstart, TCALLSTARTN);
@ -437,7 +425,7 @@ int pushframe(struct Translator* t, struct line* ln, char* retlab, int retlablen
return TFRAMEVARSN + 1; return TFRAMEVARSN + 1;
} }
void call(struct Translator* t, struct line* ln) { void call(TRANSLATOR* t, LINE* ln) {
checkfun(t, ln); checkfun(t, ln);
// return label // return label
@ -461,7 +449,7 @@ void call(struct Translator* t, struct line* ln) {
addasm(t, tcalljmp, TCALLJMPN); addasm(t, tcalljmp, TCALLJMPN);
} }
void function(struct Translator* t, struct line* ln) { void function(TRANSLATOR* t, LINE* ln) {
if(!t->returned) { if(!t->returned) {
fprintf(stderr, "Last function did not return; file %s.vm, line %i\n", t->fname, ln->truen); fprintf(stderr, "Last function did not return; file %s.vm, line %i\n", t->fname, ln->truen);
exit(1); exit(1);
@ -490,7 +478,7 @@ void function(struct Translator* t, struct line* ln) {
} }
} }
void myreturn(struct Translator* t, struct line* ln) { void myreturn(TRANSLATOR* t, LINE* ln) {
addasmlns(t, ln, tstartreturn, TSTARTRETURNN); addasmlns(t, ln, tstartreturn, TSTARTRETURNN);
for(int i = TFRAMEVARSN-1; i >= 0; i--) { for(int i = TFRAMEVARSN-1; i >= 0; i--) {
@ -501,7 +489,7 @@ void myreturn(struct Translator* t, struct line* ln) {
addasm(t, tendreturn, TENDRETURNN); addasm(t, tendreturn, TENDRETURNN);
} }
void switchpush(struct Translator* t, struct line* ln) { void switchpush(TRANSLATOR* t, LINE* ln) {
checkopamnt(t, 3, ln); checkopamnt(t, 3, ln);
char* seg = ln->tokens[1]; char* seg = ln->tokens[1];
int indlen = strlen(ln->tokens[2]); int indlen = strlen(ln->tokens[2]);
@ -518,7 +506,7 @@ void switchpush(struct Translator* t, struct line* ln) {
push(t, ln, indlen); push(t, ln, indlen);
} }
void switchpop(struct Translator* t, struct line* ln) { void switchpop(TRANSLATOR* t, LINE* ln) {
checkopamnt(t, 3, ln); checkopamnt(t, 3, ln);
char* seg = ln->tokens[1]; char* seg = ln->tokens[1];
int indlen = strlen(ln->tokens[2]); int indlen = strlen(ln->tokens[2]);
@ -533,9 +521,9 @@ void switchpop(struct Translator* t, struct line* ln) {
pop(t, ln, indlen); pop(t, ln, indlen);
} }
void switchop(struct Translator* t, struct line* ln) { void switchop(TRANSLATOR* t, LINE* ln) {
char* op = ln->tokens[0]; char* op = ln->tokens[0];
short returned = 0; bool returned = false;
if(!strcmp(op, "push")) if(!strcmp(op, "push"))
switchpush(t, ln); switchpush(t, ln);
@ -567,7 +555,7 @@ void switchop(struct Translator* t, struct line* ln) {
ifgoto(t, ln); ifgoto(t, ln);
else if(!strcmp(op, "return")) { else if(!strcmp(op, "return")) {
myreturn(t, ln); myreturn(t, ln);
returned = 1; returned = true;
} }
else if(!strcmp(op, "function")) else if(!strcmp(op, "function"))
function(t, ln); function(t, ln);
@ -581,7 +569,7 @@ void switchop(struct Translator* t, struct line* ln) {
t->returned = returned; t->returned = returned;
} }
void translate(struct Translator* t) { void translate(TRANSLATOR* t) {
for(int i = 0; i < t->lns->count; i++) for(int i = 0; i < t->lns->count; i++)
switchop(t, t->lns->lns[i]); switchop(t, t->lns->lns[i]);
@ -591,19 +579,23 @@ void translate(struct Translator* t) {
} }
} }
struct Translator* mktranslator(struct lnarray* lns, char* fname) { TRANSLATOR* mktranslator(LINEARRAY* lns, char* fname) {
struct Translator* t = (struct Translator*)malloc(sizeof(struct Translator)); TRANSLATOR* t = (TRANSLATOR*)malloc(sizeof(TRANSLATOR));
t->asmsize = sizeof(char*)*(lns->count * 15);
t->asmind = 0; t->asmlns = (STRLIST*)malloc(sizeof(STRLIST));
t->asmlns->count = 0;
t->asmlns->size = sizeof(char*)*(lns->count * 15);
t->asmlns->items = (char**)malloc(t->asmlns->size);
t->toclean = (STRLIST*)malloc(sizeof(STRLIST));
t->toclean->count = 0;
t->toclean->size = sizeof(char*)*(lns->count * 5);
t->toclean->items = (char**)malloc(t->toclean->size);
t->funcount = 0; t->funcount = 0;
t->retind = 0; t->retind = 0;
t->cmpind = 0; t->cmpind = 0;
t->returned = 1; t->returned = true;
t->asmlns = (char**)malloc(t->asmsize);
t->tocleanind = 0;
t->tocleansize = sizeof(char*)*(lns->count * 5);
t->toclean = (char**)malloc(t->tocleansize);
t->lns = lns; t->lns = lns;
t->fname = fname; t->fname = fname;

View File

@ -1,15 +1,18 @@
#ifndef translator #ifndef TRANSLATOR_H
#define translator #define TRANSLATOR_H
#include <stdbool.h>
#include "parser.h" #include "parser.h"
struct Translator { typedef struct {
char** asmlns; char** items;
int asmind; int count;
int asmsize; int size;
char** toclean; } STRLIST;
int tocleansize;
int tocleanind; typedef struct {
struct lnarray* lns; STRLIST* asmlns;
STRLIST* toclean;
LINEARRAY* lns;
char* fname; char* fname;
int fnamelen; int fnamelen;
char* lastfun; char* lastfun;
@ -17,11 +20,11 @@ struct Translator {
int funcount; int funcount;
int retind; int retind;
int cmpind; int cmpind;
short returned; bool returned;
}; } TRANSLATOR;
void freetranslator(struct Translator* t); void freetranslator(TRANSLATOR* t);
void printasmlns(struct Translator* t, FILE* stream); void printasmlns(TRANSLATOR* t, FILE* stream);
void translate(struct Translator* t); void translate(TRANSLATOR* t);
struct Translator* mktranslator(struct lnarray* lns, char* fname); TRANSLATOR* mktranslator(LINEARRAY* lns, char* fname);
#endif #endif

14
util.c
View File

@ -8,3 +8,17 @@ char* heapstr(const char* str, int len) {
strcpy(outstr, str); strcpy(outstr, str);
return outstr; return outstr;
} }
int countplaces(int n) {
int places = 1;
int divisor = 1;
if(n < 0) {
n = -n;
places++;
}
while(n / divisor >= 10) {
places++;
divisor *= 10;
}
return places;
}

1
util.h
View File

@ -1 +1,2 @@
char* heapstr(const char* str, int len); char* heapstr(const char* str, int len);
int countplaces(int n);