| 1 | module List = ListLabels |
| 2 | |
| 3 | let test_01 = |
| 4 | let code = |
| 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 \ |
| 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 |
| 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 \ |
| 36 | var arr1:arrtype := \ |
| 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) |
| 96 | |
| 97 | let test_04 = |
| 98 | let name = "define a recursive function" in |
| 99 | let code = |
| 100 | " \ |
| 101 | /* "^name^" */ \ |
| 102 | let \ |
| 103 | \ |
| 104 | /* calculate n! */ \ |
| 105 | function nfactor(n: int): int = \ |
| 106 | if n = 0 \ |
| 107 | then 1 \ |
| 108 | else n * nfactor(n-1) \ |
| 109 | \ |
| 110 | in \ |
| 111 | nfactor(10) \ |
| 112 | end \ |
| 113 | " |
| 114 | in |
| 115 | let tokens = |
| 116 | let open Tiger.Parser.Token in |
| 117 | [ LET; |
| 118 | FUNCTION; ID "nfactor"; LPAREN; ID "n"; COLON; ID "int"; RPAREN; COLON; ID "int"; EQ; |
| 119 | IF; ID "n"; EQ; INT 0; |
| 120 | THEN; INT 1; |
| 121 | ELSE; ID "n"; TIMES; ID "nfactor"; LPAREN; ID "n"; MINUS; INT 1; RPAREN; |
| 122 | IN; |
| 123 | ID "nfactor"; LPAREN; INT 10; RPAREN; |
| 124 | END |
| 125 | ] |
| 126 | in |
| 127 | (name, code, tokens) |
| 128 | |
| 129 | let tokens_of_code code = |
| 130 | let lexbuf = Lexing.from_string code in |
| 131 | let rec tokens () = |
| 132 | match Tiger.Lexer.token lexbuf with |
| 133 | | None -> [] |
| 134 | | Some token -> token :: tokens () |
| 135 | in |
| 136 | tokens () |
| 137 | |
| 138 | let tests = |
| 139 | [ test_01 |
| 140 | ; test_02 |
| 141 | ; test_03 |
| 142 | ; test_04 |
| 143 | ] |
| 144 | |
| 145 | let () = |
| 146 | let bar_sep = String.make 80 '-' in |
| 147 | let bar_end = String.make 80 '=' in |
| 148 | List.iter tests ~f:(fun (name, code, tokens_expected) -> |
| 149 | let open Printf in |
| 150 | printf "%s\n==> Test %S: " bar_sep name; |
| 151 | let tokens_emitted = tokens_of_code code in |
| 152 | (try |
| 153 | assert (tokens_emitted = tokens_expected); |
| 154 | print_endline "OK"; |
| 155 | with Assert_failure _ -> |
| 156 | let tokens_to_string tokens = |
| 157 | String.concat "; " (List.map ~f:Tiger.Parser.Token.to_string tokens) |
| 158 | in |
| 159 | printf |
| 160 | "ERROR\n Expected: %s\n Emitted : %s\n\n" |
| 161 | (tokens_to_string tokens_expected) |
| 162 | (tokens_to_string tokens_emitted) |
| 163 | ); |
| 164 | ); |
| 165 | print_endline bar_end; |