Add A instruction syntax check

This commit is contained in:
Augusto Gunsch 2020-10-25 10:51:08 -03:00
parent ce18f46910
commit f74ee3fb41
No known key found for this signature in database
GPG Key ID: F7EEFE29825C72DC
1 changed files with 13 additions and 5 deletions

View File

@ -184,11 +184,13 @@ void populatevars(struct symbol** vars, int* varscount) {
void gatherinfo(FILE* input, int* lnscount, int* labelscount, int* maxwidth) { void gatherinfo(FILE* input, int* lnscount, int* labelscount, int* maxwidth) {
char c; char c;
unsigned char readsmt = 0; unsigned char readsmt = 0;
unsigned char comment = 0;
int truelnscount = 1; int truelnscount = 1;
int lnwidth = 1; int lnwidth = 1;
while(c = fgetc(input), c != -1) { while(c = fgetc(input), c != -1) {
if(c == '\n') { if(c == '\n') {
truelnscount++; truelnscount++;
comment = 0;
if(lnwidth > *maxwidth) if(lnwidth > *maxwidth)
*maxwidth = lnwidth; *maxwidth = lnwidth;
if(readsmt) { if(readsmt) {
@ -202,15 +204,17 @@ void gatherinfo(FILE* input, int* lnscount, int* labelscount, int* maxwidth) {
lnwidth = 1; lnwidth = 1;
continue; continue;
} }
if(comment)
continue;
if(c == '(') { if(c == '(') {
(*labelscount)++; (*labelscount)++;
comment = 1;
continue; continue;
} }
if(c == '/') { if(c == '/') {
char nc = fgetc(input); char nc = fgetc(input);
if(nc == '/') { if(nc == '/') {
skipln(input); comment = 1;
truelnscount++;
continue; continue;
} }
ungetc(nc, input); ungetc(nc, input);
@ -374,8 +378,12 @@ void stripvars(struct symbol** vars, int* varscount, struct symbol** labels, int
} }
} }
void transa(char* in, char* out) { void transa(char* in, char* out, int trueln) {
int add = atoi(in+sizeof(char)); int add = atoi(in+sizeof(char));
if(add >= INST_LIMIT) {
fprintf(stderr, "'A' instruction cannot reference addresses bigger than %i; line %i\n", INST_LIMIT-1, trueln);
exit(1);
}
int lastbit = 1 << 15; int lastbit = 1 << 15;
for(int i = INST_SIZE-2;i > 0; i--) { for(int i = INST_SIZE-2;i > 0; i--) {
if(add & (lastbit >> i)) if(add & (lastbit >> i))
@ -467,7 +475,7 @@ char** translate(struct line** lns, int lnscount) {
for(int i = 0; i < lnscount; i++) for(int i = 0; i < lnscount; i++)
if(lns[i]->ln[0] == '@') if(lns[i]->ln[0] == '@')
transa(lns[i]->ln, assembled[i]); transa(lns[i]->ln, assembled[i], lns[i]->truen);
else else
transb(lns[i]->ln, assembled[i], lns[i]->truen); transb(lns[i]->ln, assembled[i], lns[i]->truen);
return assembled; return assembled;
@ -487,7 +495,7 @@ int main(int argc, char* argv[]) {
int fnamelen = strlen(argv[1]); int fnamelen = strlen(argv[1]);
int invalidext = strcmp(argv[1]+(fnamelen*sizeof(char)-(4*sizeof(char))), ".asm"); int invalidext = strcmp(argv[1]+(fnamelen*sizeof(char)-(4*sizeof(char))), ".asm");
if(invalidext) { if(invalidext) {
fprintf(stderr, "Invalid extension (must be *.asm)"); fprintf(stderr, "Invalid extension (must be *.asm)\n");
return 1; return 1;
} }
FILE* input = fopen(argv[1], "r"); FILE* input = fopen(argv[1], "r");