refractoring, making the disgusting if else chain somewhat tolerable

This commit is contained in:
nova 2024-03-24 19:36:29 +01:00
parent fb59048075
commit f428423c49
2 changed files with 31 additions and 94 deletions

View File

@ -1,11 +1,15 @@
#include <string.h> #include <string.h>
#include <stdio.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, " "); char *word = strtok(in, " ");
@ -21,79 +25,39 @@ void asm_rules(char *in, char _asm[27]){
//comments //comments
break; break;
} else { } else {
//cases make larger checks harder to read
if(word[0] == 'i') { if(word[0] == 'i') {
//integer
_asm[1] = 1; _asm[1] = 1;
} else if(word[0] == 'f') { } else if(word[0] == 'f') {
//floating //floating
_asm[1] = 2;
} }
if (word[0] == '$') { if (word[0] == '$') {
//constants //constants
_asm[0] = 2; //set constant loading (on input 1) _asm[12] = 2; //set constant loading (on input 1)
} else if (word[0] == '@') { } else if (word[0] == '@') {
//address location //address location
//beware: beginning yandev style //cases make larger checks harder to read
} else if (strstr(word, "max")) { } else if (strstr(word, "max")) {
_asm[2] = 2; //set layer to arithmetics and logic trit_flip(_asm, '+', '0', '0', '0');
_asm[3] = 0;
_asm[4] = 0;
_asm[5] = 1;
} else if (strstr(word, "min")) { } else if (strstr(word, "min")) {
_asm[2] = 2; //set layer to arithmetics and logic trit_flip(_asm, '-', '0', '0', '0');
_asm[3] = 0;
_asm[4] = 0;
_asm[5] = 2;
} else if (strstr(word, "any")) { } else if (strstr(word, "any")) {
_asm[2] = 2; //set layer to arithmetics and logic trit_flip(_asm, '0', '0', '0', '0');
_asm[3] = 0;
_asm[4] = 2;
_asm[5] = 2;
} else if (strstr(word, "cons")) { } else if (strstr(word, "cons")) {
_asm[2] = 2; //set layer to arithmetics and logic trit_flip(_asm, '-', '+', '0', '0');
_asm[3] = 0;
_asm[4] = 1;
_asm[5] = 1;
} else if (strstr(word, "add")) { } else if (strstr(word, "add")) {
_asm[2] = 2; //set layer to arithmetics and logic trit_flip(_asm, '+', '-', '+', '0');
_asm[3] = 1;
_asm[4] = 0;
_asm[5] = 1;
} else if (strstr(word, "sub")) { } else if (strstr(word, "sub")) {
_asm[2] = 2; //set layer to arithmetics and logic trit_flip(_asm, '+', '-', '-', '0');
_asm[3] = 1;
_asm[4] = 0;
_asm[5] = 2;
} else if (strstr(word, "mul")) { } else if (strstr(word, "mul")) {
_asm[2] = 2; //set layer to arithmetics and logic trit_flip(_asm, '+', '0', '+', '0');
_asm[3] = 1;
_asm[4] = 1;
_asm[5] = 0;
} else if (strstr(word, "div")) { } else if (strstr(word, "div")) {
_asm[2] = 2; //set layer to arithmetics and logic trit_flip(_asm, '+', '0', '-', '0');
_asm[3] = 1;
_asm[4] = 2;
_asm[5] = 0;
} else if (strstr(word, "dec")) { } else if (strstr(word, "dec")) {
_asm[2] = 2; //set layer to arithmetics and logic trit_flip(_asm, '-', '-', '0', '0');
_asm[3] = 2;
_asm[4] = 2;
_asm[5] = 0;
} else if (strstr(word, "inc")) { } else if (strstr(word, "inc")) {
_asm[2] = 2; //set layer to arithmetics and logic trit_flip(_asm, '-', '+', '0', '0');
_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, " "); word = strtok(NULL, " ");

49
main.c
View File

@ -3,62 +3,36 @@
void bin_to_hex(char *tryte, int *hex) { void bin_to_hex(char *tryte, int *hex) {
char *a = tryte; char *a = tryte;
int num = 0; long num = 0;
do { do {
int b = *a=='1'?1:0; long b = *a=='1'?1:0;
num = (num<<1)|b; num = (num<<1)|b;
a++; a++;
} while (*a); } while (*a);
*hex = num; *hex = num;
//printf("%d\n",num);
} }
void asm_to_hex(char *_asm, int *opcode, int *in0, int *in1, int *out){ void asm_to_hex(char *_asm, int *opcode, int *in0, int *in1, int *out){
char tryte[54]; char tryte[108] = "\0";
char t0[18]; char t0[26] = "\0";
char t1[18]; char t1[26] = "\0";
char t2[18]; char t2[26] = "\0";
char t3[18]; char t3[26] = "\0";
for (int i = 0; i < 18; i++) {
t0[i] = 0;
t1[i] = 0;
t2[i] = 0;
t3[i] = 0;
}
for (int i = 0; i < 54; i++) {
tryte[i] = 0;
}
int size = sizeof(_asm)/sizeof(_asm[0]);
//convert assembly to formated binary representation of ternary //convert assembly to formated binary representation of ternary
for (int i = 0; i < 27; i++) { for (int i = 0; i < sizeof(_asm)/4; i++) {
//printf("%c",_asm[i]); //printf("%c",_asm[i]);
if(_asm[i]==2) { if(_asm[i]=='-') {
strcat(tryte, "10"); strcat(tryte, "10");
} else if (_asm[i]==1) { } else if (_asm[i]=='+') {
strcat(tryte, "01"); strcat(tryte, "01");
} else { } else {
strcat(tryte, "00"); strcat(tryte, "00");
} }
} }
int j = 0;
for (int i = 0; i < 12; i++) {
t0[i] = tryte[j];
j++;
}
for (int i = 0; i < 14; i++) {
t1[i] = tryte[j];
j++;
}
for (int i = 0; i < 14; i++) {
t2[i] = tryte[j];
j++;
}
for (int i = 0; i < 14; i++) {
t3[i] = tryte[j];
j++;
}
bin_to_hex(t0, opcode); bin_to_hex(t0, opcode);
bin_to_hex(t1, in0); bin_to_hex(t1, in0);
@ -111,7 +85,6 @@ int main(int argc, char **argv) {
hex_in1 = 0; hex_in1 = 0;
hex_out = 0; hex_out = 0;
printf("%s/n",in_asm);
if (in_asm[strlen(in_asm)-1] == '\n') { if (in_asm[strlen(in_asm)-1] == '\n') {
in_asm[strlen(in_asm)-1] = '\0'; //cleans newline from string in_asm[strlen(in_asm)-1] = '\0'; //cleans newline from string
} }