| 1 | module Abs = Tiger_absyn |
| 2 | module Pos = Tiger_position |
| 3 | module Sym = Tiger_symbol |
| 4 | module Typ = Tiger_env_type |
| 5 | |
| 6 | type t = |
| 7 | | Invalid_syntax of Pos.t |
| 8 | | Unknown_id of {id : Sym.t; pos : Pos.t} |
| 9 | | Unknown_type of {ty_id : Sym.t; pos : Pos.t} |
| 10 | | Id_is_a_function of {id : Sym.t; pos : Pos.t} |
| 11 | | Id_not_a_function of {id : Sym.t; pos : Pos.t} |
| 12 | | No_such_field_in_record of {field : Sym.t; record : Typ.t; pos : Pos.t} |
| 13 | | Exp_not_a_record of {ty : Typ.t; pos : Pos.t} |
| 14 | | Exp_not_an_array of {ty : Typ.t; pos : Pos.t} |
| 15 | | Wrong_type of |
| 16 | { expected : Typ.t |
| 17 | ; given : Typ.t |
| 18 | ; pos : Pos.t |
| 19 | } |
| 20 | | Wrong_type_of_expression_in_var_dec of |
| 21 | { var_id : Sym.t |
| 22 | ; expected : Typ.t |
| 23 | ; given : Typ.t |
| 24 | ; pos : Pos.t |
| 25 | } |
| 26 | | Wrong_type_used_as_record of |
| 27 | { ty_id : Sym.t |
| 28 | ; ty : Typ.t |
| 29 | ; pos : Pos.t |
| 30 | } |
| 31 | | Wrong_type_used_as_array of |
| 32 | { ty_id : Sym.t |
| 33 | ; ty : Typ.t |
| 34 | ; pos : Pos.t |
| 35 | } |
| 36 | | Wrong_type_of_field_value of |
| 37 | { field_id : Sym.t |
| 38 | ; expected : Typ.t |
| 39 | ; given : Typ.t |
| 40 | ; pos : Pos.t |
| 41 | } |
| 42 | | Wrong_type_of_arg of |
| 43 | { func : Sym.t |
| 44 | ; expected : Typ.t |
| 45 | ; given : Typ.t |
| 46 | ; pos : Pos.t |
| 47 | } |
| 48 | | Wrong_number_of_args of |
| 49 | { func : Sym.t |
| 50 | ; expected : int |
| 51 | ; given : int |
| 52 | ; pos : Pos.t |
| 53 | } |
| 54 | | Invalid_operand_type of |
| 55 | { oper : Abs.oper |
| 56 | ; valid : string list |
| 57 | ; given : Typ.t |
| 58 | ; pos : Pos.t |
| 59 | } |
| 60 | | Different_operand_types of |
| 61 | { oper : Abs.oper |
| 62 | ; left : Typ.t |
| 63 | ; right : Typ.t |
| 64 | ; pos : Pos.t |
| 65 | } |
| 66 | |
| 67 | exception T of t |
| 68 | |
| 69 | let raise t = |
| 70 | raise (T t) |
| 71 | |
| 72 | let to_string = |
| 73 | let s = Printf.sprintf in |
| 74 | function |
| 75 | | Invalid_syntax pos -> |
| 76 | s "Invalid syntax in %s" (Pos.to_string pos) |
| 77 | | Unknown_id {id; pos} -> |
| 78 | s "Unknown identifier %S in %s" (Sym.to_string id) (Pos.to_string pos) |
| 79 | | Unknown_type {ty_id; pos} -> |
| 80 | s "Unknown type %S in %s" (Sym.to_string ty_id) (Pos.to_string pos) |
| 81 | | Id_is_a_function {id; pos} -> |
| 82 | s "Identifier %S is a function, it cannot be used as a variable in %s" |
| 83 | (Sym.to_string id) (Pos.to_string pos) |
| 84 | | Id_not_a_function {id; pos} -> |
| 85 | s "Identifier %S is not a function, it cannot be called in %s" |
| 86 | (Sym.to_string id) (Pos.to_string pos) |
| 87 | | No_such_field_in_record {field; record; pos} -> |
| 88 | s "No field %S in record %S in %s" |
| 89 | (Sym.to_string field) (Typ.to_string record) (Pos.to_string pos) |
| 90 | | Exp_not_a_record {ty; pos} -> |
| 91 | s ( "The expression of type %S is not a record, it cannot be" |
| 92 | ^^"accessed in %s") |
| 93 | (Typ.to_string ty) (Pos.to_string pos) |
| 94 | | Exp_not_an_array {ty; pos} -> |
| 95 | s ( "The expression of type %S is not an array, it cannot be" |
| 96 | ^^"accessed in %s") |
| 97 | (Typ.to_string ty) (Pos.to_string pos) |
| 98 | | Wrong_type {expected; given; pos} -> |
| 99 | s "Type error: expected: %S, but given: %S, in %s" |
| 100 | (Typ.to_string expected) |
| 101 | (Typ.to_string given) |
| 102 | (Pos.to_string pos) |
| 103 | | Wrong_type_of_expression_in_var_dec {var_id; expected; given; pos} -> |
| 104 | s ( "Wrong type of expression in declaration of %S. " |
| 105 | ^^"Expected: %S, given: %S. In %s") |
| 106 | (Sym.to_string var_id) |
| 107 | (Typ.to_string expected) |
| 108 | (Typ.to_string given) |
| 109 | (Pos.to_string pos) |
| 110 | | Wrong_type_used_as_array {ty_id; ty; pos} -> |
| 111 | s ( "Identifier %S is bound to type %S, not an array. " |
| 112 | ^^"It cannot be used in %s") |
| 113 | (Sym.to_string ty_id) (Typ.to_string ty) (Pos.to_string pos) |
| 114 | | Wrong_type_used_as_record {ty_id; ty; pos} -> |
| 115 | s ( "Identifier %S is bound to type %S, not a record. " |
| 116 | ^^"It cannot be used in %s") |
| 117 | (Sym.to_string ty_id) (Typ.to_string ty) (Pos.to_string pos) |
| 118 | | Wrong_type_of_field_value {field_id; expected; given; pos} -> |
| 119 | s ( "Field %S is declared to be of type %S, but is bound to expression " |
| 120 | ^^"of type %S in %s") |
| 121 | (Sym.to_string field_id) |
| 122 | (Typ.to_string expected) |
| 123 | (Typ.to_string given) |
| 124 | (Pos.to_string pos) |
| 125 | | Wrong_type_of_arg {func; expected; given; pos} -> |
| 126 | s ( "Incorrect type of argument to function %S, expected: %S, given: %S," |
| 127 | ^^" in %s") |
| 128 | (Sym.to_string func) |
| 129 | (Typ.to_string expected) |
| 130 | (Typ.to_string given) |
| 131 | (Pos.to_string pos) |
| 132 | | Wrong_number_of_args {func; expected; given; pos} -> |
| 133 | s ( "Incorrect number of arguments to function %S, " |
| 134 | ^^"expected: %d, given: %d," |
| 135 | ^^" in %s") |
| 136 | (Sym.to_string func) expected given (Pos.to_string pos) |
| 137 | | Invalid_operand_type {oper; valid; given; pos} -> |
| 138 | s ( "Invalid operand type %S for operator %S, which expects only: %s" |
| 139 | ^^". In %s") |
| 140 | (Typ.to_string given) |
| 141 | (Abs.op_show oper) |
| 142 | (String.concat ", " valid) |
| 143 | (Pos.to_string pos) |
| 144 | | Different_operand_types {oper; left; right; pos} -> |
| 145 | s "Operands of different types (%S %S %S) given in %s" |
| 146 | (Typ.to_string left) |
| 147 | (Abs.op_show oper) |
| 148 | (Typ.to_string right) |
| 149 | (Pos.to_string pos) |
| 150 | |
| 151 | let is_unknown_id t = |
| 152 | match t with |
| 153 | | Unknown_id _ -> |
| 154 | true |
| 155 | | Invalid_syntax _ |
| 156 | | Unknown_type _ |
| 157 | | Id_is_a_function _ |
| 158 | | Id_not_a_function _ |
| 159 | | No_such_field_in_record _ |
| 160 | | Exp_not_a_record _ |
| 161 | | Exp_not_an_array _ |
| 162 | | Wrong_type _ |
| 163 | | Wrong_type_of_expression_in_var_dec _ |
| 164 | | Wrong_type_used_as_array _ |
| 165 | | Wrong_type_used_as_record _ |
| 166 | | Wrong_type_of_field_value _ |
| 167 | | Wrong_type_of_arg _ |
| 168 | | Wrong_number_of_args _ |
| 169 | | Invalid_operand_type _ |
| 170 | | Different_operand_types _ -> |
| 171 | false |
| 172 | |
| 173 | let is_unknown_type t = |
| 174 | match t with |
| 175 | | Unknown_type _ -> |
| 176 | true |
| 177 | | Unknown_id _ |
| 178 | | Invalid_syntax _ |
| 179 | | Id_is_a_function _ |
| 180 | | Id_not_a_function _ |
| 181 | | No_such_field_in_record _ |
| 182 | | Exp_not_a_record _ |
| 183 | | Exp_not_an_array _ |
| 184 | | Wrong_type _ |
| 185 | | Wrong_type_of_expression_in_var_dec _ |
| 186 | | Wrong_type_used_as_array _ |
| 187 | | Wrong_type_used_as_record _ |
| 188 | | Wrong_type_of_field_value _ |
| 189 | | Wrong_type_of_arg _ |
| 190 | | Wrong_number_of_args _ |
| 191 | | Invalid_operand_type _ |
| 192 | | Different_operand_types _ -> |
| 193 | false |
| 194 | |
| 195 | let is_wrong_type t = |
| 196 | match t with |
| 197 | | Wrong_type _ -> |
| 198 | true |
| 199 | | Unknown_type _ |
| 200 | | Unknown_id _ |
| 201 | | Invalid_syntax _ |
| 202 | | Id_is_a_function _ |
| 203 | | Id_not_a_function _ |
| 204 | | No_such_field_in_record _ |
| 205 | | Exp_not_a_record _ |
| 206 | | Exp_not_an_array _ |
| 207 | | Wrong_type_of_expression_in_var_dec _ |
| 208 | | Wrong_type_used_as_array _ |
| 209 | | Wrong_type_used_as_record _ |
| 210 | | Wrong_type_of_field_value _ |
| 211 | | Wrong_type_of_arg _ |
| 212 | | Wrong_number_of_args _ |
| 213 | | Invalid_operand_type _ |
| 214 | | Different_operand_types _ -> |
| 215 | false |
| 216 | |
| 217 | let is_wrong_number_of_args t = |
| 218 | match t with |
| 219 | | Wrong_number_of_args _ -> |
| 220 | true |
| 221 | | Wrong_type _ |
| 222 | | Unknown_type _ |
| 223 | | Unknown_id _ |
| 224 | | Invalid_syntax _ |
| 225 | | Id_is_a_function _ |
| 226 | | Id_not_a_function _ |
| 227 | | No_such_field_in_record _ |
| 228 | | Exp_not_a_record _ |
| 229 | | Exp_not_an_array _ |
| 230 | | Wrong_type_of_expression_in_var_dec _ |
| 231 | | Wrong_type_used_as_array _ |
| 232 | | Wrong_type_used_as_record _ |
| 233 | | Wrong_type_of_field_value _ |
| 234 | | Wrong_type_of_arg _ |
| 235 | | Invalid_operand_type _ |
| 236 | | Different_operand_types _ -> |
| 237 | false |
| 238 | |
| 239 | let is_invalid_syntax t = |
| 240 | match t with |
| 241 | | Invalid_syntax _ -> |
| 242 | true |
| 243 | | Wrong_type _ |
| 244 | | Unknown_type _ |
| 245 | | Unknown_id _ |
| 246 | | Id_is_a_function _ |
| 247 | | Id_not_a_function _ |
| 248 | | No_such_field_in_record _ |
| 249 | | Exp_not_a_record _ |
| 250 | | Exp_not_an_array _ |
| 251 | | Wrong_type_of_expression_in_var_dec _ |
| 252 | | Wrong_type_used_as_array _ |
| 253 | | Wrong_type_used_as_record _ |
| 254 | | Wrong_type_of_field_value _ |
| 255 | | Wrong_type_of_arg _ |
| 256 | | Wrong_number_of_args _ |
| 257 | | Invalid_operand_type _ |
| 258 | | Different_operand_types _ -> |
| 259 | false |
| 260 | |
| 261 | let is_not_a_record t = |
| 262 | match t with |
| 263 | | Exp_not_a_record _ -> |
| 264 | true |
| 265 | | Invalid_syntax _ |
| 266 | | Wrong_type _ |
| 267 | | Unknown_type _ |
| 268 | | Unknown_id _ |
| 269 | | Id_is_a_function _ |
| 270 | | Id_not_a_function _ |
| 271 | | No_such_field_in_record _ |
| 272 | | Exp_not_an_array _ |
| 273 | | Wrong_type_of_expression_in_var_dec _ |
| 274 | | Wrong_type_used_as_array _ |
| 275 | | Wrong_type_used_as_record _ |
| 276 | | Wrong_type_of_field_value _ |
| 277 | | Wrong_type_of_arg _ |
| 278 | | Wrong_number_of_args _ |
| 279 | | Invalid_operand_type _ |
| 280 | | Different_operand_types _ -> |
| 281 | false |
| 282 | |
| 283 | let is_not_an_array t = |
| 284 | match t with |
| 285 | | Exp_not_an_array _ -> |
| 286 | true |
| 287 | | Exp_not_a_record _ |
| 288 | | Invalid_syntax _ |
| 289 | | Wrong_type _ |
| 290 | | Unknown_type _ |
| 291 | | Unknown_id _ |
| 292 | | Id_is_a_function _ |
| 293 | | Id_not_a_function _ |
| 294 | | No_such_field_in_record _ |
| 295 | | Wrong_type_of_expression_in_var_dec _ |
| 296 | | Wrong_type_used_as_array _ |
| 297 | | Wrong_type_used_as_record _ |
| 298 | | Wrong_type_of_field_value _ |
| 299 | | Wrong_type_of_arg _ |
| 300 | | Wrong_number_of_args _ |
| 301 | | Invalid_operand_type _ |
| 302 | | Different_operand_types _ -> |
| 303 | false |
| 304 | |
| 305 | let is_no_such_field_in_record t = |
| 306 | match t with |
| 307 | | No_such_field_in_record _ -> |
| 308 | true |
| 309 | | Exp_not_an_array _ |
| 310 | | Exp_not_a_record _ |
| 311 | | Invalid_syntax _ |
| 312 | | Wrong_type _ |
| 313 | | Unknown_type _ |
| 314 | | Unknown_id _ |
| 315 | | Id_is_a_function _ |
| 316 | | Id_not_a_function _ |
| 317 | | Wrong_type_of_expression_in_var_dec _ |
| 318 | | Wrong_type_used_as_array _ |
| 319 | | Wrong_type_used_as_record _ |
| 320 | | Wrong_type_of_field_value _ |
| 321 | | Wrong_type_of_arg _ |
| 322 | | Wrong_number_of_args _ |
| 323 | | Invalid_operand_type _ |
| 324 | | Different_operand_types _ -> |
| 325 | false |