refractoring, making the disgusting if else chain somewhat tolerable
This commit is contained in:
76
asm_rules.c
76
asm_rules.c
@ -1,11 +1,15 @@
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void asm_rules(char *in, char _asm[27]){
|
||||
void trit_flip(char *in, char n0, char n1, char n2, char n3) {
|
||||
in[0] = n0;
|
||||
in[1] = n1;
|
||||
in[2] = n2;
|
||||
in[3] = n3;
|
||||
}
|
||||
void asm_rules(char *in, char *_asm){
|
||||
|
||||
for (int i=0; i<27; i++) {
|
||||
_asm[i] = 0;
|
||||
}
|
||||
|
||||
char *word = strtok(in, " ");
|
||||
|
||||
@ -21,79 +25,39 @@ void asm_rules(char *in, char _asm[27]){
|
||||
//comments
|
||||
break;
|
||||
} else {
|
||||
//cases make larger checks harder to read
|
||||
if(word[0] == 'i') {
|
||||
//integer
|
||||
_asm[1] = 1;
|
||||
} else if(word[0] == 'f') {
|
||||
//floating
|
||||
_asm[1] = 2;
|
||||
}
|
||||
if (word[0] == '$') {
|
||||
//constants
|
||||
_asm[0] = 2; //set constant loading (on input 1)
|
||||
|
||||
_asm[12] = 2; //set constant loading (on input 1)
|
||||
} else if (word[0] == '@') {
|
||||
//address location
|
||||
|
||||
//beware: beginning yandev style
|
||||
//cases make larger checks harder to read
|
||||
} else if (strstr(word, "max")) {
|
||||
_asm[2] = 2; //set layer to arithmetics and logic
|
||||
_asm[3] = 0;
|
||||
_asm[4] = 0;
|
||||
_asm[5] = 1;
|
||||
trit_flip(_asm, '+', '0', '0', '0');
|
||||
} else if (strstr(word, "min")) {
|
||||
_asm[2] = 2; //set layer to arithmetics and logic
|
||||
_asm[3] = 0;
|
||||
_asm[4] = 0;
|
||||
_asm[5] = 2;
|
||||
trit_flip(_asm, '-', '0', '0', '0');
|
||||
} else if (strstr(word, "any")) {
|
||||
_asm[2] = 2; //set layer to arithmetics and logic
|
||||
_asm[3] = 0;
|
||||
_asm[4] = 2;
|
||||
_asm[5] = 2;
|
||||
trit_flip(_asm, '0', '0', '0', '0');
|
||||
} else if (strstr(word, "cons")) {
|
||||
_asm[2] = 2; //set layer to arithmetics and logic
|
||||
_asm[3] = 0;
|
||||
_asm[4] = 1;
|
||||
_asm[5] = 1;
|
||||
trit_flip(_asm, '-', '+', '0', '0');
|
||||
} else if (strstr(word, "add")) {
|
||||
_asm[2] = 2; //set layer to arithmetics and logic
|
||||
_asm[3] = 1;
|
||||
_asm[4] = 0;
|
||||
_asm[5] = 1;
|
||||
trit_flip(_asm, '+', '-', '+', '0');
|
||||
} else if (strstr(word, "sub")) {
|
||||
_asm[2] = 2; //set layer to arithmetics and logic
|
||||
_asm[3] = 1;
|
||||
_asm[4] = 0;
|
||||
_asm[5] = 2;
|
||||
trit_flip(_asm, '+', '-', '-', '0');
|
||||
} else if (strstr(word, "mul")) {
|
||||
_asm[2] = 2; //set layer to arithmetics and logic
|
||||
_asm[3] = 1;
|
||||
_asm[4] = 1;
|
||||
_asm[5] = 0;
|
||||
trit_flip(_asm, '+', '0', '+', '0');
|
||||
} else if (strstr(word, "div")) {
|
||||
_asm[2] = 2; //set layer to arithmetics and logic
|
||||
_asm[3] = 1;
|
||||
_asm[4] = 2;
|
||||
_asm[5] = 0;
|
||||
trit_flip(_asm, '+', '0', '-', '0');
|
||||
} else if (strstr(word, "dec")) {
|
||||
_asm[2] = 2; //set layer to arithmetics and logic
|
||||
_asm[3] = 2;
|
||||
_asm[4] = 2;
|
||||
_asm[5] = 0;
|
||||
trit_flip(_asm, '-', '-', '0', '0');
|
||||
} else if (strstr(word, "inc")) {
|
||||
_asm[2] = 2; //set layer to arithmetics and logic
|
||||
_asm[3] = 2;
|
||||
_asm[4] = 2;
|
||||
_asm[5] = 1;
|
||||
} else if (strstr(word, "not")) {
|
||||
_asm[2] = 2; //set layer to arithmetics and logic
|
||||
_asm[3] = 2;
|
||||
_asm[4] = 2;
|
||||
_asm[5] = 2;
|
||||
trit_flip(_asm, '-', '+', '0', '0');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
word = strtok(NULL, " ");
|
||||
|
Reference in New Issue
Block a user