diff --git a/src/build.sh b/src/build.sh new file mode 100644 index 0000000..a241403 --- /dev/null +++ b/src/build.sh @@ -0,0 +1,4 @@ +echo "BUILDING LOGIC" +clang ./logic.c ./logic_symb.c ./logic_run.c ./logic_ops.c ./logic_karn.c -o ./logic +echo "LOGIC HAS BEEN BUILT - NOW WE GONNA RUN IT" +./logic \ No newline at end of file diff --git a/src/logic.c b/src/logic.c new file mode 100644 index 0000000..ab4eb57 --- /dev/null +++ b/src/logic.c @@ -0,0 +1,16 @@ +#include +#include + +#include "logic_run.h" +#include "logic_karn.h" + +int main(int argc, char** argv) +{ + int x, y; + char d[] = "(Bo((-A^-C)o(A^C)))v(A^B^C)"; + + bool* c = logic_karn_run(d, 3, &x, &y); + logic_karn_render(c, x, y); + + return 0; +} \ No newline at end of file diff --git a/src/logic_karn.c b/src/logic_karn.c new file mode 100644 index 0000000..c1bec30 --- /dev/null +++ b/src/logic_karn.c @@ -0,0 +1,80 @@ +#include +#include +#include + +#include "logic_run.h" + +int logic_karn_1_gray[] = {0b0, 0b1}; +int logic_karn_2_gray[] = {0b00, 0b01, 0b11, 0b10}; + +void logic_karn_render(bool* karn, int x, int y) +{ + bool *c = karn; + + for (int ix = 0; ix < x; ix++) + { + for (int iy = 0; iy < y; iy++) + { + printf("| %d ", *c); + c++; // lmao + } + + printf("|\n"); + } +} + +bool* logic_karn_run(char* equ, int args, int *x, int *y) +{ + if (args > 4) + { + printf("\nMAX 4 ARGS FOR KARNAU\n"); + return NULL; + } + if (args < 2) + { + printf("\nMIN 2 ARGS FOR KARNAU\n"); + return NULL; + } + + *x = (args == 4) ? 4 : 2; + *y = (args >= 3) ? 4 : 2; + + printf("X: %d\nY: %d\n\n", *x, *y); + + bool *data = malloc((*x) * (*y)); + bool *dp = data; + + bool bargs[4]; + + for (int ix = 0; ix < *x; ix++) + { + if (*x == 2) + { + bargs[0] = logic_karn_1_gray[ix]; + } + else + { + bargs[0] = logic_karn_2_gray[ix] & (1 << 0); + bargs[1] = logic_karn_2_gray[ix] & (1 << 1); + } + + for (int iy = 0; iy < *y; iy++) + { + if (*y == 2) + { + bargs[((*x) / 2) + 0] = logic_karn_1_gray[iy]; + } + else + { + bargs[((*x) / 2) + 0] = logic_karn_2_gray[iy] & (1 << 0); + bargs[((*x) / 2) + 1] = logic_karn_2_gray[iy] & (1 << 1); + } + + *dp = logic_run_runner(equ, args, bargs); + printf("\nKARNAU: %d\n\n", *dp); + dp++; + } + } + + return data; +} \ No newline at end of file diff --git a/src/logic_karn.h b/src/logic_karn.h new file mode 100644 index 0000000..fd24596 --- /dev/null +++ b/src/logic_karn.h @@ -0,0 +1,24 @@ + // This header file was generated on +// z5214348.web.cse.unsw.edu.au/header_generator/ + +// header guard: https://en.wikipedia.org/wiki/Include_guard +// This avoids errors if this file is included multiple times +// in a complex source file setup + +#ifndef LOGIC_KARN_H +#define LOGIC_KARN_H + +// #includes + +#include +#include +#include +#include "logic_run.h" + +// Functions + +void logic_karn_render(bool* karn, int x, int y); +bool* logic_karn_run(char* equ, int args, int *x, int *y); + +// End of header file +#endif diff --git a/src/logic_ops.c b/src/logic_ops.c new file mode 100644 index 0000000..b49b358 --- /dev/null +++ b/src/logic_ops.c @@ -0,0 +1,22 @@ +#include +#include + +bool logic_ops_and(bool a, bool b) +{ + return a && b; +} + +bool logic_ops_or(bool a, bool b) +{ + return a || b; +} + +bool logic_ops_not(bool a) +{ + return !a; +} + +bool logic_ops_xor(bool a, bool b) +{ + return logic_ops_and(logic_ops_or(a, b), logic_ops_not(logic_ops_and(a, b))); +} \ No newline at end of file diff --git a/src/logic_ops.h b/src/logic_ops.h new file mode 100644 index 0000000..b13dc0f --- /dev/null +++ b/src/logic_ops.h @@ -0,0 +1,24 @@ + // This header file was generated on +// z5214348.web.cse.unsw.edu.au/header_generator/ + +// header guard: https://en.wikipedia.org/wiki/Include_guard +// This avoids errors if this file is included multiple times +// in a complex source file setup + +#ifndef LOGIC_OPS_H +#define LOGIC_OPS_H + +// #includes + +#include +#include + +// Functions + +bool logic_ops_and(bool a, bool b); +bool logic_ops_or(bool a, bool b); +bool logic_ops_not(bool a); +bool logic_ops_xor(bool a, bool b); + +// End of header file +#endif diff --git a/src/logic_run.c b/src/logic_run.c new file mode 100644 index 0000000..41c6f29 --- /dev/null +++ b/src/logic_run.c @@ -0,0 +1,173 @@ +#include +#include +#include +#include +#include + +#include "logic_ops.h" +#include "logic_symb.h" + +void logic_run_remove_char(char* str, char* index) +{ + memmove(index, index + 1, strlen(str) - (str - index)); +} + +void logic_run_replace_char(char* str, char old, char new) +{ + char* strp = str; + + while (*(++strp) != '\0') + { + *strp = (*strp == old) ? new : *strp; + } +} + +void logic_run_eval(char* sym) +{ + char cop = *sym; + bool (*fop)(bool, bool); + char carg1, carg2; + bool arg1, arg2; + int args; + bool result; + + switch (cop) + { + case (logic_symb_and): + fop = &logic_ops_and; + args = logic_symbs_args_num(logic_symb_args_and); + break; + case (logic_symb_or): + fop = &logic_ops_or; + args = logic_symbs_args_num(logic_symb_args_or); + break; + case (logic_symb_not): + fop = &logic_ops_not; + args = logic_symbs_args_num(logic_symb_args_not); + break; + case (logic_symb_xor): + fop = &logic_ops_xor; + args = logic_symbs_args_num(logic_symb_args_xor); + break; + default: + printf("INCORCT VALUE OMG FU %c\n", cop); + return; + } + + arg1 = false; // always default to false + arg2 = false; + + switch (args) + { + case (2): + arg2 = '0' - *(sym - 1); // convert '0' / '1' to 0 / 1 + case (1): + arg1 = '0' - *(sym + 1); // i <3 pointer math + default: + break; + } + + if (*(sym + 1) == logic_symb_not) + { + logic_run_remove_char(sym, sym + 1); + arg1 = !('0' - *(sym + 1)); + } + + result = (*fop)(arg1, arg2); // its fine if we pass too many args + + switch (args) + { + case (2): + logic_run_remove_char(sym - 1, sym - 1); // we should techinically use the origin, but we dont have that and since it doesnt realloc, it doesnt matter + sym--; // everyhing has been shifted back + case (1): + logic_run_remove_char(sym, sym + 1); + default: + *sym = '0' + result; // covert 0 / 1 to '0' / '1' + } +} + +char* logic_run_get_deepest(char* str) +{ + int cdepth = 0; + char* maxpoint = str; + int maxdepth = 0; + + str--; + + while (*(++str) != '\0') + { + if (*str == '(') + { + cdepth++; + + if (cdepth > maxdepth) + { + maxdepth = cdepth; + maxpoint = str + 1; + } + } + else if (*str == ')') + { + cdepth--; + } + } + + return maxpoint; +} + +void logic_run_remove_useless(char *str) +{ + char *pstr = str - 1; + + while (*(++pstr) != '\0') + { + if ((*pstr == '(') && (*(pstr + 2) == ')')) + { + logic_run_remove_char(str, pstr + 2); + logic_run_remove_char(str, pstr); + } + } +} + +bool logic_run_runner(char* equ, int argc, bool* argv) +{ + logic_ops_and(true, true); + + int equsize = strlen(equ) + 1; + + char* nequ = malloc(equsize + 1); // serve as a buffer for bracket remover so that it dont segfault on bad input + memcpy(nequ, equ, equsize); + + printf("ORIGINAL STRING: %s\n", nequ); + + for (char i = 'a'; (i - 'a') < argc; i++) + { + logic_run_replace_char(nequ, (i), (argv[i - 'a'] + '0')); // lower case + logic_run_replace_char(nequ, (i - 32), (argv[i - 'a'] + '0')); // upper case + } + + printf("REPLACED STRING: %s\n", nequ); + + while (nequ[1] != '\0') // when 1 char long we know it done + { + logic_run_remove_useless(nequ); + + char *deepest = logic_run_get_deepest(nequ); + + if (*deepest == '0' || *deepest == '1') + { + deepest++; + } + + logic_run_eval(deepest); + + printf("DIS ITERATION: %s\n", nequ); + } + + bool ret = '0' - *nequ; + + free(nequ); + + return ret; +} \ No newline at end of file diff --git a/src/logic_run.h b/src/logic_run.h new file mode 100644 index 0000000..c2e9a4e --- /dev/null +++ b/src/logic_run.h @@ -0,0 +1,27 @@ + // This header file was generated on +// z5214348.web.cse.unsw.edu.au/header_generator/ + +// header guard: https://en.wikipedia.org/wiki/Include_guard +// This avoids errors if this file is included multiple times +// in a complex source file setup + +#ifndef LOGIC_RUN_H +#define LOGIC_RUN_H + +// #includes + +#include +#include +#include +#include +#include +#include "logic_ops.h" +#include "logic_symb.h" + +// Functions + +void logic_run_replace_char(char* str, char old, char new); +bool logic_run_runner(char* equ, int argc, bool* argv); + +// End of header file +#endif diff --git a/src/logic_symb.c b/src/logic_symb.c new file mode 100644 index 0000000..eae3a7d --- /dev/null +++ b/src/logic_symb.c @@ -0,0 +1,19 @@ +#include +#include + + + +void logic_symbs_args_calc(int args, bool *left, bool *right) +{ + *left = args & (1 << 0); + *right = args & (1 << 1); +} + +int logic_symbs_args_num(int args) +{ + bool left, right; + + logic_symbs_args_calc(args, &left, &right); + + return left + right; +} \ No newline at end of file diff --git a/src/logic_symb.h b/src/logic_symb.h new file mode 100644 index 0000000..a5b11c1 --- /dev/null +++ b/src/logic_symb.h @@ -0,0 +1,35 @@ + // This header file was generated on +// z5214348.web.cse.unsw.edu.au/header_generator/ + +// header guard: https://en.wikipedia.org/wiki/Include_guard +// This avoids errors if this file is included multiple times +// in a complex source file setup + +#ifndef LOGIC_SYMB_H +#define LOGIC_SYMB_H + +// #includes + +#include +#include + +#define logic_symb_and '^' +#define logic_symb_or 'v' +#define logic_symb_xor 'o' +#define logic_symb_not '-' + +// arg positions +// bit 1 - left, bit 2 - right + +#define logic_symb_args_and 1 + 2 +#define logic_symb_args_or 1 + 2 +#define logic_symb_args_not 0 + 2 +#define logic_symb_args_xor 1 + 2 + +// Functions + +void logic_symbs_args_calc(int args, bool *left, bool *right); +int logic_symbs_args_num(int args); + +// End of header file +#endif