GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — posixSemaphore/truth.c at dev · onepointerT/posixSemaphore Expand file tree/ Copy pathtruth.cMore file actions 248 lines (176 loc) · 6.9 KB / Copy pathtruth.cFile metadata and controls248 lines (176 loc) · 6.9 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 // Copyright (C) 2026 The OnePointer Authors. // #include "truth.h" #include <stdlib.h> #include <string.h> truth_value_t* truthvalue_new() { truth_value_t* tvt = (union TruthValue*) malloc(sizeof(union TruthValue)); tvt->dbl = NULL; tvt->num = NULL; tvt->ptr = NULL; tvt->str = NULL; tvt->val = NULL; return tvt; } truth_value_t truthvalue_new_str( char* str ) { truth_value_t* tvt = truthvalue_new(); tvt->str = str; return *tvt; } truth_value_t truthvalue_new_ptr( void* dataptr, const char* type ) { truth_value_t* tvt = truthvalue_new(); tvt->ptr = dataptr; tvt->str = type; return *tvt; } truth_value_t truthvalue_new_boolval( bool* boolptr ) { truth_value_t* tvt = truthvalue_new(); tvt->val = boolptr; return *tvt; } truth_value_t truthvalue_new_num( int* intptr ) { truth_value_t* tvt = truthvalue_new(); tvt->num = intptr; return *tvt; } truth_value_t truthvalue_new_double( double* doubleptr ) { truth_value_t* tvt = truthvalue_new(); tvt->dbl = doubleptr; return *tvt; } bool trutheval_str( const char* lhs, const enum truth_operators_t op, const char* rhs ) { if ( ( lhs == NULL || rhs == NULL ) && op != NEGATION ) return false; if ( op == AND ) { // Bitwise AND return *lhs && *rhs; } else if ( op == OR ) { // Bitwise OR return *lhs || *rhs; } else if ( op == IMPLIES ) { return ( *lhs && *rhs ) || *lhs; } else if ( op == EQUIV ) { return strcmp( lhs, rhs ) == 0; } else if ( op == UNEQ ) { return strcmp( lhs, rhs ) != 0; } else if ( op == NEGATION ) { if ( lhs != NULL && rhs == NULL ) return ! *lhs; else if ( rhs != NULL && lhs != NULL ) return ! *rhs; return false; } else return false; } bool trutheval_bool( const bool* lhs, const enum truth_operators_t op, const bool* rhs ) { if ( ( lhs == NULL || rhs == NULL ) && op != NEGATION ) return false; if ( op == AND ) { // Bitwise AND return *lhs && *rhs; } else if ( op == OR ) { // Bitwise OR return *lhs || *rhs; } else if ( op == IMPLIES ) { return (! *lhs) || rhs; } else if ( op == EQUIV ) { return *lhs == *rhs; } else if ( op == UNEQ ) { return *lhs != *rhs; } else if ( op == NEGATION ) { if ( lhs == NULL && rhs != NULL ) return ! *rhs; else if ( rhs == NULL && lhs != NULL ) return ! *lhs; return false; } else return false; } bool trutheval_num( const int* lhs, const enum truth_operators_t op, const int* rhs ) { if ( ( lhs == NULL || rhs == NULL ) && op != NEGATION ) return false; if ( op == AND ) { // Bitwise AND return *lhs && *rhs; } else if ( op == OR ) { // Bitwise OR return *lhs || *rhs; } else if ( op == IMPLIES ) { return trutheval_num( *lhs && *rhs, NEGATION, NULL ) || *rhs; } else if ( op == EQUIV ) { return *lhs == *rhs; } else if ( op == UNEQ ) { return *lhs != *rhs; } else if ( op == NEGATION ) { if ( lhs != NULL && rhs == NULL ) return ! *lhs; else if ( rhs != NULL && lhs == NULL ) return ! *rhs; else return false; } else return false; } bool trutheval_double( const double* lhs, const enum truth_operators_t op, const double* rhs ) { if ( ( lhs == NULL || rhs == NULL ) && op != NEGATION ) return false; if ( op == AND ) { // Bitwise AND return *lhs && *rhs; } else if ( op == OR ) { // Bitwise OR return *lhs || *rhs; } else if ( op == IMPLIES ) { return trutheval_double( *lhs || *rhs, NEGATION, NULL ) && *lhs; } else if ( op == EQUIV ) { return *lhs == *rhs; } else if ( op == UNEQ ) { return *lhs != *rhs; } else if ( op == NEGATION ) { if ( rhs == NULL && lhs != NULL ) return ! *lhs; else if ( lhs == NULL && rhs != NULL ) return ! *rhs; else return false; } else return false; } bool trutheval_ptr( const void* lhs, const enum truth_operators_t op, const void* rhs, const char* type ) { if ( ( lhs == NULL || rhs == NULL ) && op != NEGATION ) return false; else if ( strcmp(type, ((struct VoidEvaluatingTruth*) lhs)->struct_type) != 0 || strcmp(type, ((struct VoidEvaluatingTruth*) rhs)->struct_type) != 0 ) return false; else return ((struct VoidEvaluatingTruth*) lhs)->trutheval_voidptr(lhs, AND, rhs); } bool trutheval( truth_value_t lhs, enum truth_operators_t op, truth_value_t rhs ) { if ( lhs.dbl != NULL ) return trutheval_double(lhs.dbl, op, rhs.dbl); else if ( lhs.num != NULL ) return trutheval_num(lhs.num, op, rhs.num); else if ( lhs.ptr != NULL ) return trutheval_ptr(lhs.ptr, op, rhs.ptr, lhs.str); else if ( lhs.str != NULL ) return trutheval_str(lhs.str, op, rhs.str); else if ( lhs.val != NULL ) return trutheval_bool(lhs.val, op, rhs.val); else return false; } struct VoidEvaluatingTruth* voidevauluatingtruth_new( const char* struct_type ) { struct VoidEvaluatingTruth* vet = (struct VoidEvaluatingTruth*) malloc(sizeof(struct VoidEvaluatingTruth)); vet->struct_type = struct_type; vet->trutheval_voidptr = &trutheval_voidptr; return vet; } struct Truth* truth_new( truth_value_t val1, enum truth_operators_t op, truth_value_t val2 ) { struct Truth* truth = (struct Truth*) malloc(sizeof(struct Truth)); truth->val1 = val1; truth->op = op; truth->val2 = val2; return truth; } bool trutheval_truth( const struct Truth* truth ) { return trutheval( truth->val1, truth->op, truth->val2 ); } struct TruthEquivation* truth_equivation_new( const size_t arr_size ) { struct TruthEquivation* te = (struct TruthEquivation*) malloc(sizeof(struct TruthEquivation)); te->equivs = (struct Truth**) malloc(sizeof(struct Truth*)*arr_size); te->ops = (enum truth_operators_t**) malloc(sizeof(truth_operators_t*)*arr_size); te->size = arr_size; te->size_used = 0; for ( unsigned int fi = 0; fi < te->size; fi++ ) { te->equivs[fi] = NULL; te->ops[fi] = NULL; } return te; } bool truth_equivation_push( struct TruthEquivation* te, struct Truth* truth, enum truth_operators_t opright ) { if ( te->size_used >= te->size ) return false; for ( unsigned int fi = 0; fi < te->size; fi++ ) { if ( te->equivs[fi] == NULL ) { te->equivs[fi] = truth; te->ops[fi] = &opright; ++te->size_used; return true; } } return false; } bool trutheval_equivation( const struct TruthEquivation* equiv ) { if (equiv->equivs[0] == NULL) return false; bool current_result = trutheval_truth(equiv->equivs[0]); for ( unsigned int ti = 1; ti < equiv->size; ti++ ) { bool next_result = trutheval_truth(equiv->equivs[ti]); current_result = trutheval_bool( ¤t_result, *(equiv->ops[ti-1]), &next_result ); if ( equiv->ops[ti] == NULL ) break; } return current_result; } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |