aboutsummaryrefslogtreecommitdiff
path: root/lib/tst_bool_expr.c
blob: 387c38b9115346aeee53c22b100d70d9b62422cd (plain)
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (c) 2020 Cyril Hrubis <chrubis@suse.cz>
 */
/*
 * Boolean expression is evaluated in three steps.
 *
 * First of all the string containing the expression is tokenized. The
 * tokenizer runs twice and we only count number of tokens in the first pass in
 * order to simplify the memory allocation.
 *
 * Secondly the the expression is transformed to a postfix (RPN) notation by
 * the shunting yard algorithm and the correctness of the expression is checked
 * during the transformation as well. The fact that parenthesis are matched is
 * asserted by the shunting yard algorithm itself while the rest is checked
 * simply by checking if the preceding token is in a set of allowed tokens.
 * This could be thought of as a simple open-coded state machine.
 *
 * An expression in the RPN form can be evaluated given a variable mapping
 * function. The evaluation ignores most of errors because invalid expression
 * will be rejected in the second step.
 */

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "tst_bool_expr.h"

static int char_to_op(char c)
{
	switch (c) {
	case '(':
		return TST_OP_LPAR;
	case ')':
		return TST_OP_RPAR;
	case '&':
		return TST_OP_AND;
	case '|':
		return TST_OP_OR;
	case '!':
		return TST_OP_NOT;
	default:
		return TST_OP_VAR;
	}
}

static int new_tok(struct tst_expr_tok **last, const char *tok, size_t tok_len)
{
	if (!tok_len)
		return 0;

	if (!(*last))
		return 1;

	(*last)->tok = tok;
	(*last)->tok_len = tok_len;
	(*last)->op = char_to_op(tok[0]);
	(*last)++;

	return 1;
}

static unsigned int tokenize(const char *expr, struct tst_expr_tok *last)
{
	size_t i, j;
	unsigned int token_cnt = 0;

	for (j = i = 0; expr[i]; i++) {
		switch (expr[i]) {
		case '(':
		case ')':
		case '!':
		case '&':
		case '|':
			token_cnt += new_tok(&last, &expr[j], i - j);
			token_cnt += new_tok(&last, &expr[i], 1);
			j = i+1;
		break;
		case '\t':
		case ' ':
			token_cnt += new_tok(&last, &expr[j], i - j);
			j = i+1;
		break;
		case '"':
			while (expr[i+1] != '"' && expr[i+1])
				i++;

			if (expr[i+1] == '"')
				i++;
		break;
		default:
		break;
		}
	}

	token_cnt += new_tok(&last, &expr[j], i - j);

	return token_cnt;
}

void tst_bool_expr_print(FILE *f, struct tst_expr *expr)
{
	struct tst_expr_tok *i;
	size_t j;

	for (i = expr->rpn; i; i = i->next) {
		for (j = 0; j < i->tok_len; j++)
			putc(i->tok[j], f);

		if (i->next)
			putc(' ', f);
	}
}

static void print_spaces(FILE *f, unsigned int spaces)
{
	while (spaces--)
		putc(' ', f);
}

static void tst_bool_expr_err(FILE *f, struct tst_expr *expr, unsigned int cnt)
{
	unsigned int i, spaces, err_len;
	const char *err;

	fprintf(f, "%s", expr->buf->tok);
	fprintf(f, "\n");

	for (i = 0; i < cnt; i++) {
		if (expr->buf[i].priv)
			break;
	}

	spaces = expr->buf[i].tok - expr->buf[0].tok;
	err = expr->buf[i].priv;
	err_len = strlen(err);

	print_spaces(f, spaces);

	fprintf(f, "^\n");

	if (err_len < spaces)
		print_spaces(f, spaces - err_len + 1);

	fprintf(f, "%s\n", err);
}

static inline void stack_push(struct tst_expr_tok *stack[], unsigned int *op_stack_pos,
                              struct tst_expr_tok *op)
{
	stack[(*op_stack_pos)++] = op;
}

static inline int stack_empty(unsigned int op_stack_pos)
{
	return op_stack_pos == 0;
}

static inline struct tst_expr_tok *stack_pop(struct tst_expr_tok *stack[],
                                             unsigned int *op_stack_pos)
{
	if (stack_empty(*op_stack_pos))
		return NULL;

	return stack[--(*op_stack_pos)];
}

#define TST_OP_NONE -1

static inline int stack_peek_op(struct tst_expr_tok *stack[],
                                unsigned int op_stack_pos)
{
	if (stack_empty(op_stack_pos))
		return TST_OP_NONE;

	return stack[op_stack_pos - 1]->op;
}

/*
 * This is a complete list of which tokens can happen before any of:
 *  - variable
 *  - left parentesis
 *  - negation
 *
 * The -1 represents start of the expression.
 */
static inline int check_one(int op)
{
	switch (op) {
	case TST_OP_AND:
	case TST_OP_OR:
	case TST_OP_NOT:
	case TST_OP_NONE:
	case TST_OP_LPAR:
		return 0;
	default:
		return 1;
	}
}

/*
 * And this checks for tokens that can happen before any of:
 * - right parentesis
 * - and
 * - or
 *
 * This is also used to check that the last token in expression is correct one.
 */
static inline int check_two(int op)
{
	switch (op) {
	case TST_OP_VAR:
	case TST_OP_RPAR:
		return 1;
	default:
		return 0;
	}
}

static int shunting_yard(struct tst_expr *expr, unsigned int cnt)
{
	struct tst_expr_tok *op_stack[cnt];
	unsigned int op_stack_pos = 0;
	struct tst_expr_tok *out[cnt + 1];
	unsigned int out_pos = 0;
	struct tst_expr_tok *i;
	unsigned int j;
	int prev_op = TST_OP_NONE;

	for (i = expr->buf; i < &(expr->buf[cnt]); i++) {
		switch (i->op) {
		case TST_OP_VAR:
			if (check_one(prev_op)) {
				i->priv = "Expected operation";
				goto err;
			}

			stack_push(out, &out_pos, i);

			while (stack_peek_op(op_stack, op_stack_pos) == TST_OP_NOT)
				stack_push(out, &out_pos, stack_pop(op_stack, &op_stack_pos));
		break;
		case TST_OP_LPAR:
			if (check_one(prev_op)) {
				i->priv = "Expected operation";
				goto err;
			}

			stack_push(op_stack, &op_stack_pos, i);
		break;
		case TST_OP_RPAR:
			if (!check_two(prev_op)) {
				i->priv = "Expected variable or )";
				goto err;
			}

			/* pop everything till ( */
			for (;;) {
				struct tst_expr_tok *op = stack_pop(op_stack, &op_stack_pos);

				if (!op) {
					i->priv = "Missing (";
					goto err;
				}

				if (op->op == TST_OP_LPAR)
					break;

				stack_push(out, &out_pos, op);
			}

			while (stack_peek_op(op_stack, op_stack_pos) == TST_OP_NOT)
				stack_push(out, &out_pos, stack_pop(op_stack, &op_stack_pos));
		break;
		case TST_OP_NOT:
			if (check_one(prev_op)) {
				i->priv = "Expected operation";
				goto err;
			}
			stack_push(op_stack, &op_stack_pos, i);
		break;
		case TST_OP_AND:
		case TST_OP_OR:
			if (!check_two(prev_op)) {
				i->priv = "Expected variable or (";
				goto err;
			}

			/*
			 * There can be at most one binary op on the stack
			 * since we pop the one present on the stack before we
			 * attempt to push new one they so never accumulate.
			 */
			switch (stack_peek_op(op_stack, op_stack_pos)) {
			case TST_OP_AND:
			case TST_OP_OR:
				stack_push(out, &out_pos, stack_pop(op_stack, &op_stack_pos));
			break;
			}

			stack_push(op_stack, &op_stack_pos, i);
		break;
		}

		prev_op = i->op;
	}

	if (!check_two(expr->buf[cnt-1].op)) {
		expr->buf[cnt-1].priv = "Unfinished expression";
		goto err;
	}

	/* pop remaining operations */
	while ((i = stack_pop(op_stack, &op_stack_pos))) {
		if (i->op == TST_OP_LPAR) {
			i->priv = "Missing )";
			goto err;
		}

		stack_push(out, &out_pos, i);
	}

	/* construct the list */
	out[out_pos] = NULL;

	for (j = 0; j < out_pos; j++)
		out[j]->next = out[j + 1];

	expr->rpn = out[0];

	return 0;
err:
	return 1;
}

struct tst_expr *tst_bool_expr_parse(const char *expr)
{
	struct tst_expr *ret;
	unsigned int tok_cnt = tokenize(expr, NULL);

	if (!tok_cnt)
		return NULL;

	ret = malloc(sizeof(struct tst_expr) + sizeof(struct tst_expr_tok) * tok_cnt);
	if (!ret)
		return NULL;

	tokenize(expr, ret->buf);

	if (shunting_yard(ret, tok_cnt)) {
		tst_bool_expr_err(stderr, ret, tok_cnt);
		tst_bool_expr_free(ret);
		return NULL;
	}

	return ret;
}

#define MAX_STACK 16

int tst_bool_expr_eval(struct tst_expr *expr,
                       int (*map)(struct tst_expr_tok *var))
{
	struct tst_expr_tok *i;
	int stack[MAX_STACK];
	int pos = -1;

	for (i = expr->rpn; i; i = i->next) {
		switch (i->op) {
		case TST_OP_NOT:
			stack[pos] = !stack[pos];
		break;
		case TST_OP_OR:
			stack[pos-1] = stack[pos] || stack[pos-1];
			pos--;
		break;
		case TST_OP_AND:
			stack[pos-1] = stack[pos] && stack[pos-1];
			pos--;
		break;
		case TST_OP_VAR:
			if (pos + 1 >= MAX_STACK) {
				fprintf(stderr, "Eval out of stack!\n");
				return -1;
			}

			stack[++pos] = map(i);

			/* map reported undefined variable -> abort */
			if (stack[pos] == -1)
				return -1;
		break;
		/* does not happen */
		default:
		break;
		}
	}

	return stack[0];
}

void tst_bool_expr_free(struct tst_expr *expr)
{
	free(expr);
}