Compare commits

..

2 Commits

Author SHA1 Message Date
nova
bfd7fd3016 refactoring 2024-04-20 23:02:54 +02:00
nova
f428423c49 refractoring, making the disgusting if else chain somewhat tolerable 2024-03-24 19:36:29 +01:00
2 changed files with 45 additions and 103 deletions

View File

@ -1,101 +1,70 @@
#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 *instruction) {
for (int i = 0; i<(sizeof(in)/sizeof(in[0])); i++) {
for (int i=0; i<27; i++) { in[i] = instruction[i];
_asm[i] = 0;
} }
}
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] == ';') {
//comments //comments
break; break;
} else { } else {
//cases make larger checks harder to read
if(word[0] == 'i') { if(word[0] == 'i') {
//integer //integer
_asm[1] = 1; _asm[7] = '0';
} else if(word[0] == 'f') { } else if(word[0] == 'f') {
//floating //floating
_asm[1] = 2; _asm[7] = '-';
} }
if (word[0] == '$') { if (word[0] == '$' || word[0] == '@') {
//constants
_asm[0] = 2; //set constant loading (on input 1)
} else if (word[0] == '@') { //cases make larger checks harder to read
//address location
//beware: beginning yandev style
} else if (strstr(word, "max")) { } else if (strstr(word, "max")) {
_asm[2] = 2; //set layer to arithmetics and logic trit_flip(_asm, "+00000+000000");
_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, "-00000+000000");
_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, "000000+000000");
_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, "-+0000+000000");
_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, "+-+000+000000");
_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, "+--000+000000");
_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+000+000000");
_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-000+000000");
_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, "--0000+000000");
_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, "-+0000+000000");
_asm[3] = 2; } else if (strstr(word, "iou")) {
_asm[4] = 2; trit_flip(_asm, "--0000-000000");
_asm[5] = 1; } else if (strstr(word, "ieq")) {
} else if (strstr(word, "not")) { trit_flip(_asm, "+-0000-000000");
_asm[2] = 2; //set layer to arithmetics and logic } else if (strstr(word, "jmp")) {
_asm[3] = 2; trit_flip(_asm, "000000-000000");
_asm[4] = 2; } else if (strstr(word, "jec")) {
_asm[5] = 2; trit_flip(_asm, "+00000-000000");
} else if (strstr(word, "mov")) {
trit_flip(_asm, "+000000000000");
} }
} }
word = strtok(NULL, " "); word = strtok(NULL, " ");
} }
pass = 0;
return 0;
} }

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
} }