Compare commits

...

5 Commits

Author SHA1 Message Date
Augusto Gunsch 8b188ddb37
Add README.md 2020-11-21 11:30:25 -03:00
Augusto Gunsch ca2c128e67
Change translator output format 2020-11-21 11:25:30 -03:00
Augusto Gunsch 6262faf28a
Improve templates 2020-11-19 10:43:25 -03:00
Augusto Gunsch d15bd48aba
Fix relative path name bug 2020-11-19 08:45:31 -03:00
Augusto Gunsch 4e532a5cb9
Code refactoring 2020-11-19 08:10:51 -03:00
12 changed files with 437 additions and 275 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 vmtranslator
tags

View File

@ -1,6 +1,6 @@
FILES = parser.c main.c translator.c util.c FILES = parser.c main.c translator.c util.c
INCLUDES = -I. INCLUDES = -I.
CFLAGS = -std=c99 -g CFLAGS = -std=c99
OUTFILE = vmtranslator OUTFILE = vmtranslator
main: ${FILES} main: ${FILES}

2
README.md Normal file
View File

@ -0,0 +1,2 @@
# vm-translator
Virtual machine language translator, as specified in [nand2tetris project 8](https://www.nand2tetris.org/project08).<br>

66
main.c
View File

@ -4,23 +4,34 @@
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>
#include <dirent.h> #include <dirent.h>
#include <stdbool.h>
#include <unistd.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 { #include <limits.h>
#ifndef PATH_MAX
#ifdef __linux__
#include <linux/limits.h>
#else
#define PATH_MAX 512
#endif
#endif
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 +45,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 +57,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 +69,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,30 +78,39 @@ 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 olddir[PATH_MAX];
getcwd(olddir, PATH_MAX);
chdir(input);
char buf[PATH_MAX];
getcwd(buf, PATH_MAX);
chdir(olddir);
char* name = getname(buf, strlen(buf));
int sz = sizeof(char) * (strlen(name)+len+6); int sz = sizeof(char) * (strlen(name)+len+6);
outname = (char*)malloc(sz); outname = (char*)malloc(sz);
snprintf(outname, sz, "%s/%s.asm", input, name); snprintf(outname, sz, "%s/%s.asm", input, name);
@ -106,7 +126,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 +142,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 +202,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 +219,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,15 +238,15 @@ 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);
printasmlns(t, output); printlns(t->output, output);
// freeing rest // freeing rest
freetranslator(t); freetranslator(t);

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 freelnarray(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,8 +18,8 @@ void freelns(struct lnarray* lns) {
free(lns); free(lns);
} }
void freeparser(struct Parser* p) { void freeparser(PARSER* p) {
freelns(p->lns); freelnarray(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

@ -1,8 +1,12 @@
#ifndef templates #ifndef templates
#define templates #define templates
#define TPUSHN 11 typedef struct {
char* tpush[TPUSHN] = { char** items;
int count;
} TEMPLATE;
char* tpushlns[] = {
"", "",
"", "",
"", "",
@ -15,9 +19,12 @@ char* tpush[TPUSHN] = {
"@SP", "@SP",
"M=M+1", "M=M+1",
}; };
TEMPLATE tpush = {
.items = tpushlns,
.count = sizeof(tpushlns) / sizeof(char*)
};
#define TPUSHCONSN 8 char* tpushconslns[] = {
char* tpushcons[TPUSHCONSN] = {
"", "",
"", "",
"D=A", "D=A",
@ -27,9 +34,12 @@ char* tpushcons[TPUSHCONSN] = {
"@SP", "@SP",
"M=M+1", "M=M+1",
}; };
TEMPLATE tpushcons = {
.items = tpushconslns,
.count = sizeof(tpushconslns) / sizeof(char*)
};
#define TPUSHSTATN 8 char* tpushstatlns[] = {
char* tpushstat[TPUSHSTATN] = {
"", "",
"", "",
"D=M", "D=M",
@ -39,15 +49,22 @@ char* tpushstat[TPUSHSTATN] = {
"@SP", "@SP",
"M=M+1", "M=M+1",
}; };
TEMPLATE tpushstat = {
.items = tpushstatlns,
.count = sizeof(tpushstatlns) / sizeof(char*)
};
#define TPUSHTEMPN TPUSHSTATN TEMPLATE tpushtemp = {
char** tpushtemp = tpushstat; .items = tpushstatlns,
.count = sizeof(tpushstatlns) / sizeof(char*)
};
#define TPUSHPOINTERN TPUSHSTATN TEMPLATE tpushpointer = {
char** tpushpointer = tpushstat; .items = tpushstatlns,
.count = sizeof(tpushstatlns) / sizeof(char*)
};
#define TPOPN 13 char* tpoplns[] = {
char* tpop[TPOPN] = {
"", "",
"", "",
"", "",
@ -62,9 +79,12 @@ char* tpop[TPOPN] = {
"A=M", "A=M",
"M=D" "M=D"
}; };
TEMPLATE tpop = {
.items = tpoplns,
.count = sizeof(tpoplns) / sizeof(char*)
};
#define TPOPSTATN 6 char* tpopstatlns[] = {
char* tpopstat[TPOPSTATN] = {
"", "",
"@SP", "@SP",
"AM=M-1", "AM=M-1",
@ -72,15 +92,22 @@ char* tpopstat[TPOPSTATN] = {
"", "",
"" ""
}; };
TEMPLATE tpopstat = {
.items = tpopstatlns,
.count = sizeof(tpopstatlns) / sizeof(char*)
};
#define TPOPTEMPN TPOPSTATN TEMPLATE tpoptemp = {
char** tpoptemp = tpopstat; .items = tpopstatlns,
.count = sizeof(tpopstatlns) / sizeof(char*)
};
#define TPOPPOINTERN TPOPSTATN TEMPLATE tpoppointer = {
char** tpoppointer = tpopstat; .items = tpopstatlns,
.count = sizeof(tpopstatlns) / sizeof(char*)
};
#define TARITHN 6 char* tarithlns[] = {
char* tarith[TARITHN] = {
"", "",
"@SP", "@SP",
"AM=M-1", "AM=M-1",
@ -88,25 +115,34 @@ char* tarith[TARITHN] = {
"A=A-1", "A=A-1",
"" ""
}; };
TEMPLATE tarith = {
.items = tarithlns,
.count = sizeof(tarithlns) / sizeof(char*)
};
#define TNEGN 4 char* tneglns[] = {
char* tneg[TNEGN] = {
"", "",
"@SP", "@SP",
"A=M-1", "A=M-1",
"M=-M", "M=-M",
}; };
TEMPLATE tneg = {
.items = tneglns,
.count = sizeof(tneglns) / sizeof(char*)
};
#define TNOTN 4 char* tnotlns[] = {
char* tnot[TNOTN] = {
"", "",
"@SP", "@SP",
"A=M-1", "A=M-1",
"M=!M", "M=!M",
}; };
TEMPLATE tnot = {
.items = tnotlns,
.count = sizeof(tnotlns) / sizeof(char*)
};
#define TCOMPN 13 char* tcomplns[] = {
char* tcomp[TCOMPN] = {
"", "",
"@SP", "@SP",
"AM=M-1", "AM=M-1",
@ -121,22 +157,31 @@ char* tcomp[TCOMPN] = {
"M=0", "M=0",
"" ""
}; };
TEMPLATE tcomp = {
.items = tcomplns,
.count = sizeof(tcomplns) / sizeof(char*)
};
#define TLABELN 2 char* tlabellns[] = {
char* tlabel[TLABELN] = {
"", "",
"" ""
}; };
TEMPLATE tlabel = {
.items = tlabellns,
.count = sizeof(tlabellns) / sizeof(char*)
};
#define TGOTON 3 char* tgotolns[] = {
char* tgoto[TGOTON] = {
"", "",
"", "",
"0;JMP" "0;JMP"
}; };
TEMPLATE tgoto = {
.items = tgotolns,
.count = sizeof(tgotolns) / sizeof(char*)
};
#define TIFGOTON 6 char* tifgotolns[] = {
char* tifgoto[TIFGOTON] = {
"", "",
"@SP", "@SP",
"AM=M-1", "AM=M-1",
@ -144,9 +189,12 @@ char* tifgoto[TIFGOTON] = {
"", "",
"D;JNE" "D;JNE"
}; };
TEMPLATE tifgoto = {
.items = tifgotolns,
.count = sizeof(tifgotolns) / sizeof(char*)
};
#define TCALLSTARTN 8 char* tcallstartlns[] = {
char* tcallstart[TCALLSTARTN] = {
"", "",
"", "",
"D=A", "D=A",
@ -156,9 +204,12 @@ char* tcallstart[TCALLSTARTN] = {
"@SP", "@SP",
"M=M+1", "M=M+1",
}; };
TEMPLATE tcallstart = {
.items = tcallstartlns,
.count = sizeof(tcallstartlns) / sizeof(char*)
};
#define TCALLPUSHN 7 char* tcallpushlns[] = {
char* tcallpush[TCALLPUSHN] = {
"", "",
"D=M", "D=M",
"@SP", "@SP",
@ -167,9 +218,12 @@ char* tcallpush[TCALLPUSHN] = {
"@SP", "@SP",
"M=M+1", "M=M+1",
}; };
TEMPLATE tcallpush = {
.items = tcallpushlns,
.count = sizeof(tcallpushlns) / sizeof(char*)
};
#define TCALLSETARGN 8 char* tcallsetarglns[] = {
char* tcallsetarg[TCALLSETARGN] = {
"@SP", "@SP",
"D=M", "D=M",
"@LCL", "@LCL",
@ -179,39 +233,54 @@ char* tcallsetarg[TCALLSETARGN] = {
"@ARG", "@ARG",
"M=D" "M=D"
}; };
TEMPLATE tcallsetarg = {
.items = tcallsetarglns,
.count = sizeof(tcallsetarglns) / sizeof(char*)
};
#define TCALLJMPN 3 char* tcalljmplns[] = {
char* tcalljmp[TCALLJMPN] = {
"", "",
"0;JMP", "0;JMP",
"" ""
}; };
TEMPLATE tcalljmp = {
.items = tcalljmplns,
.count = sizeof(tcalljmplns) / sizeof(char*)
};
#define TFRAMEVARSN 4 char* tframevarslns[] = {
char* tframevars[TFRAMEVARSN] = {
"@LCL", "@LCL",
"@ARG", "@ARG",
"@THIS", "@THIS",
"@THAT" "@THAT"
}; };
TEMPLATE tframevars = {
.items = tframevarslns,
.count = sizeof(tframevarslns) / sizeof(char*)
};
#define TFUNCTIONN 2 char* tfunctionlns[] = {
char* tfunction[TFUNCTIONN] = {
"", "",
"" ""
}; };
TEMPLATE tfunction = {
.items = tfunctionlns,
.count = sizeof(tfunctionlns) / sizeof(char*)
};
#define TFUNCTIONPUSHN 5 char* tfunctionpushlns[] = {
char* tfunctionpush[TFUNCTIONPUSHN] = {
"@SP", "@SP",
"A=M", "A=M",
"M=0", "M=0",
"@SP", "@SP",
"M=M+1" "M=M+1"
}; };
TEMPLATE tfunctionpush = {
.items = tfunctionpushlns,
.count = sizeof(tfunctionpushlns) / sizeof(char*)
};
#define TSTARTRETURNN 18 char* tstartreturnlns[] = {
char* tstartreturn[TSTARTRETURNN] = {
"", "",
"@LCL", "@LCL",
"D=M", "D=M",
@ -231,21 +300,31 @@ char* tstartreturn[TSTARTRETURNN] = {
"@SP", "@SP",
"M=D" "M=D"
}; };
TEMPLATE tstartreturn = {
.items = tstartreturnlns,
.count = sizeof(tstartreturnlns) / sizeof(char*)
};
#define TRETPOPN 5 char* tretpoplns[] = {
char* tretpop[TRETPOPN] = {
"@LCL", "@LCL",
"AM=M-1", "AM=M-1",
"D=M", "D=M",
"", "",
"M=D", "M=D",
}; };
TEMPLATE tretpop = {
.items = tretpoplns,
.count = sizeof(tretpoplns) / sizeof(char*)
};
#define TENDRETURNN 3 char* tendreturnlns[] = {
char* tendreturn[TENDRETURNN] = {
"@R13", "@R13",
"A=M", "A=M",
"0;JMP" "0;JMP"
}; };
TEMPLATE tendreturn = {
.items = tendreturnlns,
.count = sizeof(tendreturnlns) / sizeof(char*)
};
#endif #endif

View File

@ -6,40 +6,38 @@
#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);
freelns(t->output);
freetoclean(t); freetoclean(t);
free(t); free(t);
} }
void printasmlns(struct Translator* t, FILE* stream) { char* heapstrtoclean(TRANSLATOR* t, const char* input) {
for(int i = 0; i < t->asmind; i++)
fprintf(stream, "%s\n", t->asmlns[i]);
}
char* heapstrtoclean(struct 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 +51,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 +63,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 +85,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 +93,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 +122,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 +139,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 +147,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 +155,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 +163,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 +171,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 +180,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 +190,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 +214,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 +234,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,130 +253,138 @@ void checkfun(struct Translator* t, struct line* ln) {
} }
} }
void addasm(struct Translator* t, char** insts, int instcount) { void pushln(TRANSLATOR* t, char* content) {
checkasmsize(t, instcount); t->curln->content = content;
t->curln->truen = t->lncount;
for(int i = 0; i < instcount; i++) { LINELIST* nextln = (LINELIST*)malloc(sizeof(LINELIST));
t->asmlns[t->asmind] = insts[i]; t->curln->next = nextln;
t->asmind++; t->lastln = t->curln;
} t->curln = nextln;
t->lncount++;
} }
void addasmlns(struct Translator* t, struct line* ln, char** insts, int instcount) { void addasm(TRANSLATOR* t, TEMPLATE* tp) {
checkasmsize(t, tp->count);
for(int i = 0; i < tp->count; i++)
pushln(t, tp->items[i]);
}
void addasmlns(TRANSLATOR* t, LINE* ln, TEMPLATE* tp) {
// instruction comment // instruction comment
insts[0] = mkcom(t, ln); tp->items[0] = mkcom(t, ln);
addasm(t, insts, instcount); addasm(t, tp);
} }
void startpoppush(struct Translator* t, struct line* ln, int indlen, char** insts) { void startpoppush(TRANSLATOR* t, LINE* ln, int indlen, TEMPLATE* tp) {
// @segment // @segment
insts[1] = switchseg(t, ln); tp->items[1] = switchseg(t, ln);
// D=M // D=M
insts[2] = heapstrtoclean(t, "D=M"); tp->items[2] = heapstrtoclean(t, "D=M");
// @i // @i
insts[3] = mkind(t, ln, indlen); tp->items[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.items[1] = mkind(t, ln, indlen);
addasmlns(t, ln, tpushcons, TPUSHCONSN); addasmlns(t, ln, &tpushcons);
} }
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.items[1] = mkstatind(t, ln, indlen);
addasmlns(t, ln, tpushstat, TPUSHSTATN); addasmlns(t, ln, &tpushstat);
} }
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.items[1] = mktempind(t, ln, indlen);
addasmlns(t, ln, tpushtemp, TPUSHTEMPN); addasmlns(t, ln, &tpushtemp);
} }
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.items[1] = mkpointerind(t, ln, indlen);
addasmlns(t, ln, tpushpointer, TPUSHPOINTERN); addasmlns(t, ln, &tpushpointer);
} }
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);
} }
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.items[tpopstat.count-2] = mkstatind(t, ln, indlen);
// M=D // M=D
tpopstat[TPOPSTATN-1] = heapstrtoclean(t, "M=D"); tpopstat.items[tpopstat.count-1] = heapstrtoclean(t, "M=D");
addasmlns(t, ln, tpopstat, TPOPSTATN); addasmlns(t, ln, &tpopstat);
} }
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.items[tpoptemp.count-2] = mktempind(t, ln, indlen);
// M=D // M=D
tpoptemp[TPOPTEMPN-1] = heapstrtoclean(t, "M=D"); tpoptemp.items[tpoptemp.count-1] = heapstrtoclean(t, "M=D");
addasmlns(t, ln, tpoptemp, TPOPTEMPN); addasmlns(t, ln, &tpoptemp);
} }
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.items[tpoppointer.count-2] = mkpointerind(t, ln, indlen);
// M=D // M=D
tpoppointer[TPOPPOINTERN-1] = heapstrtoclean(t, "M=D"); tpoppointer.items[tpoppointer.count-1] = heapstrtoclean(t, "M=D");
addasmlns(t, ln, tpoppointer, TPOPPOINTERN); addasmlns(t, ln, &tpoppointer);
} }
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);
} }
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.items[tarith.count-1] = heapstrtoclean(t, op);
addasmlns(t, ln, tarith, TARITHN); addasmlns(t, ln, &tarith);
} }
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);
// @label // @label
tcomp[TCOMPN-6] = atlab(t, label, labellen); tcomp.items[tcomp.count-6] = atlab(t, label, labellen);
// D;J(op) // D;J(op)
int opsz = sizeof(char) * 6; int opsz = sizeof(char) * 6;
char* trueop = (char*)malloc(opsz); char* trueop = (char*)malloc(opsz);
snprintf(trueop, opsz, "D;J%s", op); snprintf(trueop, opsz, "D;J%s", op);
tcomp[TCOMPN-5] = trueop; tcomp.items[tcomp.count-5] = trueop;
pushtoclean(t, trueop); pushtoclean(t, trueop);
// (label) // (label)
tcomp[TCOMPN-1] = mklab(t, label, labellen); tcomp.items[tcomp.count-1] = mklab(t, label, labellen);
addasmlns(t, ln, tcomp, TCOMPN); addasmlns(t, ln, &tcomp);
}; };
void label(struct Translator* t, struct line* ln) { void label(TRANSLATOR* t, LINE* ln) {
checklab(t, ln); checklab(t, ln);
// (funcname$label) // (funcname$label)
@ -401,43 +393,43 @@ void label(struct Translator* t, struct line* ln) {
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]);
pushtoclean(t, lab); pushtoclean(t, lab);
tlabel[TLABELN-1] = lab; tlabel.items[tlabel.count-1] = lab;
addasmlns(t, ln, tlabel, TLABELN); addasmlns(t, ln, &tlabel);
} }
void mygoto(struct Translator* t, struct line* ln) { void mygoto(TRANSLATOR* t, LINE* ln) {
checklab(t, ln); checklab(t, ln);
// @label // @label
tgoto[TGOTON-2] = mkgotolab(t, ln); tgoto.items[tgoto.count-2] = mkgotolab(t, ln);
addasmlns(t, ln, tgoto, TGOTON); addasmlns(t, ln, &tgoto);
} }
void ifgoto(struct Translator* t, struct line* ln) { void ifgoto(TRANSLATOR* t, LINE* ln) {
checklab(t, ln); checklab(t, ln);
// @label // @label
tifgoto[TIFGOTON-2] = mkgotolab(t, ln); tifgoto.items[tifgoto.count-2] = mkgotolab(t, ln);
addasmlns(t, ln, tifgoto, TIFGOTON); addasmlns(t, ln, &tifgoto);
} }
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.items[1] = atlab(t, retlab, retlablen);
addasmlns(t, ln, tcallstart, TCALLSTARTN); addasmlns(t, ln, &tcallstart);
for(int i = 0; i < TFRAMEVARSN; i++) { for(int i = 0; i < tframevars.count; i++) {
tcallpush[0] = tframevars[i]; tcallpush.items[0] = tframevars.items[i];
addasm(t, tcallpush, TCALLPUSHN); addasm(t, &tcallpush);
} }
return TFRAMEVARSN + 1; return tframevars.count + 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
@ -451,17 +443,17 @@ void call(struct Translator* t, struct line* ln) {
int nargslen = strlen(ln->tokens[2]); int nargslen = strlen(ln->tokens[2]);
checknargs(t, ln, nargslen); checknargs(t, ln, nargslen);
int nargs = atoi(ln->tokens[2]); int nargs = atoi(ln->tokens[2]);
tcallsetarg[TCALLSETARGN-4] = atn(t, nargs + framesize); tcallsetarg.items[tcallsetarg.count-4] = atn(t, nargs + framesize);
addasm(t, tcallsetarg, TCALLSETARGN); addasm(t, &tcallsetarg);
// jmp // jmp
int jmplen = strlen(ln->tokens[1]); int jmplen = strlen(ln->tokens[1]);
tcalljmp[TCALLJMPN-3] = atlab(t, ln->tokens[1], jmplen); tcalljmp.items[tcalljmp.count-3] = atlab(t, ln->tokens[1], jmplen);
tcalljmp[TCALLJMPN-1] = mklab(t, retlab, retlablen); tcalljmp.items[tcalljmp.count-1] = mklab(t, retlab, retlablen);
addasm(t, tcalljmp, TCALLJMPN); addasm(t, &tcalljmp);
} }
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);
@ -477,8 +469,8 @@ void function(struct Translator* t, struct line* ln) {
t->cmpind = 0; t->cmpind = 0;
// (funcname) // (funcname)
tfunction[1] = mklab(t, ln->tokens[1], funlen); tfunction.items[1] = mklab(t, ln->tokens[1], funlen);
addasmlns(t, ln, tfunction, TFUNCTIONN); addasmlns(t, ln, &tfunction);
// repeat nVars times: // repeat nVars times:
int nlocalslen = strlen(ln->tokens[2]); int nlocalslen = strlen(ln->tokens[2]);
@ -486,22 +478,22 @@ void function(struct Translator* t, struct line* ln) {
int nlocals = atoi(ln->tokens[2]); int nlocals = atoi(ln->tokens[2]);
for(int i = 0; i < nlocals; i++) { for(int i = 0; i < nlocals; i++) {
addasm(t, tfunctionpush, TFUNCTIONPUSHN); addasm(t, &tfunctionpush);
} }
} }
void myreturn(struct Translator* t, struct line* ln) { void myreturn(TRANSLATOR* t, LINE* ln) {
addasmlns(t, ln, tstartreturn, TSTARTRETURNN); addasmlns(t, ln, &tstartreturn);
for(int i = TFRAMEVARSN-1; i >= 0; i--) { for(int i = tframevars.count-1; i >= 0; i--) {
tretpop[TRETPOPN-2] = tframevars[i]; tretpop.items[tretpop.count-2] = tframevars.items[i];
addasm(t, tretpop, TRETPOPN); addasm(t, &tretpop);
} }
addasm(t, tendreturn, TENDRETURNN); addasm(t, &tendreturn);
} }
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 +510,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 +525,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);
@ -546,7 +538,7 @@ void switchop(struct Translator* t, struct line* ln) {
else if(!strcmp(op, "sub")) else if(!strcmp(op, "sub"))
arith(t, ln, "M=M-D"); arith(t, ln, "M=M-D");
else if(!strcmp(op, "neg")) else if(!strcmp(op, "neg"))
addasmlns(t, ln, tneg, TNEGN); addasmlns(t, ln, &tneg);
else if(!strcmp(op, "eq")) else if(!strcmp(op, "eq"))
comp(t, ln, "EQ"); comp(t, ln, "EQ");
else if(!strcmp(op, "gt")) else if(!strcmp(op, "gt"))
@ -558,7 +550,7 @@ void switchop(struct Translator* t, struct line* ln) {
else if(!strcmp(op, "or")) else if(!strcmp(op, "or"))
arith(t, ln, "M=D|M"); arith(t, ln, "M=D|M");
else if(!strcmp(op, "not")) else if(!strcmp(op, "not"))
addasmlns(t, ln, tnot, TNOTN); addasmlns(t, ln, &tnot);
else if(!strcmp(op, "label")) else if(!strcmp(op, "label"))
label(t, ln); label(t, ln);
else if(!strcmp(op, "goto")) else if(!strcmp(op, "goto"))
@ -567,7 +559,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 +573,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]);
@ -589,21 +581,32 @@ void translate(struct Translator* t) {
fprintf(stderr, "Expected return before end of file; file %s.vm, line %i\n", t->fname, t->lns->count-1); fprintf(stderr, "Expected return before end of file; file %s.vm, line %i\n", t->fname, t->lns->count-1);
exit(1); exit(1);
} }
t->lastln->next = NULL;
free(t->curln);
} }
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);
LINELIST* newln = (LINELIST*)malloc(sizeof(LINELIST));
t->output = newln;
t->curln = newln;
t->lncount = 0;
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,19 @@
#ifndef translator #ifndef TRANSLATOR_H
#define translator #define TRANSLATOR_H
#include <stdbool.h>
#include "parser.h" #include "parser.h"
#include "util.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 +21,15 @@ struct Translator {
int funcount; int funcount;
int retind; int retind;
int cmpind; int cmpind;
short returned; bool returned;
};
void freetranslator(struct Translator* t); LINELIST* output;
void printasmlns(struct Translator* t, FILE* stream); LINELIST* curln;
void translate(struct Translator* t); LINELIST* lastln;
struct Translator* mktranslator(struct lnarray* lns, char* fname); int lncount;
} TRANSLATOR;
void freetranslator(TRANSLATOR* t);
void translate(TRANSLATOR* t);
TRANSLATOR* mktranslator(LINEARRAY* lns, char* fname);
#endif #endif

29
util.c
View File

@ -8,3 +8,32 @@ 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;
}
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);
}

16
util.h
View File

@ -1 +1,17 @@
#ifndef UTIL_H
#define UTIL_H
#include <stdio.h>
char* heapstr(const char* str, int len); 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