Add CLI options to execute different test suites
[tiger.ml.git] / compiler / src / lib / tiger / tiger_test_cases_book.ml
CommitLineData
38ffcb1f
SK
1module List = ListLabels
2
3module Test = Tiger_test
4
5let read_file filepath =
6 let {Unix.st_size=size; _} = Unix.stat filepath in
7 let buf = Buffer.create size in
8 let ic = open_in filepath in
9 let rec read () =
10 try
11 Buffer.add_channel buf ic size;
12 read ()
13 with End_of_file ->
14 ()
15 in
16 read ();
17 close_in ic;
18 Buffer.contents buf
19
20let out_lexing_of_filename =
21 let open Tiger_parser in
22 function
23 | "test01.tig" ->
24 Some
25 [ LET;
26 TYPE; ID "arrtype"; EQ; ARRAY; OF; ID "int";
27 VAR; ID "arr1"; COLON; ID "arrtype"; ASSIGN;
28 ID "arrtype"; LBRACK; INT 10; RBRACK; OF; INT 0;
29 IN;
30 ID "arr1";
31 END
32 ]
33 | "test02.tig" ->
34 Some
35 [ LET;
36 TYPE; ID "myint"; EQ; ID "int";
37 TYPE; ID "arrtype"; EQ; ARRAY; OF; ID "myint";
38 VAR; ID "arr1"; COLON; ID "arrtype"; ASSIGN;
39 ID "arrtype"; LBRACK; INT 10; RBRACK; OF; INT 0;
40 IN;
41 ID "arr1";
42 END
43 ]
44 | "test03.tig" ->
45 Some
46 [ LET;
47 TYPE; ID "rectype"; EQ;
48 LBRACE; ID "name"; COLON; ID "string";
49 COMMA; ID "age"; COLON; ID "int";
50 RBRACE;
51 VAR; ID "rec1"; COLON; ID "rectype"; ASSIGN;
52 ID "rectype";
53 LBRACE; ID "name"; EQ; STRING "Nobody";
54 COMMA; ID "age"; EQ; INT 1000;
55 RBRACE;
56 IN;
57 ID "rec1"; DOT; ID "name"; ASSIGN; STRING "Somebody"; SEMICOLON;
58 ID "rec1";
59 END
60 ]
61 | "test04.tig" ->
62 Some
63 [ LET;
64 FUNCTION; ID "nfactor"; LPAREN; ID "n"; COLON; ID "int"; RPAREN; COLON; ID "int"; EQ;
65 IF; ID "n"; EQ; INT 0;
66 THEN; INT 1;
67 ELSE; ID "n"; TIMES; ID "nfactor"; LPAREN; ID "n"; MINUS; INT 1; RPAREN;
68 IN;
69 ID "nfactor"; LPAREN; INT 10; RPAREN;
70 END
71 ]
72 | "test09.tig" ->
73 Some
74 [IF; LPAREN; INT 5; GT; INT 4; RPAREN; THEN; INT 13; ELSE; STRING " "]
75 | _ ->
76 (* TODO: Fill-in other expected cases *)
77 None
78
79let out_parsing_of_filename _ =
80 (* TODO: Fill-in expected cases *)
81 None
82
89037894
SK
83let is_error_expected_parsing_of_filename =
84 let module E = Tiger_error in
85 function
86 | "test49.tig" ->
87 Some (function E.Invalid_syntax _ -> true | _ -> false)
88 (* TODO: Be more specific - test position *)
89 | _ ->
90 (* TODO: Fill-in other expected cases *)
91 None
92
38ffcb1f
SK
93let is_error_expected_semant_of_filename _ =
94 (* TODO: Fill-in expected cases *)
95 None
96
97let test_case_of_filename filename ~dir =
98 Test.case
99 filename
100 ~code:(read_file (Filename.concat dir filename))
101 ~out_lexing:(out_lexing_of_filename filename)
102 ~out_parsing:(out_parsing_of_filename filename)
89037894 103 ~is_error_expected_parsing:(is_error_expected_parsing_of_filename filename)
38ffcb1f
SK
104 ~is_error_expected_semant:(is_error_expected_semant_of_filename filename)
105
106let read ~from_dir:dir =
107 Sys.readdir dir
108 |> Array.to_list
109 |> List.sort ~cmp:compare
110 |> List.map ~f:(test_case_of_filename ~dir)
This page took 0.043018 seconds and 4 git commands to generate.