refactoring
This commit is contained in:
parent
f428423c49
commit
bfd7fd3016
59
asm_rules.c
59
asm_rules.c
@ -2,23 +2,18 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
void trit_flip(char *in, char n0, char n1, char n2, char n3) {
|
void trit_flip(char *in, char *instruction) {
|
||||||
in[0] = n0;
|
for (int i = 0; i<(sizeof(in)/sizeof(in[0])); i++) {
|
||||||
in[1] = n1;
|
in[i] = instruction[i];
|
||||||
in[2] = n2;
|
}
|
||||||
in[3] = n3;
|
|
||||||
}
|
}
|
||||||
void asm_rules(char *in, char *_asm){
|
int asm_rules(char *in, char _asm[27], char _asm_addr[27]){
|
||||||
|
|
||||||
|
int pass = 0;
|
||||||
|
|
||||||
char *word = strtok(in, " ");
|
char *word = strtok(in, " ");
|
||||||
|
|
||||||
while(word != NULL) {
|
while(word != NULL) {
|
||||||
|
|
||||||
//instruction set
|
|
||||||
//aaaaaa bbbbbbb ccccccc ddddddd
|
|
||||||
//a = opcode, b = input 0, c = input 1, d = output
|
|
||||||
|
|
||||||
if (strcmp(word, "nop") == 0) {
|
if (strcmp(word, "nop") == 0) {
|
||||||
//keeps the default value (0) if nop
|
//keeps the default value (0) if nop
|
||||||
} else if (word[0] == ';') {
|
} else if (word[0] == ';') {
|
||||||
@ -26,40 +21,50 @@ void asm_rules(char *in, char *_asm){
|
|||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
if(word[0] == 'i') {
|
if(word[0] == 'i') {
|
||||||
_asm[1] = 1;
|
//integer
|
||||||
|
_asm[7] = '0';
|
||||||
} else if(word[0] == 'f') {
|
} else if(word[0] == 'f') {
|
||||||
//floating
|
//floating
|
||||||
|
_asm[7] = '-';
|
||||||
}
|
}
|
||||||
if (word[0] == '$') {
|
if (word[0] == '$' || word[0] == '@') {
|
||||||
//constants
|
|
||||||
_asm[12] = 2; //set constant loading (on input 1)
|
|
||||||
} else if (word[0] == '@') {
|
|
||||||
//address location
|
|
||||||
|
|
||||||
//cases make larger checks harder to read
|
//cases make larger checks harder to read
|
||||||
} else if (strstr(word, "max")) {
|
} else if (strstr(word, "max")) {
|
||||||
trit_flip(_asm, '+', '0', '0', '0');
|
trit_flip(_asm, "+00000+000000");
|
||||||
} else if (strstr(word, "min")) {
|
} else if (strstr(word, "min")) {
|
||||||
trit_flip(_asm, '-', '0', '0', '0');
|
trit_flip(_asm, "-00000+000000");
|
||||||
} else if (strstr(word, "any")) {
|
} else if (strstr(word, "any")) {
|
||||||
trit_flip(_asm, '0', '0', '0', '0');
|
trit_flip(_asm, "000000+000000");
|
||||||
} else if (strstr(word, "cons")) {
|
} else if (strstr(word, "cons")) {
|
||||||
trit_flip(_asm, '-', '+', '0', '0');
|
trit_flip(_asm, "-+0000+000000");
|
||||||
} else if (strstr(word, "add")) {
|
} else if (strstr(word, "add")) {
|
||||||
trit_flip(_asm, '+', '-', '+', '0');
|
trit_flip(_asm, "+-+000+000000");
|
||||||
} else if (strstr(word, "sub")) {
|
} else if (strstr(word, "sub")) {
|
||||||
trit_flip(_asm, '+', '-', '-', '0');
|
trit_flip(_asm, "+--000+000000");
|
||||||
} else if (strstr(word, "mul")) {
|
} else if (strstr(word, "mul")) {
|
||||||
trit_flip(_asm, '+', '0', '+', '0');
|
trit_flip(_asm, "+0+000+000000");
|
||||||
} else if (strstr(word, "div")) {
|
} else if (strstr(word, "div")) {
|
||||||
trit_flip(_asm, '+', '0', '-', '0');
|
trit_flip(_asm, "+0-000+000000");
|
||||||
} else if (strstr(word, "dec")) {
|
} else if (strstr(word, "dec")) {
|
||||||
trit_flip(_asm, '-', '-', '0', '0');
|
trit_flip(_asm, "--0000+000000");
|
||||||
} else if (strstr(word, "inc")) {
|
} else if (strstr(word, "inc")) {
|
||||||
trit_flip(_asm, '-', '+', '0', '0');
|
trit_flip(_asm, "-+0000+000000");
|
||||||
|
} else if (strstr(word, "iou")) {
|
||||||
|
trit_flip(_asm, "--0000-000000");
|
||||||
|
} else if (strstr(word, "ieq")) {
|
||||||
|
trit_flip(_asm, "+-0000-000000");
|
||||||
|
} else if (strstr(word, "jmp")) {
|
||||||
|
trit_flip(_asm, "000000-000000");
|
||||||
|
} else if (strstr(word, "jec")) {
|
||||||
|
trit_flip(_asm, "+00000-000000");
|
||||||
|
} else if (strstr(word, "mov")) {
|
||||||
|
trit_flip(_asm, "+000000000000");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
word = strtok(NULL, " ");
|
word = strtok(NULL, " ");
|
||||||
}
|
}
|
||||||
|
pass = 0;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user