tern-assembler/asm_rules.c

102 lines
2.3 KiB
C
Raw Normal View History

2024-02-19 22:25:25 +01:00
#include <string.h>
#include <stdio.h>
2024-02-19 22:25:25 +01:00
2024-02-29 22:53:11 +01:00
void asm_rules(char *in, char _asm[27]){
2024-02-19 22:25:25 +01:00
2024-03-12 10:18:56 +01:00
for (int i=0; i<27; i++) {
_asm[i] = 0;
}
2024-02-19 22:25:25 +01:00
char *word = strtok(in, " ");
while(word != NULL) {
//instruction set
//aaaaaa bbbbbbb ccccccc ddddddd
//a = opcode, b = input 0, c = input 1, d = output
2024-02-19 22:25:25 +01:00
if (strcmp(word, "nop") == 0) {
//keeps the default value (0) if nop
} else if (word[0] == ';') {
//comments
break;
2024-02-19 22:25:25 +01:00
} 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;
2024-02-19 22:25:25 +01:00
}
if (word[0] == '$') {
2024-03-12 10:18:56 +01:00
//constants
_asm[0] = 2; //set constant loading (on input 1)
2024-03-12 10:18:56 +01:00
} 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;
2024-03-12 10:18:56 +01:00
}
2024-02-19 22:25:25 +01:00
}
word = strtok(NULL, " ");
}
}