tern-assembler/asm_rules.c

82 lines
2.0 KiB
C
Raw Normal View History

2024-02-19 22:25:25 +01:00
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
2024-02-19 22:25:25 +01:00
2024-04-20 23:02:54 +02:00
void trit_flip(char *in, char *instruction) {
for (int i = 0; i<(sizeof(in)/sizeof(in[0])); i++) {
in[i] = instruction[i];
}
}
2024-04-30 23:31:16 +02:00
int asm_rules(char *in, char _asm[27]){
2024-04-20 23:02:54 +02:00
2024-04-30 23:31:16 +02:00
for (int i = 0; i < 27; i++) {
_asm[i] = 0;
}
2024-04-20 23:02:54 +02:00
int pass = 0;
2024-02-19 22:25:25 +01:00
char *word = strtok(in, " ");
while(word != NULL) {
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 {
if(word[0] == 'i') {
2024-04-20 23:02:54 +02:00
//integer
_asm[7] = '0';
} else if(word[0] == 'f') {
//floating
2024-04-20 23:02:54 +02:00
_asm[7] = '-';
2024-02-19 22:25:25 +01:00
}
2024-04-20 23:02:54 +02:00
if (word[0] == '$' || word[0] == '@') {
2024-03-12 10:18:56 +01:00
//cases make larger checks harder to read
} else if (strstr(word, "max")) {
2024-04-20 23:02:54 +02:00
trit_flip(_asm, "+00000+000000");
} else if (strstr(word, "min")) {
2024-04-20 23:02:54 +02:00
trit_flip(_asm, "-00000+000000");
} else if (strstr(word, "any")) {
2024-04-20 23:02:54 +02:00
trit_flip(_asm, "000000+000000");
} else if (strstr(word, "cons")) {
2024-04-20 23:02:54 +02:00
trit_flip(_asm, "-+0000+000000");
} else if (strstr(word, "add")) {
2024-04-20 23:02:54 +02:00
trit_flip(_asm, "+-+000+000000");
} else if (strstr(word, "sub")) {
2024-04-20 23:02:54 +02:00
trit_flip(_asm, "+--000+000000");
} else if (strstr(word, "mul")) {
2024-04-20 23:02:54 +02:00
trit_flip(_asm, "+0+000+000000");
} else if (strstr(word, "div")) {
2024-04-20 23:02:54 +02:00
trit_flip(_asm, "+0-000+000000");
} else if (strstr(word, "dec")) {
2024-04-20 23:02:54 +02:00
trit_flip(_asm, "--0000+000000");
} else if (strstr(word, "inc")) {
2024-04-20 23:02:54 +02:00
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");
2024-04-30 23:31:16 +02:00
} else if (strstr(word, "Nst")) { //underflow test
trit_flip(_asm, "-------------");
} else if (strstr(word, "Pst")) { //overflow test
trit_flip(_asm, "+++++++++++++");
2024-03-12 10:18:56 +01:00
}
2024-04-30 23:31:16 +02:00
2024-02-19 22:25:25 +01:00
}
word = strtok(NULL, " ");
}
2024-04-20 23:02:54 +02:00
pass = 0;
return 0;
2024-02-19 22:25:25 +01:00
}
2024-04-30 23:31:16 +02:00