tern-assembler/asm_rules.c

102 lines
2.3 KiB
C

#include <string.h>
#include <stdio.h>
void asm_rules(char *in, char _asm[27]){
for (int i=0; i<27; i++) {
_asm[i] = 0;
}
char *word = strtok(in, " ");
while(word != NULL) {
//instruction set
//aaaaaa bbbbbbb ccccccc ddddddd
//a = opcode, b = input 0, c = input 1, d = output
if (strcmp(word, "nop") == 0) {
//keeps the default value (0) if nop
} else if (word[0] == ';') {
//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)
} else if (word[0] == '@') {
//address location
//beware: beginning yandev style
} else if (strstr(word, "max")) {
_asm[2] = 2; //set layer to arithmetics and logic
_asm[3] = 0;
_asm[4] = 0;
_asm[5] = 1;
} else if (strstr(word, "min")) {
_asm[2] = 2; //set layer to arithmetics and logic
_asm[3] = 0;
_asm[4] = 0;
_asm[5] = 2;
} else if (strstr(word, "any")) {
_asm[2] = 2; //set layer to arithmetics and logic
_asm[3] = 0;
_asm[4] = 2;
_asm[5] = 2;
} else if (strstr(word, "cons")) {
_asm[2] = 2; //set layer to arithmetics and logic
_asm[3] = 0;
_asm[4] = 1;
_asm[5] = 1;
} else if (strstr(word, "add")) {
_asm[2] = 2; //set layer to arithmetics and logic
_asm[3] = 1;
_asm[4] = 0;
_asm[5] = 1;
} else if (strstr(word, "sub")) {
_asm[2] = 2; //set layer to arithmetics and logic
_asm[3] = 1;
_asm[4] = 0;
_asm[5] = 2;
} else if (strstr(word, "mul")) {
_asm[2] = 2; //set layer to arithmetics and logic
_asm[3] = 1;
_asm[4] = 1;
_asm[5] = 0;
} else if (strstr(word, "div")) {
_asm[2] = 2; //set layer to arithmetics and logic
_asm[3] = 1;
_asm[4] = 2;
_asm[5] = 0;
} else if (strstr(word, "dec")) {
_asm[2] = 2; //set layer to arithmetics and logic
_asm[3] = 2;
_asm[4] = 2;
_asm[5] = 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;
}
}
word = strtok(NULL, " ");
}
}