Commit | Line | Data |
---|---|---|
38ffcb1f SK |
1 | module List = ListLabels |
2 | ||
3 | module Test = Tiger_test | |
4 | ||
5 | let 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 | ||
20 | let 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 | ||
79 | let out_parsing_of_filename _ = | |
80 | (* TODO: Fill-in expected cases *) | |
81 | None | |
82 | ||
83 | let is_error_expected_semant_of_filename _ = | |
84 | (* TODO: Fill-in expected cases *) | |
85 | None | |
86 | ||
87 | let test_case_of_filename filename ~dir = | |
88 | Test.case | |
89 | filename | |
90 | ~code:(read_file (Filename.concat dir filename)) | |
91 | ~out_lexing:(out_lexing_of_filename filename) | |
92 | ~out_parsing:(out_parsing_of_filename filename) | |
93 | ~is_error_expected_semant:(is_error_expected_semant_of_filename filename) | |
94 | ||
95 | let read ~from_dir:dir = | |
96 | Sys.readdir dir | |
97 | |> Array.to_list | |
98 | |> List.sort ~cmp:compare | |
99 | |> List.map ~f:(test_case_of_filename ~dir) |