Add file outputting

This commit is contained in:
Augusto Gunsch 2020-11-01 19:11:11 -03:00
parent 3241592364
commit 5fc441477e
No known key found for this signature in database
GPG Key ID: F7EEFE29825C72DC
4 changed files with 28 additions and 14 deletions

26
main.c
View File

@ -29,6 +29,17 @@ char* verfname(char* fname) {
exit(1);
}
char* getoutname(char* fname) {
int len = strlen(fname);
int sz = sizeof(char)*(len+2);
char* outname = (char*)malloc(sz);
strcpy(outname, fname);
int ind = len-2;
char* ext = sizeof(char)*ind+outname;
snprintf(ext, 4, "asm");
return outname;
}
int main(int argc, char* argv[]) {
if(argc < 2) {
fprintf(stderr, "Usage: %s {file}\n", argv[0]);
@ -52,12 +63,17 @@ int main(int argc, char* argv[]) {
struct Translator* t = mktranslator(p->lns, fname);
translate(t);
freeparser(p);
// printing
printasmlns(t);
// freeing asmlns
free(fname);
// output
char* outname = getoutname(argv[1]);
FILE* output = fopen(outname, "w");
printasmlns(t, output);
fclose(output);
// freeing rest
free(outname);
freetranslator(t);
return 0;

View File

@ -105,21 +105,19 @@ char* tarith[TARITHN] = {
""
};
#define TNEGN 5
#define TNEGN 4
char* tneg[TNEGN] = {
"",
"@SP",
"M=M-1",
"A=M",
"A=M-1",
"M=-M",
};
#define TNOTN 5
#define TNOTN 4
char* tnot[TNOTN] = {
"",
"@SP",
"M=M-1",
"A=M",
"A=M-1",
"M=!M",
};

View File

@ -39,9 +39,9 @@ void freetranslator(struct Translator* t) {
free(t);
}
void printasmlns(struct Translator* t) {
void printasmlns(struct Translator* t, FILE* stream) {
for(int i = 0; i < t->asmind; i++)
printf("%s\n", t->asmlns[i]->instr);
fprintf(stream, "%s\n", t->asmlns[i]->instr);
}
char* heapstr(struct Translator* t, const char* input) {

View File

@ -20,7 +20,7 @@ struct Translator {
};
void freetranslator(struct Translator* t);
void printasmlns(struct Translator* t);
void printasmlns(struct Translator* t, FILE* stream);
void translate(struct Translator* t);
struct Translator* mktranslator(struct lnarray* lns, char* fname);
#endif