bool

Програм за тестирање логичких израза
git clone https://git.sr.ht/~strahinja/bool
Дневник | Датотеке | Референце | ПРОЧИТАЈМЕ | ЛИЦЕНЦА

чување 768865e8737ab748be50c3f5d9845f0a738a25a7
родитељ 8cd16ee13cb7b2ef679dd28169767235eaae06c6
Аутор: Страхиња Радић <sr@strahinja.org>
Датум:   Tue, 20 Jan 2026 09:05:04 +0100

bool.y: Add WIP simulate, evaluate; sort declarations

Diffstat:
Mbool.y | 225++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------
измењених датотека: 1, додавања: 160(+), брисања: 65(-)

diff --git a/bool.y b/bool.y @@ -34,6 +34,7 @@ #define YYDEBUG 1 static char** ids = NULL; +static signed char* vals = NULL; static int ids_size = 0; static int num_ids = 0; static int debug = 0; @@ -41,10 +42,19 @@ static int debug = 0; extern FILE* yyin; int colno = 1, lineno = 1; +void yydbg(const char* format, ...); +void yyerror(const char* format, ...); + +int add_id(const char* id); char* get_id(const int index); +signed char get_val(const int index); int get_index(const char* id); -void print_node(const int level, const int debug_only, struct expr_node* node); -void yyerror(const char* format, ...); +struct expr_node* new_expr(const enum NodeType node_type, const enum Operator +operator, const int nargs, ...); +static void print_node(const int level, const int debug_only, struct expr_node* node); +static int simulate(const int index); +static void usage(void); +static void version(const int full); extern int yylex(void); @@ -67,37 +77,87 @@ const char* operator_images[] = { static struct expr_node* root = NULL; -static void -usage(void) +void +yydbg(const char* format, ...) { - puts("Usage: " PROGNAME " [-dhVv] [file]"); + va_list ap; + char buf[1024]; + if (!debug) + return; + va_start(ap, format); + vsnprintf(buf, sizeof buf, format, ap); + fprintf(stderr, "DEBUG: %s\n", buf); + va_end(ap); } -static void -version(const int full) +void +yyerror(const char* format, ...) { - printf("%s %s, committed on %s\n", PROGNAME, VERSION, DATE); - if (full) - puts(COPYRIGHT); + va_list ap; + char buf[1024]; + va_start(ap, format); + vsnprintf(buf, sizeof buf, format, ap); + fprintf(stderr, "%s:%d:%d: %s\n", PROGNAME, lineno, colno, buf); + va_end(ap); } int add_id(const char* id) { - char** temp = NULL; + char** tempi = NULL; + signed char* tempv = NULL; if (num_ids >= ids_size) { - temp = realloc(ids, (ids_size + IDS_DELTA) * sizeof(char*)); - if (!temp) - { - perror("realloc"); - exit(errno); - } + tempi = realloc(ids, (ids_size + IDS_DELTA) * sizeof(char*)); + if (!tempi) + goto add_id_realloc_error; + ids = tempi; + tempv = realloc(vals, (ids_size + IDS_DELTA)); + if (!tempv) + goto add_id_realloc_error; + vals = tempv; ids_size += IDS_DELTA; - ids = temp; } ids[num_ids++] = strdup(id); + vals[num_ids-1] = 0; return num_ids-1; + +add_id_realloc_error: + perror("realloc"); + exit(errno); +} + +char* +get_id(const int index) +{ + if (index >= 0 && index < num_ids) + return ids[index]; + return "N/A"; +} + +signed char +get_val(const int index) +{ + if (index >= 0 && index < num_ids) + return vals[index]; + return -1; +} + +int +get_index(const char* id) +{ + int idx = 0; + int found = 0; + while (!found && idx < num_ids) + { + if (!strcmp(ids[idx], id)) + { + found = 1; + continue; + } + idx++; + } + return found ? idx : -1; } struct expr_node* @@ -155,31 +215,6 @@ new_expr_va_arg_error: return NULL; } -char* -get_id(const int index) -{ - if (index >= 0 && index < num_ids) - return ids[index]; - return "N/A"; -} - -int -get_index(const char* id) -{ - int idx = 0; - int found = 0; - while (!found && idx < num_ids) - { - if (!strcmp(ids[idx], id)) - { - found = 1; - continue; - } - idx++; - } - return found ? idx : -1; -} - void print_node(const int level, const int debug_only, struct expr_node* node) { @@ -220,28 +255,82 @@ print_node(const int level, const int debug_only, struct expr_node* node) } } -void -yydbg(const char* format, ...) +static int +evaluate(const struct expr_node* node) { - va_list ap; - char buf[1024]; - if (!debug) - return; - va_start(ap, format); - vsnprintf(buf, sizeof buf, format, ap); - fprintf(stderr, "DEBUG: %s\n", buf); - va_end(ap); + int result = -1; + switch (node->type) + { + case NT_UNARY: + switch (node->expr.unary->operator) + { + case OP_NOT: + result = !evaluate(node->expr.unary->arg); + break; + default:; + } + break; + case NT_BINARY: + switch (node->expr.binary->operator) + { + case OP_AND: + result = evaluate(node->expr.binary->args[0]) + & evaluate(node->expr.binary->args[1]); + break; + case OP_OR: + result = evaluate(node->expr.binary->args[0]) + | evaluate(node->expr.binary->args[1]); + break; + case OP_XOR: + result = evaluate(node->expr.binary->args[0]) + ^ evaluate(node->expr.binary->args[1]); + break; + default:; + } + break; + case NT_BRACKETS: + result = evaluate(node->expr.brackets->arg); + break; + case NT_IDENT: + result = get_val(node->expr.ident->id); + break; + default:; + } + return result; } -void -yyerror(const char* format, ...) +static int +simulate(const int index) { - va_list ap; - char buf[1024]; - va_start(ap, format); - vsnprintf(buf, sizeof buf, format, ap); - fprintf(stderr, "%s:%d:%d: %s\n", PROGNAME, lineno, colno, buf); - va_end(ap); + int ii; + signed char val; + if (index >= num_ids) + return 0; + for (val = 0; val < 2; val++) + { + vals[index] = val; + simulate(index+1); + /*if (!simulate(index+1)) + continue;*/ + for (ii = 0; ii < num_ids; ii++) + printf("%c\t", get_val(ii) ? 'T' : 'F'); + printf("= %c\n", evaluate(root) ? 'T' : 'F'); + } + return 1; +} + +static void +usage(void) +{ + puts("Usage: " PROGNAME " [-dhVv] [file]"); +} + +static void +version(const int full) +{ + printf("%s %s, committed on %s\n", PROGNAME, VERSION, DATE); + if (full) + puts(COPYRIGHT); } %} @@ -282,11 +371,14 @@ int main(int argc, char** argv) { int ch; + int showtree = 0; ids_size = IDS_DELTA; num_ids = 0; ids = malloc(ids_size * sizeof(char*)); ids[0] = NULL; + vals = malloc(ids_size); + vals[0] = 0; //yydebug = 1; if ((ch = getopt(argc, argv, ":dhVv")) != -1) @@ -299,6 +391,9 @@ main(int argc, char** argv) case 'h': usage(); exit(0); + case 't': + showtree = 1; + break; case 'V': version(1); exit(0); @@ -320,11 +415,11 @@ main(int argc, char** argv) exit(errno); } } - //yylex(); yyparse(); - - if (root) - print_node(0, 0, root); if (yyin && yyin != stdin) fclose(yyin); + + if (root && showtree) + print_node(0, 0, root); + simulate(0); }