2 * Copyright (C) 2017 "IoT.bzh"
3 * Author José Bollo <jose.bollo@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
33 "a and b and c and d",
36 "(a or b) and (c or d)",
37 "not (a or b or c or d)",
38 "a and not (b or c or d)",
39 "b and not (a or c or d)",
40 "c and not (a or b or d)",
41 "d and not (a or b or c)",
45 int check(void *closure, const char *name)
50 if (name[0] < 'a' || name[0] > 'd' || name[1])
52 return 1 & (x >> (name[0] - 'a'));
55 int test(const char *expr)
58 struct afb_perm *perm;
62 perm = afb_perm_parse(expr);
64 printf("error for %s\n", expr);
66 printf("\nabcd %s\n", expr);
67 for (x = 0; x < 16 ; x++) {
68 c = afb_perm_check(perm, check, &x);
69 printf("%c%c%c%c %d\n",
70 '0'+!!(x&1), '0'+!!(x&2), '0'+!!(x&4), '0'+!!(x&8),
81 void add(char *buffer, const char *fmt, ...)
92 void mke(int value, int bits, char *buffer)
95 int sval = 1 << (bits - 1);
96 int mask = (1 << nval) - 1;
97 int smask = (1 << sval) - 1;
98 int val = value & mask;
99 int val0 = val & smask;
100 int val1 = (val >> sval) & smask;
101 char c = (char)('a' + bits - 1);
105 case 0: add(buffer, "x"); break;
106 case 1: add(buffer, "not %c", c); break;
107 case 2: add(buffer, "%c", c); break;
108 case 3: add(buffer, "(%c or not %c) ", c, c); break;
110 } else if (val0 != val1) {
112 add(buffer, "not %c", c);
114 add(buffer, " and (");
115 mke(val0, bits - 1, buffer);
122 add(buffer, "%c", c);
124 add(buffer, " and (");
125 mke(val1, bits - 1, buffer);
130 mke(val0, bits - 1, buffer);
134 void makeexpr(int value, char *buffer)
140 mke(value, 4, buffer);
150 for (i = 0 ; i < 65536 ; i++) {
153 printf("[[[ %d %s %d ]]]\n", i, i==j?"==":"!=", j);