-
Notifications
You must be signed in to change notification settings - Fork 0
/
cu_firstset.c
1040 lines (797 loc) · 25.9 KB
/
cu_firstset.c
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
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**********************************************************************
This file is part of Copper.
Copper is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Copper 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Copper. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************/
/***************************
**
** Project: Copper
**
** Routine List:
** <routine-list-end>
**
**
**
***/
#include "copper.h"
#include "copper_inline.h"
/* */
#include <string.h>
#include <error.h>
#include <stdarg.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
/* */
struct meta_blob {
struct cu_metafirst meta;
struct cu_metaset first;
};
static inline bool set_Contains(CuMetaSet contains, CuMetaSet group) {
unsigned char *cbits = contains->bitfield;
unsigned char *gbits = group->bitfield;
unsigned inx = 0;
for ( ; inx < 32 ; ++inx) {
if (cbits[inx] != gbits[inx]) {
unsigned char check = cbits[inx] & gbits[inx];
if (gbits[inx] != check) {
return false;
}
}
}
return true;
}
static inline const char* node_label(CuNode node) {
static char buffer[10];
if (node->label) return node->label;
sprintf(buffer, "%lx__", (unsigned long) node);
return buffer;
}
static bool meta_StartFirstSets(CuCallback input, CuNode node, CuMetaFirst *target)
{
CuMetaFirst result = 0;
inline bool allocate(bool bits) {
if (!bits) {
unsigned fullsize = sizeof(struct cu_metafirst);
result = malloc(fullsize);
if (!result) return false;
if (target) *target = result;
memset(result, 0, fullsize);
result->done = false;
result->type = pft_fixed;
result->node = node;
node->metadata = result;
return true;
}
unsigned fullsize = sizeof(struct meta_blob);
struct meta_blob* temp = malloc(fullsize);
if (!temp) return false;
result = (CuMetaFirst) temp;
if (target) *target = result;
memset(temp, 0, fullsize);
CuMetaSet set = &temp->first;
result->done = false;
result->node = node;
result->type = pft_fixed;
result->first = set;
node->metadata = result;
return true;
}
inline bool copy_node() {
if (!node->first) return true;
if (!result->first) return false;
const unsigned char *src = node->first->bitfield;
unsigned char *bits = result->first->bitfield;
unsigned inx = 0;
for ( ; inx < 32 ; ++inx) {
bits[inx] |= src[inx];
}
return true;
}
inline void merge(CuMetaFirst from) {
assert(from);
assert(result);
assert(result->first);
if (!from->first) return;
unsigned char *src = from->first->bitfield;
unsigned char *bits = result->first->bitfield;
unsigned inx = 0;
for ( ; inx < 32 ; ++inx) {
bits[inx] |= src[inx];
}
}
//-----------------------------------------
// - %predicate
// - dot
inline bool do_Opaque() {
if (!allocate(false)) return false;
result->done = true;
result->type = pft_opaque;
return true;
}
// @name - an named event
// set state.begin
// set state.begin
// {...} - an unnamed event
inline bool do_Event() {
if (!allocate(false)) return false;
result->done = true;
result->type = pft_event;
return true;
}
// 'chr
// [...]
// "..."
// begin-end
inline bool do_CopyNode() {
if (!allocate(true)) return false;
copy_node();
result->type = node->type;
result->done = true;
return true;
}
// name
inline bool do_MatchName() {
if (!allocate(true)) return false;
const char *name = node->arg.name;
CuNode value;
if (!input->node(input, name, &value)) {
CU_ERROR("node \'%s\' not found\n", name);
return false;
}
CuMetaFirst child;
if (!meta_StartFirstSets(input, value, &child)) return false;
result->type = child->type;
merge(child);
return true;
}
// e !
inline bool do_AssertFalse() {
if (!allocate(false)) return false;
CuMetaFirst child;
if (!meta_StartFirstSets(input, node->arg.node, &child)) return false;
result->done = true;
result->type = inWithout(child->type);
return true;
}
// e &
// e +
inline bool do_AssertChild() {
if (!allocate(true)) return false;
CuMetaFirst child;
if (!meta_StartFirstSets(input, node->arg.node, &child)) {
CU_DEBUG(1, "meta_StartFirstSets checking child failed\n");
return false;
}
// T(f&) = f, T(o&) = o, T(t&) = t, T(e&) = e
// T(f+) = f, T(o+) = o, T(t+) = t, T(e+) = ERROR
result->type = inRequired(child->type);
merge(child);
return true;
}
// e ?
// e *
inline bool do_TestChild() {
if (!allocate(true)) return false;
CuMetaFirst child;
if (!meta_StartFirstSets(input, node->arg.node, &child)) {
CU_DEBUG(1, "meta_StartFirstSets checking child failed\n");
return false;
}
// T(f?) = t, T(o?) = o, T(t?) = t, T(e?) = e
// T(f*) = t, T(o*) = o, T(t*) = t, T(e+) = ERROR
result->type = inOptional(child->type);
merge(child);
return true;
}
// e1 e2 /
inline bool do_Choice() {
if (!allocate(true)) return false;
CuMetaFirst left;
CuMetaFirst right;
if (!meta_StartFirstSets(input, node->arg.pair->left, &left)) {
CU_DEBUG(1, "meta_StartFirstSets checking left failed\n");
return false;
}
if (!meta_StartFirstSets(input, node->arg.pair->right, &right)) {
CU_DEBUG(1, "meta_StartFirstSets checking right failed\n");
return false;
}
result->type = inChoice(left->type, right->type);
merge(left);
merge(right);
return true;
}
// e1 e2
inline bool do_Sequence() {
if (!allocate(true)) return false;
CuMetaFirst left;
CuMetaFirst right;
if (!meta_StartFirstSets(input, node->arg.pair->left, &left)) {
CU_DEBUG(1, "meta_StartFirstSets checking left failed\n");
return false;
}
if (!meta_StartFirstSets(input, node->arg.pair->right, &right)) {
CU_DEBUG(1, "meta_StartFirstSets checking right failed\n");
return false;
}
result->type = inSequence(left->type, right->type);
merge(left);
if (pft_transparent == left->type) {
merge(right);
}
if (pft_event == left->type) {
merge(right);
}
return true;
}
// e1 ; e2
inline bool do_Loop() {
if (!allocate(true)) return false;
CuMetaFirst left;
CuMetaFirst right;
if (!meta_StartFirstSets(input, node->arg.pair->left, &left)) {
CU_DEBUG(1, "meta_StartFirstSets checking left failed\n");
return false;
}
if (!meta_StartFirstSets(input, node->arg.pair->right, &right)) {
CU_DEBUG(1, "meta_StartFirstSets checking right failed\n");
return false;
}
result->type = inSequence(left->type, right->type);
merge(left);
if (pft_transparent == left->type) {
merge(right);
}
if (pft_event == left->type) {
merge(right);
}
return true;
}
if (!input) return false;
if (!node) return false;
if (node->metadata) {
if (target) *target = node->metadata;
return true;
}
switch (node->oper) {
case cu_Apply: return do_Event();
case cu_Argument: return do_Event();
case cu_AssertFalse: return do_AssertFalse();
case cu_AssertTrue: return do_AssertChild();
case cu_Begin: return do_Event();
case cu_Choice: return do_Choice();
case cu_End: return do_Event();
case cu_Loop: return do_Loop();
case cu_MatchChar: return do_CopyNode();
case cu_MatchDot: return do_Opaque();
case cu_MatchName: return do_MatchName();
case cu_MatchRange: return do_CopyNode();
case cu_MatchSet: return do_CopyNode();
case cu_MatchText: return do_CopyNode();
case cu_OneOrMore: return do_AssertChild();
case cu_Predicate: return do_Opaque();
case cu_Sequence: return do_Sequence();
case cu_ZeroOrMore: return do_TestChild();
case cu_ZeroOrOne: return do_TestChild();
case cu_Void:
break;
}
return false;
}
static bool meta_Recheck(CuCallback input, CuNode node, CuMetaFirst *target, bool *changed)
{
struct cu_metaset holding;
CuMetaFirst result = 0;
inline void hold() {
unsigned char *src = result->first->bitfield;
unsigned char *bits = holding.bitfield;
unsigned inx = 0;
for ( ; inx < 32 ; ++inx) {
bits[inx] = src[inx];
}
}
inline bool match() {
unsigned char *src = result->first->bitfield;
unsigned char *bits = holding.bitfield;
unsigned inx = 0;
for ( ; inx < 32 ; ++inx) {
if (bits[inx] != src[inx]) {
*changed = true;
return false;
}
}
return true;
}
inline bool merge(CuMetaFirst from) {
assert(from);
assert(result);
assert(result->first);
if (!from->first) return false;
unsigned char *src = from->first->bitfield;
unsigned char *bits = result->first->bitfield;
unsigned inx = 0;
for ( ; inx < 32 ; ++inx) {
bits[inx] |= src[inx];
}
return match();
}
//------------------------------
// %predicate
// dot
// @name - an named event
// :[name] - an argument name
// set state.begin
// set state.begin
// {...} - an unnamed event
// 'chr
// [...]
// "..."
// begin-end
inline bool do_Nothing() {
if (!result->done) {
CU_DEBUG(1, "meta_Recheck node(%s) not done\n", oper2name(node->oper));
}
return true;
}
// name
inline bool do_MatchName() {
hold();
const char *name = node->arg.name;
CuNode value;
if (!input->node(input, name, &value)) {
CU_ERROR("node %s not found\n", name);
return false;
}
CuMetaFirst child = value->metadata;
result->type = child->type;
merge(child);
result->done = match();
return true;
}
// e !
inline bool do_AssertFalse() {
CuMetaFirst child;
if (!meta_Recheck(input, node->arg.node, &child, changed)) return false;
result->type = inWithout(child->type);
return true;
}
// e &
// e +
inline bool do_AssertChild() {
hold();
CuMetaFirst child;
if (!meta_Recheck(input, node->arg.node, &child, changed)) return false;
// T(f&) = f, T(o&) = o, T(t&) = t, T(e&) = e
// T(f+) = f, T(o+) = o, T(t+) = t, T(e+) = ERROR
result->type = inRequired(child->type);
merge(child);
result->done = match();
return true;
}
// e ?
// e *
inline bool do_TestChild() {
hold();
CuMetaFirst child;
if (!meta_Recheck(input, node->arg.node, &child, changed)) return false;
// T(f?) = t, T(o?) = o, T(t?) = t, T(e?) = e
// T(f*) = t, T(o*) = o, T(t*) = t, T(e+) = ERROR
result->type = inOptional(child->type);
merge(child);
result->done = match();
return true;
}
// e1 e2 /
inline bool do_Choice() {
hold();
CuMetaFirst left;
CuMetaFirst right;
if (!meta_Recheck(input, node->arg.pair->left, &left, changed)) return false;
if (!meta_Recheck(input, node->arg.pair->right, &right, changed)) return false;
result->type = inChoice(left->type, right->type);
merge(left);
merge(right);
result->done = match();
return true;
}
// e1 e2
inline bool do_Sequence() {
hold();
CuMetaFirst left;
CuMetaFirst right;
if (!meta_Recheck(input, node->arg.pair->left, &left, changed)) return false;
if (!meta_Recheck(input, node->arg.pair->right, &right, changed)) return false;
result->type = inSequence(left->type, right->type);
merge(left);
if (pft_transparent == left->type) {
merge(right);
}
if (pft_event == left->type) {
merge(right);
}
result->done = match();
return true;
}
// e1 ; e2
inline bool do_Loop() {
hold();
CuMetaFirst left;
CuMetaFirst right;
if (!meta_Recheck(input, node->arg.pair->left, &left, changed)) return false;
if (!meta_Recheck(input, node->arg.pair->right, &right, changed)) return false;
result->type = inSequence(left->type, right->type);
merge(left);
if (pft_transparent == left->type) {
merge(right);
}
if (pft_event == left->type) {
merge(right);
}
result->done = match();
return true;
}
if (!input) return false;
if (!node) return false;
if (!changed) return false;
result = node->metadata;
if (target) *target = result;
switch (node->oper) {
case cu_Apply: return do_Nothing();
case cu_Argument: return do_Nothing();
case cu_AssertFalse: return do_AssertFalse();
case cu_AssertTrue: return do_AssertChild();
case cu_Begin: return do_Nothing();
case cu_Choice: return do_Choice();
case cu_End: return do_Nothing();
case cu_Loop: return do_Loop();
case cu_MatchChar: return do_Nothing();
case cu_MatchDot: return do_Nothing();
case cu_MatchName: return do_MatchName();
case cu_MatchRange: return do_Nothing();
case cu_MatchSet: return do_Nothing();
case cu_MatchText: return do_Nothing();
case cu_OneOrMore: return do_AssertChild();
case cu_Predicate: return do_Nothing();
case cu_Sequence: return do_Sequence();
case cu_ZeroOrMore: return do_TestChild();
case cu_ZeroOrOne: return do_TestChild();
case cu_Void:
break;
}
return false;
}
static void meta_DebugSets(FILE *output, unsigned level, CuNode node)
{
inline void char_Write(unsigned char value)
{
switch (value) {
case '\a': fprintf(output, "\'%s\'", "\\a"); return; /* bel */
case '\b': fprintf(output, "\'%s\'", "\\b"); return; /* bs */
case '\e': fprintf(output, "\'%s\'", "\\e"); return; /* esc */
case '\f': fprintf(output, "\'%s\'", "\\f"); return; /* ff */
case '\n': fprintf(output, "\'%s\'", "\\n"); return; /* nl */
case '\r': fprintf(output, "\'%s\'", "\\r"); return; /* cr */
case '\t': fprintf(output, "\'%s\'", "\\t"); return; /* ht */
case '\v': fprintf(output, "\'%s\'", "\\v"); return; /* vt */
case '\'': fprintf(output, "\'%s\'", "\\'"); return; /* ' */
case '\"': fprintf(output, "\'%s\'", "\\\""); return; /* " */
case '\\': fprintf(output, "\'%s\'", "\\\\"); return; /* \ */
default: fprintf(output, "\'%c\'", value); return;
}
}
inline void debug_charclass() {
if (!node) return;
CuMetaFirst meta = node->metadata;
if (!meta) return;
if (!meta->first) {
unsigned inx = 0;
for ( ; inx < 32; ++inx) {
fprintf(output, "----");
}
return;
}
unsigned char *bits = meta->first->bitfield;
if (!bits) {
unsigned inx = 0;
for ( ; inx < 32; ++inx) {
fprintf(output, "----");
}
return;
}
unsigned inx = 0;
for ( ; inx < 32; ++inx) {
if (0 == bits[inx]) {
fprintf(output, "\\---");
} else {
fprintf(output, "\\%03o", bits[inx]);
}
}
}
inline void debug_type() {
if (!node) return;
CuMetaFirst meta = node->metadata;
if (!meta) return;
fprintf(output, "%17s ", first2name(meta->type));
}
inline void debug_label(CuNode cnode) {
if (!cnode) {
fprintf(output,"%lx_ ", (unsigned long) cnode);
return;
}
if (node->label) {
fprintf(output, "%s ", cnode->label);
return;
}
fprintf(output,"%lx_ ", (unsigned long) cnode);
}
inline void debug_oper() {
unsigned inx = 0;
for ( ; inx < level; ++inx) {
fprintf(output, " |");
}
fprintf(output, "%s ", oper2name(node->oper));
if (cu_MatchName == node->oper) {
fprintf(output, "%s ", node->arg.name);
}
if (cu_MatchSet == node->oper) {
fprintf(output, "[%s] ", node->arg.set->label);
}
if (cu_Sequence == node->oper) {
debug_label(node->arg.pair->left);
debug_label(node->arg.pair->right);
}
if (cu_Choice == node->oper) {
fprintf(output, "( ");
debug_label(node->arg.pair->left);
debug_label(node->arg.pair->right);
fprintf(output, ") ");
}
}
inline void do_Child() {
meta_DebugSets(output, level + 1, node->arg.node);
}
inline void do_Childern() {
meta_DebugSets(output, level + 1, node->arg.pair->left);
meta_DebugSets(output, level + 1, node->arg.pair->right);
}
if (!node) return;
fprintf(output, "{ ");
debug_charclass();
fprintf(output, " } ");
debug_type();
debug_label(node);
debug_oper();
fprintf(output, "\n");
switch (node->oper) {
case cu_Apply: return;
case cu_Argument: return;
case cu_AssertFalse: do_Child(); return;
case cu_AssertTrue: do_Child(); return;
case cu_Begin: return;
case cu_Choice: do_Childern(); return;
case cu_End: return;
case cu_Loop: do_Childern(); return;
case cu_MatchChar: return;
case cu_MatchDot: return;
case cu_MatchName: return;
case cu_MatchRange: return;
case cu_MatchSet: return;
case cu_MatchText: return;
case cu_OneOrMore: do_Child(); return;
case cu_Predicate: return;
case cu_Sequence: do_Childern(); return;
case cu_ZeroOrMore: do_Child(); return;
case cu_ZeroOrOne: do_Child(); return;
case cu_Void:
break;
}
return;
}
static bool meta_Clear(Copper input, CuNode node)
{
inline bool do_Nothing() {
return true;
}
inline bool do_Child() {
return meta_Clear(input, node->arg.node);
}
inline bool do_Childern() {
if (!meta_Clear(input, node->arg.pair->left)) return false;
if (!meta_Clear(input, node->arg.pair->right)) return false;
return true;
}
if (!node) return true;
CuMetaFirst metadata = node->metadata;
if (metadata) free(metadata);
node->metadata = 0;
switch (node->oper) {
case cu_Apply: return do_Nothing();
case cu_Argument: return do_Nothing();
case cu_AssertFalse: return do_Child();
case cu_AssertTrue: return do_Child();
case cu_Begin: return do_Nothing();
case cu_Choice: return do_Childern();
case cu_End: return do_Nothing();
case cu_Loop: return do_Childern();
case cu_MatchChar: return do_Nothing();
case cu_MatchDot: return do_Nothing();
case cu_MatchName: return do_Nothing();
case cu_MatchRange: return do_Nothing();
case cu_MatchSet: return do_Nothing();
case cu_MatchText: return do_Nothing();
case cu_OneOrMore: return do_Child();
case cu_Predicate: return do_Nothing();
case cu_Sequence: return do_Childern();
case cu_ZeroOrMore: return do_Child();
case cu_ZeroOrOne: return do_Child();
case cu_Void:
break;
}
return false;
}
static bool tree_Clear(CuCallback input, CuTree tree) {
if (!tree) return true;
tree->node = 0;
if (!tree_Clear(input, tree->left)) return false;
return tree_Clear(input, tree->right);
}
static bool tree_Fill(CuCallback input, CuTree tree) {
if (!input) return false;
if (!tree) return true;
const char *name = tree->name;
if (!input->node(input, name, &tree->node)) {
CU_ERROR("node %s not found\n", name);
return false;
}
if (!tree_Clear(input, tree->left)) return false;
return tree_Clear(input, tree->right);
}
static bool tree_StartFirstSets(CuCallback input, CuTree tree) {
if (!input) return false;
if (!tree) return true;
if (!tree->node) return false;
CuNode node = tree->node;
if (!meta_StartFirstSets(input, node, 0)) {
CU_DEBUG(1, "meta_StartFirstSets checking %s failed\n", oper2name(node->oper));
return false;
}
if (!node->metadata) {
CU_DEBUG(1, "meta_StartFirstSets error: not metadata for %s\n", oper2name(node->oper));
return true;
}
if (!tree_StartFirstSets(input, tree->left)) {
CU_DEBUG(1, "tree_StartFirstSets checking left failed\n");
return false;
}
if (!tree_StartFirstSets(input, tree->right)) {
CU_DEBUG(1, "tree_StartFirstSets checking right failed\n");
return false;
}
return true;
}
static bool tree_MergeFirstSets(CuCallback input, CuTree tree, bool *changed) {
if (!input) return false;
if (!tree) return true;
if (!tree->node) return false;
if (!changed) return false;
CuNode node = tree->node;
if (!node->metadata) {
CU_DEBUG(1, "tree_MergeFirstSets error: not metadata for %s\n", oper2name(node->oper));
return false;
}
if (!meta_Recheck(input, node, 0, changed)) {
CU_DEBUG(1, "meta_Recheck checking %s failed\n", oper2name(node->oper));
return false;
}
if (!tree_MergeFirstSets(input, tree->left, changed)) return false;
return tree_MergeFirstSets(input, tree->right, changed);
}
static bool tree_DebugSets(CuCallback input, CuTree tree) {
if (!input) return false;
if (!tree) return true;
if (!tree->node) return false;
if (!tree_DebugSets(input, tree->left)) return false;
fprintf(stdout, "--- %s ---\n", tree->name);
meta_DebugSets(stdout, 0, tree->node);
return tree_DebugSets(input, tree->right);
}
extern bool cu_FillMetadata(CuCallback input, CuTree *map) {
if (!input) {
CU_DEBUG(1, "cu_FillMetadata error: no input\n");
return false;
}
if (!map) {
CU_DEBUG(1, "cu_FillMetadata error: no map\n");
return false;
} else {
CuTree root = *map;
if (!root) {
CU_DEBUG(1, "cu_FillMetadata error: no root\n");
return false;
}
CU_DEBUG(1, "filling metadata %lx\n", (unsigned long) root);
if (!tree_StartFirstSets(input, root)) {
CU_DEBUG(1, "tree_StartFirstSets error\n");
return false;
}
bool changed = true;
for ( ; changed ; ) {
changed = false;
CU_ON_DEBUG(6, { tree_DebugSets(input, root); });
CU_DEBUG(4, "merging metadata %lx\n", (unsigned long) root);
if (!tree_MergeFirstSets(input, root, &changed)) {
CU_DEBUG(1, "tree_MergeFirstSets error\n");
return false;
}
}
CU_ON_DEBUG(7, { tree_DebugSets(input, root); });
CU_DEBUG(7, "filling metadata %lx done\n", (unsigned long) root);
return true;
}
(void) tree_Fill;
(void) tree_StartFirstSets;
(void) tree_MergeFirstSets;
}
extern bool cu_AddNode(CuCallback input,
CuName name,
CuNode node,
CuTree *map)
{
assert(0 != input);
FindNode findnode = input->node;
AttachNode attachnode = input->attach_node;
assert(0 != findnode);
assert(0 != attachnode);
assert(0 != name);
assert(0 != node);
assert(0 != map);
inline bool fetch(CuNode *target) {
if (!findnode(input, name, target)) {
CU_ERROR("node %s not found\n", name);
return false;
}
return true;
}
inline bool allocate(CuTree *target) {
unsigned fullsize = sizeof(struct cu_tree);
CuTree result = malloc(fullsize);
if (!result) return false;
*target = result;