-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreference_validator.py
More file actions
1547 lines (1321 loc) · 56.9 KB
/
reference_validator.py
File metadata and controls
1547 lines (1321 loc) · 56.9 KB
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
from __future__ import annotations
from copy import deepcopy
from typing import Any, Dict, List, Optional, Tuple
from Implementations.Reference.common import (
DEMO_ARTIFACT_VERSION,
LoadedSource,
ValidationResult,
)
SUPPORTED_PRIMITIVES = {
"frog.core.add",
"frog.core.delay",
"frog.ui.property_write",
}
SUPPORTED_WIDGET_CLASSES = {
"frog.ui.standard.numeric_control",
"frog.ui.standard.numeric_indicator",
}
SUPPORTED_WIDGET_ROLES = {
"control",
"indicator",
}
SUPPORTED_NUMERIC_TYPES = {
"u16",
"i32",
"f64",
}
SUPPORTED_CONSTANT_TYPES = {
"u16",
"i32",
"i64",
"f64",
"frog.ui.color",
}
SUPPORTED_COLOR_TYPE = "frog.ui.color"
SUPPORTED_FACE_TEMPLATE_FORMATS = {
"svg",
}
def _fail(status: str, source_ref: Dict[str, Any], diagnostics: List[Dict[str, Any]]) -> ValidationResult:
artifact = {
"artifact_kind": "frog_validation_result",
"artifact_version": DEMO_ARTIFACT_VERSION,
"status": status,
"source_ref": source_ref,
"diagnostics": diagnostics,
}
return ValidationResult(artifact=artifact)
def _is_object(value: Any) -> bool:
return isinstance(value, dict)
def _is_array(value: Any) -> bool:
return isinstance(value, list)
def _diag(code: str, message: str, *, location: Optional[str] = None, severity: str = "error") -> Dict[str, Any]:
diag = {
"code": code,
"severity": severity,
"message": message,
}
if location is not None:
diag["location"] = location
return diag
def _require_top_level_sections(doc: Dict[str, Any], source_ref: Dict[str, Any]) -> Optional[ValidationResult]:
required = ["spec_version", "metadata", "interface", "diagram"]
missing = [name for name in required if name not in doc]
if missing:
return _fail(
"structural_invalid",
source_ref,
[_diag("missing_top_level_sections", f"Missing top-level sections: {', '.join(missing)}.")],
)
return None
def _validate_interface(
interface: Dict[str, Any],
source_ref: Dict[str, Any],
) -> Tuple[Optional[ValidationResult], Dict[str, str], Dict[str, str]]:
if not _is_object(interface):
return (
_fail(
"structural_invalid",
source_ref,
[_diag("invalid_interface_shape", "Top-level 'interface' must be an object.")],
),
{},
{},
)
inputs = interface.get("inputs", [])
outputs = interface.get("outputs", [])
if not _is_array(inputs) or not _is_array(outputs):
return (
_fail(
"structural_invalid",
source_ref,
[_diag("invalid_interface_ports_shape", "'interface.inputs' and 'interface.outputs' must be arrays.")],
),
{},
{},
)
input_types: Dict[str, str] = {}
output_types: Dict[str, str] = {}
diagnostics: List[Dict[str, Any]] = []
for item in inputs:
if not _is_object(item) or "id" not in item or "type" not in item:
diagnostics.append(_diag("invalid_input_port", "Each interface input must contain 'id' and 'type'."))
continue
input_types[item["id"]] = item["type"]
for item in outputs:
if not _is_object(item) or "id" not in item or "type" not in item:
diagnostics.append(_diag("invalid_output_port", "Each interface output must contain 'id' and 'type'."))
continue
output_types[item["id"]] = item["type"]
if diagnostics:
return _fail("structural_invalid", source_ref, diagnostics), {}, {}
return None, input_types, output_types
def _validate_face_template(
value: Any,
*,
widget_id: str,
diagnostics: List[Dict[str, Any]],
) -> None:
if not _is_object(value):
diagnostics.append(
_diag(
"invalid_face_template_shape",
"Widget 'face_template' must be an object.",
location=f"front_panel.widget:{widget_id}.props.face_template",
)
)
return
kind = value.get("kind")
fmt = value.get("format")
path = value.get("path")
if kind != "resource":
diagnostics.append(
_diag(
"invalid_face_template_kind",
"Widget 'face_template.kind' must equal 'resource' in the reference subset.",
location=f"front_panel.widget:{widget_id}.props.face_template.kind",
)
)
if fmt not in SUPPORTED_FACE_TEMPLATE_FORMATS:
diagnostics.append(
_diag(
"unsupported_face_template_format",
f"Unsupported face_template format in reference subset: {fmt}.",
location=f"front_panel.widget:{widget_id}.props.face_template.format",
)
)
if not isinstance(path, str) or not path:
diagnostics.append(
_diag(
"invalid_face_template_path",
"Widget 'face_template.path' must be a non-empty string.",
location=f"front_panel.widget:{widget_id}.props.face_template.path",
)
)
def _validate_front_panel(
front_panel: Dict[str, Any],
source_ref: Dict[str, Any],
) -> Tuple[Optional[ValidationResult], Dict[str, Dict[str, Any]]]:
if not _is_object(front_panel):
return (
_fail(
"structural_invalid",
source_ref,
[_diag("invalid_front_panel_shape", "Top-level 'front_panel' must be an object when present.")],
),
{},
)
widgets = front_panel.get("widgets", [])
if not _is_array(widgets):
return (
_fail(
"structural_invalid",
source_ref,
[_diag("invalid_front_panel_widgets_shape", "'front_panel.widgets' must be an array when present.")],
),
{},
)
widget_by_id: Dict[str, Dict[str, Any]] = {}
diagnostics: List[Dict[str, Any]] = []
for widget in widgets:
if not _is_object(widget):
diagnostics.append(_diag("invalid_widget_shape", "Each front-panel widget must be an object."))
continue
widget_id = widget.get("id")
widget_class = widget.get("widget")
widget_role = widget.get("role")
value_type = widget.get("value_type")
props = widget.get("props", {})
if not widget_id or not widget_class or not widget_role:
diagnostics.append(_diag("missing_widget_fields", "Each widget must define 'id', 'widget', and 'role'."))
continue
if widget_id in widget_by_id:
diagnostics.append(_diag("duplicate_widget_id", f"Duplicate widget id '{widget_id}'."))
continue
if widget_class not in SUPPORTED_WIDGET_CLASSES:
diagnostics.append(
_diag(
"unsupported_widget_class",
f"Unsupported widget class in reference subset: {widget_class}.",
location=f"front_panel.widget:{widget_id}",
)
)
if widget_role not in SUPPORTED_WIDGET_ROLES:
diagnostics.append(
_diag(
"unsupported_widget_role",
f"Unsupported widget role in reference subset: {widget_role}.",
location=f"front_panel.widget:{widget_id}",
)
)
if value_type != "u16":
diagnostics.append(
_diag(
"unsupported_widget_value_type",
f"Unsupported widget value_type in reference subset: {value_type}. Expected 'u16'.",
location=f"front_panel.widget:{widget_id}",
)
)
if not _is_object(props):
diagnostics.append(
_diag(
"invalid_widget_props_shape",
"Widget 'props' must be an object when present.",
location=f"front_panel.widget:{widget_id}.props",
)
)
props = {}
if "face_color" in props and not isinstance(props["face_color"], str):
diagnostics.append(
_diag(
"invalid_face_color_shape",
"Widget 'face_color' must be a string in canonical source.",
location=f"front_panel.widget:{widget_id}.props.face_color",
)
)
if "face_template" in props:
_validate_face_template(props["face_template"], widget_id=widget_id, diagnostics=diagnostics)
widget_by_id[widget_id] = deepcopy(widget)
if diagnostics:
return _fail("unsupported_but_valid", source_ref, diagnostics), {}
return None, widget_by_id
def _collect_diagram_node_map(
diagram: Dict[str, Any],
source_ref: Dict[str, Any],
) -> Tuple[Optional[ValidationResult], Dict[str, Dict[str, Any]], List[Dict[str, Any]]]:
if not _is_object(diagram):
return (
_fail(
"structural_invalid",
source_ref,
[_diag("invalid_diagram_shape", "Top-level 'diagram' must be an object.")],
),
{},
[],
)
nodes = diagram.get("nodes", [])
edges = diagram.get("edges", [])
if not _is_array(nodes) or not _is_array(edges):
return (
_fail(
"structural_invalid",
source_ref,
[_diag("invalid_diagram_collections", "'diagram.nodes' and 'diagram.edges' must be arrays.")],
),
{},
[],
)
node_map: Dict[str, Dict[str, Any]] = {}
diagnostics: List[Dict[str, Any]] = []
for node in nodes:
if not _is_object(node) or "id" not in node or "kind" not in node:
diagnostics.append(_diag("invalid_node_shape", "Each diagram node must define 'id' and 'kind'."))
continue
node_id = node["id"]
if node_id in node_map:
diagnostics.append(_diag("duplicate_node_id", f"Duplicate diagram node id '{node_id}'."))
continue
node_map[node_id] = deepcopy(node)
if diagnostics:
return _fail("structural_invalid", source_ref, diagnostics), {}, []
return None, node_map, deepcopy(edges)
def _build_incoming_edge_map(
edges: List[Dict[str, Any]],
source_ref: Dict[str, Any],
) -> Tuple[Optional[ValidationResult], Dict[Tuple[str, str], List[Dict[str, Any]]]]:
incoming: Dict[Tuple[str, str], List[Dict[str, Any]]] = {}
diagnostics: List[Dict[str, Any]] = []
for edge in edges:
if not _is_object(edge):
diagnostics.append(_diag("invalid_edge_shape", "Each diagram edge must be an object."))
continue
edge_id = edge.get("id")
edge_from = edge.get("from")
edge_to = edge.get("to")
if not edge_id or not _is_object(edge_from) or not _is_object(edge_to):
diagnostics.append(_diag("invalid_edge_fields", "Each edge must define 'id', 'from', and 'to'."))
continue
from_node = edge_from.get("node")
from_port = edge_from.get("port")
to_node = edge_to.get("node")
to_port = edge_to.get("port")
if not from_node or not from_port or not to_node or not to_port:
diagnostics.append(_diag("invalid_edge_endpoints", "Each edge endpoint must define 'node' and 'port'."))
continue
key = (to_node, to_port)
incoming.setdefault(key, []).append(deepcopy(edge))
if diagnostics:
return _fail("structural_invalid", source_ref, diagnostics), {}
return None, incoming
def _find_constant_input_value(
*,
target_node_id: str,
target_port: str,
incoming: Dict[Tuple[str, str], List[Dict[str, Any]]],
node_map: Dict[str, Dict[str, Any]],
) -> Optional[Any]:
candidates = incoming.get((target_node_id, target_port), [])
if len(candidates) != 1:
return None
edge = candidates[0]
source_node = node_map.get(edge["from"]["node"])
if source_node is None or source_node.get("kind") != "constant":
return None
return source_node.get("value")
def _resolve_source_port_type(
*,
node: Dict[str, Any],
port: str,
input_types: Dict[str, str],
output_types: Dict[str, str],
widget_by_id: Dict[str, Dict[str, Any]],
structure_output_types: Dict[Tuple[str, str], str],
) -> Optional[str]:
kind = node["kind"]
if kind == "constant":
return node.get("type")
if kind == "interface_input" and port == "value":
return input_types.get(node["interface_port"])
if kind == "interface_output" and port == "value":
return output_types.get(node["interface_port"])
if kind == "widget_value" and port == "value":
widget = widget_by_id.get(node["widget"])
return None if widget is None else widget.get("value_type")
if kind == "widget_reference" and port == "ref":
return "widget_reference"
if kind == "primitive":
primitive_ref = node["type"]
if primitive_ref == "frog.core.add" and port in {"a", "b", "result"}:
return node.get("_resolved_value_type")
if primitive_ref == "frog.core.delay" and port in {"in", "initial", "out"}:
return node.get("_resolved_value_type")
if primitive_ref == "frog.ui.property_write":
if port == "ref":
return "widget_reference"
if port == "value":
return node.get("_resolved_value_type")
if kind == "structure":
return structure_output_types.get((node["id"], port))
return None
def _resolve_region_port_type(
*,
region_node: Dict[str, Any],
port: str,
boundary_input_types: Dict[str, str],
boundary_output_types: Dict[str, str],
structure_terminals: Dict[str, Dict[str, Any]],
) -> Optional[str]:
kind = region_node.get("kind")
if kind == "boundary_input" and port == "value":
return boundary_input_types.get(region_node.get("boundary_port"))
if kind == "boundary_output" and port == "value":
return boundary_output_types.get(region_node.get("boundary_port"))
if kind == "structure_terminal":
terminal_name = region_node.get("terminal")
terminal = structure_terminals.get(terminal_name, {})
if port == "value":
return terminal.get("type")
if kind == "primitive":
primitive_ref = region_node.get("type")
if primitive_ref == "frog.core.add" and port in {"a", "b", "result"}:
return region_node.get("_resolved_value_type")
if primitive_ref == "frog.core.delay" and port in {"in", "initial", "out"}:
return region_node.get("_resolved_value_type")
return None
def _build_region_incoming_edge_map(
region_edges: List[Dict[str, Any]],
*,
source_ref: Dict[str, Any],
location_prefix: str,
) -> Tuple[Optional[ValidationResult], Dict[Tuple[str, str], List[Dict[str, Any]]]]:
incoming: Dict[Tuple[str, str], List[Dict[str, Any]]] = {}
diagnostics: List[Dict[str, Any]] = []
for edge in region_edges:
if not _is_object(edge):
diagnostics.append(_diag("invalid_region_edge_shape", "Each region edge must be an object.", location=location_prefix))
continue
edge_id = edge.get("id")
edge_from = edge.get("from")
edge_to = edge.get("to")
if not edge_id or not _is_object(edge_from) or not _is_object(edge_to):
diagnostics.append(
_diag("invalid_region_edge_fields", "Each region edge must define 'id', 'from', and 'to'.", location=location_prefix)
)
continue
from_node = edge_from.get("node")
from_port = edge_from.get("port")
to_node = edge_to.get("node")
to_port = edge_to.get("port")
if not from_node or not from_port or not to_node or not to_port:
diagnostics.append(
_diag("invalid_region_edge_endpoints", "Each region edge endpoint must define 'node' and 'port'.", location=location_prefix)
)
continue
incoming.setdefault((to_node, to_port), []).append(deepcopy(edge))
if diagnostics:
return _fail("structural_invalid", source_ref, diagnostics), {}
return None, incoming
def _validate_for_loop_structure(
*,
node_id: str,
node: Dict[str, Any],
node_map: Dict[str, Dict[str, Any]],
incoming: Dict[Tuple[str, str], List[Dict[str, Any]]],
widget_by_id: Dict[str, Dict[str, Any]],
input_types: Dict[str, str],
output_types: Dict[str, str],
structure_output_types: Dict[Tuple[str, str], str],
source_ref: Dict[str, Any],
) -> Optional[ValidationResult]:
diagnostics: List[Dict[str, Any]] = []
if node.get("kind") != "structure" or node.get("structure_type") != "for_loop":
diagnostics.append(
_diag(
"unsupported_structure_kind",
"Reference subset expects a canonical structure node with structure_type 'for_loop'.",
location=f"diagram.node:{node_id}",
)
)
return _fail("unsupported_but_valid", source_ref, diagnostics)
boundary = node.get("boundary")
structure_terminals = node.get("structure_terminals")
regions = node.get("regions")
if not _is_object(boundary):
diagnostics.append(_diag("invalid_structure_boundary", "for_loop 'boundary' must be an object.", location=f"diagram.node:{node_id}"))
return _fail("structural_invalid", source_ref, diagnostics)
if not _is_object(structure_terminals):
diagnostics.append(
_diag("invalid_structure_terminals", "for_loop 'structure_terminals' must be an object.", location=f"diagram.node:{node_id}")
)
return _fail("structural_invalid", source_ref, diagnostics)
if not _is_array(regions):
diagnostics.append(_diag("invalid_structure_regions", "for_loop 'regions' must be an array.", location=f"diagram.node:{node_id}"))
return _fail("structural_invalid", source_ref, diagnostics)
boundary_inputs = boundary.get("inputs", [])
boundary_outputs = boundary.get("outputs", [])
if not _is_array(boundary_inputs) or not _is_array(boundary_outputs):
diagnostics.append(
_diag(
"invalid_structure_boundary_collections",
"for_loop boundary inputs and outputs must be arrays.",
location=f"diagram.node:{node_id}",
)
)
return _fail("structural_invalid", source_ref, diagnostics)
boundary_input_types: Dict[str, str] = {}
boundary_output_types: Dict[str, str] = {}
expected_inputs = {"input_value", "initial_state"}
expected_outputs = {"final_value"}
actual_inputs = set()
actual_outputs = set()
for item in boundary_inputs:
if not _is_object(item) or "id" not in item or "type" not in item:
diagnostics.append(
_diag(
"invalid_for_loop_boundary_input",
"Each for_loop boundary input must define 'id' and 'type'.",
location=f"diagram.node:{node_id}",
)
)
continue
actual_inputs.add(item["id"])
boundary_input_types[item["id"]] = item["type"]
for item in boundary_outputs:
if not _is_object(item) or "id" not in item or "type" not in item:
diagnostics.append(
_diag(
"invalid_for_loop_boundary_output",
"Each for_loop boundary output must define 'id' and 'type'.",
location=f"diagram.node:{node_id}",
)
)
continue
actual_outputs.add(item["id"])
boundary_output_types[item["id"]] = item["type"]
if actual_inputs != expected_inputs:
diagnostics.append(
_diag(
"unsupported_for_loop_boundary_inputs",
"Reference subset expects exactly boundary inputs 'input_value' and 'initial_state'.",
location=f"diagram.node:{node_id}",
)
)
if actual_outputs != expected_outputs:
diagnostics.append(
_diag(
"unsupported_for_loop_boundary_outputs",
"Reference subset expects exactly boundary output 'final_value'.",
location=f"diagram.node:{node_id}",
)
)
if diagnostics:
return _fail("unsupported_but_valid", source_ref, diagnostics)
input_value_type = boundary_input_types["input_value"]
initial_state_type = boundary_input_types["initial_state"]
final_value_type = boundary_output_types["final_value"]
if input_value_type != initial_state_type or input_value_type != final_value_type:
diagnostics.append(
_diag(
"invalid_for_loop_boundary_types",
"for_loop boundary input/output types must match in the reference subset.",
location=f"diagram.node:{node_id}",
)
)
if input_value_type != "u16":
diagnostics.append(
_diag(
"unsupported_for_loop_value_type",
f"Unsupported for_loop value type in reference subset: {input_value_type}. Expected 'u16'.",
location=f"diagram.node:{node_id}",
)
)
final_output = next((item for item in boundary_outputs if item["id"] == "final_value"), None)
if final_output is None or final_output.get("mode") != "last_value":
diagnostics.append(
_diag(
"invalid_for_loop_output_mode",
"Reference subset expects boundary output 'final_value' with mode 'last_value'.",
location=f"diagram.node:{node_id}",
)
)
elif final_output.get("zero_iteration_value") != 0:
diagnostics.append(
_diag(
"invalid_for_loop_zero_iteration_value",
"Reference subset expects boundary output 'final_value' zero_iteration_value to equal 0.",
location=f"diagram.node:{node_id}",
)
)
count_terminal = structure_terminals.get("count")
index_terminal = structure_terminals.get("index")
if not _is_object(count_terminal) or count_terminal.get("type") != "i64" or count_terminal.get("outer_visible") is not True:
diagnostics.append(
_diag(
"invalid_for_loop_count_terminal",
"Reference subset expects a 'count' terminal of type 'i64' with outer_visible true.",
location=f"diagram.node:{node_id}",
)
)
if not _is_object(index_terminal) or index_terminal.get("type") != "i64" or index_terminal.get("exposed_in_body") is not True:
diagnostics.append(
_diag(
"invalid_for_loop_index_terminal",
"Reference subset expects an 'index' terminal of type 'i64' exposed in the body.",
location=f"diagram.node:{node_id}",
)
)
incoming_count = incoming.get((node_id, "count"), [])
incoming_input_value = incoming.get((node_id, "input_value"), [])
incoming_initial_state = incoming.get((node_id, "initial_state"), [])
if len(incoming_count) != 1:
diagnostics.append(
_diag(
"invalid_for_loop_count_input",
"Reference subset expects exactly one incoming edge for structure terminal 'count'.",
location=f"diagram.node:{node_id}",
)
)
if len(incoming_input_value) != 1 or len(incoming_initial_state) != 1:
diagnostics.append(
_diag(
"invalid_for_loop_boundary_inputs_edges",
"Reference subset expects exactly one incoming edge for 'input_value' and 'initial_state'.",
location=f"diagram.node:{node_id}",
)
)
if diagnostics:
return _fail("semantic_rejected", source_ref, diagnostics)
count_source_edge = incoming_count[0]
count_source_node = node_map.get(count_source_edge["from"]["node"])
count_source_port = count_source_edge["from"]["port"]
count_source_type = None
if count_source_node is not None:
count_source_type = _resolve_source_port_type(
node=count_source_node,
port=count_source_port,
input_types=input_types,
output_types=output_types,
widget_by_id=widget_by_id,
structure_output_types=structure_output_types,
)
if count_source_node is None or count_source_node.get("kind") != "constant" or count_source_port != "value":
diagnostics.append(
_diag(
"unsupported_for_loop_count_source",
"Reference subset expects structure terminal 'count' to be driven by a constant node 'value' port.",
location=f"diagram.node:{node_id}",
)
)
elif count_source_type != "i64" or int(count_source_node.get("value")) != 5:
diagnostics.append(
_diag(
"unsupported_for_loop_iteration_count",
"Reference subset currently supports only a constant i64 iteration count of exactly 5.",
location=f"diagram.node:{node_id}",
)
)
input_source_edge = incoming_input_value[0]
initial_source_edge = incoming_initial_state[0]
input_source_node = node_map.get(input_source_edge["from"]["node"])
initial_source_node = node_map.get(initial_source_edge["from"]["node"])
input_source_type = None
initial_source_type = None
if input_source_node is not None:
input_source_type = _resolve_source_port_type(
node=input_source_node,
port=input_source_edge["from"]["port"],
input_types=input_types,
output_types=output_types,
widget_by_id=widget_by_id,
structure_output_types=structure_output_types,
)
if initial_source_node is not None:
initial_source_type = _resolve_source_port_type(
node=initial_source_node,
port=initial_source_edge["from"]["port"],
input_types=input_types,
output_types=output_types,
widget_by_id=widget_by_id,
structure_output_types=structure_output_types,
)
if input_source_type != "u16" or initial_source_type != "u16":
diagnostics.append(
_diag(
"invalid_for_loop_input_types",
"Reference subset expects both 'input_value' and 'initial_state' inputs to resolve to type 'u16'.",
location=f"diagram.node:{node_id}",
)
)
if len(regions) != 1 or not _is_object(regions[0]) or regions[0].get("id") != "body":
diagnostics.append(
_diag(
"invalid_for_loop_regions",
"Reference subset expects exactly one for_loop region with id 'body'.",
location=f"diagram.node:{node_id}",
)
)
return _fail("semantic_rejected", source_ref, diagnostics)
body_region = regions[0]
body_diagram = body_region.get("diagram")
if not _is_object(body_diagram):
diagnostics.append(
_diag(
"invalid_for_loop_body_diagram",
"for_loop body region must define a valid 'diagram' object.",
location=f"diagram.node:{node_id}.regions:body",
)
)
return _fail("structural_invalid", source_ref, diagnostics)
region_nodes = body_diagram.get("nodes", [])
region_edges = body_diagram.get("edges", [])
if not _is_array(region_nodes) or not _is_array(region_edges):
diagnostics.append(
_diag(
"invalid_for_loop_body_collections",
"for_loop body diagram must define 'nodes' and 'edges' arrays.",
location=f"diagram.node:{node_id}.regions:body",
)
)
return _fail("structural_invalid", source_ref, diagnostics)
region_node_map: Dict[str, Dict[str, Any]] = {}
for region_node in region_nodes:
if not _is_object(region_node) or "id" not in region_node or "kind" not in region_node:
diagnostics.append(
_diag(
"invalid_region_node_shape",
"Each region node must define 'id' and 'kind'.",
location=f"diagram.node:{node_id}.regions:body",
)
)
continue
region_node_id = region_node["id"]
if region_node_id in region_node_map:
diagnostics.append(
_diag(
"duplicate_region_node_id",
f"Duplicate region node id '{region_node_id}'.",
location=f"diagram.node:{node_id}.regions:body",
)
)
continue
region_node_map[region_node_id] = deepcopy(region_node)
if diagnostics:
return _fail("structural_invalid", source_ref, diagnostics)
region_incoming_failure, region_incoming = _build_region_incoming_edge_map(
region_edges,
source_ref=source_ref,
location_prefix=f"diagram.node:{node_id}.regions:body",
)
if region_incoming_failure is not None:
return region_incoming_failure
expected_region_kinds = {
"body_input_value": "boundary_input",
"body_initial_state": "boundary_input",
"body_index": "structure_terminal",
"delay_state": "primitive",
"add_step": "primitive",
"body_final_value": "boundary_output",
}
for expected_id, expected_kind in expected_region_kinds.items():
region_node = region_node_map.get(expected_id)
if region_node is None or region_node.get("kind") != expected_kind:
diagnostics.append(
_diag(
"unsupported_for_loop_region_shape",
f"Reference subset expects region node '{expected_id}' of kind '{expected_kind}'.",
location=f"diagram.node:{node_id}.regions:body",
)
)
if diagnostics:
return _fail("unsupported_but_valid", source_ref, diagnostics)
if region_node_map["body_input_value"].get("boundary_port") != "input_value":
diagnostics.append(
_diag(
"invalid_region_boundary_input_mapping",
"Region node 'body_input_value' must reference boundary port 'input_value'.",
location=f"diagram.node:{node_id}.regions:body.node:body_input_value",
)
)
if region_node_map["body_initial_state"].get("boundary_port") != "initial_state":
diagnostics.append(
_diag(
"invalid_region_boundary_input_mapping",
"Region node 'body_initial_state' must reference boundary port 'initial_state'.",
location=f"diagram.node:{node_id}.regions:body.node:body_initial_state",
)
)
if region_node_map["body_final_value"].get("boundary_port") != "final_value":
diagnostics.append(
_diag(
"invalid_region_boundary_output_mapping",
"Region node 'body_final_value' must reference boundary port 'final_value'.",
location=f"diagram.node:{node_id}.regions:body.node:body_final_value",
)
)
if region_node_map["body_index"].get("terminal") != "index":
diagnostics.append(
_diag(
"invalid_region_structure_terminal_mapping",
"Region node 'body_index' must reference structure terminal 'index'.",
location=f"diagram.node:{node_id}.regions:body.node:body_index",
)
)
if region_node_map["delay_state"].get("type") != "frog.core.delay":
diagnostics.append(
_diag(
"invalid_region_delay_primitive",
"Region node 'delay_state' must be primitive 'frog.core.delay'.",
location=f"diagram.node:{node_id}.regions:body.node:delay_state",
)
)
if region_node_map["add_step"].get("type") != "frog.core.add":
diagnostics.append(
_diag(
"invalid_region_add_primitive",
"Region node 'add_step' must be primitive 'frog.core.add'.",
location=f"diagram.node:{node_id}.regions:body.node:add_step",
)
)
if diagnostics:
return _fail("semantic_rejected", source_ref, diagnostics)
add_node = region_node_map["add_step"]
delay_node = region_node_map["delay_state"]
add_incoming_a = region_incoming.get(("add_step", "a"), [])
add_incoming_b = region_incoming.get(("add_step", "b"), [])
delay_incoming_in = region_incoming.get(("delay_state", "in"), [])
delay_incoming_initial = region_incoming.get(("delay_state", "initial"), [])
body_final_incoming = region_incoming.get(("body_final_value", "value"), [])
if len(add_incoming_a) != 1 or len(add_incoming_b) != 1:
diagnostics.append(
_diag(
"invalid_region_add_inputs",
"Region primitive 'add_step' requires exactly one incoming edge on ports 'a' and 'b'.",
location=f"diagram.node:{node_id}.regions:body.node:add_step",
)
)
if len(delay_incoming_in) != 1 or len(delay_incoming_initial) != 1:
diagnostics.append(
_diag(
"invalid_region_delay_inputs",
"Region primitive 'delay_state' requires exactly one incoming edge on ports 'in' and 'initial'.",
location=f"diagram.node:{node_id}.regions:body.node:delay_state",
)
)
if len(body_final_incoming) != 1:
diagnostics.append(
_diag(
"invalid_region_boundary_output_input",
"Region boundary output 'body_final_value' requires exactly one incoming edge on port 'value'.",
location=f"diagram.node:{node_id}.regions:body.node:body_final_value",
)
)
if diagnostics:
return _fail("semantic_rejected", source_ref, diagnostics)
add_a_source = region_node_map[add_incoming_a[0]["from"]["node"]]
add_b_source = region_node_map[add_incoming_b[0]["from"]["node"]]
delay_in_source = region_node_map[delay_incoming_in[0]["from"]["node"]]
delay_initial_source = region_node_map[delay_incoming_initial[0]["from"]["node"]]
body_final_source = region_node_map[body_final_incoming[0]["from"]["node"]]
add_a_type = _resolve_region_port_type(
region_node=add_a_source,
port=add_incoming_a[0]["from"]["port"],
boundary_input_types=boundary_input_types,
boundary_output_types=boundary_output_types,
structure_terminals=structure_terminals,
)
add_b_type = _resolve_region_port_type(
region_node=add_b_source,
port=add_incoming_b[0]["from"]["port"],
boundary_input_types=boundary_input_types,
boundary_output_types=boundary_output_types,
structure_terminals=structure_terminals,
)
if add_a_type != "u16" or add_b_type != "u16" or add_a_type != add_b_type:
diagnostics.append(
_diag(
"invalid_region_add_types",
"Region primitive 'add_step' requires both inputs to resolve to type 'u16' in the reference subset.",
location=f"diagram.node:{node_id}.regions:body.node:add_step",
)