Commit | Line | Data |
---|---|---|
78c9eca5 SK |
1 | module List = ListLabels |
2 | ||
3 | let test_01 = | |
4 | let code = | |
8543fc37 SK |
5 | " \ |
6 | /* an array type and an array variable */ \ | |
7 | let \ | |
8 | type arrtype = array of int \ | |
9 | var arr1:arrtype := \ | |
10 | arrtype [10] of 0 \ | |
11 | in \ | |
12 | arr1 \ | |
13 | end \ | |
78c9eca5 SK |
14 | " |
15 | in | |
16 | let tokens = | |
17 | let open Tiger.Parser.Token in | |
18 | [ LET; | |
19 | TYPE; ID "arrtype"; EQ; ARRAY; OF; ID "int"; | |
20 | VAR; ID "arr1"; COLON; ID "arrtype"; ASSIGN; | |
21 | ID "arrtype"; LBRACK; INT 10; RBRACK; OF; INT 0; | |
22 | IN; | |
23 | ID "arr1"; | |
24 | END | |
25 | ] | |
26 | in | |
8543fc37 SK |
27 | ("test_01", code, tokens) |
28 | ||
29 | let test_02 = | |
30 | let code = | |
31 | " \ | |
32 | /* arr1 is valid since expression 0 is int = myint */ \ | |
33 | let \ | |
34 | type myint = int \ | |
35 | type arrtype = array of myint \ | |
b828a6e7 | 36 | var arr1:arrtype := \ |
8543fc37 SK |
37 | arrtype [10] of 0 \ |
38 | in \ | |
39 | arr1 \ | |
40 | end \ | |
41 | " | |
42 | in | |
43 | let tokens = | |
44 | let open Tiger.Parser.Token in | |
45 | [ LET; | |
46 | TYPE; ID "myint"; EQ; ID "int"; | |
47 | TYPE; ID "arrtype"; EQ; ARRAY; OF; ID "myint"; | |
48 | VAR; ID "arr1"; COLON; ID "arrtype"; ASSIGN; | |
49 | ID "arrtype"; LBRACK; INT 10; RBRACK; OF; INT 0; | |
50 | IN; | |
51 | ID "arr1"; | |
52 | END | |
53 | ] | |
54 | in | |
55 | ("test_02", code, tokens) | |
56 | ||
57 | let test_03 = | |
58 | let code = | |
59 | " \ | |
60 | /* a record type and a record variable */ \ | |
61 | let \ | |
62 | type rectype = \ | |
63 | { name : string \ | |
64 | , age : int \ | |
65 | } \ | |
66 | var rec1 : rectype := \ | |
67 | rectype \ | |
68 | { name = \"Nobody\" \ | |
69 | , age = 1000 \ | |
70 | } \ | |
71 | in \ | |
72 | rec1.name := \"Somebody\"; \ | |
73 | rec1 \ | |
74 | end \ | |
75 | " | |
76 | in | |
77 | let tokens = | |
78 | let open Tiger.Parser.Token in | |
79 | [ LET; | |
80 | TYPE; ID "rectype"; EQ; | |
81 | LBRACE; ID "name"; COLON; ID "string"; | |
82 | COMMA; ID "age"; COLON; ID "int"; | |
83 | RBRACE; | |
84 | VAR; ID "rec1"; COLON; ID "rectype"; ASSIGN; | |
85 | ID "rectype"; | |
86 | LBRACE; ID "name"; EQ; STRING "Nobody"; | |
87 | COMMA; ID "age"; EQ; INT 1000; | |
88 | RBRACE; | |
89 | IN; | |
90 | ID "rec1"; DOT; ID "name"; ASSIGN; STRING "Somebody"; SEMICOLON; | |
91 | ID "rec1"; | |
92 | END | |
93 | ] | |
94 | in | |
95 | ("test_03", code, tokens) | |
78c9eca5 SK |
96 | |
97 | let tokens_of_code code = | |
98 | let lexbuf = Lexing.from_string code in | |
99 | let rec tokens () = | |
100 | match Tiger.Lexer.token lexbuf with | |
b828a6e7 SK |
101 | | None -> [] |
102 | | Some token -> token :: tokens () | |
78c9eca5 SK |
103 | in |
104 | tokens () | |
105 | ||
106 | let tests = | |
107 | [ test_01 | |
8543fc37 SK |
108 | ; test_02 |
109 | ; test_03 | |
78c9eca5 SK |
110 | ] |
111 | ||
112 | let () = | |
8543fc37 SK |
113 | let bar_sep = String.make 80 '-' in |
114 | let bar_end = String.make 80 '=' in | |
115 | List.iter tests ~f:(fun (name, code, tokens_expected) -> | |
116 | let open Printf in | |
117 | printf "%s\n==> Test %S: " bar_sep name; | |
118 | let tokens_emitted = tokens_of_code code in | |
119 | (try | |
120 | assert (tokens_emitted = tokens_expected); | |
121 | print_endline "OK"; | |
122 | with Assert_failure _ -> | |
123 | let tokens_to_string tokens = | |
b828a6e7 | 124 | String.concat "; " (List.map ~f:Tiger.Parser.Token.to_string tokens) |
8543fc37 SK |
125 | in |
126 | printf | |
127 | "ERROR\n Expected: %s\n Emitted : %s\n\n" | |
128 | (tokens_to_string tokens_expected) | |
129 | (tokens_to_string tokens_emitted) | |
130 | ); | |
131 | ); | |
132 | print_endline bar_end; |