Commit | Line | Data |
---|---|---|
d3bdde4b | 1 | module Test = Tiger_test |
f752b2c7 | 2 | |
d3bdde4b | 3 | let book = |
f752b2c7 SK |
4 | [ Test.case |
5 | "Book test 1: an array type and an array variable" | |
6 | ~code: | |
7 | " \ | |
8 | /* an array type and an array variable */ \ | |
9 | let \ | |
10 | type arrtype = array of int \ | |
11 | var arr1:arrtype := \ | |
12 | arrtype [10] of 0 \ | |
13 | in \ | |
14 | arr1 \ | |
15 | end \ | |
16 | " | |
17 | ~out_lexing:( | |
d3bdde4b | 18 | let open Tiger_parser in |
f752b2c7 SK |
19 | [ LET; |
20 | TYPE; ID "arrtype"; EQ; ARRAY; OF; ID "int"; | |
21 | VAR; ID "arr1"; COLON; ID "arrtype"; ASSIGN; | |
22 | ID "arrtype"; LBRACK; INT 10; RBRACK; OF; INT 0; | |
23 | IN; | |
24 | ID "arr1"; | |
25 | END | |
26 | ] | |
27 | ) | |
28 | ; Test.case | |
29 | "Book test 2: arr1 is valid since expression 0 is int = myint" | |
30 | ~code: | |
31 | " \ | |
32 | /* arr1 is valid since expression 0 is int = myint */ \ | |
33 | let \ | |
34 | type myint = int \ | |
35 | type arrtype = array of myint \ | |
36 | var arr1:arrtype := \ | |
37 | arrtype [10] of 0 \ | |
38 | in \ | |
39 | arr1 \ | |
40 | end \ | |
41 | " | |
42 | ~out_lexing:( | |
d3bdde4b | 43 | let open Tiger_parser in |
f752b2c7 SK |
44 | [ LET; |
45 | TYPE; ID "myint"; EQ; ID "int"; | |
46 | TYPE; ID "arrtype"; EQ; ARRAY; OF; ID "myint"; | |
47 | VAR; ID "arr1"; COLON; ID "arrtype"; ASSIGN; | |
48 | ID "arrtype"; LBRACK; INT 10; RBRACK; OF; INT 0; | |
49 | IN; | |
50 | ID "arr1"; | |
51 | END | |
52 | ] | |
53 | ) | |
54 | ; Test.case | |
55 | "Book test 3: a record type and a record variable" | |
56 | ~code: | |
57 | " \ | |
58 | /* a record type and a record variable */ \ | |
59 | let \ | |
60 | type rectype = \ | |
61 | { name : string \ | |
62 | , age : int \ | |
63 | } \ | |
64 | var rec1 : rectype := \ | |
65 | rectype \ | |
66 | { name = \"Nobody\" \ | |
67 | , age = 1000 \ | |
68 | } \ | |
69 | in \ | |
70 | rec1.name := \"Somebody\"; \ | |
71 | rec1 \ | |
72 | end \ | |
73 | " | |
74 | ~out_lexing:( | |
d3bdde4b | 75 | let open Tiger_parser in |
f752b2c7 SK |
76 | [ LET; |
77 | TYPE; ID "rectype"; EQ; | |
78 | LBRACE; ID "name"; COLON; ID "string"; | |
79 | COMMA; ID "age"; COLON; ID "int"; | |
80 | RBRACE; | |
81 | VAR; ID "rec1"; COLON; ID "rectype"; ASSIGN; | |
82 | ID "rectype"; | |
83 | LBRACE; ID "name"; EQ; STRING "Nobody"; | |
84 | COMMA; ID "age"; EQ; INT 1000; | |
85 | RBRACE; | |
86 | IN; | |
87 | ID "rec1"; DOT; ID "name"; ASSIGN; STRING "Somebody"; SEMICOLON; | |
88 | ID "rec1"; | |
89 | END | |
90 | ] | |
91 | ) | |
92 | ; Test.case | |
93 | "Book test 4: define a recursive function" | |
94 | ~code: | |
95 | " \ | |
96 | /* define a recursive function */ \ | |
97 | let \ | |
98 | \ | |
99 | /* calculate n! */ \ | |
100 | function nfactor(n: int): int = \ | |
101 | if n = 0 \ | |
102 | then 1 \ | |
103 | else n * nfactor(n-1) \ | |
104 | \ | |
105 | in \ | |
106 | nfactor(10) \ | |
107 | end \ | |
108 | " | |
109 | ~out_lexing:( | |
d3bdde4b | 110 | let open Tiger_parser in |
f752b2c7 SK |
111 | [ LET; |
112 | FUNCTION; ID "nfactor"; LPAREN; ID "n"; COLON; ID "int"; RPAREN; COLON; ID "int"; EQ; | |
113 | IF; ID "n"; EQ; INT 0; | |
114 | THEN; INT 1; | |
115 | ELSE; ID "n"; TIMES; ID "nfactor"; LPAREN; ID "n"; MINUS; INT 1; RPAREN; | |
116 | IN; | |
117 | ID "nfactor"; LPAREN; INT 10; RPAREN; | |
118 | END | |
119 | ] | |
120 | ) | |
121 | ; Test.case | |
122 | "Book test 9: error : types of then - else differ" | |
123 | ~code: | |
124 | " \ | |
125 | /* error : types of then - else differ */ \ | |
126 | if (5>4) then 13 else \" \" \ | |
127 | " | |
128 | ~out_lexing:( | |
d3bdde4b | 129 | let open Tiger_parser in |
f752b2c7 SK |
130 | [ IF; LPAREN; INT 5; GT; INT 4; RPAREN; THEN; INT 13; ELSE; STRING " " |
131 | ] | |
132 | ) | |
133 | ] | |
cb00a20d | 134 | |
15ade902 | 135 | (* |
68a223c2 | 136 | let test_case_from_book_queens = |
82e73e6c | 137 | let code = |
68a223c2 | 138 | "\ |
82e73e6c SK |
139 | /* A program to solve the 8-queens problem */ \n\ |
140 | \n\ | |
141 | let \n\ | |
142 | var N := 8 \n\ | |
143 | \n\ | |
144 | type intArray = array of int \n\ | |
145 | \n\ | |
146 | var row := intArray [ N ] of 0 \n\ | |
147 | var col := intArray [ N ] of 0 \n\ | |
148 | var diag1 := intArray [N+N-1] of 0 \n\ | |
149 | var diag2 := intArray [N+N-1] of 0 \n\ | |
150 | \n\ | |
151 | function printboard() = ( \n\ | |
152 | for i := 0 to N-1 do ( \n\ | |
153 | for j := 0 to N-1 do print(if col[i]=j then \" O\" else \" .\"); \n\ | |
154 | print(\"\n\") \n\ | |
155 | ); \n\ | |
156 | print(\"\n\") \n\ | |
157 | ) \n\ | |
158 | \n\ | |
159 | function try(c:int) = ( \n\ | |
160 | /* for i:= 0 to c do print(\".\"); print(\"\n\"); flush();*/ \n\ | |
161 | if c=N \n\ | |
162 | then printboard() \n\ | |
163 | else \n\ | |
164 | for r := 0 to N-1 \n\ | |
165 | do \n\ | |
166 | if row[r]=0 & diag1[r+c]=0 & diag2[r+7-c]=0 \n\ | |
167 | then ( \n\ | |
168 | row[r] := 1; \n\ | |
169 | diag1[r+c] := 1; \n\ | |
170 | diag2[r+7-c] := 1; \n\ | |
171 | col[c] := r; \n\ | |
172 | try(c+1); \n\ | |
173 | row[r] := 0; \n\ | |
174 | diag1[r+c] := 0; \n\ | |
175 | diag2[r+7-c] := 0 \n\ | |
176 | ) \n\ | |
177 | ) \n\ | |
178 | in \n\ | |
179 | try(0) \n\ | |
180 | end \n\ | |
181 | " | |
182 | in | |
183 | (code, code, []) | |
15ade902 | 184 | *) |
82e73e6c | 185 | |
d3bdde4b SK |
186 | let micro = |
187 | let open Tiger_parser in | |
2c754e82 SK |
188 | [ (let code = "nil" in Test.case code ~code ~out_lexing:[NIL]) |
189 | ; (let code = "5" in Test.case code ~code ~out_lexing:[INT 5]) | |
190 | ; (let code = "-5" in Test.case code ~code ~out_lexing:[MINUS; INT 5]) | |
191 | ; (let code = "f()" in Test.case code ~code ~out_lexing:[ID "f"; LPAREN; RPAREN]) | |
192 | ; (let code = "abc.i" in Test.case code ~code ~out_lexing:[ID "abc"; DOT; ID "i"]) | |
193 | ; (let code = "abc[0]" in Test.case code ~code ~out_lexing:[ID "abc"; LBRACK; INT 0; RBRACK]) | |
78c9eca5 | 194 | |
2c754e82 | 195 | ; (let code = "abc[0] := foo()" in Test.case code ~code |
ead887f4 SK |
196 | ~out_lexing: |
197 | [ID "abc"; LBRACK; INT 0; RBRACK; ASSIGN; ID "foo"; LPAREN; RPAREN]) | |
68a223c2 | 198 | |
2c754e82 | 199 | ; (let code = "abc [5] of nil" in Test.case code ~code |
ead887f4 SK |
200 | ~out_lexing: |
201 | [ID "abc"; LBRACK; INT 5; RBRACK; OF; NIL]) | |
543d3420 | 202 | |
2c754e82 | 203 | ; (let code = "f(\"a\", 3, foo)" in Test.case code ~code |
ead887f4 SK |
204 | ~out_lexing: |
205 | [ID "f"; LPAREN; STRING "a"; COMMA; INT 3; COMMA; ID "foo"; RPAREN]) | |
f752b2c7 | 206 | ] |
543d3420 | 207 | |
d3bdde4b SK |
208 | let all = |
209 | book @ micro |