File size: 84,815 Bytes
63dd93e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
import json
import copy
import re

def generate_blocks_from_opcodes(opcode_counts, all_block_definitions):
    """

    Generates a dictionary of Scratch-like blocks based on a list of opcodes and a reference block definition.

    It now correctly links parent and menu blocks using their generated unique keys, and handles various block categories.

    It ensures that menu blocks are only generated as children of their respective parent blocks.



    Args:

        opcode_counts (list): An array of objects, each with an 'opcode' and 'count' property.

                              Example: [{"opcode": "motion_gotoxy", "count": 1}]

        all_block_definitions (dict): A comprehensive dictionary containing definitions for all block types.



    Returns:

        tuple: A tuple containing:

            - dict: A JSON object where keys are generated block IDs and values are the block definitions.

            - dict: The opcode_occurrences dictionary for consistent unique key generation across functions.

    """
    generated_blocks = {}
    opcode_occurrences = {}  # To keep track of how many times each opcode (main or menu) has been used for unique keys

    # Define explicit parent-menu relationships for linking purposes
    # This maps main_opcode -> list of (input_field_name, menu_opcode)
    explicit_menu_links = {
        "motion_goto": [("TO", "motion_goto_menu")],
        "motion_glideto": [("TO", "motion_glideto_menu")],
        "motion_pointtowards": [("TOWARDS", "motion_pointtowards_menu")],
        "sensing_keypressed": [("KEY_OPTION", "sensing_keyoptions")],
        "sensing_of": [("OBJECT", "sensing_of_object_menu")],
        "sensing_touchingobject": [("TOUCHINGOBJECTMENU", "sensing_touchingobjectmenu")],
        "control_create_clone_of": [("CLONE_OPTION", "control_create_clone_of_menu")],
        "sound_play": [("SOUND_MENU", "sound_sounds_menu")],
        "sound_playuntildone": [("SOUND_MENU", "sound_sounds_menu")],
        "looks_switchcostumeto": [("COSTUME", "looks_costume")],
        "looks_switchbackdropto": [("BACKDROP", "looks_backdrops")],
    }

    # --- Step 1: Process explicitly requested opcodes and generate their instances and associated menus ---
    for item in opcode_counts:
        opcode = item.get("opcode")
        count = item.get("count", 1)

        # Special handling for 'sensing_istouching' which maps to 'sensing_touchingobject'
        if opcode == "sensing_istouching":
            opcode = "sensing_touchingobject"

        if not opcode:
            print("Warning: Skipping item with missing 'opcode'.")
            continue

        if opcode not in all_block_definitions:
            print(f"Warning: Opcode '{opcode}' not found in all_block_definitions. Skipping.")
            continue

        for _ in range(count):
            # Increment occurrence count for the current main opcode
            opcode_occurrences[opcode] = opcode_occurrences.get(opcode, 0) + 1
            main_block_instance_num = opcode_occurrences[opcode]
            
            main_block_unique_key = f"{opcode}_{main_block_instance_num}"
            
            # Create a deep copy of the main block definition
            main_block_data = copy.deepcopy(all_block_definitions[opcode])
            
            # Set properties for a top-level main block
            main_block_data["parent"] = None
            main_block_data["next"] = None
            main_block_data["topLevel"] = True
            main_block_data["shadow"] = False # Main blocks are typically not shadows

            generated_blocks[main_block_unique_key] = main_block_data

            # If this main block has associated menus, generate and link them now
            if opcode in explicit_menu_links:
                for input_field_name, menu_opcode_type in explicit_menu_links[opcode]:
                    if menu_opcode_type in all_block_definitions:
                        # Increment the occurrence for the menu block type
                        opcode_occurrences[menu_opcode_type] = opcode_occurrences.get(menu_opcode_type, 0) + 1
                        menu_block_instance_num = opcode_occurrences[menu_opcode_type]

                        menu_block_data = copy.deepcopy(all_block_definitions[menu_opcode_type])
                        
                        # Generate a unique key for this specific menu instance
                        menu_unique_key = f"{menu_opcode_type}_{menu_block_instance_num}"
                        
                        # Set properties for a shadow menu block
                        menu_block_data["shadow"] = True
                        menu_block_data["topLevel"] = False
                        menu_block_data["next"] = None
                        menu_block_data["parent"] = main_block_unique_key # Link menu to its parent instance

                        # Update the main block's input to point to this unique menu instance
                        if input_field_name in main_block_data.get("inputs", {}) and \
                           isinstance(main_block_data["inputs"][input_field_name], list) and \
                           len(main_block_data["inputs"][input_field_name]) > 1 and \
                           main_block_data["inputs"][input_field_name][0] == 1:
                            
                            main_block_data["inputs"][input_field_name][1] = menu_unique_key
                        
                        generated_blocks[menu_unique_key] = menu_block_data
            
    return generated_blocks, opcode_occurrences

def interpret_pseudo_code_and_update_blocks(generated_blocks_json, pseudo_code, all_block_definitions, opcode_occurrences):
    """

    Interprets pseudo-code to update the generated Scratch blocks, replacing static values

    with dynamic values and establishing stacking/nesting logic.



    Args:

        generated_blocks_json (dict): The JSON object of pre-generated blocks.

        pseudo_code (str): The pseudo-code string to interpret.

        all_block_definitions (dict): A comprehensive dictionary containing definitions for all block types.

        opcode_occurrences (dict): A dictionary to keep track of opcode occurrences for unique key generation.



    Returns:

        dict: The updated JSON object of Scratch blocks.

    """
    updated_blocks = copy.deepcopy(generated_blocks_json)
    
    # Helper to create a new block instance (used for shadows/nested blocks)
    def create_block_instance_for_parsing(opcode, parent_key=None, is_shadow=False, is_top_level=False):
        opcode_occurrences[opcode] = opcode_occurrences.get(opcode, 0) + 1
        unique_key = f"{opcode}_{opcode_occurrences[opcode]}"
        
        new_block = copy.deepcopy(all_block_definitions.get(opcode, {}))
        if not new_block:
            print(f"Error: Definition for opcode '{opcode}' not found when creating instance for parsing.")
            return None, None

        new_block["parent"] = parent_key
        new_block["next"] = None
        new_block["topLevel"] = is_top_level
        new_block["shadow"] = is_shadow

        # Initialize inputs/fields to default empty values, but preserve structure
        if "inputs" in new_block:
            for input_name, input_data in new_block["inputs"].items():
                if isinstance(input_data, list) and len(input_data) > 1:
                    if input_data[0] == 1: # Block reference
                        new_block["inputs"][input_name][1] = None # Placeholder for linked block ID
                    elif input_data[0] in [4, 5, 6, 7, 8, 9, 10]: # Literal types
                        new_block["inputs"][input_name][1] = "" # Default empty literal
        if "fields" in new_block:
            for field_name, field_data in new_block["fields"].items():
                if isinstance(field_data, list) and len(field_data) > 0:
                    new_block["fields"][field_name][0] = "" # Default empty field value

        updated_blocks[unique_key] = new_block
        return unique_key, new_block

    # Helper to parse input values from pseudo-code and create nested blocks if necessary
    def parse_and_link_input(parent_block_key, input_type, pseudo_value, input_name_in_parent=None, field_name_in_parent=None):
        pseudo_value = pseudo_value.strip()

        # 1. Handle literal numbers (e.g., "2", "+1", "-135")
        if re.fullmatch(r"[-+]?\d+(\.\d+)?", pseudo_value):
            return [4, pseudo_value] # Type 4 for number literal

        # 2. Handle literal strings (e.g., "Game Over") - often in quotes or simply text
        # If it's a broadcast message, it's typically a string literal.
        # If it's a 'say' message, it's a string literal.
        # If it's a variable name, it's handled by specific patterns later.
        if pseudo_value.startswith('"') and pseudo_value.endswith('"'):
            return [10, pseudo_value.strip('"')] # Type 10 for string literal
        
        # 3. Handle variable names (e.g., "[score v]", "[Sprite1 v]", "[Game Over v]")
        # These can be fields or inputs that expect a variable reporter block.
        var_match = re.match(r"\[(.+?) v\]", pseudo_value)
        if var_match:
            var_name = var_match.group(1).strip()
            # If it's a field (like for set variable, show variable)
            if field_name_in_parent:
                return var_name # Return name, parent block will set its field
            # If it's an input that expects a variable reporter (e.g., 'say (score)')
            # Create a data_variable shadow block
            var_reporter_key, var_reporter_data = create_block_instance_for_parsing(
                "data_variable", parent_key=parent_block_key, is_shadow=True
            )
            if var_reporter_key:
                var_reporter_data["fields"]["VARIABLE"] = [var_name, f"`var_{var_name}"] # Placeholder ID
                return [1, var_reporter_key] # Type 1 for block reference
            else:
                print(f"Warning: Could not create data_variable block for '{var_name}'")
                return [10, var_name] # Fallback to string literal

        # 4. Handle nested reporter blocks (e.g., "(x position)")
        reporter_match = re.match(r"\((.+?)\)", pseudo_value)
        if reporter_match:
            inner_content = reporter_match.group(1).strip()
            # Check if it's a known reporter block (like "x position")
            if inner_content in reporter_opcode_lookup:
                reporter_opcode = reporter_opcode_lookup[inner_content]
                reporter_block_key, reporter_block_data = create_block_instance_for_parsing(
                    reporter_opcode, parent_key=parent_block_key, is_shadow=True
                )
                if reporter_block_key:
                    return [1, reporter_block_key] # Type 1 for block reference
                else:
                    print(f"Warning: Could not create reporter block for '{inner_content}'")
                    return [10, inner_content] # Fallback to string literal
            else: # It's a literal number or string inside parentheses
                return parse_and_link_input(parent_block_key, input_type, inner_content) # Recurse for inner content

        # 5. Handle nested boolean blocks (e.g., "<(...) < (...)>")
        boolean_match = re.match(r"<(.+?)>", pseudo_value)
        if boolean_match:
            inner_condition_str = boolean_match.group(1).strip()
            # This is typically handled by the parent block's parsing (e.g., control_if)
            # For now, if called directly, it implies a boolean reporter.
            # We'll need specific logic for operator_lt, operator_and, etc.
            # This part is complex and often handled by the parent block's specific regex.
            # For this problem, the 'if' block's logic will create the boolean shadow.
            print(f"Warning: Direct parsing of standalone boolean '{pseudo_value}' not fully supported here.")
            return [10, pseudo_value] # Fallback to string literal

        # Default to string literal if no other pattern matches
        return [10, pseudo_value]

    lines = [line.strip() for line in pseudo_code.strip().split('\n') if line.strip()]

    # Track the current script and nesting
    current_script_head = None
    block_stack = [] # Stores (parent_block_key, indent_level, last_child_key_in_scope)

    # Create a mapping from block name patterns to their opcodes and input/field details
    # The 'input_map' keys are the internal Scratch input names, values are regex group indices.
    # The 'field_map' keys are the internal Scratch field names, values are regex group indices.
    # 'condition_opcode' is for 'if' blocks that take a specific boolean reporter.
    pseudo_code_to_opcode_map = {
        re.compile(r"when green flag clicked"): {"opcode": "event_whenflagclicked"},
        re.compile(r"go to x: \((.+?)\) y: \((.+?)\)"): {"opcode": "motion_gotoxy", "input_map": {"X": 0, "Y": 1}},
        re.compile(r"set \[(.+?) v\] to (.+)"): {"opcode": "data_setvariableto", "field_map": {"VARIABLE": 0}, "input_map": {"VALUE": 1}},
        re.compile(r"show variable \[(.+?) v\]"): {"opcode": "data_showvariable", "field_map": {"VARIABLE": 0}},
        re.compile(r"forever"): {"opcode": "control_forever"},
        re.compile(r"glide \((.+?)\) seconds to x: \((.+?)\) y: \((.+?)\)"): {"opcode": "motion_glidesecstoxy", "input_map": {"SECS": 0, "X": 1, "Y": 2}},
        re.compile(r"if <\((.+)\) < \((.+)\)> then"): {"opcode": "control_if", "condition_type": "operator_lt", "condition_input_map": {"OPERAND1": 0, "OPERAND2": 1}},
        re.compile(r"set x to \((.+?)\)"): {"opcode": "motion_setx", "input_map": {"X": 0}},
        re.compile(r"if <touching \[(.+?) v\]\?> then"): {"opcode": "control_if", "condition_type": "sensing_touchingobject", "condition_field_map": {"TOUCHINGOBJECTMENU": 0}},
        re.compile(r"broadcast \[(.+?) v\]"): {"opcode": "event_broadcast", "input_map": {"BROADCAST_INPUT": 0}},
        re.compile(r"stop \[(.+?) v\]"): {"opcode": "control_stop", "field_map": {"STOP_OPTION": 0}},
        re.compile(r"end"): {"opcode": "end_block"}, # Special marker for script end/C-block end
    }

    # Create a reverse lookup for reporter block opcodes based on their pseudo-code representation
    reporter_opcode_lookup = {}
    for opcode, definition in all_block_definitions.items():
        if definition.get("block_shape") == "Reporter Block":
            block_name = definition.get("block_name")
            if block_name:
                # Clean up block name for matching: remove parentheses, 'v' for variable, etc.
                clean_name = block_name.replace("(", "").replace(")", "").replace("[", "").replace("]", "").replace(" v", "").strip()
                reporter_opcode_lookup[clean_name] = opcode
                # Add specific entries for common reporters if their pseudo-code differs from clean_name
                if opcode == "motion_xposition":
                    reporter_opcode_lookup["x position"] = opcode
                elif opcode == "motion_yposition":
                    reporter_opcode_lookup["y position"] = opcode
                # Add more as needed based on pseudo-code patterns

    for line_idx, raw_line in enumerate(lines):
        current_line_indent = len(raw_line) - len(raw_line.lstrip())
        line = raw_line.strip()

        # Adjust block_stack based on current indent level
        while block_stack and current_line_indent <= block_stack[-1][1]:
            block_stack.pop()

        matched_block_info = None
        matched_values = None
        
        # Try to match the line against known block patterns
        for pattern_regex, info in pseudo_code_to_opcode_map.items():
            match = pattern_regex.match(line)
            if match:
                matched_block_info = info
                matched_values = match.groups()
                break
        
        if not matched_block_info:
            print(f"Warning: Could not interpret line: '{line}' at line {line_idx + 1}")
            continue

        opcode = matched_block_info["opcode"]

        # Handle 'end' block separately as it signifies closing a C-block
        if opcode == "end_block":
            # This 'end' matches the most recent C-block on the stack.
            # The while loop at the beginning of the iteration already handles popping.
            continue

        parent_key = None
        if block_stack:
            parent_key = block_stack[-1][0] # The last block on the stack is the parent

        # Create the new block instance
        new_block_key, new_block_data = create_block_instance_for_parsing(
            opcode, 
            parent_key=parent_key, 
            is_top_level=(parent_key is None)
        )
        if not new_block_key:
            continue

        # Link to previous block in the same script/nesting level
        if block_stack:
            # Update the 'next' of the previous block in the current scope
            last_child_key_in_scope = block_stack[-1][2] if len(block_stack[-1]) > 2 else None
            if last_child_key_in_scope and last_child_key_in_scope in updated_blocks:
                updated_blocks[last_child_key_in_scope]["next"] = new_block_key
            
            # Update the last child in the current scope
            block_stack[-1] = (block_stack[-1][0], block_stack[-1][1], new_block_key)
        
        # Populate inputs and fields based on matched_block_info and matched_values
        if matched_values:
            # Handle fields
            if "field_map" in matched_block_info:
                for field_name, group_idx in matched_block_info["field_map"].items():
                    pseudo_field_value = matched_values[group_idx].replace(' v', '').strip()
                    if field_name == "VARIABLE":
                        # For variable fields, the actual variable ID is often derived or generated.
                        # For now, we use a placeholder and the name.
                        new_block_data["fields"][field_name] = [pseudo_field_value, f"`var_{pseudo_field_value}"]
                    elif field_name == "STOP_OPTION":
                        new_block_data["fields"][field_name] = [pseudo_field_value, None] # No ID needed for dropdown option
                    else:
                        new_block_data["fields"][field_name][0] = pseudo_field_value

            # Handle inputs
            if "input_map" in matched_block_info:
                for input_name, group_idx in matched_block_info["input_map"].items():
                    pseudo_input_value = matched_values[group_idx].strip()
                    
                    parsed_input_info = parse_and_link_input(new_block_key, new_block_data["inputs"][input_name][0], pseudo_input_value, input_name_in_parent=input_name)
                    
                    if parsed_input_info:
                        if parsed_input_info[0] == 1: # It's a linked block (shadow reporter/boolean/menu)
                            new_block_data["inputs"][input_name][1] = parsed_input_info[1] # Link block ID
                            new_block_data["inputs"][input_name][0] = parsed_input_info[0] # Set type to 1 (block)
                        else: # It's a literal value
                            new_block_data["inputs"][input_name][1] = parsed_input_info[1]
                            new_block_data["inputs"][input_name][0] = parsed_input_info[0] # Set appropriate type (4 for number, 10 for string)
            
            # Special handling for 'if' block conditions
            if opcode == "control_if":
                condition_type = matched_block_info.get("condition_type")
                if condition_type:
                    condition_block_key, condition_block_data = create_block_instance_for_parsing(
                        condition_type, parent_key=new_block_key, is_shadow=True
                    )
                    if condition_block_key:
                        new_block_data["inputs"]["CONDITION"] = [2, condition_block_key] # Type 2 for boolean block reference

                        # Populate inputs for the condition block (e.g., operator_lt)
                        if "condition_input_map" in matched_block_info:
                            for cond_input_name, cond_group_idx in matched_block_info["condition_input_map"].items():
                                pseudo_cond_value = matched_values[cond_group_idx].strip()
                                parsed_cond_input_info = parse_and_link_input(condition_block_key, condition_block_data["inputs"][cond_input_name][0], pseudo_cond_value)
                                if parsed_cond_input_info:
                                    if parsed_cond_input_info[0] == 1:
                                        condition_block_data["inputs"][cond_input_name][1] = parsed_cond_input_info[1]
                                        condition_block_data["inputs"][cond_input_name][0] = parsed_cond_input_info[0]
                                    else:
                                        condition_block_data["inputs"][cond_input_name][1] = parsed_cond_input_info[1]
                                        condition_block_data["inputs"][cond_input_name][0] = parsed_cond_input_info[0]
                        
                        # Populate fields for the condition block (e.g., sensing_touchingobject's menu)
                        if "condition_field_map" in matched_block_info:
                            for cond_field_name, cond_group_idx in matched_block_info["condition_field_map"].items():
                                pseudo_cond_field_value = matched_values[cond_group_idx].replace(' v', '').strip()
                                if cond_field_name == "TOUCHINGOBJECTMENU":
                                    # Create the menu block for TOUCHINGOBJECTMENU
                                    menu_opcode = "sensing_touchingobjectmenu"
                                    menu_key, menu_data = create_block_instance_for_parsing(
                                        menu_opcode,
                                        parent_key=condition_block_key,
                                        is_shadow=True
                                    )
                                    if menu_key:
                                        condition_block_data["inputs"]["TOUCHINGOBJECTMENU"] = [1, menu_key] # Link to menu block
                                        menu_data["fields"]["TOUCHINGOBJECTMENU"] = [pseudo_cond_field_value, None] # Set menu value
                                    else:
                                        print(f"Warning: Could not create menu block for touching object: '{pseudo_cond_field_value}'")
                                else:
                                    condition_block_data["fields"][cond_field_name][0] = pseudo_cond_field_value
                    else:
                        print(f"Warning: Could not create condition block '{condition_type}' for 'if' statement.")


        # For C-blocks, push onto stack to track nesting
        # 'control_if' is a C-block, but its 'next' is inside its substack.
        # 'control_forever' is also a C-block.
        if all_block_definitions[opcode].get("block_shape") == "C-Block" and opcode != "control_if":
            # For C-blocks, the 'next' of the parent is usually null, and children start in 'SUBSTACK'
            # We add the block to the stack, and its "next" will be its first child.
            # The 'next' of the *last child* in its substack will point to the block after the C-block.
            block_stack.append((new_block_key, current_line_indent, None)) # (parent_key, indent, last_child_key_in_substack)
            
            # For C-blocks, the first child is linked via the 'SUBSTACK' input
            new_block_data["inputs"]["SUBSTACK"] = [2, None] # Placeholder for the first child block ID

        # For 'if' blocks, the 'next' of the parent is usually null, and children start in 'SUBSTACK'
        if opcode == "control_if":
            block_stack.append((new_block_key, current_line_indent, None))
            new_block_data["inputs"]["SUBSTACK"] = [2, None] # Placeholder for the first child block ID


    # Final pass to ensure topLevel is correctly set for the very first block of a script
    for key, block in updated_blocks.items():
        if block.get("parent") is None and block.get("next") is not None:
            block["topLevel"] = True
        elif block.get("parent") is None and block.get("next") is None and block.get("opcode") == "event_whenflagclicked":
             block["topLevel"] = True # Ensure hat blocks are always topLevel

    return updated_blocks

# --- Consolidated Block Definitions from all provided JSONs ---
# This dictionary should contain ALL block definitions from your JSON files.
# I'm using the provided definitions from the previous turn.
all_block_definitions = {
    # motion_block.json
    "motion_movesteps": {
        "block_name": "move () steps", "block_type": "Motion", "block_shape": "Stack Block", "op_code": "motion_movesteps",
        "functionality": "Moves the sprite forward by the specified number of steps in the direction it is currently facing. A positive value moves it forward, and a negative value moves it backward.",
        "inputs": {"STEPS": [1, [4, "10"]]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "motion_turnright": {
        "block_name": "turn right () degrees", "block_type": "Motion", "block_shape": "Stack Block", "op_code": "motion_turnright",
        "functionality": "Turns the sprite clockwise by the specified number of degrees.",
        "inputs": {"DEGREES": [1, [4, "15"]]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "motion_turnleft": {
        "block_name": "turn left () degrees", "block_type": "Motion", "block_shape": "Stack Block", "op_code": "motion_turnleft",
        "functionality": "Turns the sprite counter-clockwise by the specified number of degrees.",
        "inputs": {"DEGREES": [1, [4, "15"]]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "motion_goto": {
        "block_name": "go to ()", "block_type": "Motion", "block_shape": "Stack Block", "op_code": "motion_goto",
        "functionality": "Moves the sprite to a specified location, which can be a random position or at the mouse pointer or another to the sprite.",
        "inputs": {"TO": [1, "motion_goto_menu"]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "motion_goto_menu": {
        "block_name": "go to menu", "block_type": "Motion", "block_shape": "Reporter Block", "op_code": "motion_goto_menu",
        "functionality": "Menu for go to block.",
        "inputs": {}, "fields": {"TO": ["_random_", None]}, "shadow": True, "topLevel": False
    },
    "motion_gotoxy": {
        "block_name": "go to x: () y: ()", "block_type": "Motion", "block_shape": "Stack Block", "op_code": "motion_gotoxy",
        "functionality": "Moves the sprite to the specified X and Y coordinates on the stage.",
        "inputs": {"X": [1, [4, "0"]], "Y": [1, [4, "0"]]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "motion_glideto": {
        "block_name": "glide () secs to ()", "block_type": "Motion", "block_shape": "Stack Block", "op_code": "motion_glideto",
        "functionality": "Glides the sprite smoothly to a specified location (random position, mouse pointer, or another sprite) over a given number of seconds.",
        "inputs": {"SECS": [1, [4, "1"]], "TO": [1, "motion_glideto_menu"]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "motion_glideto_menu": {
        "block_name": "glide to menu", "block_type": "Motion", "block_shape": "Reporter Block", "op_code": "motion_glideto_menu",
        "functionality": "Menu for glide to block.",
        "inputs": {}, "fields": {"TO": ["_random_", None]}, "shadow": True, "topLevel": False
    },
    "motion_glidesecstoxy": {
        "block_name": "glide () secs to x: () y: ()", "block_type": "Motion", "block_shape": "Stack Block", "op_code": "motion_glidesecstoxy",
        "functionality": "Glides the sprite smoothly to the specified X and Y coordinates over a given number of seconds.",
        "inputs": {"SECS": [1, [4, "1"]], "X": [1, [4, "0"]], "Y": [1, [4, "0"]]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "motion_pointindirection": {
        "block_name": "point in direction ()", "block_type": "Motion", "block_shape": "Stack Block", "op_code": "motion_pointindirection",
        "functionality": "Sets the sprite's direction to a specified angle in degrees (0 = up, 90 = right, 180 = down, -90 = left).",
        "inputs": {"DIRECTION": [1, [8, "90"]]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "motion_pointtowards": {
        "block_name": "point towards ()", "block_type": "Motion", "block_shape": "Stack Block", "op_code": "motion_pointtowards",
        "functionality": "Points the sprite towards the mouse pointer or another specified sprite.",
        "inputs": {"TOWARDS": [1, "motion_pointtowards_menu"]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "motion_pointtowards_menu": {
        "block_name": "point towards menu", "block_type": "Motion", "block_shape": "Reporter Block", "op_code": "motion_pointtowards_menu",
        "functionality": "Menu for point towards block.",
        "inputs": {}, "fields": {"TOWARDS": ["_mouse_", None]}, "shadow": True, "topLevel": False
    },
    "motion_changexby": {
        "block_name": "change x by ()", "block_type": "Motion", "block_shape": "Stack Block", "op_code": "motion_changexby",
        "functionality": "Changes the sprite's X-coordinate by the specified amount, moving it horizontally.",
        "inputs": {"DX": [1, [4, "10"]]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "motion_setx": {
        "block_name": "set x to ()", "block_type": "Motion", "block_shape": "Stack Block", "op_code": "motion_setx",
        "functionality": "Sets the sprite's X-coordinate to a specific value, placing it at a precise horizontal position.",
        "inputs": {"X": [1, [4, "0"]]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "motion_changeyby": {
        "block_name": "change y by ()", "block_type": "Motion", "block_shape": "Stack Block", "op_code": "motion_changeyby",
        "functionality": "Changes the sprite's Y-coordinate by the specified amount, moving it vertically.",
        "inputs": {"DY": [1, [4, "10"]]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "motion_sety": {
        "block_name": "set y to ()", "block_type": "Motion", "block_shape": "Stack Block", "op_code": "motion_sety",
        "functionality": "Sets the sprite's Y-coordinate to a specific value, placing it at a precise vertical position.",
        "inputs": {"Y": [1, [4, "0"]]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "motion_ifonedgebounce": {
        "block_name": "if on edge, bounce", "block_type": "Motion", "block_shape": "Stack Block", "op_code": "motion_ifonedgebounce",
        "functionality": "Reverses the sprite's direction if it touches the edge of the stage.",
        "inputs": {}, "fields": {}, "shadow": False, "topLevel": True
    },
    "motion_setrotationstyle": {
        "block_name": "set rotation style ()", "block_type": "Motion", "block_shape": "Stack Block", "op_code": "motion_setrotationstyle",
        "functionality": "Determines how the sprite rotates: 'left-right' (flips horizontally), 'don't rotate' (stays facing one direction), or 'all around' (rotates freely).",
        "inputs": {}, "fields": {"STYLE": ["left-right", None]}, "shadow": False, "topLevel": True
    },
    "motion_xposition": {
        "block_name": "(x position)", "block_type": "Motion", "block_shape": "Reporter Block", "op_code": "motion_xposition",
        "functionality": "Reports the current X-coordinate of the sprite.[NOTE: not used in stage/backdrops]",
        "inputs": {}, "fields": {}, "shadow": False, "topLevel": True
    },
    "motion_yposition": {
        "block_name": "(y position)", "block_type": "Motion", "block_shape": "Reporter Block", "op_code": "motion_yposition",
        "functionality": "Reports the current Y coordinate of the sprite on the stage.[NOTE: not used in stage/backdrops]",
        "inputs": {}, "fields": {}, "shadow": False, "topLevel": True
    },
    "motion_direction": {
        "block_name": "(direction)", "block_type": "Motion", "block_shape": "Reporter Block", "op_code": "motion_direction",
        "functionality": "Reports the current direction of the sprite in degrees (0 = up, 90 = right, 180 = down, -90 = left).[NOTE: not used in stage/backdrops]",
        "inputs": {}, "fields": {}, "shadow": False, "topLevel": True
    },

    # control_block.json
    "control_wait": {
        "block_name": "wait () seconds", "block_type": "Control", "block_shape": "Stack Block", "op_code": "control_wait",
        "functionality": "Pauses the script for a specified duration.",
        "inputs": {"DURATION": [1, [5, "1"]]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "control_repeat": {
        "block_name": "repeat ()", "block_type": "Control", "block_shape": "C-Block", "op_code": "control_repeat",
        "functionality": "Repeats the blocks inside it a specified number of times.",
        "inputs": {"TIMES": [1, [6, "10"]], "SUBSTACK": [2, None]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "control_forever": {
        "block_name": "forever", "block_type": "Control", "block_shape": "C-Block", "op_code": "control_forever",
        "functionality": "Continuously runs the blocks inside it.",
        "inputs": {"SUBSTACK": [2, None]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "control_if": {
        "block_name": "if <> then", "block_type": "Control", "block_shape": "C-Block", "op_code": "control_if",
        "functionality": "Executes the blocks inside it only if the specified boolean condition is true. [NOTE: it takes boolean blocks as input]",
        "inputs": {"CONDITION": [2, None], "SUBSTACK": [2, None]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "control_if_else": {
        "block_name": "if <> then else", "block_type": "Control", "block_shape": "C-Block", "op_code": "control_if_else",
        "functionality": "Executes one set of blocks if the specified boolean condition is true, and a different set of blocks if the condition is false. [NOTE: it takes boolean blocks as input]",
        "inputs": {"CONDITION": [2, None], "SUBSTACK": [2, None], "SUBSTACK2": [2, None]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "control_wait_until": {
        "block_name": "wait until <>", "block_type": "Control", "block_shape": "Stack Block", "op_code": "control_wait_until",
        "functionality": "Pauses the script until the specified boolean condition becomes true. [NOTE: it takes boolean blocks as input]",
        "inputs": {"CONDITION": [2, None]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "control_repeat_until": {
        "block_name": "repeat until <>", "block_type": "Control", "block_shape": "C-Block", "op_code": "control_repeat_until",
        "functionality": "Repeats the blocks inside it until the specified boolean condition becomes true. [NOTE: it takes boolean blocks as input]",
        "inputs": {"CONDITION": [2, None], "SUBSTACK": [2, None]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "control_stop": {
        "block_name": "stop [v]", "block_type": "Control", "block_shape": "Cap Block", "op_code": "control_stop",
        "functionality": "Halts all scripts, only the current script, or other scripts within the same sprite. Its shape can dynamically change based on the selected option.",
        "inputs": {}, "fields": {"STOP_OPTION": ["all", None]}, "shadow": False, "topLevel": True, "mutation": {"tagName": "mutation", "children": [], "hasnext": "false"}
    },
    "control_start_as_clone": {
        "block_name": "When I Start as a Clone", "block_type": "Control", "block_shape": "Hat Block", "op_code": "control_start_as_clone",
        "functionality": "This Hat block initiates the script when a clone of the sprite is created. It defines the behavior of individual clones.",
        "inputs": {}, "fields": {}, "shadow": False, "topLevel": True
    },
    "control_create_clone_of": {
        "block_name": "create clone of ()", "block_type": "Control", "block_shape": "Stack Block", "op_code": "control_create_clone_of",
        "functionality": "Generates a copy, or clone, of a specified sprite (or 'myself' for the current sprite).",
        "inputs": {"CLONE_OPTION": [1, "control_create_clone_of_menu"]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "control_create_clone_of_menu": {
        "block_name": "create clone of menu", "block_type": "Control", "block_shape": "Reporter Block", "op_code": "control_create_clone_of_menu",
        "functionality": "Menu for create clone of block.",
        "inputs": {}, "fields": {"CLONE_OPTION": ["_myself_", None]}, "shadow": True, "topLevel": False
    },
    "control_delete_this_clone": {
        "block_name": "delete this clone", "block_type": "Control", "block_shape": "Cap Block", "op_code": "control_delete_this_clone",
        "functionality": "Removes the clone that is executing it from the stage.",
        "inputs":None, "fields": {}, "shadow": False, "topLevel": True
    },

    # data_block.json
    "data_setvariableto": {
        "block_name": "set [my variable v] to ()", "block_type": "Data", "block_shape": "Stack Block", "op_code": "data_setvariableto",
        "functionality": "Assigns a specific value (number, string, or boolean) to a variable.",
        "inputs": {"VALUE": [1, [10, "0"]]}, "fields": {"VARIABLE": ["my variable", "`jEk@4|i[#Fk?(8x)AV.-my variable"]}, "shadow": False, "topLevel": True
    },
    "data_changevariableby": {
        "block_name": "change [my variable v] by ()", "block_type": "Data", "block_shape": "Stack Block", "op_code": "data_changevariableby",
        "functionality": "Increases or decreases a variable's numerical value by a specified amount.",
        "inputs": {"VALUE": [1, [4, "1"]]}, "fields": {"VARIABLE": ["my variable", "`jEk@4|i[#Fk?(8x)AV.-my variable"]}, "shadow": False, "topLevel": True
    },
    "data_showvariable": {
        "block_name": "show variable [my variable v]", "block_type": "Data", "block_shape": "Stack Block", "op_code": "data_showvariable",
        "functionality": "Makes a variable's monitor visible on the stage.",
        "inputs": {}, "fields": {"VARIABLE": ["my variable", "`jEk@4|i[#Fk?(8x)AV.-my variable"]}, "shadow": False, "topLevel": True
    },
    "data_hidevariable": {
        "block_name": "hide variable [my variable v]", "block_type": "Data", "block_shape": "Stack Block", "op_code": "data_hidevariable",
        "functionality": "Hides a variable's monitor from the stage.",
        "inputs": {}, "fields": {"VARIABLE": ["my variable", "`jEk@4|i[#Fk?(8x)AV.-my variable"]}, "shadow": False, "topLevel": True
    },
    "data_addtolist": {
        "block_name": "add () to [my list v]", "block_type": "Data", "block_shape": "Stack Block", "op_code": "data_addtolist",
        "functionality": "Appends an item to the end of a list.",
        "inputs": {"ITEM": [1, [10, "thing"]]}, "fields": {"LIST": ["MY_LIST", "o6`kIhtT{xWH+rX(5d,A"]}, "shadow": False, "topLevel": True
    },
    "data_deleteoflist": {
        "block_name": "delete () of [my list v]", "block_type": "Data", "block_shape": "Stack Block", "op_code": "data_deleteoflist",
        "functionality": "Removes an item from a list by its index or by selecting 'all' items.",
        "inputs": {"INDEX": [1, [7, "1"]]}, "fields": {"LIST": ["MY_LIST", "o6`kIhtT{xWH+rX(5d,A"]}, "shadow": False, "topLevel": True
    },
    "data_deletealloflist": {
        "block_name": "delete all of [my list v]", "block_type": "Data", "block_shape": "Stack Block", "op_code": "data_deletealloflist",
        "functionality": "Removes all items from a list.",
        "inputs": {}, "fields": {"LIST": ["MY_LIST", "o6`kIhtT{xWH+rX(5d,A"]}, "shadow": False, "topLevel": True
    },
    "data_insertatlist": {
        "block_name": "insert () at () of [my list v]", "block_type": "Data", "block_shape": "Stack Block", "op_code": "data_insertatlist",
        "functionality": "Inserts an item at a specific position within a list.",
        "inputs": {"ITEM": [1, [10, "thing"]], "INDEX": [1, [7, "1"]]}, "fields": {"LIST": ["MY_LIST", "o6`kIhtT{xWH+rX(5d,A"]}, "shadow": False, "topLevel": True
    },
    "data_replaceitemoflist": {
        "block_name": "replace item () of [my list v] with ()", "block_type": "Data", "block_shape": "Stack Block", "op_code": "data_replaceitemoflist",
        "functionality": "Replaces an item at a specific position in a list with a new value.",
        "inputs": {"INDEX": [1, [7, "1"]], "ITEM": [1, [10, "thing"]]}, "fields": {"LIST": ["MY_LIST", "o6`kIhtT{xWH+rX(5d,A"]}, "shadow": False, "topLevel": True
    },
    "data_itemoflist": {
        "block_name": "(item (2) of [myList v])", "block_type": "Data", "block_shape": "Reporter Block", "op_code": "data_itemoflist",
        "functionality": "Reports the item located at a specific position in a list.",
        "inputs": {"INDEX": [1, [7, "1"]]}, "fields": {"LIST": ["MY_LIST", "o6`kIhtT{xWH+rX(5d,A"]}, "shadow": False, "topLevel": True
    },
    "data_itemnumoflist": {
        "block_name": "(item # of [Dog] in [myList v])", "block_type": "Data", "block_shape": "Reporter Block", "op_code": "data_itemnumoflist",
        "functionality": "Reports the index number of the first occurrence of a specified item in a list. If the item is not found, it reports 0.",
        "inputs": {"ITEM": [1, [10, "thing"]]}, "fields": {"LIST": ["MY_LIST", "o6`kIhtT{xWH+rX(5d,A"]}, "shadow": False, "topLevel": True
    },
    "data_lengthoflist": {
        "block_name": "(length of [myList v])", "block_type": "Data", "block_shape": "Reporter Block", "op_code": "data_lengthoflist",
        "functionality": "Provides the total number of items contained in a list.",
        "inputs": {}, "fields": {"LIST": ["MY_LIST", "o6`kIhtT{xWH+rX(5d,A"]}, "shadow": False, "topLevel": True
    },
    "data_listcontainsitem": {
        "block_name": "<[my list v] contains ()?>", "block_type": "Data", "block_shape": "Boolean Block", "op_code": "data_listcontainsitem",
        "functionality": "Checks if a list includes a specific item.",
        "inputs": {"ITEM": [1, [10, "thing"]]}, "fields": {"LIST": ["MY_LIST", "o6`kIhtT{xWH+rX(5d,A"]}, "shadow": False, "topLevel": True
    },
    "data_showlist": {
        "block_name": "show list [my list v]", "block_type": "Data", "block_shape": "Stack Block", "op_code": "data_showlist",
        "functionality": "Makes a list's monitor visible on the stage.",
        "inputs": {}, "fields": {"LIST": ["MY_LIST", "o6`kIhtT{xWH+rX(5d,A"]}, "shadow": False, "topLevel": True
    },
    "data_hidelist": {
        "block_name": "hide list [my list v]", "block_type": "Data", "block_shape": "Stack Block", "op_code": "data_hidelist",
        "functionality": "Hides a list's monitor from the stage.",
        "inputs": {}, "fields": {"LIST": ["MY_LIST", "o6`kIhtT{xWH+rX(5d,A"]}, "shadow": False, "topLevel": True
    },
    "data_variable": { # This is a reporter block for a variable's value
        "block_name": "[variable v]", "block_type": "Data", "block_shape": "Reporter Block", "op_code": "data_variable",
        "functionality": "Provides the current value stored in a variable.",
        "inputs": {}, "fields": {"VARIABLE": ["my variable", None]}, "shadow": True, "topLevel": False
    },

    # event_block.json
    "event_whenflagclicked": {
        "block_name": "when green flag pressed", "block_type": "Events", "op_code": "event_whenflagclicked", "block_shape": "Hat Block",
        "functionality": "This Hat block initiates the script when the green flag is clicked, serving as the common starting point for most Scratch projects.",
        "inputs": {}, "fields": {}, "shadow": False, "topLevel": True
    },
    "event_whenkeypressed": {
        "block_name": "when () key pressed", "block_type": "Events", "op_code": "event_whenkeypressed", "block_shape": "Hat Block",
        "functionality": "This Hat block initiates the script when a specified keyboard key is pressed.",
        "inputs": {}, "fields": {"KEY_OPTION": ["space", None]}, "shadow": False, "topLevel": True
    },
    "event_whenthisspriteclicked": {
        "block_name": "when this sprite clicked", "block_type": "Events", "op_code": "event_whenthisspriteclicked", "block_shape": "Hat Block",
        "functionality": "This Hat block starts the script when the sprite itself is clicked.",
        "inputs": {}, "fields": {}, "shadow": False, "topLevel": True
    },
    "event_whenbackdropswitchesto": {
        "block_name": "when backdrop switches to ()", "block_type": "Events", "op_code": "event_whenbackdropswitchesto", "block_shape": "Hat Block",
        "functionality": "This Hat block triggers the script when the stage backdrop changes to a specified backdrop.",
        "inputs": {}, "fields": {"BACKDROP": ["backdrop1", None]}, "shadow": False, "topLevel": True
    },
    "event_whengreaterthan": {
        "block_name": "when () > ()", "block_type": "Events", "op_code": "event_whengreaterthan", "block_shape": "Hat Block",
        "functionality": "This Hat block starts the script when a certain value (e.g., loudness from a microphone, or the timer) exceeds a defined threshold.",
        "inputs": {"VALUE": [1, [4, "10"]]}, "fields": {"WHENGREATERTHANMENU": ["LOUDNESS", None]}, "shadow": False, "topLevel": True
    },
    "event_whenbroadcastreceived": {
        "block_name": "when I receive ()", "block_type": "Events", "op_code": "event_whenbroadcastreceived", "block_shape": "Hat Block",
        "functionality": "This Hat block initiates the script upon the reception of a specific broadcast message. This mechanism facilitates indirect communication between sprites or the stage.",
        "inputs": {}, "fields": {"BROADCAST_OPTION": ["message1", "5O!nei;S$!c!=hCT}0:a"]}, "shadow": False, "topLevel": True
    },
    "event_broadcast": {
        "block_name": "broadcast ()", "block_type": "Events", "block_shape": "Stack Block", "op_code": "event_broadcast",
        "functionality": "Sends a broadcast message throughout the Scratch program, activating any 'when I receive ()' blocks that are set to listen for that message, enabling indirect communication.",
        "inputs": {"BROADCAST_INPUT": [1, [11, "message1", "5O!nei;S$!c!=hCT}0:a"]]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "event_broadcastandwait": {
        "block_name": "broadcast () and wait", "block_type": "Events", "block_shape": "Stack Block", "op_code": "event_broadcastandwait",
        "functionality": "Sends a broadcast message and pauses the current script until all other scripts activated by that broadcast have completed their execution, ensuring sequential coordination.",
        "inputs": {"BROADCAST_INPUT": [1, [11, "message1", "5O!nei;S$!c!=hCT}0:a"]]}, "fields": {}, "shadow": False, "topLevel": True
    },

    # looks_block.json
    "looks_sayforsecs": {
        "block_name": "say () for () seconds", "block_type": "Looks", "block_shape": "Stack Block", "op_code": "looks_sayforsecs",
        "functionality": "Displays a speech bubble containing specified text for a set duration.",
        "inputs": {"MESSAGE": [1, [10, "Hello!"]], "SECS": [1, [4, "2"]]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "looks_say": {
        "block_name": "say ()", "block_type": "Looks", "block_shape": "Stack Block", "op_code": "looks_say",
        "functionality": "Displays a speech bubble with the specified text indefinitely until another 'say' or 'think' block is activated.",
        "inputs": {"MESSAGE": [1, [10, "Hello!"]]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "looks_thinkforsecs": {
        "block_name": "think () for () seconds", "block_type": "Looks", "block_shape": "Stack Block", "op_code": "looks_thinkforsecs",
        "functionality": "Displays a thought bubble containing specified text for a set duration.",
        "inputs": {"MESSAGE": [1, [10, "Hmm..."]], "SECS": [1, [4, "2"]]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "looks_think": {
        "block_name": "think ()", "block_type": "Looks", "block_shape": "Stack Block", "op_code": "looks_think",
        "functionality": "Displays a thought bubble with the specified text indefinitely until another 'say' or 'think' block is activated.",
        "inputs": {"MESSAGE": [1, [10, "Hmm..."]]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "looks_switchcostumeto": {
        "block_name": "switch costume to ()", "block_type": "Looks", "block_shape": "Stack Block", "op_code": "looks_switchcostumeto",
        "functionality": "Alters the sprite's appearance to a designated costume.",
        "inputs": {"COSTUME": [1, "looks_costume"]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "looks_costume": {
        "block_name": "costume menu", "block_type": "Looks", "block_shape": "Reporter Block", "op_code": "looks_costume",
        "functionality": "Menu for switch costume to block.",
        "inputs": {}, "fields": {"COSTUME": ["costume1", None]}, "shadow": True, "topLevel": False
    },
    "looks_nextcostume": {
        "block_name": "next costume", "block_type": "Looks", "block_shape": "Stack Block", "op_code": "looks_nextcostume",
        "functionality": "Switches the sprite's costume to the next one in its costume list. If it's the last costume, it cycles back to the first.",
        "inputs": {}, "fields": {}, "shadow": False, "topLevel": True
    },
    "looks_switchbackdropto": {
        "block_name": "switch backdrop to ()", "block_type": "Looks", "block_shape": "Stack Block", "op_code": "looks_switchbackdropto",
        "functionality": "Changes the stage's backdrop to a specified backdrop.",
        "inputs": {"BACKDROP": [1, "looks_backdrops"]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "looks_backdrops": {
        "block_name": "backdrop menu", "block_type": "Looks", "block_shape": "Reporter Block", "op_code": "looks_backdrops",
        "functionality": "Menu for switch backdrop to block.",
        "inputs": {}, "fields": {"BACKDROP": ["backdrop1", None]}, "shadow": True, "topLevel": False
    },
    "looks_switchbackdroptowait": {
        "block_name": "switch backdrop to () and wait", "block_type": "Looks", "block_shape": "Stack Block", "op_code": "looks_switchbackdroptowait",
        "functionality": "Changes the stage's backdrop to a specified backdrop and pauses the script until any 'When backdrop switches to' scripts for that backdrop have finished.",
        "inputs": {"BACKDROP": [1, "looks_backdrops"]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "looks_nextbackdrop": {
        "block_name": "next backdrop", "block_type": "Looks", "block_shape": "Stack Block", "op_code": "looks_nextbackdrop",
        "functionality": "Switches the stage's backdrop to the next one in its backdrop list. If it's the last backdrop, it cycles back to the first.",
        "inputs": {}, "fields": {}, "shadow": False, "topLevel": True
    },
    "looks_changesizeby": {
        "block_name": "change size by ()", "block_type": "Looks", "block_shape": "Stack Block", "op_code": "looks_changesizeby",
        "functionality": "Changes the sprite's size by a specified percentage. Positive values make it larger, negative values make it smaller.",
        "inputs": {"CHANGE": [1, [4, "10"]]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "looks_setsizeto": {
        "block_name": "set size to () %", "block_type": "Looks", "block_shape": "Stack Block", "op_code": "looks_setsizeto",
        "functionality": "Sets the sprite's size to a specific percentage of its original size.",
        "inputs": {"SIZE": [1, [4, "100"]]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "looks_changeeffectby": {
        "block_name": "change () effect by ()", "block_type": "Looks", "block_shape": "Stack Block", "op_code": "looks_changeeffectby",
        "functionality": "Changes a visual effect on the sprite by a specified amount (e.g., color, fisheye, whirl, pixelate, mosaic, brightness, ghost).",
        "inputs": {"CHANGE": [1, [4, "25"]]}, "fields": {"EFFECT": ["COLOR", None]}, "shadow": False, "topLevel": True
    },
    "looks_seteffectto": {
        "block_name": "set () effect to ()", "block_type": "Looks", "block_shape": "Stack Block", "op_code": "looks_seteffectto",
        "functionality": "Sets a visual effect on the sprite to a specific value.",
        "inputs": {"VALUE": [1, [4, "0"]]}, "fields": {"EFFECT": ["COLOR", None]}, "shadow": False, "topLevel": True
    },
    "looks_cleargraphiceffects": {
        "block_name": "clear graphic effects", "block_type": "Looks", "block_shape": "Stack Block", "op_code": "looks_cleargraphiceffects",
        "functionality": "Removes all visual effects applied to the sprite.",
        "inputs": {}, "fields": {}, "shadow": False, "topLevel": True
    },
    "looks_show": {
        "block_name": "show", "block_type": "Looks", "block_shape": "Stack Block", "op_code": "looks_show",
        "functionality": "Makes the sprite visible on the stage.",
        "inputs": {}, "fields": {}, "shadow": False, "topLevel": True
    },
    "looks_hide": {
        "block_name": "hide", "block_type": "Looks", "block_shape": "Stack Block", "op_code": "looks_hide",
        "functionality": "Makes the sprite invisible on the stage.",
        "inputs": {}, "fields": {}, "shadow": False, "topLevel": True
    },
    "looks_gotofrontback": {
        "block_name": "go to () layer", "block_type": "Looks", "block_shape": "Stack Block", "op_code": "looks_gotofrontback",
        "functionality": "Moves the sprite to the front-most or back-most layer of other sprites on the stage.",
        "inputs": {}, "fields": {"FRONT_BACK": ["front", None]}, "shadow": False, "topLevel": True
    },
    "looks_goforwardbackwardlayers": {
        "block_name": "go () layers", "block_type": "Looks", "block_shape": "Stack Block", "op_code": "looks_goforwardbackwardlayers",
        "functionality": "Moves the sprite forward or backward a specified number of layers in relation to other sprites.",
        "inputs": {"NUM": [1, [7, "1"]]}, "fields": {"FORWARD_BACKWARD": ["forward", None]}, "shadow": False, "topLevel": True
    },
    "looks_costumenumbername": {
        "block_name": "(costume ())", "block_type": "Looks", "block_shape": "Reporter Block", "op_code": "looks_costumenumbername",
        "functionality": "Reports the current costume's number or name.",
        "inputs": {}, "fields": {"NUMBER_NAME": ["number", None]}, "shadow": False, "topLevel": True
    },
    "looks_backdropnumbername": {
        "block_name": "(backdrop ())", "block_type": "Looks", "block_shape": "Reporter Block", "op_code": "looks_backdropnumbername",
        "functionality": "Reports the current backdrop's number or name.",
        "inputs": {}, "fields": {"NUMBER_NAME": ["number", None]}, "shadow": False, "topLevel": True
    },
    "looks_size": {
        "block_name": "(size)", "block_type": "Looks", "block_shape": "Reporter Block", "op_code": "looks_size",
        "functionality": "Reports the current size of the sprite as a percentage.",
        "inputs": {}, "fields": {}, "shadow": False, "topLevel": True
    },

    # operator_block.json
    "operator_add": {
        "block_name": "(() + ())", "block_type": "operator", "block_shape": "Reporter Block", "op_code": "operator_add",
        "functionality": "Adds two numerical values.",
        "inputs": {"NUM1": [1, [4, ""]], "NUM2": [1, [4, ""]]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "operator_subtract": {
        "block_name": "(() - ())", "block_type": "operator", "block_shape": "Reporter Block", "op_code": "operator_subtract",
        "functionality": "Subtracts the second numerical value from the first.",
        "inputs": {"NUM1": [1, [4, ""]], "NUM2": [1, [4, ""]]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "operator_multiply": {
        "block_name": "(() * ())", "block_type": "operator", "block_shape": "Reporter Block", "op_code": "operator_multiply",
        "functionality": "Multiplies two numerical values.",
        "inputs": {"NUM1": [1, [4, ""]], "NUM2": [1, [4, ""]]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "operator_divide": {
        "block_name": "(() / ())", "block_type": "operator", "block_shape": "Reporter Block", "op_code": "operator_divide",
        "functionality": "Divides the first numerical value by the second.",
        "inputs": {"NUM1": [1, [4, ""]], "NUM2": [1, [4, ""]]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "operator_random": {
        "block_name": "(pick random () to ())", "block_type": "operator", "block_shape": "Reporter Block", "op_code": "operator_random",
        "functionality": "Generates a random integer within a specified inclusive range.",
        "inputs": {"FROM": [1, [4, "1"]], "TO": [1, [4, "10"]]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "operator_gt": {
        "block_name": "<() > ()>", "block_type": "operator", "block_shape": "Boolean Block", "op_code": "operator_gt",
        "functionality": "Checks if the first value is greater than the second.",
        "inputs": {"OPERAND1": [1, [10, ""]], "OPERAND2": [1, [10, "50"]]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "operator_lt": {
        "block_name": "<() < ()>", "block_type": "operator", "block_shape": "Boolean Block", "op_code": "operator_lt",
        "functionality": "Checks if the first value is less than the second.",
        "inputs": {"OPERAND1": [1, [10, ""]], "OPERAND2": [1, [10, "50"]]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "operator_equals": {
        "block_name": "<() = ()>", "block_type": "operator", "block_shape": "Boolean Block", "op_code": "operator_equals",
        "functionality": "Checks if two values are equal.",
        "inputs": {"OPERAND1": [1, [10, ""]], "OPERAND2": [1, [10, "50"]]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "operator_and": {
        "block_name": "<<> and <>>", "block_type": "operator", "block_shape": "Boolean Block", "op_code": "operator_and",
        "functionality": "Returns 'true' if both provided Boolean conditions are 'true'.",
        "inputs": {"OPERAND1": [2, None], "OPERAND2": [2, None]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "operator_or": {
        "block_name": "<<> or <>>", "block_type": "operator", "block_shape": "Boolean Block", "op_code": "operator_or",
        "functionality": "Returns 'true' if at least one of the provided Boolean conditions is 'true'.",
        "inputs": {"OPERAND1": [2, None], "OPERAND2": [2, None]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "operator_not": {
        "block_name": "<not <>>", "block_type": "operator", "block_shape": "Boolean Block", "op_code": "operator_not",
        "functionality": "Returns 'true' if the provided Boolean condition is 'false', and 'false' if it is 'true'.",
        "inputs": {"OPERAND": [2, None]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "operator_join": {
        "block_name": "(join ()())", "block_type": "operator", "block_shape": "Reporter Block", "op_code": "operator_join",
        "functionality": "Concatenates two strings or values into a single string.",
        "inputs": {"STRING1": [1, [10, "apple "]], "STRING2": [1, [10, "banana"]]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "operator_letterof": {
        "block_name": "letter () of ()", "block_type": "operator", "block_shape": "Reporter Block", "op_code": "operator_letterof",
        "functionality": "Reports the character at a specific numerical position within a string.",
        "inputs": {"LETTER": [1, [6, "1"]], "STRING": [1, [10, "apple"]]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "operator_length": {
        "block_name": "(length of ())", "block_type": "operator", "block_shape": "Reporter Block", "op_code": "operator_length",
        "functionality": "Reports the total number of characters in a given string.",
        "inputs": {"STRING": [1, [10, "apple"]]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "operator_contains": {
        "block_name": "<() contains ()?>", "block_type": "operator", "block_shape": "Boolean Block", "op_code": "operator_contains",
        "functionality": "Checks if one string contains another string.",
        "inputs": {"STRING1": [1, [10, "apple"]], "STRING2": [1, [10, "a"]]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "operator_mod": {
        "block_name": "(() mod ())", "block_type": "operator", "block_shape": "Reporter Block", "op_code": "operator_mod",
        "functionality": "Reports the remainder when the first number is divided by the second.",
        "inputs": {"NUM1": [1, [4, ""]], "NUM2": [1, [4, ""]]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "operator_round": {
        "block_name": "(round ())", "block_type": "operator", "block_shape": "Reporter Block", "op_code": "operator_round",
        "functionality": "Rounds a numerical value to the nearest integer.",
        "inputs": {"NUM": [1, [4, ""]]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "operator_mathop": {
        "block_name": "(() of ())", "block_type": "operator", "block_shape": "Reporter Block", "op_code": "operator_mathop",
        "functionality": "Performs various mathematical functions (e.g., absolute value, square root, trigonometric functions).",
        "inputs": {"NUM": [1, [4, ""]]}, "fields": {"OPERATOR": ["abs", None]}, "shadow": False, "topLevel": True
    },

    # sensing_block.json
    "sensing_touchingobject": {
        "block_name": "<touching [edge v]?>", "block_type": "Sensing", "op_code": "sensing_touchingobject", "block_shape": "Boolean Block",
        "functionality": "Checks if its sprite is touching the mouse-pointer, edge, or another specified sprite.",
        "inputs": {"TOUCHINGOBJECTMENU": [1, "sensing_touchingobjectmenu"]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "sensing_touchingobjectmenu": {
        "block_name": "touching object menu", "block_type": "Sensing", "block_shape": "Reporter Block", "op_code": "sensing_touchingobjectmenu",
        "functionality": "Menu for touching object block.",
        "inputs": {}, "fields": {"TOUCHINGOBJECTMENU": ["_mouse_", None]}, "shadow": True, "topLevel": False
    },
    "sensing_touchingcolor": {
        "block_name": "<touching color ()?>", "block_type": "Sensing", "op_code": "sensing_touchingcolor", "block_shape": "Boolean Block",
        "functionality": "Checks whether its sprite is touching a specified color.",
        "inputs": {"COLOR": [1, [9, "#55b888"]]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "sensing_coloristouchingcolor": {
        "block_name": "<color () is touching ()?>", "block_type": "Sensing", "op_code": "sensing_coloristouchingcolor", "block_shape": "Boolean Block",
        "functionality": "Checks whether a specific color on its sprite is touching another specified color on the stage or another sprite.",
        "inputs": {"COLOR1": [1, [9, "#d019f2"]], "COLOR2": [1, [9, "#2b0de3"]]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "sensing_askandwait": {
        "block_name": "Ask () and Wait", "block_type": "Sensing", "block_shape": "Stack Block", "op_code": "sensing_askandwait",
        "functionality": "Displays an input box with specified text at the bottom of the screen, allowing users to input text, which is stored in the 'Answer' block.",
        "inputs": {"QUESTION": [1, [10, "What's your name?"]]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "sensing_answer": {
        "block_name": "(answer)", "block_type": "Sensing", "block_shape": "Reporter Block", "op_code": "sensing_answer",
        "functionality": "Holds the most recent text inputted using the 'Ask () and Wait' block.",
        "inputs": {}, "fields": {}, "shadow": False, "topLevel": True
    },
    "sensing_keypressed": {
        "block_name": "<key () pressed?>", "block_type": "Sensing", "op_code": "sensing_keypressed", "block_shape": "Boolean Block",
        "functionality": "Checks if a specified keyboard key is currently being pressed.",
        "inputs": {"KEY_OPTION": [1, "sensing_keyoptions"]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "sensing_keyoptions": {
        "block_name": "key options menu", "block_type": "Sensing", "block_shape": "Reporter Block", "op_code": "sensing_keyoptions",
        "functionality": "Menu for key pressed block.",
        "inputs": {}, "fields": {"KEY_OPTION": ["space", None]}, "shadow": True, "topLevel": False
    },
    "sensing_mousedown": {
        "block_name": "<mouse down?>", "block_type": "Sensing", "op_code": "sensing_mousedown", "block_shape": "Boolean Block",
        "functionality": "Checks if the computer mouse's primary button is being clicked while the cursor is over the stage.",
        "inputs": {}, "fields": {}, "shadow": False, "topLevel": True
    },
    "sensing_mousex": {
        "block_name": "(mouse x)", "block_type": "Sensing", "block_shape": "Reporter Block", "op_code": "sensing_mousex",
        "functionality": "Reports the mouse-pointer’s current X position on the stage.",
        "inputs": {}, "fields": {}, "shadow": False, "topLevel": True
    },
    "sensing_mousey": {
        "block_name": "(mouse y)", "block_type": "Sensing", "block_shape": "Reporter Block", "op_code": "sensing_mousey",
        "functionality": "Reports the mouse-pointer’s current Y position on the stage.",
        "inputs": {}, "fields": {}, "shadow": False, "topLevel": True
    },
    "sensing_setdragmode": {
        "block_name": "set drag mode [draggable v]", "block_type": "Sensing", "block_shape": "Stack Block", "op_code": "sensing_setdragmode",
        "functionality": "Sets whether the sprite can be dragged by the mouse on the stage.",
        "inputs": {}, "fields": {"DRAG_MODE": ["draggable", None]}, "shadow": False, "topLevel": True
    },
    "sensing_loudness": {
        "block_name": "(loudness)", "block_type": "Sensing", "block_shape": "Reporter Block", "op_code": "sensing_loudness",
        "functionality": "Reports the loudness of noise received by a microphone on a scale of 0 to 100.",
        "inputs": {}, "fields": {}, "shadow": False, "topLevel": True
    },
    "sensing_timer": {
        "block_name": "(timer)", "block_type": "Sensing", "block_shape": "Reporter Block", "op_code": "sensing_timer",
        "functionality": "Reports the elapsed time since Scratch was launched or the timer was reset, increasing by 1 every second.",
        "inputs": {}, "fields": {}, "shadow": False, "topLevel": True
    },
    "sensing_resettimer": {
        "block_name": "Reset Timer", "block_type": "Sensing", "block_shape": "Stack Block", "op_code": "sensing_resettimer",
        "functionality": "Sets the timer’s value back to 0.0.",
        "inputs": {}, "fields": {}, "shadow": False, "topLevel": True
    },
    "sensing_of": {
        "block_name": "(() of ())", "block_type": "Sensing", "block_shape": "Reporter Block", "op_code": "sensing_of",
        "functionality": "Reports a specified value (e.g., x position, direction, costume number) of a specified sprite or the Stage to be accessed in current sprite or stage.",
        "inputs": {"OBJECT": [1, "sensing_of_object_menu"]}, "fields": {"PROPERTY": ["backdrop #", None]}, "shadow": False, "topLevel": True
    },
    "sensing_of_object_menu": {
        "block_name": "of object menu", "block_type": "Sensing", "block_shape": "Reporter Block", "op_code": "sensing_of_object_menu",
        "functionality": "Menu for of block.",
        "inputs": {}, "fields": {"OBJECT": ["_stage_", None]}, "shadow": True, "topLevel": False
    },
    "sensing_current": {
        "block_name": "(current ())", "block_type": "Sensing", "block_shape": "Reporter Block", "op_code": "sensing_current",
        "functionality": "Reports the current local year, month, date, day of the week, hour, minutes, or seconds.",
        "inputs": {}, "fields": {"CURRENTMENU": ["YEAR", None]}, "shadow": False, "topLevel": True
    },
    "sensing_dayssince2000": {
        "block_name": "(days since 2000)", "block_type": "Sensing", "block_shape": "Reporter Block", "op_code": "sensing_dayssince2000",
        "functionality": "Reports the number of days (and fractions of a day) since 00:00:00 UTC on January 1, 2000.",
        "inputs": {}, "fields": {}, "shadow": False, "topLevel": True
    },
    "sensing_username": {
        "block_name": "(username)", "block_type": "Sensing", "block_shape": "Reporter Block", "op_code": "sensing_username",
        "functionality": "Reports the username of the user currently logged into Scratch. If no user is logged in, it reports nothing.",
        "inputs": {}, "fields": {}, "shadow": False, "topLevel": True
    },

    # sound_block.json
    "sound_playuntildone": {
        "block_name": "play sound () until done", "block_type": "Sound", "block_shape": "Stack Block", "op_code": "sound_playuntildone",
        "functionality": "Plays a specified sound and pauses the script's execution until the sound has completed.",
        "inputs": {"SOUND_MENU": [1, "sound_sounds_menu"]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "sound_sounds_menu": {
        "block_name": "sound menu", "block_type": "Sound", "block_shape": "Reporter Block", "op_code": "sound_sounds_menu",
        "functionality": "Menu for sound blocks.",
        "inputs": {}, "fields": {"SOUND_MENU": ["Meow", None]}, "shadow": True, "topLevel": False
    },
    "sound_play": {
        "block_name": "start sound ()", "block_type": "Sound", "block_shape": "Stack Block", "op_code": "sound_play",
        "functionality": "Initiates playback of a specified sound without pausing the script, allowing other actions to proceed concurrently.",
        "inputs": {"SOUND_MENU": [1, "sound_sounds_menu"]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "sound_stopallsounds": {
        "block_name": "stop all sounds", "block_type": "Sound", "block_shape": "Stack Block", "op_code": "sound_stopallsounds",
        "functionality": "Stops all currently playing sounds.",
        "inputs": {}, "fields": {}, "shadow": False, "topLevel": True
    },
    "sound_changeeffectby": {
        "block_name": "change () effect by ()", "block_type": "Sound", "block_shape": "Stack Block", "op_code": "sound_changeeffectby",
        "functionality": "Changes the project's sound effect by a specified amount.",
        "inputs": {"VALUE": [1, [4, "10"]]}, "fields": {"EFFECT": ["PITCH", None]}, "shadow": False, "topLevel": True
    },
    "sound_seteffectto": {
        "block_name": "set () effect to ()", "block_type": "Sound", "block_shape": "Stack Block", "op_code": "sound_seteffectto",
        "functionality": "Sets the sound effect to a specific value.",
        "inputs": {"VALUE": [1, [4, "100"]]}, "fields": {"EFFECT": ["PITCH", None]}, "shadow": False, "topLevel": True
    },
    "sound_cleareffects": {
        "block_name": "clear sound effects", "block_type": "Sound", "block_shape": "Stack Block", "op_code": "sound_cleareffects",
        "functionality": "Removes all sound effects applied to the sprite.",
        "inputs": {}, "fields": {}, "shadow": False, "topLevel": True
    },
    "sound_changevolumeby": {
        "block_name": "change volume by ()", "block_type": "Sound", "block_shape": "Stack Block", "op_code": "sound_changevolumeby",
        "functionality": "Changes the project's sound volume by a specified amount.",
        "inputs": {"VOLUME": [1, [4, "-10"]]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "sound_setvolumeto": {
        "block_name": "set volume to () %", "block_type": "Sound", "block_shape": "Stack Block", "op_code": "sound_setvolumeto",
        "functionality": "Sets the sound volume to a specific percentage (0-100).",
        "inputs": {"VOLUME": [1, [4, "100"]]}, "fields": {}, "shadow": False, "topLevel": True
    },
    "sound_volume": {
        "block_name": "(volume)", "block_type": "Sound", "block_shape": "Reporter Block", "op_code": "sound_volume",
        "functionality": "Reports the current volume level of the sprite.",
        "inputs": {}, "fields": {}, "shadow": False, "topLevel": True
    },
}

# Example input with opcodes for the initial generation
initial_opcode_counts = [
    {"opcode":"event_whenflagclicked","count":1},
    {"opcode":"motion_gotoxy","count":1},
    {"opcode":"motion_glidesecstoxy","count":1},
    {"opcode":"motion_xposition","count":1},
    {"opcode":"motion_setx","count":1},
    {"opcode":"control_forever","count":1},
    {"opcode":"control_if","count":1},
    {"opcode":"control_stop","count":1},
    {"opcode":"operator_lt","count":1},
    {"opcode":"sensing_touchingobject","count":1}, # Changed from sensing_istouching
    {"opcode":"sensing_touchingobjectmenu","count":1},
    {"opcode":"event_broadcast","count":1},
    {"opcode":"data_setvariableto","count":2},
    {"opcode":"data_showvariable","count":2},
]

# Generate the initial blocks and get the opcode_occurrences
generated_output_json, initial_opcode_occurrences = generate_blocks_from_opcodes(initial_opcode_counts, all_block_definitions)

# Pseudo-code to interpret
pseudo_code_input = """

when green flag clicked

  go to x: (240) y: (-135)

  set [score v] to (1)

  set [speed v] to (1)

  show variable [score v]

  show variable [speed v]

  forever

    glide (2) seconds to x: (-240) y: (-135)

    if <((x position)) < (-235)> then

      set x to (240)

    end

    if <touching [Sprite1 v]?> then

      broadcast [Game Over v]

      stop [all v]

    end

  end

end

"""

# Interpret the pseudo-code and update the blocks, passing opcode_occurrences
final_generated_blocks = interpret_pseudo_code_and_update_blocks(generated_output_json, pseudo_code_input, all_block_definitions, initial_opcode_occurrences)

print(initial_opcode_occurrences)

#print(json.dumps(final_generated_blocks, indent=2))
# ```json
# {
#   "event_whenflagclicked_1": {
#     "block_name": "when green flag pressed",
#     "block_type": "Events",
#     "op_code": "event_whenflagclicked",
#     "block_shape": "Hat Block",
#     "functionality": "This Hat block initiates the script when the green flag is clicked, serving as the common starting point for most Scratch projects.",
#     "inputs": {},
#     "fields": {},
#     "shadow": false,
#     "topLevel": true,
#     "parent": null,
#     "next": "motion_gotoxy_1"
#   },
#   "motion_gotoxy_1": {
#     "block_name": "go to x: () y: ()",
#     "block_type": "Motion",
#     "op_code": "motion_gotoxy",
#     "functionality": "Moves the sprite to the specified X and Y coordinates on the stage.",
#     "inputs": {
#       "X": [
#         4,
#         "240"
#       ],
#       "Y": [
#         4,
#         "-135"
#       ]
#     },
#     "fields": {},
#     "shadow": false,
#     "topLevel": false,
#     "parent": null,
#     "next": "data_setvariableto_1"
#   },
#   "motion_glidesecstoxy_1": {
#     "block_name": "glide () secs to x: () y: ()",
#     "block_type": "Motion",
#     "op_code": "motion_glidesecstoxy",
#     "functionality": "Glides the sprite smoothly to the specified X and Y coordinates over a given number of seconds.",
#     "inputs": {
#       "SECS": [
#         4,
#         "2"
#       ],
#       "X": [
#         4,
#         "-240"
#       ],
#       "Y": [
#         4,
#         "-135"
#       ]
#     },
#     "fields": {},
#     "shadow": false,
#     "topLevel": false,
#     "parent": "control_forever_1",
#     "next": "control_if_1"
#   },
#   "motion_xposition_1": {
#     "block_name": "(x position)",
#     "block_type": "Motion",
#     "op_code": "motion_xposition",
#     "functionality": "Reports the current X-coordinate of the sprite.[NOTE: not used in stage/backdrops]",
#     "inputs": {},
#     "fields": {},
#     "shadow": true,
#     "topLevel": false,
#     "parent": "operator_lt_1",
#     "next": null
#   },
#   "motion_setx_1": {
#     "block_name": "set x to ()",
#     "block_type": "Motion",
#     "op_code": "motion_setx",
#     "functionality": "Sets the sprite's X-coordinate to a specific value, placing it at a precise horizontal position.",
#     "inputs": {
#       "X": [
#         4,
#         "240"
#       ]
#     },
#     "fields": {},
#     "shadow": false,
#     "topLevel": false,
#     "parent": "control_if_1",
#     "next": null
#   },
#   "control_forever_1": {
#     "block_name": "forever",
#     "block_type": "Control",
#     "op_code": "control_forever",
#     "functionality": "Continuously runs the blocks inside it.",
#     "inputs": {
#       "SUBSTACK": [
#         2,
#         "motion_glidesecstoxy_1"
#       ]
#     },
#     "fields": {},
#     "shadow": false,
#     "topLevel": false,
#     "parent": null,
#     "next": null
#   },
#   "control_if_1": {
#     "block_name": "if <> then",
#     "block_type": "Control",
#     "op_code": "control_if",
#     "functionality": "Executes the blocks inside it only if the specified boolean condition is true. [NOTE: it takes boolean blocks as input]",
#     "inputs": {
#       "CONDITION": [
#         2,
#         "operator_lt_1"
#       ],
#       "SUBSTACK": [
#         2,
#         "motion_setx_1"
#       ]
#     },
#     "fields": {},
#     "shadow": false,
#     "topLevel": false,
#     "parent": "control_forever_1",
#     "next": "control_if_2"
#   },
#   "control_stop_1": {
#     "block_name": "stop [v]",
#     "block_type": "Control",
#     "op_code": "control_stop",
#     "functionality": "Halts all scripts, only the current script, or other scripts within the same sprite. Its shape can dynamically change based on the selected option.",
#     "inputs": {},
#     "fields": {
#       "STOP_OPTION": [
#         "all",
#         null
#       ]
#     },
#     "shadow": false,
#     "topLevel": false,
#     "parent": "control_if_2",
#     "mutation": {
#       "tagName": "mutation",
#       "children": [],
#       "hasnext": "false"
#     },
#     "next": null
#   },
#   "operator_lt_1": {
#     "block_name": "<() < ()>",
#     "block_type": "operator",
#     "op_code": "operator_lt",
#     "functionality": "Checks if the first value is less than the second.",
#     "inputs": {
#       "OPERAND1": [
#         1,
#         "motion_xposition_1"
#       ],
#       "OPERAND2": [
#         4,
#         "-235"
#       ]
#     },
#     "fields": {},
#     "shadow": true,
#     "topLevel": false,
#     "parent": "control_if_1",
#     "next": null
#   },
#   "sensing_touchingobject_1": {
#     "block_name": "<touching [edge v]?>",
#     "block_type": "Sensing",
#     "op_code": "sensing_touchingobject",
#     "functionality": "Checks if its sprite is touching the mouse-pointer, edge, or another specified sprite.",
#     "inputs": {
#       "TOUCHINGOBJECTMENU": [
#         1,
#         "sensing_touchingobjectmenu_1"
#       ]
#     },
#     "fields": {},
#     "shadow": true,
#     "topLevel": false,
#     "parent": "control_if_2",
#     "next": null
#   },
#   "sensing_touchingobjectmenu_1": {
#     "block_name": "touching object menu",
#     "block_type": "Sensing",
#     "op_code": "sensing_touchingobjectmenu",
#     "functionality": "Menu for touching object block.",
#     "inputs": {},
#     "fields": {
#       "TOUCHINGOBJECTMENU": [
#         "Sprite1",
#         null
#       ]
#     },
#     "shadow": true,
#     "topLevel": false,
#     "parent": "sensing_touchingobject_1",
#     "next": null
#   },
#   "event_broadcast_1": {
#     "block_name": "broadcast ()",
#     "block_type": "Events",
#     "op_code": "event_broadcast",
#     "functionality": "Sends a broadcast message throughout the Scratch program, activating any 'when I receive ()' blocks that are set to listen for that message, enabling indirect communication.",
#     "inputs": {
#       "BROADCAST_INPUT": [
#         10,
#         "Game Over"
#       ]
#     },
#     "fields": {},
#     "shadow": false,
#     "topLevel": false,
#     "parent": "control_if_2",
#     "next": "control_stop_1"
#   },
#   "data_setvariableto_1": {
#     "block_name": "set [my variable v] to ()",
#     "block_type": "Data",
#     "op_code": "data_setvariableto",
#     "functionality": "Assigns a specific value (number, string, or boolean) to a variable.",
#     "inputs": {
#       "VALUE": [
#         4,
#         "1"
#       ]
#     },
#     "fields": {
#       "VARIABLE": [
#         "score",
#         "`var_score"
#       ]
#     },
#     "shadow": false,
#     "topLevel": false,
#     "parent": null,
#     "next": "data_setvariableto_2"
#   },
#   "data_setvariableto_2": {
#     "block_name": "set [my variable v] to ()",
#     "block_type": "Data",
#     "op_code": "data_setvariableto",
#     "functionality": "Assigns a specific value (number, string, or boolean) to a variable.",
#     "inputs": {
#       "VALUE": [
#         4,
#         "1"
#       ]
#     },
#     "fields": {
#       "VARIABLE": [
#         "speed",
#         "`var_speed"
#       ]
#     },
#     "shadow": false,
#     "topLevel": false,
#     "parent": null,
#     "next": "data_showvariable_1"
#   },
#   "data_showvariable_1": {
#     "block_name": "show variable [my variable v]",
#     "block_type": "Data",
#     "op_code": "data_showvariable",
#     "functionality": "Makes a variable's monitor visible on the stage.",
#     "inputs": {},
#     "fields": {
#       "VARIABLE": [
#         "score",
#         "`var_score"
#       ]
#     },
#     "shadow": false,
#     "topLevel": false,
#     "parent": null,
#     "next": "data_showvariable_2"
#   },
#   "data_showvariable_2": {
#     "block_name": "show variable [my variable v]",
#     "block_type": "Data",
#     "op_code": "data_showvariable",
#     "functionality": "Makes a variable's monitor visible on the stage.",
#     "inputs": {},
#     "fields": {
#       "VARIABLE": [
#         "speed",
#         "`var_speed"
#       ]
#     },
#     "shadow": false,
#     "topLevel": false,
#     "parent": null,
#     "next": "control_forever_1"
#   },
#   "control_if_2": {
#     "block_name": "if <> then",
#     "block_type": "Control",
#     "op_code": "control_if",
#     "functionality": "Executes the blocks inside it only if the specified boolean condition is true. [NOTE: it takes boolean blocks as input]",
#     "inputs": {
#       "CONDITION": [
#         2,
#         "sensing_touchingobject_1"
#       ],
#       "SUBSTACK": [
#         2,
#         "event_broadcast_1"
#       ]
#     },
#     "fields": {},
#     "shadow": false,
#     "topLevel": false,
#     "parent": "control_forever_1",
#     "next": null
#   }
# }