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 <stdio.h>
#include <stdlib.h>
void asm_rules(char *in, char _asm[27]){
for (int i=0; i<27; i++) {
_asm[i] = 0;
void trit_flip(char *in, char *instruction) {
for (int i = 0; i<(sizeof(in)/sizeof(in[0])); i++) {
in[i] = instruction[i];
}
}
int asm_rules(char *in, char _asm[27], char _asm_addr[27]){
int pass = 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;
_asm[7] = '0';
} else if(word[0] == 'f') {
//floating
_asm[1] = 2;
_asm[7] = '-';
}
if (word[0] == '$') {
//constants
_asm[0] = 2; //set constant loading (on input 1)
if (word[0] == '$' || word[0] == '@') {
} else if (word[0] == '@') {
//address location
//beware: beginning yandev style
//cases make larger checks harder to read
} else if (strstr(word, "max")) {
_asm[2] = 2; //set layer to arithmetics and logic
_asm[3] = 0;
_asm[4] = 0;
_asm[5] = 1;
trit_flip(_asm, "+00000+000000");
} else if (strstr(word, "min")) {
_asm[2] = 2; //set layer to arithmetics and logic
_asm[3] = 0;
_asm[4] = 0;
_asm[5] = 2;
trit_flip(_asm, "-00000+000000");
} else if (strstr(word, "any")) {
_asm[2] = 2; //set layer to arithmetics and logic
_asm[3] = 0;
_asm[4] = 2;
_asm[5] = 2;
trit_flip(_asm, "000000+000000");
} else if (strstr(word, "cons")) {
_asm[2] = 2; //set layer to arithmetics and logic
_asm[3] = 0;
_asm[4] = 1;
_asm[5] = 1;
trit_flip(_asm, "-+0000+000000");
} else if (strstr(word, "add")) {
_asm[2] = 2; //set layer to arithmetics and logic
_asm[3] = 1;
_asm[4] = 0;
_asm[5] = 1;
trit_flip(_asm, "+-+000+000000");
} else if (strstr(word, "sub")) {
_asm[2] = 2; //set layer to arithmetics and logic
_asm[3] = 1;
_asm[4] = 0;
_asm[5] = 2;
trit_flip(_asm, "+--000+000000");
} else if (strstr(word, "mul")) {
_asm[2] = 2; //set layer to arithmetics and logic
_asm[3] = 1;
_asm[4] = 1;
_asm[5] = 0;
trit_flip(_asm, "+0+000+000000");
} else if (strstr(word, "div")) {
_asm[2] = 2; //set layer to arithmetics and logic
_asm[3] = 1;
_asm[4] = 2;
_asm[5] = 0;
trit_flip(_asm, "+0-000+000000");
} else if (strstr(word, "dec")) {
_asm[2] = 2; //set layer to arithmetics and logic
_asm[3] = 2;
_asm[4] = 2;
_asm[5] = 0;
trit_flip(_asm, "--0000+000000");
} 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;
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");
}
}
word = strtok(NULL, " ");
}
pass = 0;
return 0;
}

49
main.c
View File

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