1 module Test = Tiger_test
5 "Book test 1: an array type and an array variable"
8 /* an array type and an array variable */ \
10 type arrtype = array of int \
18 let open Tiger_parser in
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;
29 "Book test 2: arr1 is valid since expression 0 is int = myint"
32 /* arr1 is valid since expression 0 is int = myint */ \
35 type arrtype = array of myint \
43 let open Tiger_parser in
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;
55 "Book test 3: a record type and a record variable"
58 /* a record type and a record variable */ \
64 var rec1 : rectype := \
70 rec1.name := \"Somebody\"; \
75 let open Tiger_parser in
77 TYPE; ID "rectype"; EQ;
78 LBRACE; ID "name"; COLON; ID "string";
79 COMMA; ID "age"; COLON; ID "int";
81 VAR; ID "rec1"; COLON; ID "rectype"; ASSIGN;
83 LBRACE; ID "name"; EQ; STRING "Nobody";
84 COMMA; ID "age"; EQ; INT 1000;
87 ID "rec1"; DOT; ID "name"; ASSIGN; STRING "Somebody"; SEMICOLON;
93 "Book test 4: define a recursive function"
96 /* define a recursive function */ \
100 function nfactor(n: int): int = \
103 else n * nfactor(n-1) \
110 let open Tiger_parser in
112 FUNCTION; ID "nfactor"; LPAREN; ID "n"; COLON; ID "int"; RPAREN; COLON; ID "int"; EQ;
113 IF; ID "n"; EQ; INT 0;
115 ELSE; ID "n"; TIMES; ID "nfactor"; LPAREN; ID "n"; MINUS; INT 1; RPAREN;
117 ID "nfactor"; LPAREN; INT 10; RPAREN;
122 "Book test 9: error : types of then - else differ"
125 /* error : types of then - else differ */ \
126 if (5>4) then 13 else \" \" \
129 let open Tiger_parser in
130 [ IF; LPAREN; INT 5; GT; INT 4; RPAREN; THEN; INT 13; ELSE; STRING " "
136 let test_case_from_book_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\
187 let open Tiger_parser in
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])
195 ; (let code = "abc[0] := foo()" in Test.case code ~code
197 [ID "abc"; LBRACK; INT 0; RBRACK; ASSIGN; ID "foo"; LPAREN; RPAREN])
199 ; (let code = "abc [5] of nil" in Test.case code ~code
201 [ID "abc"; LBRACK; INT 5; RBRACK; OF; NIL])
203 ; (let code = "f(\"a\", 3, foo)" in Test.case code ~code
205 [ID "f"; LPAREN; STRING "a"; COMMA; INT 3; COMMA; ID "foo"; RPAREN])