Fix several bugs

This commit is contained in:
Augusto Gunsch 2020-10-31 12:40:06 -03:00
parent 30ea5c33e3
commit bece0c4f64
No known key found for this signature in database
GPG Key ID: F7EEFE29825C72DC
3 changed files with 48 additions and 47 deletions

22
main.c
View File

@ -26,25 +26,19 @@ int main(int argc, char* argv[]) {
fclose(input); fclose(input);
// translating // translating
int asmind = 0; int asmcount = 0;
struct asmln** asmlns = translate(lns, lncount, &asmind); struct asmln** asmlns = translate(lns, lncount, &asmcount);
// freeing lns
freelns(lns, lncount);
// printing // printing
for(int i = 0; i < asmind; i++) { for(int i = 0; i < asmcount; i++) {
printf("%s\n", asmlns[i]->instr); printf("%s\n", asmlns[i]->instr);
} }
/*
for(int i = 0; i < lncount; i++) {
int tkcount = lns[i]->tokenscount;
for(int j = 0; j < tkcount; j++) {
printf("%s ", lns[i]->tokens[j]);
}
printf("\n");
}
*/
// freeing // freeing asmlns
freelns(lns, lncount); freeasmlns(asmlns, asmcount);
return 0; return 0;
} }

View File

@ -16,6 +16,14 @@ enum operation getop(struct line* ln) {
exit(1); exit(1);
} }
void freeasmlns(struct asmln** lns, int count) {
for(int i = 0; i < count; i++) {
free(lns[i]->instr);
free(lns[i]);
}
free(lns);
}
char* heapstr(const char* input) { char* heapstr(const char* input) {
char* newstr = (char*)malloc(sizeof(char)*(strlen(input)+1)); char* newstr = (char*)malloc(sizeof(char)*(strlen(input)+1));
strcpy(newstr, input); strcpy(newstr, input);
@ -59,11 +67,10 @@ char* mkind(char* ind, int indsz) {
return newind; return newind;
} }
void checkasmsize(int* size, struct asmln** lns, int targ) { void checkasmsize(int* size, struct asmln*** lns, int targ) {
if(targ >= (*size)) { if(targ >= (*size)) {
printf("realloc'd");
(*size)=targ*2; (*size)=targ*2;
(*lns) = realloc((*lns), (*size)); (*lns) = (struct asmln**)realloc((*lns), (*size));
} }
} }
@ -74,31 +81,44 @@ struct asmln* mkasmln(struct line* ln, char* content) {
return newln; return newln;
} }
void startpoppush(struct line* ln, struct asmln** asmlns, int* asmind) { void checkopamnt(int amnt, struct line* ln) {
if(ln->tokenscount < 2) {
fprintf(stderr, "Missing memory segment; line %i", ln->truen);
exit(1);
}
if(amnt > 2)
if(ln->tokenscount < 3) {
fprintf(stderr, "Missing operation index; line %i", ln->truen);
exit(1);
}
}
void startpoppush(struct line* ln, struct asmln*** asmlns, int* asmind) {
checkopamnt(3, ln);
int asmi = *asmind; int asmi = *asmind;
// //operation segment i // // operation segment i
int indlen = strlen(ln->tokens[2]); int indlen = strlen(ln->tokens[2]);
asmlns[asmi] = mkasmln(ln, mkcom(ln, indlen)); (*asmlns)[asmi] = mkasmln(ln, mkcom(ln, indlen));
asmi++; asmi++;
// @segment // @segment
asmlns[asmi] = mkasmln(ln, switchseg(ln)); (*asmlns)[asmi] = mkasmln(ln, switchseg(ln));
asmi++; asmi++;
// D=M // D=M
asmlns[asmi] = mkasmln(ln, heapstr("D=M")); (*asmlns)[asmi] = mkasmln(ln, heapstr("D=M"));
asmi++; asmi++;
// @i // @i
checkind(ln, indlen); checkind(ln, indlen);
asmlns[asmi] = mkasmln(ln, mkind(ln->tokens[2], indlen)); (*asmlns)[asmi] = mkasmln(ln, mkind(ln->tokens[2], indlen));
asmi++; asmi++;
(*asmind) = asmi; (*asmind) = asmi;
} }
void mkpush(struct line* ln, struct asmln** asmlns, int* asmind, int* asmsize) { void mkpush(struct line* ln, struct asmln*** asmlns, int* asmind, int* asmsize) {
int asmi = *asmind; int asmi = *asmind;
int totalinstrs = 11; int totalinstrs = 11;
checkasmsize(asmsize, asmlns, sizeof(struct asmln*)*(asmi+totalinstrs)); checkasmsize(asmsize, asmlns, sizeof(struct asmln*)*(asmi+totalinstrs));
@ -117,14 +137,14 @@ void mkpush(struct line* ln, struct asmln** asmlns, int* asmind, int* asmsize) {
}; };
for(int i = 0; i < instcount; i++) { for(int i = 0; i < instcount; i++) {
asmlns[asmi] = mkasmln(ln, heapstr(instrs[i])); (*asmlns)[asmi] = mkasmln(ln, heapstr(instrs[i]));
asmi++; asmi++;
} }
(*asmind) = asmi; (*asmind) = asmi;
} }
void mkpop(struct line* ln, struct asmln** asmlns, int* asmind, int* asmsize) { void mkpop(struct line* ln, struct asmln*** asmlns, int* asmind, int* asmsize) {
int asmi = *asmind; int asmi = *asmind;
int finalasmi = 14; int finalasmi = 14;
checkasmsize(asmsize, asmlns, sizeof(struct asmln*)*(asmi+finalasmi)); checkasmsize(asmsize, asmlns, sizeof(struct asmln*)*(asmi+finalasmi));
@ -146,45 +166,31 @@ void mkpop(struct line* ln, struct asmln** asmlns, int* asmind, int* asmsize) {
}; };
for(int i = 0; i < instcount; i++) { for(int i = 0; i < instcount; i++) {
asmlns[asmi] = mkasmln(ln, heapstr(instrs[i])); (*asmlns)[asmi] = mkasmln(ln, heapstr(instrs[i]));
asmi++; asmi++;
} }
(*asmind) = asmi; (*asmind) = asmi;
} }
void mkpoppush(struct line* ln, struct asmln** asmlns, int* asmind, int* asmsize, enum operation op) { void switchop(struct line* ln, struct asmln*** asmlns, int* asmind, int* asmsize, enum operation op) {
if(op == push) if(op == push)
mkpush(ln, asmlns, asmind, asmsize); mkpush(ln, asmlns, asmind, asmsize);
else if(op == pop) else if(op == pop)
mkpop(ln, asmlns, asmind, asmsize); mkpop(ln, asmlns, asmind, asmsize);
} }
void checkopamnt(struct line* ln) { struct asmln** translate(struct line** lns, int lnscount, int* asmcount) {
if(ln->tokenscount < 2) {
fprintf(stderr, "Missing memory segment; line %i", ln->truen);
exit(1);
}
if(ln->tokenscount < 3) {
fprintf(stderr, "Missing operation index; line %i", ln->truen);
exit(1);
}
}
struct asmln** translate(struct line** lns, int lnscount, int* asmind) {
int sizebet = sizeof(struct asmln*)*(lnscount * 15); int sizebet = sizeof(struct asmln*)*(lnscount * 15);
int asmi = (*asmind); int asmi = (*asmcount);
struct asmln** asmlns = (struct asmln**)malloc(sizebet); struct asmln** asmlns = (struct asmln**)malloc(sizebet);
for(int i = 0; i < lnscount; i++) { for(int i = 0; i < lnscount; i++) {
struct line* ln = lns[i]; struct line* ln = lns[i];
enum operation op = getop(ln); enum operation op = getop(ln);
if(op == pop || op == push) { switchop(ln, &asmlns, &asmi, &sizebet, op);
checkopamnt(ln);
mkpoppush(ln, asmlns, &asmi, &sizebet, op);
}
} }
(*asmind) = asmi; (*asmcount) = asmi;
return asmlns; return asmlns;
} }

View File

@ -6,5 +6,6 @@ struct asmln {
int truen; int truen;
}; };
struct asmln** translate(struct line** lns, int lnscount, int* asmind); void freeasmlns(struct asmln** lns, int count);
struct asmln** translate(struct line** lns, int lnscount, int* asmcount);
#endif #endif