-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathtest_parser.ml
More file actions
1050 lines (975 loc) · 32.5 KB
/
Copy pathtest_parser.ml
File metadata and controls
1050 lines (975 loc) · 32.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(*
* Copyright 2025 Multikernel Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*)
open Kernelscript.Ast
open Kernelscript.Parse
open Alcotest
(** Helper functions for creating AST nodes in tests *)
let dummy_loc = {
line = 1;
column = 1;
filename = "test";
}
let make_int_lit value = {
expr_desc = Literal (IntLit (Signed64 (Int64.of_int value), None));
expr_type = Some U32;
expr_pos = dummy_loc;
type_checked = false;
program_context = None;
map_scope = None;
}
let make_id name = {
expr_desc = Identifier name;
expr_type = None;
expr_pos = dummy_loc;
type_checked = false;
program_context = None;
map_scope = None;
}
let make_binop left op right = {
expr_desc = BinaryOp (left, op, right);
expr_type = None;
expr_pos = dummy_loc;
type_checked = false;
program_context = None;
map_scope = None;
}
let make_call name args = {
expr_desc = Call (make_id name, args);
expr_type = None;
expr_pos = dummy_loc;
type_checked = false;
program_context = None;
map_scope = None;
}
let make_array elements = {
expr_desc = Literal (ArrayLit (ExplicitArray (List.map (function
| {expr_desc = Literal lit; _} -> lit
| _ -> IntLit (Signed64 0L, None) (* fallback *)
) elements)));
expr_type = None;
expr_pos = dummy_loc;
type_checked = false;
program_context = None;
map_scope = None;
}
let make_decl name expr = {
stmt_desc = Declaration (name, None, Some expr);
stmt_pos = dummy_loc;
}
let make_for_stmt var start_expr end_expr body = {
stmt_desc = For (var, start_expr, end_expr, body);
stmt_pos = dummy_loc;
}
let make_for_iter_stmt index_var value_var expr body = {
stmt_desc = ForIter (index_var, value_var, expr, body);
stmt_pos = dummy_loc;
}
(** Helper function to parse string with builtin types loaded via symbol table *)
let parse_string_with_builtins code =
let ast = parse_string code in
(* Create symbol table with test builtin types *)
let symbol_table = Test_utils.Helpers.create_test_symbol_table ast in
(* Run type checking with builtin types loaded *)
let (typed_ast, _) = Kernelscript.Type_checker.type_check_and_annotate_ast ~symbol_table:(Some symbol_table) ast in
typed_ast
(** Helper function to test parsing statements *)
let test_parse_statements input expected =
let program_text = Printf.sprintf {|
@xdp fn test() -> u32 {
%s
return 0
}
|} input in
try
let ast = parse_string program_text in
match List.hd ast with
| AttributedFunction attr_func ->
let main_func = attr_func.attr_function in
let actual_stmts = List.rev (List.tl (List.rev main_func.func_body)) in
check int "statement count" (List.length expected) (List.length actual_stmts)
| _ -> fail "Expected attributed function declaration"
with
| e -> fail ("Failed to parse statements: " ^ Printexc.to_string e)
(** Test simple program parsing *)
let test_simple_program () =
let program_text = {|
@xdp fn test(ctx: *xdp_md) -> xdp_action {
return 2
}
|} in
try
let ast = parse_string program_text in
check int "AST length" 1 (List.length ast);
match List.hd ast with
| AttributedFunction attr_func ->
check string "function name" "test" attr_func.attr_function.func_name;
(* Check attribute is xdp *)
let has_xdp_attr = List.exists (function SimpleAttribute "xdp" -> true | _ -> false) attr_func.attr_list in
check bool "has xdp attribute" true has_xdp_attr
| _ -> fail "Expected attributed function declaration"
with
| _ -> fail "Failed to parse simple program"
(** Test expression parsing *)
let test_expression_parsing () =
let expressions = [
("42", true);
("x + y", true);
("func(a, b)", true);
("arr[index]", true);
("obj.field", true);
("(x + y) * z", true);
("!condition", true);
("-value", true);
] in
List.iter (fun (expr_text, should_succeed) ->
let program_text = Printf.sprintf {|
@xdp fn test() -> u32 {
var result = %s
return 0
}
|} expr_text in
try
let _ = parse_string program_text in
check bool ("expression parsing: " ^ expr_text) should_succeed true
with
| _ -> check bool ("expression parsing: " ^ expr_text) should_succeed false
) expressions
(** Test statement parsing *)
let test_statement_parsing () =
let statements = [
("var x = 42", true);
("var y: u32 = 100", true);
("x = 50", true);
("return x", true);
("return", true);
("if (true) { return 1 }", true);
("if (x > 0) { return 1 } else { return 0 }", true);
] in
List.iter (fun (stmt_text, should_succeed) ->
let program_text = Printf.sprintf {|
@xdp fn test() -> u32 {
%s
return 0
}
|} stmt_text in
try
let _ = parse_string program_text in
check bool ("statement parsing: " ^ stmt_text) should_succeed true
with
| _ -> check bool ("statement parsing: " ^ stmt_text) should_succeed false
) statements
(** Test function declaration parsing *)
let test_function_declaration () =
let program_text = {|
@helper
fn helper(x: u32, y: u32) -> u32 {
return x + y
}
@xdp fn test(ctx: *xdp_md) -> xdp_action {
var result = helper(10, 20)
return 2
}
|} in
try
let ast = parse_string program_text in
(* First item should be the helper attributed function *)
match List.hd ast with
| AttributedFunction attr_func ->
check string "helper function name" "helper" attr_func.attr_function.func_name;
check int "helper parameters" 2 (List.length attr_func.attr_function.func_params);
check bool "helper return type" true (attr_func.attr_function.func_return_type = Some (make_unnamed_return U32));
let has_helper_attr = List.exists (function
| SimpleAttribute "helper" -> true
| _ -> false
) attr_func.attr_list in
check bool "has helper attribute" true has_helper_attr
| _ -> fail "Expected attributed function declaration"
with
| _ -> fail "Failed to parse function declarations"
(** Test program type parsing *)
let test_program_types () =
let program_types = [
("xdp", Xdp);
("tc", Tc);
("probe", Probe Fprobe); (* @probe without offset defaults to fprobe *)
("tracepoint", Tracepoint);
] in
List.iter (fun (type_text, _expected_type) ->
let program_text = Printf.sprintf {|
@%s fn test() -> u32 {
return 0
}
|} type_text in
try
let ast = parse_string program_text in
match List.hd ast with
| AttributedFunction attr_func ->
let has_expected_attr = List.exists (function
| SimpleAttribute attr_name -> attr_name = type_text
| _ -> false
) attr_func.attr_list in
check bool ("program type: " ^ type_text) true has_expected_attr
| _ -> fail "Expected attributed function declaration"
with
| _ -> fail ("Failed to parse program type: " ^ type_text)
) program_types
(** Test BPF type parsing *)
let test_bpf_type_parsing () =
let types = [
("u8", U8);
("u32", U32);
("u64", U64);
("bool", Bool);
("char", Char);
] in
List.iter (fun (type_text, expected_type) ->
let program_text = Printf.sprintf {|
@xdp fn test() -> u32 {
var x: %s = 0
return 0
}
|} type_text in
try
let ast = parse_string program_text in
match List.hd ast with
| AttributedFunction attr_func ->
let main_func = attr_func.attr_function in
let decl_stmt = List.hd main_func.func_body in
(match decl_stmt.stmt_desc with
| Declaration (_, Some parsed_type, _) ->
check bool ("BPF type: " ^ type_text) true (parsed_type = expected_type)
| _ -> fail "Expected declaration statement")
| _ -> fail "Expected attributed function declaration"
with
| _ -> fail ("Failed to parse BPF type: " ^ type_text)
) types
(** Test control flow parsing *)
let test_control_flow_parsing () =
let program_text = {|
@xdp fn test(ctx: *xdp_md) -> xdp_action {
var x = 10
if (x > 5) {
x = x + 1
} else {
x = x - 1
}
while (x > 0) {
x = x - 1
}
return 2
}
|} in
try
let ast = parse_string program_text in
match List.hd ast with
| AttributedFunction attr_func ->
let main_func = attr_func.attr_function in
check bool "control flow statements" true (List.length main_func.func_body >= 4)
| _ -> fail "Expected attributed function declaration"
with
| _ -> fail "Failed to parse control flow"
(** Test error handling *)
let test_error_handling () =
let invalid_programs = [
"invalid syntax";
"@xdp fn test { }"; (* missing parameters and return type *)
"@xdp fn test() { }"; (* missing return type *)
"@xdp fn test() -> u32"; (* missing body *)
] in
List.iter (fun invalid_text ->
try
let _ = parse_string invalid_text in
fail ("Should have failed to parse: " ^ invalid_text)
with
| _ -> ()
) invalid_programs
(** Test operator precedence *)
let test_operator_precedence () =
let program_text = {|
@xdp fn test() -> u32 {
var result = 1 + 2 * 3
var comparison = x < y && a > b
var complex = (a + b) * c - d / e
return 0
}
|} in
try
let _ = parse_string program_text in
()
with
| _ -> fail "Failed to parse operator precedence"
(** Test complete program parsing *)
let test_complete_program_parsing () =
let program_text = {|
var packet_count : hash<u32, u64>(1024)
@helper
fn process_packet(src_ip: u32) -> u64 {
var count = packet_count[src_ip]
packet_count[src_ip] = count + 1
return count
}
@xdp fn packet_filter(ctx: *xdp_md) -> xdp_action {
var src_ip = 0x12345678
var count = process_packet(src_ip)
if (count > 100) {
return XDP_DROP
}
return XDP_PASS
}
|} in
try
let ast = parse_string_with_builtins program_text in
check int "complete program AST length" 3 (List.length ast);
(* Check global variable declaration with map type *)
(match List.hd ast with
| GlobalVarDecl global_var ->
check string "map variable name" "packet_count" global_var.global_var_name;
(match global_var.global_var_type with
| Some (Map (key_type, value_type, map_type, size)) ->
check bool "map key type" true (key_type = U32);
check bool "map value type" true (value_type = U64);
check bool "map type" true (map_type = Hash);
check int "map size" 1024 size
| _ -> fail "Expected map type")
| _ -> fail "Expected global variable declaration with map type");
(* Check helper function declaration *)
(match List.nth ast 1 with
| AttributedFunction attr_func ->
check string "helper function name" "process_packet" attr_func.attr_function.func_name;
check int "helper function parameters" 1 (List.length attr_func.attr_function.func_params);
check bool "helper function return type" true (attr_func.attr_function.func_return_type = Some (make_unnamed_return U64));
let has_helper_attr = List.exists (function
| SimpleAttribute "helper" -> true
| _ -> false
) attr_func.attr_list in
check bool "has helper attribute" true has_helper_attr
| _ -> fail "Expected helper attributed function declaration");
(* Check attributed function declaration *)
(match List.nth ast 2 with
| AttributedFunction attr_func ->
check string "function name" "packet_filter" attr_func.attr_function.func_name
| _ -> fail "Expected attributed function declaration")
with
| _ -> fail "Failed to parse complete program"
(** Test simple if statement without else *)
let test_simple_if () =
let program_text = {|
@xdp fn test(ctx: *xdp_md) -> xdp_action {
var x = 10
if (x > 5) {
return 1
}
return 2
}
|} in
try
let ast = parse_string program_text in
match List.hd ast with
| AttributedFunction attr_func ->
let main_func = attr_func.attr_function in
let if_stmt = List.nth main_func.func_body 1 in
(match if_stmt.stmt_desc with
| If (_, then_stmts, None) ->
check int "then branch has statements" 1 (List.length then_stmts);
check bool "no else branch" true (None = None)
| _ -> fail "Expected if statement without else")
| _ -> fail "Expected attributed function declaration"
with
| e -> fail ("Failed to parse simple if: " ^ Printexc.to_string e)
(** Test if-else statement *)
let test_if_else () =
let program_text = {|
@xdp fn test(ctx: *xdp_md) -> xdp_action {
var x = 10
if (x > 15) {
return 1
} else {
return 2
}
}
|} in
try
let ast = parse_string program_text in
match List.hd ast with
| AttributedFunction attr_func ->
let main_func = attr_func.attr_function in
let if_stmt = List.nth main_func.func_body 1 in
(match if_stmt.stmt_desc with
| If (_, then_stmts, Some else_stmts) ->
check int "then branch has statements" 1 (List.length then_stmts);
check int "else branch has statements" 1 (List.length else_stmts)
| _ -> fail "Expected if-else statement")
| _ -> fail "Expected attributed function declaration"
with
| e -> fail ("Failed to parse if-else: " ^ Printexc.to_string e)
(** Test if-else if-else chain *)
let test_if_else_if_else () =
let program_text = {|
@xdp fn test(ctx: *xdp_md) -> xdp_action {
var x = 10
if (x > 20) {
return 1
} else if (x > 10) {
return 2
} else if (x > 5) {
return 3
} else {
return 4
}
}
|} in
try
let ast = parse_string program_text in
match List.hd ast with
| AttributedFunction attr_func ->
let main_func = attr_func.attr_function in
let if_stmt = List.nth main_func.func_body 1 in
(match if_stmt.stmt_desc with
| If (_, then_stmts, Some else_stmts) ->
check int "first then branch" 1 (List.length then_stmts);
check int "else contains nested if" 1 (List.length else_stmts);
(* Check that else contains another if statement *)
(match (List.hd else_stmts).stmt_desc with
| If (_, _, Some _) -> ()
| _ -> fail "Expected nested if-else")
| _ -> fail "Expected if-else statement")
| _ -> fail "Expected attributed function declaration"
with
| e -> fail ("Failed to parse if-else if-else: " ^ Printexc.to_string e)
(** Test nested if statements *)
let test_nested_if () =
let program_text = {|
@xdp fn test(ctx: *xdp_md) -> xdp_action {
var x = 10
var y = 20
if (x > 5) {
if (y > 15) {
return 1
} else {
return 2
}
} else {
return 3
}
}
|} in
try
let ast = parse_string program_text in
match List.hd ast with
| AttributedFunction attr_func ->
let main_func = attr_func.attr_function in
let if_stmt = List.nth main_func.func_body 2 in
(match if_stmt.stmt_desc with
| If (_, then_stmts, Some _) ->
check int "outer then branch" 1 (List.length then_stmts);
(* Check nested if in then branch *)
(match (List.hd then_stmts).stmt_desc with
| If (_, nested_then, Some nested_else) ->
check int "nested then" 1 (List.length nested_then);
check int "nested else" 1 (List.length nested_else)
| _ -> fail "Expected nested if in then branch")
| _ -> fail "Expected nested if statement")
| _ -> fail "Expected attributed function declaration"
with
| e -> fail ("Failed to parse nested if: " ^ Printexc.to_string e)
(** Test if statements with multiple statements in branches *)
let test_multiple_statements_in_branches () =
let program_text = {|
@xdp fn test(ctx: *xdp_md) -> xdp_action {
var x = 10
if (x > 5) {
var y = x + 1
var z = y * 2
x = z - 1
return 1
} else {
x = x - 1
var w = x / 2
return 2
}
}
|} in
try
let ast = parse_string program_text in
match List.hd ast with
| AttributedFunction attr_func ->
let main_func = attr_func.attr_function in
let if_stmt = List.nth main_func.func_body 1 in
(match if_stmt.stmt_desc with
| If (_, then_stmts, Some else_stmts) ->
check int "then branch multiple statements" 4 (List.length then_stmts);
check int "else branch multiple statements" 3 (List.length else_stmts)
| _ -> fail "Expected if statement with multiple statements")
| _ -> fail "Expected attributed function declaration"
with
| e -> fail ("Failed to parse multiple statements: " ^ Printexc.to_string e)
(** Test that SPEC-compliant syntax works correctly *)
let test_spec_compliant_syntax () =
let program_text = {|
@xdp fn test(ctx: *xdp_md) -> xdp_action {
var x = 10
var y = 20
// SPEC-compliant syntax with mandatory parentheses around condition
if (x > 5) {
return 1
}
// Complex conditions also require parentheses
if (x > 5 && y < 25) {
return 2
}
// Parentheses for grouping expressions should still work
if ((x + y) > 25) {
return 3
}
return 0
}
|} in
try
let ast = parse_string program_text in
match List.hd ast with
| AttributedFunction attr_func ->
let main_func = attr_func.attr_function in
(* Should have multiple if statements *)
check bool "SPEC-compliant syntax works" true (List.length main_func.func_body >= 6)
| _ -> fail "Expected attributed function declaration"
with
| e -> fail ("Failed to parse SPEC-compliant syntax: " ^ Printexc.to_string e)
(** Test if statement error cases *)
let test_if_error_cases () =
let error_cases = [
("missing condition", {|
@xdp fn test() -> u32 {
if {
return 1
}
return 0
}
|});
("missing braces", {|
@xdp fn test() -> u32 {
if x > 5
return 1
return 0
}
|});
] in
List.iter (fun (desc, code) ->
try
let _ = parse_string code in
fail ("Should have failed: " ^ desc)
with
| Parse_error (_, _) -> ()
| _ -> fail ("Expected parse error for: " ^ desc)
) error_cases
(** Test simple for loop *)
let test_simple_for_loop () =
let program_text = {|
@xdp fn test(ctx: *xdp_md) -> xdp_action {
for (i in 0..10) {
return 1
}
return 2
}
|} in
try
let ast = parse_string program_text in
match List.hd ast with
| AttributedFunction attr_func ->
let main_func = attr_func.attr_function in
let for_stmt = List.hd main_func.func_body in
(match for_stmt.stmt_desc with
| For (var, _, _, body) ->
check string "for loop variable" "i" var;
check int "for loop body has statements" 1 (List.length body)
| _ -> fail "Expected for loop")
| _ -> fail "Expected attributed function declaration"
with
| e -> fail ("Failed to parse simple for loop: " ^ Printexc.to_string e)
(** Test for loop with expressions *)
let test_for_loop_with_expressions () =
let program_text = {|
@xdp fn test(ctx: *xdp_md) -> xdp_action {
for (i in 0..5) {
var x = i * 2
}
return 2
}
|} in
try
let ast = parse_string program_text in
match List.hd ast with
| AttributedFunction attr_func ->
let main_func = attr_func.attr_function in
let for_stmt = List.hd main_func.func_body in
(match for_stmt.stmt_desc with
| For (var, _, _, body) ->
check string "for loop variable" "i" var;
check int "for loop body has statements" 1 (List.length body)
| _ -> fail "Expected for loop")
| _ -> fail "Expected attributed function declaration"
with
| e -> fail ("Failed to parse for loop with expressions: " ^ Printexc.to_string e)
(** Test for iter syntax support *)
let test_for_iter_syntax () =
let program_text = {|
@xdp fn test(ctx: *xdp_md) -> xdp_action {
for (i in 0..3) {
var v = i
return v
}
return 2
}
|} in
try
let ast = parse_string program_text in
match List.hd ast with
| AttributedFunction attr_func ->
let main_func = attr_func.attr_function in
let for_stmt = List.hd main_func.func_body in
(match for_stmt.stmt_desc with
| For (var, _, _, body) ->
check string "for loop variable" "i" var;
check int "for loop body has statements" 2 (List.length body)
| _ -> fail "Expected for loop")
| _ -> fail "Expected attributed function declaration"
with
| e -> fail ("Failed to parse for iter syntax: " ^ Printexc.to_string e)
(** Test nested for loops *)
let test_nested_for_loops () =
let program_text = {|
@xdp fn test(ctx: *xdp_md) -> xdp_action {
for (i in 0..3) {
for (j in 0..2) {
return 1
}
}
return 2
}
|} in
try
let ast = parse_string program_text in
match List.hd ast with
| AttributedFunction attr_func ->
let main_func = attr_func.attr_function in
let outer_for = List.hd main_func.func_body in
(match outer_for.stmt_desc with
| For (_, _, _, outer_body) ->
check int "outer for loop body has statements" 1 (List.length outer_body);
(* Check nested for loop *)
let inner_for = List.hd outer_body in
(match inner_for.stmt_desc with
| For (_, _, _, inner_body) ->
check int "inner for loop body has statements" 1 (List.length inner_body)
| _ -> fail "Expected nested for loop")
| _ -> fail "Expected outer for loop")
| _ -> fail "Expected attributed function declaration"
with
| e -> fail ("Failed to parse nested for loops: " ^ Printexc.to_string e)
(** Test for loop edge cases *)
let test_for_loop_edge_cases () =
let test_cases = [
(* Zero range - should work *)
("for (i in 5..5) { var x = i }",
[make_for_stmt "i" (make_int_lit 5) (make_int_lit 5) [make_decl "x" (make_id "i")]]);
(* Variable bounds - use simple constants *)
("for (j in 2..8) { var y = j }",
[make_for_stmt "j" (make_int_lit 2) (make_int_lit 8) [make_decl "y" (make_id "j")]]);
] in
List.iter (fun (input, expected) ->
test_parse_statements input expected
) test_cases
let test_for_comprehensive () =
let input = "for (i in 0..3) { var x = i } for (j in 1..5) { var y = j }" in
let expected = [
make_for_stmt "i" (make_int_lit 0) (make_int_lit 3) [make_decl "x" (make_id "i")];
make_for_stmt "j" (make_int_lit 1) (make_int_lit 5) [make_decl "y" (make_id "j")];
] in
test_parse_statements input expected
let test_loop_bounds_analysis () =
(* Test that we can parse different kinds of loop bounds *)
let input = "for (i in 0..5) { var x = i } for (j in 2..8) { var y = j }" in
let expected = [
make_for_stmt "i" (make_int_lit 0) (make_int_lit 5) [make_decl "x" (make_id "i")];
make_for_stmt "j" (make_int_lit 2) (make_int_lit 8) [make_decl "y" (make_id "j")];
] in
test_parse_statements input expected
let test_variable_declaration () =
let test_cases = [
("var x: u32 = 10", true);
("var y = 20", true);
("var z: bool = true", true);
] in
List.iter (fun (input, should_pass) ->
try
let _ = parse_string input in
if not should_pass then
Printf.printf "ERROR: Expected %s to fail\n" input
with
| _ when should_pass ->
Printf.printf "ERROR: Expected %s to pass\n" input
| _ -> () (* Expected failure *)
) test_cases
let test_if_statements () =
let test_cases = [
("if (true) { var x = 10 }", true);
("if (false) { var y = 20 } else { var z = 30 }", true);
] in
List.iter (fun (input, should_pass) ->
try
let _ = parse_string input in
if not should_pass then
Printf.printf "ERROR: Expected %s to fail\n" input
with
| _ when should_pass ->
Printf.printf "ERROR: Expected %s to pass\n" input
| _ -> () (* Expected failure *)
) test_cases
let test_while_loops () =
let test_cases = [
("while (true) { var x = 10 }", true);
("while (false) { break }", true);
] in
List.iter (fun (input, should_pass) ->
try
let _ = parse_string input in
if not should_pass then
Printf.printf "ERROR: Expected %s to fail\n" input
with
| _ when should_pass ->
Printf.printf "ERROR: Expected %s to pass\n" input
| _ -> () (* Expected failure *)
) test_cases
let test_for_loops () =
let test_cases = [
("for (i in 0..10) { var x = 10 }", true);
("for (j in 1..5) { break }", true);
] in
List.iter (fun (input, should_pass) ->
try
let _ = parse_string input in
if not should_pass then
Printf.printf "ERROR: Expected %s to fail\n" input
with
| _ when should_pass ->
Printf.printf "ERROR: Expected %s to pass\n" input
| _ -> () (* Expected failure *)
) test_cases
let test_return_statements () =
let test_cases = [
("return 42", true);
("return true", true);
("return", true);
] in
List.iter (fun (input, should_pass) ->
try
let _ = parse_string input in
if not should_pass then
Printf.printf "ERROR: Expected %s to fail\n" input
with
| _ when should_pass ->
Printf.printf "ERROR: Expected %s to pass\n" input
| _ -> () (* Expected failure *)
) test_cases
let test_function_calls () =
let test_cases = [
("print(42)", true);
("helper(x, y)", true);
("process()", true);
("var scaled = read(cache).scaled", true);
] in
List.iter (fun (input, should_pass) ->
try
let _ = parse_string input in
if not should_pass then
Printf.printf "ERROR: Expected %s to fail\n" input
with
| _ when should_pass ->
Printf.printf "ERROR: Expected %s to pass\n" input
| _ -> () (* Expected failure *)
) test_cases
let test_nested_statements () =
let test_cases = [
("if (true) { while (false) { var x = 10 } }", true);
("for (i in 0..5) { if (i == 2) { break } }", true);
] in
List.iter (fun (input, should_pass) ->
try
let _ = parse_string input in
if not should_pass then
Printf.printf "ERROR: Expected %s to fail\n" input
with
| _ when should_pass ->
Printf.printf "ERROR: Expected %s to pass\n" input
| _ -> () (* Expected failure *)
) test_cases
let test_range_expressions () =
let test_cases = [
("for (i in 0..10) { var x = 10 }", true);
("for (j in 1..100) { var x = 10 }", true);
] in
List.iter (fun (input, should_pass) ->
try
let _ = parse_string input in
if not should_pass then
Printf.printf "ERROR: Expected %s to fail\n" input
with
| _ when should_pass ->
Printf.printf "ERROR: Expected %s to pass\n" input
| _ -> () (* Expected failure *)
) test_cases
let test_complex_expressions () =
let test_cases = [
("for (i in 0..5) { var x = 10 }", true);
("while (i < 10) { var x = 10 }", true);
] in
List.iter (fun (input, should_pass) ->
try
let _ = parse_string input in
if not should_pass then
Printf.printf "ERROR: Expected %s to fail\n" input
with
| _ when should_pass ->
Printf.printf "ERROR: Expected %s to pass\n" input
| _ -> () (* Expected failure *)
) test_cases
let test_combined_statements () =
let test_cases = [
("for (i in 0..3) { var x = i }", true);
("for (j in 1..5) { var y = j }", true);
] in
List.iter (fun (input, should_pass) ->
try
let _ = parse_string input in
if not should_pass then
Printf.printf "ERROR: Expected %s to fail\n" input
with
| _ when should_pass ->
Printf.printf "ERROR: Expected %s to pass\n" input
| _ -> () (* Expected failure *)
) test_cases
let test_range_boundary_conditions () =
let test_cases = [
("for (i in 5..5) { var x = i }", true);
("for (i in 0..0) { var y = i }", true);
] in
List.iter (fun (input, should_pass) ->
try
let _ = parse_string input in
if not should_pass then
Printf.printf "ERROR: Expected %s to fail\n" input
with
| _ when should_pass ->
Printf.printf "ERROR: Expected %s to pass\n" input
| _ -> () (* Expected failure *)
) test_cases
let test_multiple_statements_parsing () =
let complex_tests = [
("for (i in 0..3) { var x = i } for (j in 1..5) { var y = j }", "multiple for loops with variables");
("for (i in 0..5) { var x = i } for (j in 2..8) { var y = j }", "larger range for loops with variables");
] in
List.iter (fun (input, description) ->
try
let _ = parse_string input in
Printf.printf "✓ %s: %s\n" description input;
with
| e -> Printf.printf "✗ %s failed: %s\n" description (Printexc.to_string e)
) complex_tests
let test_compound_assignment () =
let source = {|
fn test() -> i32 {
var x: u32 = 10
x += 5
x -= 3
x *= 2
x /= 4
x %= 3
return 0
}
|} in
try
let ast = parse_string source in
let func = List.find (function
| GlobalFunction f when f.func_name = "test" -> true
| _ -> false) ast in
(match func with
| GlobalFunction f ->
let statements = f.func_body in
(* Check that we have 6 statements: var declaration + 5 compound assignments + return *)
assert (List.length statements = 7);
(* Check the compound assignment statements *)
(match List.nth statements 1 with
| { stmt_desc = CompoundAssignment ("x", Add, _); _ } -> ()
| _ -> failwith "Expected x += 5");
(match List.nth statements 2 with
| { stmt_desc = CompoundAssignment ("x", Sub, _); _ } -> ()
| _ -> failwith "Expected x -= 3");
(match List.nth statements 3 with
| { stmt_desc = CompoundAssignment ("x", Mul, _); _ } -> ()
| _ -> failwith "Expected x *= 2");
(match List.nth statements 4 with
| { stmt_desc = CompoundAssignment ("x", Div, _); _ } -> ()
| _ -> failwith "Expected x /= 4");
(match List.nth statements 5 with
| { stmt_desc = CompoundAssignment ("x", Mod, _); _ } -> ()
| _ -> failwith "Expected x %= 3");