File: constraint.c

package info (click to toggle)
pcb-rnd 3.0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 29,624 kB
  • sloc: ansic: 197,571; yacc: 6,153; sh: 5,808; awk: 2,708; makefile: 2,139; lex: 1,107; python: 519; xml: 261; lisp: 169; tcl: 67; perl: 34; javascript: 6; ruby: 5
file content (476 lines) | stat: -rw-r--r-- 11,981 bytes parent folder | download | duplicates (3)
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <librnd/core/compat_misc.h>
#include <librnd/core/rnd_printf.h>
#include <librnd/core/error.h>
#include "const_gram.h"
#include "constraint.h"
#include "const_gram.h"
#include "const_lex.h"

/* Error is handled on the push side */
void pcb_ordc_error(pcb_ordc_ctx_t *ctx, pcb_ordc_STYPE tok, const char *s) { }

int pcb_ordc_parse_verbose = 0;

int pcb_ordc_parse_str(pcb_ordc_ctx_t *octx, const char *script)
{
	pcb_ordc_yyctx_t yyctx;
	pcb_ordc_STYPE lval;
	pcb_ordc_ureglex_t lctx;
	int tok, yres;

	if (octx->root == NULL) {
		octx->root = calloc(sizeof(pcb_ordc_node_t), 1);
		octx->root->type = PCB_ORDC_BLOCK;
	}

	pcb_ordc_lex_init(&lctx, pcb_ordc_rules);
	pcb_ordc_parse_init(&yyctx);

	/* read all bytes of the binary file */
	for(;*script != '\0';script++) {
		/* feed the lexer */
		if (pcb_ordc_parse_verbose) printf("in: '%c'\n", *script);
		tok = pcb_ordc_lex_char(&lctx, &lval, *script);
		if (tok == UREGLEX_MORE)
			continue;
		if (pcb_ordc_parse_verbose) printf(" tok=%d\n", tok);


		/* feed the grammar */
		lval.line = lctx.loc_line[0];
		lval.first_col = lctx.loc_col[0];
		yres = pcb_ordc_parse(&yyctx, octx, tok, &lval);

		if (pcb_ordc_parse_verbose) printf("  yy=%d\n", yres);

#if 0
		/* see io_bxl */
		if ((octx.in_error) && ((tok == T_ID) || (tok == T_QSTR)))
			free(lval.un.s);
#endif

		if (yres != 0) {
			fprintf(stderr, "order constraint script syntax error at %ld:%ld\n", lval.line, lval.first_col);
			goto error;
		}
		pcb_ordc_lex_reset(&lctx); /* prepare for the next token */
	}

	/* flush pending token with an EOF */
	pcb_ordc_parse(&yyctx, octx, EOF, &lval);


	return 0;

	error:;
TODO("free octx tree");
	return -1;
}

static void print_ind(FILE *f, int lev)
{
	for(;lev > 0; lev--)
		fputc(' ', f);
}

void pcb_ordc_print_tree(FILE *f, pcb_ordc_ctx_t *ctx, pcb_ordc_node_t *node, int indlev)
{
	pcb_ordc_node_t *n;

	print_ind(f, indlev);

	switch(node->type) {
		case PCB_ORDC_BLOCK:   printf("block\n"); break;

		case PCB_ORDC_IF:      printf("if()\n"); break;
		case PCB_ORDC_ERROR:   printf("error()\n"); break;

		case PCB_ORDC_CINT:    printf("const int %ld\n", node->val.l); break;
		case PCB_ORDC_CFLOAT:  printf("const float %f\n", node->val.d); break;
		case PCB_ORDC_QSTR:    printf("const qstr '%s'\n", node->val.s); break;
		case PCB_ORDC_ID:      printf("const id '%s'\n", node->val.s); break;
		case PCB_ORDC_VAR:     printf("var '$%s'\n", node->val.s); break;

		case PCB_ORDC_INT:     printf("int()\n"); break;
		case PCB_ORDC_FLOAT:   printf("float()\n"); break;
		case PCB_ORDC_STRING:  printf("string()\n"); break;

		case PCB_ORDC_NEG:     printf("neg\n"); break;
		case PCB_ORDC_EQ:      printf("eq\n"); break;
		case PCB_ORDC_NEQ:     printf("neq\n"); break;
		case PCB_ORDC_GE:      printf("ge\n"); break;
		case PCB_ORDC_LE:      printf("le\n"); break;
		case PCB_ORDC_GT:      printf("gt\n"); break;
		case PCB_ORDC_LT:      printf("lt\n"); break;

		case PCB_ORDC_AND:     printf("and\n"); break;
		case PCB_ORDC_OR:      printf("or\n"); break;
		case PCB_ORDC_NOT:     printf("not\n"); break;

		case PCB_ORDC_ADD:     printf("add\n"); break;
		case PCB_ORDC_SUB:     printf("sub\n"); break;
		case PCB_ORDC_MULT:    printf("mult\n"); break;
		case PCB_ORDC_DIV:     printf("div\n"); break;
		case PCB_ORDC_MOD:     printf("mod\n"); break;

		default:
			printf("UNKNONW %d\n", node->type);
	}

	for(n = node->ch_first; n != NULL; n = n->next)
		pcb_ordc_print_tree(f, ctx, n, indlev+1);
}


void pcb_ordc_free_tree(pcb_ordc_ctx_t *ctx, pcb_ordc_node_t *node)
{
	pcb_ordc_node_t *n, *next;

	switch(node->type) {
		case PCB_ORDC_QSTR:
		case PCB_ORDC_ID:
		case PCB_ORDC_VAR:
			free(node->val.s);
			break;
		default:
			break;
	}

	for(n = node->ch_first; n != NULL; n = next) {
		next = n->next;
		pcb_ordc_free_tree(ctx, n);
	}

	free(node);
}

void pcb_ordc_uninit(pcb_ordc_ctx_t *ctx)
{
	pcb_ordc_free_tree(ctx, ctx->root);
	ctx->root = NULL;
}

/* Returns 0 or 1 for valid bool values, -1 for error */
static int val2bool(pcb_ordc_val_t v)
{
	switch(v.type) {
		case PCB_ORDC_VLNG: return !!v.val.l;
		case PCB_ORDC_VDBL: return v.val.l != 0;
		case PCB_ORDC_VCSTR: return *v.val.s != '\0';
		case PCB_ORDC_VDSTR: return *v.val.s != '\0';
		case PCB_ORDC_VERR: return -1;
	}
	return -1;
}

static void val_free(pcb_ordc_val_t *v)
{
	if (v->type == PCB_ORDC_VDSTR) {
		free(v->val.s);
		v->val.s = NULL;
	}
}

static void conv2bool(pcb_ordc_val_t *dst, pcb_ordc_val_t src)
{
	int b = val2bool(src);
	if (b >= 0) {
		dst->type = PCB_ORDC_VLNG; dst->val.l = b;
	}
	else
		dst->type = PCB_ORDC_VERR;
}

static void conv2lng(pcb_ordc_val_t *dst, pcb_ordc_val_t src)
{
	dst->type = PCB_ORDC_VLNG;
	dst->val.l = 0;

	switch(src.type) {
		case PCB_ORDC_VLNG: dst->val.l = src.val.l; break;
		case PCB_ORDC_VDBL: dst->val.l = rnd_round(src.val.d); break;
		case PCB_ORDC_VCSTR:
		case PCB_ORDC_VDSTR:
			dst->val.l = strtol(src.val.s, NULL, 10);
			break;
		case PCB_ORDC_VERR: dst->type = PCB_ORDC_VERR;
	}
}

static void conv2dbl(pcb_ordc_val_t *dst, pcb_ordc_val_t src)
{
	dst->type = PCB_ORDC_VDBL;
	dst->val.d = 0;

	switch(src.type) {
		case PCB_ORDC_VLNG: dst->val.d = src.val.l; break;
		case PCB_ORDC_VDBL: dst->val.d = src.val.d; break;
		case PCB_ORDC_VCSTR:
		case PCB_ORDC_VDSTR:
			dst->val.d = strtod(src.val.s, NULL);
			break;
		case PCB_ORDC_VERR: dst->type = PCB_ORDC_VERR;
	}
}

static void conv2str(pcb_ordc_val_t *dst, pcb_ordc_val_t src)
{
	dst->type = PCB_ORDC_VDSTR;

	switch(src.type) {
		case PCB_ORDC_VLNG: dst->val.s = rnd_strdup_printf("%ld", src.val.l); break;
		case PCB_ORDC_VDBL: dst->val.s = rnd_strdup_printf("%f", src.val.d); break;
		case PCB_ORDC_VCSTR: dst->val.s = src.val.s; dst->type = PCB_ORDC_VCSTR; break;
		case PCB_ORDC_VDSTR: dst->val.s = rnd_strdup(src.val.s); break;
		case PCB_ORDC_VERR: dst->type = PCB_ORDC_VERR;
	}
}

/* convert two child subtrees to operands, then set binop_str.
   If binop_str is 1, load sa and sb to point to the strings.
   If binop_str is 1, load da and db to numbers loaded from the ops.
   return error early; propagate long to double or anything to string */
#define BINOP_GET_OPS \
	do { \
		c.type = PCB_ORDC_VLNG; \
		binop_str = 0; \
		pcb_ordc_exec_node(ctx, &a, node->ch_first); \
		pcb_ordc_exec_node(ctx, &b, node->ch_first->next); \
		if ((a.type == PCB_ORDC_VERR) || (b.type == PCB_ORDC_VERR)) { \
			dst->type = PCB_ORDC_VERR; \
		} \
		else if ((a.type == PCB_ORDC_VCSTR) || (a.type == PCB_ORDC_VDSTR)) { \
			binop_str = 1; \
			sa = a.val.s; \
			if ((b.type != PCB_ORDC_VCSTR) && (b.type != PCB_ORDC_VDSTR)) { \
				conv2str(&c, b); \
				sb = c.val.s; \
			} \
			else \
				sb = b.val.s; \
		} \
		else if ((b.type == PCB_ORDC_VCSTR) || (b.type == PCB_ORDC_VDSTR)) { \
			binop_str = 1; \
			sb = b.val.s; \
			if ((a.type != PCB_ORDC_VCSTR) && (a.type != PCB_ORDC_VDSTR)) { \
				conv2str(&c, a); \
				sa = c.val.s; \
			} \
			else \
				sa = a.val.s; \
		} \
		else { \
			da = (a.type == PCB_ORDC_VDBL) ? a.val.d : a.val.l; \
			db = (b.type == PCB_ORDC_VDBL) ? b.val.d : b.val.l; \
		} \
	} while(0)


/* Free temporary vals used by BINOP_GET_OPS */
#define BINOP_FREE_OPS  do { val_free(&a); val_free(&b); val_free(&c); } while(0)


/* Convert a and b to double and set dst long val executing code on da and db.
   If any op is string, return error */
#define BINOP_NUMERIC(code) \
	do { \
		BINOP_GET_OPS; \
		if (!binop_str) { code; } \
		else { \
			rnd_message(RND_MSG_ERROR, "order: constraint script error: string in numeric op\n"); \
			dst->type = PCB_ORDC_VERR; \
		} \
		BINOP_FREE_OPS; \
	} while(0)

void pcb_ordc_exec_node(pcb_ordc_ctx_t *ctx, pcb_ordc_val_t *dst, pcb_ordc_node_t *node)
{
	pcb_ordc_node_t *n;
	pcb_ordc_val_t a, b, c;
	const char *sa, *sb;
	double da, db;
	int binop_str, r;

	dst->type = PCB_ORDC_VLNG; dst->val.l = 0;

	switch(node->type) {
		case PCB_ORDC_BLOCK:
			for(n = node->ch_first; n != NULL; n = n->next) {
				pcb_ordc_exec_node(ctx, &a, n);
				if (a.type == PCB_ORDC_VERR)
					dst->type = PCB_ORDC_VERR;
				val_free(&a);
			}
			break;

		case PCB_ORDC_IF:
			pcb_ordc_exec_node(ctx, &a, node->ch_first);
			r = val2bool(a);
			if (r == 1) {
				pcb_ordc_exec_node(ctx, &b, node->ch_first->next);
				val_free(&b);
			}
			else if (r == -1)
				dst->type = PCB_ORDC_VERR;
			val_free(&a);
			break;

		case PCB_ORDC_ERROR:
			if (ctx->error_cb == NULL)
				break;

			pcb_ordc_exec_node(ctx, &a, node->ch_first);
			pcb_ordc_exec_node(ctx, &b, node->ch_first->next);
			ctx->error_cb(ctx, a.val.s, b.val.s, &node->ch_first->ucache); /* no need to convert; grammar ensures static strings */
			val_free(&a);
			val_free(&b);
			break;

		case PCB_ORDC_CINT:
			dst->type = PCB_ORDC_VLNG; dst->val.l = node->val.l;
			break;

		case PCB_ORDC_CFLOAT:
			dst->type = PCB_ORDC_VDBL; dst->val.d = node->val.d;
			break;

		case PCB_ORDC_QSTR:
			dst->type = PCB_ORDC_VCSTR; dst->val.s = node->val.s;
			break;

		case PCB_ORDC_ID:
			dst->type = PCB_ORDC_VCSTR; dst->val.s = node->val.s;
			break;

		case PCB_ORDC_VAR:
			dst->type = PCB_ORDC_VERR; /* assume error and let var_cb override it */

			if (ctx->var_cb == NULL) {
				rnd_message(RND_MSG_ERROR, "order: internal error: no var_cb provided\n");
				break; /* everything evaluates to error */
			}

			ctx->var_cb(ctx, dst, node->val.s, &node->ucache); /* no need to convert; grammar ensures static string */
			if (dst->type == PCB_ORDC_VERR)
				rnd_message(RND_MSG_ERROR, "order: constraint script error: no such variable '%s'\n", node->val.s);
			break;

		case PCB_ORDC_INT:
			pcb_ordc_exec_node(ctx, &a, node->ch_first);
			conv2lng(dst, a);
			val_free(&a);
			break;

		case PCB_ORDC_FLOAT:
			pcb_ordc_exec_node(ctx, &a, node->ch_first);
			conv2dbl(dst, a);
			val_free(&a);
			break;

		case PCB_ORDC_STRING:
			pcb_ordc_exec_node(ctx, &a, node->ch_first);
			conv2str(dst, a);
			val_free(&a);
			break;

		case PCB_ORDC_NEG:
			pcb_ordc_exec_node(ctx, &a, node->ch_first);
			conv2bool(dst, a);
			dst->val.l = !dst->val.l;
			val_free(&a);
			break;

		case PCB_ORDC_EQ:
			BINOP_GET_OPS;
			dst->val.l = binop_str ? (strcmp(sa, sb) == 0) : (da == db);
			BINOP_FREE_OPS;
			break;

		case PCB_ORDC_NEQ:
			BINOP_GET_OPS;
			dst->val.l = binop_str ? (strcmp(sa, sb) != 0) : (da != db);
			BINOP_FREE_OPS;
			break;

		case PCB_ORDC_GE:
			BINOP_NUMERIC(dst->val.l = (da >= db));
			break;

		case PCB_ORDC_LE:
			BINOP_NUMERIC(dst->val.l = (da <= db));
			break;

		case PCB_ORDC_GT:
			BINOP_NUMERIC(dst->val.l = (da > db));
			break;

		case PCB_ORDC_LT:
			BINOP_NUMERIC(dst->val.l = (da < db));
			break;

		case PCB_ORDC_AND:
			BINOP_GET_OPS;
			dst->val.l = binop_str ? ((*sa != '\0') && (*sb != '\0')) : (!!da && !!db);
			BINOP_FREE_OPS;
			break;

		case PCB_ORDC_OR:
			BINOP_GET_OPS;
			dst->val.l = binop_str ? ((*sa != '\0') || (*sb != '\0')) : (!!da || !!db);
			BINOP_FREE_OPS;
			break;

		case PCB_ORDC_NOT:
			pcb_ordc_exec_node(ctx, &a, node->ch_first);
			r = val2bool(a);
			if (r >= 0)
				dst->val.l = !r;
			else
				dst->type = PCB_ORDC_VERR;
			val_free(&a);
			break;

		case PCB_ORDC_ADD:
			dst->type = PCB_ORDC_VDBL;
			BINOP_NUMERIC(dst->val.d = da + db);
			break;

		case PCB_ORDC_SUB:
			dst->type = PCB_ORDC_VDBL;
			BINOP_NUMERIC(dst->val.d = da - db);
			break;

		case PCB_ORDC_MULT:
			dst->type = PCB_ORDC_VDBL;
			BINOP_NUMERIC(dst->val.d = da * db);
			break;

		case PCB_ORDC_DIV:
			dst->type = PCB_ORDC_VDBL;
			BINOP_NUMERIC(dst->val.d = (db == 0) ? 0 : (da / db));
			break;

		case PCB_ORDC_MOD:
			dst->type = PCB_ORDC_VDBL;
			BINOP_NUMERIC(dst->val.d = (db == 0) ? 0 : fmod(da, db));
			break;

		default:
			dst->type = PCB_ORDC_VERR;
			rnd_message(RND_MSG_ERROR, "order: internal error: uknown instruction\n");
			break;
	}
}

int pcb_ordc_exec(pcb_ordc_ctx_t *ctx)
{
	pcb_ordc_val_t res;

	pcb_ordc_exec_node(ctx, &res, ctx->root);

	return val2bool(res);
}