1 module Error = Tiger_error
2 module Test = Tiger_test
6 "Book test 1: an array type and an array variable"
9 /* an array type and an array variable */ \
11 type arrtype = array of int \
19 let open Tiger_parser in
21 TYPE; ID "arrtype"; EQ; ARRAY; OF; ID "int";
22 VAR; ID "arr1"; COLON; ID "arrtype"; ASSIGN;
23 ID "arrtype"; LBRACK; INT 10; RBRACK; OF; INT 0;
30 "Book test 2: arr1 is valid since expression 0 is int = myint"
33 /* arr1 is valid since expression 0 is int = myint */ \
36 type arrtype = array of myint \
44 let open Tiger_parser in
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;
56 "Book test 3: a record type and a record variable"
59 /* a record type and a record variable */ \
65 var rec1 : rectype := \
71 rec1.name := \"Somebody\"; \
76 let open Tiger_parser in
78 TYPE; ID "rectype"; EQ;
79 LBRACE; ID "name"; COLON; ID "string";
80 COMMA; ID "age"; COLON; ID "int";
82 VAR; ID "rec1"; COLON; ID "rectype"; ASSIGN;
84 LBRACE; ID "name"; EQ; STRING "Nobody";
85 COMMA; ID "age"; EQ; INT 1000;
88 ID "rec1"; DOT; ID "name"; ASSIGN; STRING "Somebody"; SEMICOLON;
94 "Book test 4: define a recursive function"
97 /* define a recursive function */ \
101 function nfactor(n: int): int = \
104 else n * nfactor(n-1) \
111 let open Tiger_parser in
113 FUNCTION; ID "nfactor"; LPAREN; ID "n"; COLON; ID "int"; RPAREN; COLON; ID "int"; EQ;
114 IF; ID "n"; EQ; INT 0;
116 ELSE; ID "n"; TIMES; ID "nfactor"; LPAREN; ID "n"; MINUS; INT 1; RPAREN;
118 ID "nfactor"; LPAREN; INT 10; RPAREN;
123 "Book test 9: error : types of then - else differ"
126 /* error : types of then - else differ */ \
127 if (5>4) then 13 else \" \" \
130 let open Tiger_parser in
131 [ IF; LPAREN; INT 5; GT; INT 4; RPAREN; THEN; INT 13; ELSE; STRING " "
134 ~is_error_expected_semant:Error.is_wrong_type (* TODO: Be more specific *)
136 "Book test: 8-queens"
139 /* A program to solve the 8-queens problem */ \n\
144 type intArray = array of int \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\
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\
159 function try(c:int) = ( \n\
160 /* for i:= 0 to c do print(\".\"); print(\"\n\"); flush();*/ \n\
162 then printboard() \n\
164 for r := 0 to N-1 \n\
166 if row[r]=0 & diag1[r+c]=0 & diag2[r+7-c]=0 \n\
170 diag2[r+7-c] := 1; \n\
175 diag2[r+7-c] := 0 \n\
185 let open Tiger_parser in
186 [ (let code = "nil" in Test.case code ~code ~out_lexing:[NIL])
187 ; (let code = "5" in Test.case code ~code ~out_lexing:[INT 5])
188 ; (let code = "-5" in Test.case code ~code ~out_lexing:[MINUS; INT 5])
189 ; ( let code = "f()" in
193 ~out_lexing:[ID "f"; LPAREN; RPAREN]
194 ~is_error_expected_semant:Error.is_unknown_id (* TODO: Be more specific *)
196 ; ( let code = "abc.i" in
200 ~out_lexing:[ID "abc"; DOT; ID "i"]
201 ~is_error_expected_semant:Error.is_unknown_id (* TODO: Be more specific *)
203 ; ( let code = "abc[0]" in
207 ~out_lexing:[ID "abc"; LBRACK; INT 0; RBRACK]
208 ~is_error_expected_semant:Error.is_unknown_id (* TODO: Be more specific *)
210 ; ( let code = "abc[0] := foo()" in
215 [ID "abc"; LBRACK; INT 0; RBRACK; ASSIGN; ID "foo"; LPAREN; RPAREN]
216 ~is_error_expected_semant:Error.is_unknown_id (* TODO: Be more specific *)
218 ; ( let code = "abc [5] of nil" in
222 ~out_lexing:[ID "abc"; LBRACK; INT 5; RBRACK; OF; NIL]
223 ~is_error_expected_semant:Error.is_unknown_type (* TODO: Be more specific *)
225 ; ( let code = "f(\"a\", 3, foo)" in
230 [ID "f"; LPAREN; STRING "a"; COMMA; INT 3; COMMA; ID "foo"; RPAREN]
231 ~is_error_expected_semant:Error.is_unknown_id
251 type a = {x:int, y:int} \
252 type b = {x:int, y:int} /* new type generated */ \
253 var foo : a := a {x = 1, y = 2} \
254 var bar : b := b {x = 1, y = 2} \
256 foo = bar /* incompatible types */ \
263 ~is_error_expected_semant:Error.is_wrong_type (* TODO: Be more specific *)