File: query_exec.c

package info (click to toggle)
pcb-rnd 3.1.7b-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,108 kB
  • sloc: ansic: 213,400; yacc: 6,241; sh: 4,698; awk: 3,016; makefile: 2,254; lex: 1,166; python: 519; xml: 261; lisp: 154; tcl: 67; perl: 34; javascript: 6; ruby: 5
file content (900 lines) | stat: -rw-r--r-- 25,239 bytes parent folder | download
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
/*
 *                            COPYRIGHT
 *
 *  pcb-rnd, interactive printed circuit board design
 *  Copyright (C) 2016,2019 Tibor 'Igor2' Palinkas
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *  Contact:
 *    Project page: http://repo.hu/projects/pcb-rnd
 *    lead developer: http://repo.hu/projects/pcb-rnd/contact.html
 *    mailing list: pcb-rnd (at) list.repo.hu (send "subscribe")
 */

/* Query language - execution */

#include <stdio.h>
#include "config.h"
#include "data.h"
#include "query.h"
#include "query_exec.h"
#include "query_access.h"
#include "net_len.h"
#include <librnd/core/rnd_printf.h>

#define PCB dontuse

void pcb_qry_setup(pcb_qry_exec_t *ctx, pcb_board_t *pcb, pcb_qry_node_t *root)
{
	ctx->pcb = pcb;
	ctx->root = root;
	ctx->iter = NULL;
}

void pcb_qry_init(pcb_qry_exec_t *ctx, pcb_board_t *pcb, pcb_qry_node_t *root, int bufno)
{
	memset(ctx, 0, sizeof(pcb_qry_exec_t));
	ctx->all.type = PCBQ_VT_LST;
	if (bufno == -1)
		pcb_qry_list_all_pcb(&ctx->all, PCB_OBJ_ANY & (~PCB_OBJ_LAYER));
	else
		pcb_qry_list_all_data(&ctx->all, pcb_buffers[bufno].Data, PCB_OBJ_ANY & (~PCB_OBJ_LAYER));

	pcb_qry_setup(ctx, pcb, root);
}

void pcb_qry_autofree(pcb_qry_exec_t *ctx)
{
	long l;

	if (ctx->iter != NULL) {
		for(l = 0; l < ctx->iter->num_vars; l++) {
			if (ctx->iter->lst[l].data.lst.array != ctx->all.data.lst.array) /* some lists are just alias to ->all, do not free them */
				pcb_qry_list_free(&ctx->iter->lst[l]);
		}
	}

	for(l = 0; l < ctx->autofree.used; l++)
		free(ctx->autofree.array[l]);
	vtp0_uninit(&ctx->autofree);

	ctx->iter = NULL; /* no need to free the iterator: ctx->root free handled that as it was allocated in one of the nodes */
}

extern void pcb_qry_uninit_layer_setup(pcb_qry_exec_t *ectx);
extern void pcb_qry_uninit_layer_ko_match(pcb_qry_exec_t *ectx);

void pcb_qry_uninit(pcb_qry_exec_t *ctx)
{
	pcb_qry_autofree(ctx);

	pcb_qry_list_free(&ctx->all);

	if (ctx->obj2netterm_inited)
		htpp_uninit(&ctx->obj2netterm);

	if (ctx->obj2lenseg_inited) {
		long n;
		htpp_uninit(&ctx->obj2lenseg);
		htpi_uninit(&ctx->obj2lenseg_cc);
		for(n = 0; n < ctx->obj2lenseg_free.used; n++) {
			pcb_qry_netseg_len_t *ns = ctx->obj2lenseg_free.array[n];
			pcb_qry_lenseg_free_fields(ns);
			free(ns);
		}
		vtp0_uninit(&ctx->obj2lenseg_free);
	}

	vtp0_uninit(&ctx->tmplst);
	pcb_qry_uninit_layer_setup(ctx);
	pcb_qry_uninit_layer_ko_match(ctx);

	ctx->obj2netterm_inited = ctx->obj2lenseg_inited = \
	ctx->layer_setup_inited = ctx->warned_missing_thickness = 0;
}

static void val_free_fields(pcb_qry_val_t *val)
{
	switch(val->type) {
		case PCBQ_VT_LST:
			pcb_qry_list_free(val);
			break;
		case PCBQ_VT_STRING:
			TODO("decide if strings are strdup'd; action return may need to, ther est not; maybe introduce DSTR for dynamic string?");
			break;
		case PCBQ_VT_VOID:
		case PCBQ_VT_OBJ:
		case PCBQ_VT_COORD:
		case PCBQ_VT_LONG:
		case PCBQ_VT_DOUBLE:
			break;
	}
}

void pcb_qry_val_free_fields(pcb_qry_val_t *val)
{
	val_free_fields(val);
}

static int pcb_qry_run_(pcb_qry_exec_t *ec, pcb_qry_node_t *prg, pcb_qry_val_t *res, int it_reset, int eval_list, void (*cb)(void *user_ctx, pcb_qry_val_t *res, pcb_any_obj_t *current), void *user_ctx)
{
	pcb_qry_val_t restmp;
	int errs = 0, keep_res = 1;

	if (it_reset && (pcb_qry_it_reset(ec, prg) != 0))
		return -1;

	if (res == NULL) {
		res = &restmp;
		keep_res = 0;
	}
	res->type = PCBQ_VT_VOID;

	do {
		val_free_fields(res);
		if ((pcb_qry_eval(ec, prg, res, cb, user_ctx) == 0) && (cb != NULL)) {
			if ((eval_list) && (res->type == PCBQ_VT_LST)) {
				long n;

				/* special case: result is a list, need to return each object so 'let' gets it */
				for(n = 0; n < res->data.lst.used; n++)
					cb(user_ctx, res, res->data.lst.array[n]);
			}
			else if (ec->iter->last_obj != NULL) {
				cb(user_ctx, res, ec->iter->last_obj);
			}
		}
		else
			errs++;
	} while(pcb_qry_it_next(ec));
	if (!keep_res)
		val_free_fields(res);
	return errs;
}


typedef struct {
	pcb_qry_exec_t *ctx;
	vtp0_t *vt;
} let_ctx_t;

static void let_cb(void *user_ctx, pcb_qry_val_t *res, pcb_any_obj_t *current)
{
	let_ctx_t *lctx = user_ctx;

	if (res->type == PCBQ_VT_OBJ) /* result overwrites last checked node */
		current = res->data.obj;

	if (pcb_qry_is_true(res))
		vtp0_append(lctx->vt, current);
}

static void pcb_qry_let(pcb_qry_exec_t *ctx, pcb_qry_node_t *node)
{
	let_ctx_t lctx;
	int vi = node->data.children->data.crd;
	pcb_qry_node_t *expr = node->data.children->next;

	lctx.ctx = ctx;
	pcb_qry_it_reset(ctx, node);

	/* set up the list */
	ctx->iter->lst[vi].type = PCBQ_VT_LST;
	lctx.vt = &ctx->iter->lst[vi].data.lst;

	/* evaluate 'let' the expression, filling up the list */
	pcb_qry_it_reset_(lctx.ctx, 0);
	ctx->iter->it_active = node->precomp.it_active;
	pcb_qry_run_(lctx.ctx, expr, NULL, 0, 1,let_cb, &lctx);
	ctx->iter->it_active = NULL;

	/* initialize the iterator */
	ctx->iter->vects[vi] = &ctx->iter->lst[vi].data.lst;
	ctx->iter->idx[vi] = 0;
}

/* Run one node; increment ret by the number of hits and return that, or -1 on error.
   If res is not NULL the value of PCBQ_RETURN is loaded in it.
   Call cb/user_ctx when assert is true. */
static int pcb_qry_run_one(pcb_qry_exec_t *ec, pcb_qry_node_t *prg, int ret, pcb_qry_val_t *res, void (*cb)(void *user_ctx, pcb_qry_val_t *res, pcb_any_obj_t *current), void *user_ctx)
{
	int r;
	pcb_qry_node_t *start;
	int is_ret = 0;

	if (res != NULL)
		res->type = PCBQ_VT_VOID;

	if (prg->type == PCBQ_FUNCTION) {
		if (ec->trace)
			rnd_message(RND_MSG_INFO, "query: entering user function %s\n", prg->data.children->next->data.str);
		start = prg->data.children->next->next;
		while(start->type == PCBQ_ARG)
			start = start->next;
		goto run_all;
	}
	else if (prg->type == PCBQ_RULE) {
		pcb_qry_node_t *n;
		start = prg->data.children->next->next;

		/* execute 'let' statements first */
		run_all:;
		for(n = start; n != NULL; n = n->next) {
			if (n->type == PCBQ_LET)
				pcb_qry_let(ec, n);
			if (n->type == PCBQ_RETURN)
				break;
		}

		for(n = start; n != NULL; n = n->next) {
			if (ec->trace)
				rnd_message(RND_MSG_INFO, "query: %p %s\n", n, pcb_qry_nodetype_name(n->type));
			switch(n->type) {
				case PCBQ_LET: break;
				case PCBQ_RETURN:
					is_ret = 1;
				case PCBQ_ASSERT:
					ec->root = n;
					if (ec->iter != NULL)
						ec->iter->it_active = n->precomp.it_active;
					r = pcb_qry_run_(ec, n->data.children, (is_ret ? res : NULL), 1, 0, (n->type == PCBQ_RETURN ? NULL : cb), user_ctx);
					if (ec->iter != NULL)
						ec->iter->it_active = NULL;
					if (r < 0)
						ret = r;
					else if (ret >= 0)
						ret += r;
					ec->root = NULL;
					break;
				default:;
			}
			if (is_ret)
				break;
		}
	}
	else
		return -1;

	return ret;
}

static int pcb_qry_run_all(pcb_qry_exec_t *ec, pcb_qry_node_t *prg, int ret, void (*cb)(void *user_ctx, pcb_qry_val_t *res, pcb_any_obj_t *current), void *user_ctx)
{
	while(prg != NULL) { /* execute a list of rules */
		if (prg->type != PCBQ_FUNCTION)
			ret = pcb_qry_run_one(ec, prg, ret, NULL, cb, user_ctx);
		prg = prg->next;
	}
	return ret;
}

int pcb_qry_run(pcb_qry_exec_t *ec, pcb_board_t *pcb, pcb_qry_node_t *prg, int bufno, void (*cb)(void *user_ctx, pcb_qry_val_t *res, pcb_any_obj_t *current), void *user_ctx)
{
	int ret = 0, ec_uninit = 0;
	pcb_qry_exec_t ec_local;

	if (ec == NULL) {
		ec = &ec_local;
		ec_uninit = 1;
		pcb_qry_init(ec, pcb, prg, bufno);
	}
	else {
		if (ec->pcb != pcb)
			return -1;
		pcb_qry_setup(ec, pcb, prg);
	}

	if (prg->type == PCBQ_EXPR_PROG)
		ret = pcb_qry_run_(ec, prg, NULL, 1, 0, cb, user_ctx);
	else
		ret = pcb_qry_run_all(ec, prg, ret, cb, user_ctx);

	if (ec_uninit)
		pcb_qry_uninit(&ec_local);
	else
		pcb_qry_autofree(ec);
	return ret;
}

static int qry_exec_user_func(pcb_qry_exec_t *ectx, pcb_qry_node_t *fdef, int argc, pcb_qry_val_t *argv, pcb_qry_val_t *res, void (*cb)(void *user_ctx, pcb_qry_val_t *res, pcb_any_obj_t *current), void *user_ctx)
{
	pcb_qry_exec_t fctx;
	int n, ret, alloced[PCB_QRY_MAX_FUNC_ARGS];
	pcb_qry_node_t *fname;

	if (argc > PCB_QRY_MAX_FUNC_ARGS)
		return -1;

	if (fdef == NULL) {
		rnd_message(RND_MSG_ERROR, "call to unknown/undefined function\n");
		return -1;
	}

	fctx = *ectx;

	assert(fdef->type == PCBQ_FUNCTION);
	fname = fdef->data.children;
	assert(fname->type == PCBQ_ITER_CTX);
	fctx.iter = fname->data.iter_ctx;
	fname = fname->next; /* skip iter */

	pcb_qry_iter_init(fctx.iter);

	/* accept only objects and lists as argument */
	for(n = 0; n < argc; n++) {
		if ((argv[n].type != PCBQ_VT_OBJ) && (argv[n].type != PCBQ_VT_LST))
			return -1;
	}

	for(n = 0; n < argc; n++) {
		if (argv[n].type == PCBQ_VT_OBJ) {
			fctx.iter->lst[n].type = PCBQ_VT_LST;
			vtp0_init(&fctx.iter->lst[n].data.lst);
			vtp0_append(&fctx.iter->lst[n].data.lst, argv[n].data.obj);
			alloced[n] = 1;
		}
		else {
			fctx.iter->lst[n] = argv[n];
			alloced[n] = 0;
		}
		fctx.iter->vects[n] = &fctx.iter->lst[n].data.lst;
		fctx.iter->idx[n] = 0;
	}

	/* reset lists from the previous calls */
	for(; n < fctx.iter->num_vars; n++) {
		vtp0_t *v = fctx.iter->vects[n];
		if (v != NULL)
			v->used = 0;
	}

	ret = pcb_qry_run_one(&fctx, fdef, 0, res, cb, user_ctx);

	for(n = 0; n < argc; n++) {
		if (alloced[n])
			vtp0_uninit(&fctx.iter->lst[n].data.lst);
	}

	return ret;
}

/* load unary operand to o1 */
#define UNOP() \
do { \
	if ((node->data.children == NULL) || (node->data.children->next != NULL)) \
		return -1; \
	if (pcb_qry_eval(ctx, node->data.children, &o1, cb, user_ctx) < 0) \
		return -1; \
} while(0)

/* load 1st binary operand to o1 */
#define BINOPS1() \
do { \
	if ((node->data.children == NULL) || (node->data.children->next == NULL) || (node->data.children->next->next != NULL)) \
		return -1; \
	if (pcb_qry_eval(ctx, node->data.children, &o1, cb, user_ctx) < 0) \
		return -1; \
} while(0)

/* load 2nd binary operand to o2 */
#define BINOPS2() \
do { \
	if (pcb_qry_eval(ctx, node->data.children->next, &o2, cb, user_ctx) < 0) \
		return -1; \
} while(0)

/* load binary operands to o1 and o2 */
#define BINOPS() \
do { \
	BINOPS1(); \
	BINOPS2(); \
} while(0)

static int promote(pcb_qry_val_t *a, pcb_qry_val_t *b)
{
	if ((a->type == PCBQ_VT_VOID) || (b->type == PCBQ_VT_VOID)) {
		a->type = b->type = PCBQ_VT_VOID;
		return 0;
	}
	if (a->type == b->type)
		return 0;
	switch(a->type) {
		case PCBQ_VT_OBJ:   return -1;
		case PCBQ_VT_LST:   return -1;
		case PCBQ_VT_COORD:
			if (b->type == PCBQ_VT_DOUBLE)  PCB_QRY_RET_DBL(a, a->data.crd);
			if (b->type == PCBQ_VT_LONG)    PCB_QRY_RET_COORD(b, b->data.lng);
			return -1;
		case PCBQ_VT_LONG:
			if (b->type == PCBQ_VT_DOUBLE)  PCB_QRY_RET_DBL(a, a->data.lng);
			if (b->type == PCBQ_VT_COORD)   PCB_QRY_RET_COORD(a, a->data.lng);
			return -1;
		case PCBQ_VT_DOUBLE:
			if (b->type == PCBQ_VT_COORD)  PCB_QRY_RET_DBL(b, b->data.crd);
			if (b->type == PCBQ_VT_LONG)   PCB_QRY_RET_DBL(b, b->data.lng);
			return -1;
		case PCBQ_VT_STRING:
		case PCBQ_VT_VOID:
			return -1;
	}
	return -1;
}

static const char *op2str(char *buff, int buff_size, pcb_qry_val_t *val)
{
	switch(val->type) {
		case PCBQ_VT_COORD:
			rnd_snprintf(buff, buff_size, "%mI", val->data.crd);
			return buff;
		case PCBQ_VT_LONG:
			rnd_snprintf(buff, buff_size, "%ld", val->data.lng);
			return buff;
		case PCBQ_VT_DOUBLE:
			rnd_snprintf(buff, buff_size, "%f", val->data.crd);
			return buff;
		case PCBQ_VT_STRING:
			return val->data.str;
		case PCBQ_VT_VOID:
		case PCBQ_VT_OBJ:
		case PCBQ_VT_LST:   return NULL;
	}
	return NULL;
}

int pcb_qry_is_true(pcb_qry_val_t *val)
{
	switch(val->type) {
		case PCBQ_VT_VOID:     return 0;
		case PCBQ_VT_OBJ:      return val->data.obj->type != PCB_OBJ_VOID;
		case PCBQ_VT_LST:      return vtp0_len(&val->data.lst) > 0;
		case PCBQ_VT_COORD:    return val->data.crd;
		case PCBQ_VT_LONG:     return val->data.lng;
		case PCBQ_VT_DOUBLE:   return val->data.dbl;
		case PCBQ_VT_STRING:   return (val->data.str != NULL) && (*val->data.str != '\0');
	}
	return 0;
}

static void setup_iter_list(pcb_qry_exec_t *ctx, int var_id, pcb_qry_val_t *listval)
{
	ctx->iter->lst[var_id] = *listval;
	assert(listval->type == PCBQ_VT_LST);

	ctx->iter->vects[var_id] = &listval->data.lst;
	ctx->iter->idx[var_id] = 0;
}

static int pcb_qry_it_active(pcb_query_iter_t *iter, int i)
{
	if (iter->it_active == NULL) return 1; /* no activity map: all iterators are active */
	if (i >= iter->it_active->used) return 0;
	return iter->it_active->array[i];
}

int pcb_qry_it_reset_(pcb_qry_exec_t *ctx, int persistent)
{
	int n;

	if (!persistent)
		pcb_qry_iter_init(ctx->iter);

	for(n = 0; n < ctx->iter->num_vars; n++) {
		if (strcmp(ctx->iter->vn[n], "@") == 0) {
			setup_iter_list(ctx, n, &ctx->all);
			ctx->iter->last_obj = NULL;
			break;
		}
	}

	ctx->iter->last_active = -1;
	for(n = 0; n < ctx->iter->num_vars; n++)
		if ((ctx->iter->vects[n] != NULL) && pcb_qry_it_active(ctx->iter, n))
			ctx->iter->last_active = n;

	return 0;
}

int pcb_qry_it_reset(pcb_qry_exec_t *ctx, pcb_qry_node_t *node)
{
	ctx->iter = pcb_qry_find_iter(node);
	if (ctx->iter == NULL)
		return -1;

	return pcb_qry_it_reset_(ctx, 0);
}


#define PROGRESS_CB(ctx, at, total, cancel) \
do { \
	time_t now; \
	cancel = 0; \
	if (ctx->progress_cb == NULL) break; \
	now = time(NULL); \
	if (ctx->last_prog_cb == 0) \
		ctx->last_prog_cb = now; \
	else if (now > ctx->last_prog_cb) { \
		ctx->last_prog_cb = now; \
		cancel = ctx->progress_cb(ctx, at, total); \
	} \
} while(0)

int pcb_qry_it_next(pcb_qry_exec_t *ctx)
{
	int i;
	for(i = 0; i < ctx->iter->num_vars; i++) {
		if ((ctx->iter->vects[i] != NULL) && pcb_qry_it_active(ctx->iter, i)) {
			long ilen = vtp0_len(ctx->iter->vects[i]);
			long at = ++ctx->iter->idx[i];
			if (at < ilen) {
				if (i == ctx->iter->last_active) {
					int cancel;
					PROGRESS_CB(ctx, at, ilen, cancel);
					if (cancel)
						return 0;
				}
				return 1;
			}
		}
		ctx->iter->idx[i] = 0;
	}
	return 0;
}


/* load s1 and s2 from o1 and o2, convert empty string to NULL */
#define load_strings_null() \
do { \
	s1 = o1.data.str; \
	s2 = o2.data.str; \
	if ((s1 != NULL) && (*s1 == '\0')) s1 = NULL; \
	if ((s2 != NULL) && (*s2 == '\0')) s2 = NULL; \
} while(0)

static char *pcb_qry_empty = "";

/* load s1 and s2 from o1 and o2, convert NULL to empty string */
#define load_strings_empty() \
do { \
	s1 = o1.data.str; \
	s2 = o2.data.str; \
	if (s1 == NULL) s1 = pcb_qry_empty; \
	if (s2 == NULL) s2 = pcb_qry_empty; \
} while(0)

int pcb_qry_eval(pcb_qry_exec_t *ctx, pcb_qry_node_t *node, pcb_qry_val_t *res, void (*cb)(void *user_ctx, pcb_qry_val_t *res, pcb_any_obj_t *current), void *user_ctx)
{
	pcb_qry_val_t o1, o2;
	pcb_any_obj_t **tmp;
	const char *s1, *s2;

	switch(node->type) {
		case PCBQ_EXPR:
			return pcb_qry_eval(ctx, node->data.children, res, cb, user_ctx);

		case PCBQ_EXPR_PROG: {
			pcb_qry_node_t *itn, *exprn;
			itn = node->data.children;
			if (itn == NULL)
				return -1;
			exprn = itn->next;
			if (exprn == NULL)
				return -1;
			if (itn->type != PCBQ_ITER_CTX)
				return -1;
			if (ctx->iter != itn->data.iter_ctx)
				return -1;
			return pcb_qry_eval(ctx, exprn, res, cb, user_ctx);
		}


		case PCBQ_OP_AND: /* lazy */
			BINOPS1();
			if (!pcb_qry_is_true(&o1))
				PCB_QRY_RET_INT(res, 0);
			BINOPS2();
			if (!pcb_qry_is_true(&o2))
				PCB_QRY_RET_INT(res, 0);
			PCB_QRY_RET_INT(res, 1);

		case PCBQ_OP_THUS: /* lazy */
			BINOPS1();
			if (!pcb_qry_is_true(&o1))
				PCB_QRY_RET_INV(res);
			BINOPS2();
			*res = o2;
			return 0;

		case PCBQ_OP_OR: /* lazy */
			BINOPS1();
			if (pcb_qry_is_true(&o1))
				PCB_QRY_RET_INT(res, 1);
			BINOPS2();
			if (pcb_qry_is_true(&o2))
				PCB_QRY_RET_INT(res, 1);
			PCB_QRY_RET_INT(res, 0);

		case PCBQ_OP_EQ:
			BINOPS();
			if (promote(&o1, &o2) != 0)
				return -1;
			switch(o1.type) {
				case PCBQ_VT_VOID:     PCB_QRY_RET_INV(res);
				case PCBQ_VT_OBJ:      PCB_QRY_RET_INT(res, ((o1.data.obj) == (o2.data.obj)));
				case PCBQ_VT_LST:      PCB_QRY_RET_INT(res, pcb_qry_list_cmp(&o1, &o2));
				case PCBQ_VT_COORD:    PCB_QRY_RET_INT(res, o1.data.crd == o2.data.crd);
				case PCBQ_VT_LONG:     PCB_QRY_RET_INT(res, o1.data.lng == o2.data.lng);
				case PCBQ_VT_DOUBLE:   PCB_QRY_RET_INT(res, o1.data.dbl == o2.data.dbl);
				case PCBQ_VT_STRING:
					load_strings_null();
					if (s1 == s2)
						PCB_QRY_RET_INT(res, 1);
					if ((s1 == NULL) || (s2 == NULL))
						PCB_QRY_RET_INT(res, 0);
					PCB_QRY_RET_INT(res, strcmp(s1, s2) == 0);
			}
			return -1;

		case PCBQ_OP_NEQ:
			BINOPS();
			if (promote(&o1, &o2) != 0)
				return -1;
			switch(o1.type) {
				case PCBQ_VT_VOID:     PCB_QRY_RET_INV(res);
				case PCBQ_VT_OBJ:      PCB_QRY_RET_INT(res, ((o1.data.obj) != (o2.data.obj)));
				case PCBQ_VT_LST:      PCB_QRY_RET_INT(res, !pcb_qry_list_cmp(&o1, &o2));
				case PCBQ_VT_COORD:    PCB_QRY_RET_INT(res, o1.data.crd != o2.data.crd);
				case PCBQ_VT_LONG:     PCB_QRY_RET_INT(res, o1.data.lng != o2.data.lng);
				case PCBQ_VT_DOUBLE:   PCB_QRY_RET_INT(res, o1.data.dbl != o2.data.dbl);
				case PCBQ_VT_STRING:
					load_strings_null();
					if (s1 == s2)
						PCB_QRY_RET_INT(res, 0);
					if ((s1 == NULL) || (s2 == NULL))
						PCB_QRY_RET_INT(res, 1);
					PCB_QRY_RET_INT(res, strcmp(s1, s2) != 0);
			}
			return -1;

		case PCBQ_OP_GTEQ:
			BINOPS();
			if (promote(&o1, &o2) != 0)
				return -1;
			switch(o1.type) {
				case PCBQ_VT_VOID:     PCB_QRY_RET_INV(res);
				case PCBQ_VT_COORD:    PCB_QRY_RET_INT(res, o1.data.crd >= o2.data.crd);
				case PCBQ_VT_LONG:     PCB_QRY_RET_INT(res, o1.data.lng >= o2.data.lng);
				case PCBQ_VT_DOUBLE:   PCB_QRY_RET_INT(res, o1.data.dbl >= o2.data.dbl);
				case PCBQ_VT_STRING:
					load_strings_empty();
					PCB_QRY_RET_INT(res, strcmp(s1, s2) >= 0);
				default:               return -1;
			}
			return -1;

		case PCBQ_OP_LTEQ:
			BINOPS();
			if (promote(&o1, &o2) != 0)
				return -1;
			switch(o1.type) {
				case PCBQ_VT_VOID:     PCB_QRY_RET_INV(res);
				case PCBQ_VT_COORD:    PCB_QRY_RET_INT(res, o1.data.crd <= o2.data.crd);
				case PCBQ_VT_LONG:     PCB_QRY_RET_INT(res, o1.data.lng <= o2.data.lng);
				case PCBQ_VT_DOUBLE:   PCB_QRY_RET_INT(res, o1.data.dbl <= o2.data.dbl);
				case PCBQ_VT_STRING:
					load_strings_empty();
					PCB_QRY_RET_INT(res, strcmp(s1, s2) <= 0);
				default:               return -1;
			}
			return -1;

		case PCBQ_OP_GT:
			BINOPS();
			if (promote(&o1, &o2) != 0)
				return -1;
			switch(o1.type) {
				case PCBQ_VT_VOID:     PCB_QRY_RET_INV(res);
				case PCBQ_VT_COORD:    PCB_QRY_RET_INT(res, o1.data.crd > o2.data.crd);
				case PCBQ_VT_LONG:     PCB_QRY_RET_INT(res, o1.data.lng > o2.data.lng);
				case PCBQ_VT_DOUBLE:   PCB_QRY_RET_INT(res, o1.data.dbl > o2.data.dbl);
				case PCBQ_VT_STRING:
					load_strings_empty();
					PCB_QRY_RET_INT(res, strcmp(s1, s2) > 0);
				default:               return -1;
			}
			return -1;
		case PCBQ_OP_LT:
			BINOPS();
			if (promote(&o1, &o2) != 0)
				return -1;
			switch(o1.type) {
				case PCBQ_VT_VOID:     PCB_QRY_RET_INV(res);
				case PCBQ_VT_COORD:    PCB_QRY_RET_INT(res, o1.data.crd < o2.data.crd);
				case PCBQ_VT_LONG:     PCB_QRY_RET_INT(res, o1.data.lng < o2.data.lng);
				case PCBQ_VT_DOUBLE:   PCB_QRY_RET_INT(res, o1.data.dbl < o2.data.dbl);
				case PCBQ_VT_STRING:
					load_strings_empty();
					PCB_QRY_RET_INT(res, strcmp(s1, s2) < 0);
				default:               return -1;
			}
			return -1;

		case PCBQ_OP_ADD:
			BINOPS();
			if (promote(&o1, &o2) != 0)
				return -1;
			switch(o1.type) {
				case PCBQ_VT_VOID:     PCB_QRY_RET_INV(res);
				case PCBQ_VT_COORD:    PCB_QRY_RET_INT(res, o1.data.crd + o2.data.crd);
				case PCBQ_VT_LONG:     PCB_QRY_RET_INT(res, o1.data.lng + o2.data.lng);
				case PCBQ_VT_DOUBLE:   PCB_QRY_RET_DBL(res, o1.data.dbl + o2.data.dbl);
				default:               return -1;
			}
			return -1;

		case PCBQ_OP_SUB:
			BINOPS();
			if (promote(&o1, &o2) != 0)
				return -1;
			switch(o1.type) {
				case PCBQ_VT_VOID:     PCB_QRY_RET_INV(res);
				case PCBQ_VT_COORD:    PCB_QRY_RET_INT(res, o1.data.crd - o2.data.crd);
				case PCBQ_VT_LONG:     PCB_QRY_RET_INT(res, o1.data.lng - o2.data.lng);
				case PCBQ_VT_DOUBLE:   PCB_QRY_RET_DBL(res, o1.data.dbl - o2.data.dbl);
				default:               return -1;
			}
			return -1;

		case PCBQ_OP_MUL:
			BINOPS();
			if (promote(&o1, &o2) != 0)
				return -1;
			switch(o1.type) {
				case PCBQ_VT_VOID:     PCB_QRY_RET_INV(res);
				case PCBQ_VT_COORD:    PCB_QRY_RET_INT(res, o1.data.crd * o2.data.crd);
				case PCBQ_VT_LONG:     PCB_QRY_RET_INT(res, o1.data.lng * o2.data.lng);
				case PCBQ_VT_DOUBLE:   PCB_QRY_RET_DBL(res, o1.data.dbl * o2.data.dbl);
				default:               return -1;
			}
			return -1;

		case PCBQ_OP_DIV:
			BINOPS();
			if (promote(&o1, &o2) != 0)
				return -1;
			switch(o1.type) {
				case PCBQ_VT_VOID:     PCB_QRY_RET_INV(res);
				case PCBQ_VT_COORD:    PCB_QRY_RET_INT(res, o1.data.crd / o2.data.crd);
				case PCBQ_VT_LONG:     PCB_QRY_RET_INT(res, o1.data.lng / o2.data.lng);
				case PCBQ_VT_DOUBLE:   PCB_QRY_RET_DBL(res, o1.data.dbl / o2.data.dbl);
				default:               return -1;
			}
			return -1;

		case PCBQ_OP_MATCH:
			BINOPS1();
			{
				pcb_qry_node_t *o2n =node->data.children->next;
				char buff[128];
				const char *s;
				if (o2n->type != PCBQ_DATA_REGEX)
					return -1;
				s = op2str(buff, sizeof(buff), &o1);
				if (s != NULL)
					PCB_QRY_RET_INT(res, re_se_exec(o2n->precomp.regex, s));
			}
			PCB_QRY_RET_INV(res);

		case PCBQ_OP_NOT:
			UNOP();
			PCB_QRY_RET_INT(res, !pcb_qry_is_true(&o1));

		case PCBQ_FIELD_OF:
			if ((node->data.children == NULL) || (node->data.children->next == NULL))
				return -1;
			if (pcb_qry_eval(ctx, node->data.children, &o1, cb, user_ctx) < 0)
				return -1;
			return pcb_qry_obj_field(ctx, &o1, node->data.children->next, res);

		case PCBQ_LET:
			/* no-op: present only in rules and are executed before any assert expression */
			return 0;

		case PCBQ_ASSERT:
			/* no-op: present only in rules and are executed manually */
			return 0;

		case PCBQ_VAR:
			assert((node->data.crd >= 0) && (node->data.crd < ctx->iter->num_vars));
			res->source = NULL;
			res->type = PCBQ_VT_VOID;
			if (ctx->iter->vects[node->data.crd] == NULL)
				return -1;
			tmp = (pcb_any_obj_t **)vtp0_get(ctx->iter->vects[node->data.crd], ctx->iter->idx[node->data.crd], 0);
			if ((tmp == NULL) || (*tmp == NULL))
				return -1;
			res->type = PCBQ_VT_OBJ;
			res->data.obj = ctx->iter->last_obj = *tmp;
			return 0;

		case PCBQ_LISTVAR: {
			int vi = pcb_qry_iter_var(ctx->iter, node->data.str, 0);
			if ((vi < 0) && (strcmp(node->data.str, "@") == 0)) {
				res->source = NULL;
				res->type = PCBQ_VT_LST;
				res->data.lst = ctx->all.data.lst;
				return 0;
			}
			if (vi >= 0) {
				res->source = NULL;
				res->type = PCBQ_VT_LST;
				res->data.lst = ctx->iter->lst[vi].data.lst;
				return 0;
			}
		}
		return -1;

		case PCBQ_FNAME:
			return -1; /* shall not eval such a node */

		case PCBQ_FCALL: {
			pcb_qry_val_t args[PCB_QRY_MAX_FUNC_ARGS];
			int n;
			pcb_qry_node_t *farg, *fname = node->data.children;
			if (fname == NULL)
				return -1;
			farg = fname->next;
			if (fname->type !=  PCBQ_FNAME)
				return -1;
			memset(args, 0, sizeof(args));
			for(n = 0; (n < PCB_QRY_MAX_FUNC_ARGS) && (farg != NULL); n++, farg = farg->next)
				if (pcb_qry_eval(ctx, farg, &args[n], cb, user_ctx) < 0)
					return -1;

			if (farg != NULL) {
				rnd_message(RND_MSG_ERROR, "too many function arguments\n");
				return -1;
			}
			if (fname->precomp.fnc.bui != NULL)
				return fname->precomp.fnc.bui(ctx, n, args, res);
			else
				return qry_exec_user_func(ctx, fname->precomp.fnc.uf, n, args, res, cb, user_ctx);
		}

		case PCBQ_DATA_COORD:       PCB_QRY_RET_INT_SRC(res, node->data.crd, node);
		case PCBQ_DATA_DOUBLE:      PCB_QRY_RET_DBL_SRC(res, node->data.dbl, node);
		case PCBQ_DATA_STRING:      PCB_QRY_RET_STR_SRC(res, node->data.str, node);
		case PCBQ_DATA_CONST:       PCB_QRY_RET_INT_SRC(res, node->precomp.cnst, node);
		case PCBQ_DATA_OBJ:         PCB_QRY_RET_OBJ_SRC(res, node->precomp.obj, node);
		case PCBQ_DATA_INVALID:     PCB_QRY_RET_INV_SRC(res, node);

		/* not yet implemented: */
		case PCBQ_RULE:

		/* must not meet these while executing a node */
		case PCBQ_FLAG:
		case PCBQ_DATA_REGEX:
		case PCBQ_nodetype_max:
		case PCBQ_FIELD:
		case PCBQ_RNAME:
		case PCBQ_DATA_LYTC:
		case PCBQ_ITER_CTX:
		case PCBQ_FUNCTION:
		case PCBQ_RETURN:
		case PCBQ_ARG:
			return -1;
	}
	return -1;
}