Add file outputting
This commit is contained in:
parent
3241592364
commit
5fc441477e
26
main.c
26
main.c
|
@ -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;
|
||||
|
|
10
templates.h
10
templates.h
|
@ -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",
|
||||
};
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue