Add constant pushing

This commit is contained in:
Augusto Gunsch 2020-10-31 19:00:09 -03:00
parent bece0c4f64
commit 5a0cb9d3bc
No known key found for this signature in database
GPG Key ID: F7EEFE29825C72DC
2 changed files with 68 additions and 2 deletions

28
main.c
View File

@ -1,14 +1,42 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <stdlib.h>
#include "parser.h" #include "parser.h"
#include "translator.h" #include "translator.h"
char* verfname(char* fname) {
int len = strlen(fname);
if(len > 3) {
int extind = len - 3;
char* extstr = fname + (sizeof(char)*extind);
if(strcmp(extstr, ".vm") == 0) {
int startind = 0;
for(int i = extind-1; i >= 0; i--) {
if(fname[i] == '/') {
startind = i+1;
break;
}
}
int size = sizeof(char)*(extind - startind);
char* startstr = fname + (sizeof(char)*startind);
char* retstr = (char*)malloc(size);
strncpy(retstr, startstr, size);
return retstr;
}
}
fprintf(stderr, "Name format must be Xxx.vm\n");
exit(1);
}
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
if(argc < 2) { if(argc < 2) {
fprintf(stderr, "Usage: %s {file}\n", argv[0]); fprintf(stderr, "Usage: %s {file}\n", argv[0]);
return 1; return 1;
} }
// file name validating
char* fname = verfname(argv[1]);
printf("%s\n", fname);
FILE* input = fopen(argv[1], "r"); FILE* input = fopen(argv[1], "r");

View File

@ -12,7 +12,7 @@ enum operation getop(struct line* ln) {
return push; return push;
if(strcmp(tok, "pop") == 0) if(strcmp(tok, "pop") == 0)
return pop; return pop;
fprintf(stderr, "Invalid operation '%s'; line %i", tok, ln->truen); fprintf(stderr, "Invalid operation '%s'; line %i\n", tok, ln->truen);
exit(1); exit(1);
} }
@ -118,9 +118,44 @@ void startpoppush(struct line* ln, struct asmln*** asmlns, int* asmind) {
(*asmind) = asmi; (*asmind) = asmi;
} }
void pushcons(struct line* ln, struct asmln*** asmlns, int* asmind, int* asmsize) {
int asmi = *asmind;
int totalinstrs = 8;
checkasmsize(asmsize, asmlns, sizeof(struct asmln*)*(asmi+totalinstrs));
// // operation segment i
int indlen = strlen(ln->tokens[2]);
(*asmlns)[asmi] = mkasmln(ln, mkcom(ln, indlen));
asmi++;
// @i
checkind(ln, indlen);
(*asmlns)[asmi] = mkasmln(ln, mkind(ln->tokens[2], indlen));
asmi++;
const int instcount = 6;
const char* instrs[] = {
"D=A",
"@SP",
"A=M",
"M=D",
"@SP",
"M=M+1"
};
for(int i = 0; i < instcount; i++) {
(*asmlns)[asmi] = mkasmln(ln, heapstr(instrs[i]));
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));
startpoppush(ln, asmlns, &asmi); startpoppush(ln, asmlns, &asmi);
@ -175,6 +210,9 @@ void mkpop(struct line* ln, struct asmln*** asmlns, int* asmind, int* asmsize) {
void switchop(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)
if(strcmp(ln->tokens[1], "constant") == 0)
pushcons(ln, asmlns, asmind, asmsize);
else
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);