1 module Abs = Tiger_absyn
2 module Pos = Tiger_position
3 module Sym = Tiger_symbol
4 module Typ = Tiger_env_type
7 | Cycle_in_type_decs of
13 | Break_outside_loop of Pos.t
14 | Invalid_syntax of Pos.t
15 | Unknown_id of {id : Sym.t; pos : Pos.t}
16 | Unknown_type of {ty_id : Sym.t; pos : Pos.t}
17 | Id_is_a_function of {id : Sym.t; pos : Pos.t}
18 | Id_not_a_function of {id : Sym.t; pos : Pos.t}
19 | No_such_field_in_record of {field : Sym.t; record : Typ.t; pos : Pos.t}
20 | Exp_not_a_record of {ty : Typ.t; pos : Pos.t}
21 | Exp_not_an_array of {ty : Typ.t; pos : Pos.t}
27 | Wrong_type_of_expression_in_var_dec of
33 | Wrong_type_used_as_record of
38 | Wrong_type_used_as_array of
43 | Wrong_type_of_field_value of
49 | Wrong_type_of_arg of
55 | Wrong_number_of_args of
61 | Invalid_operand_type of
67 | Different_operand_types of
80 let s = Printf.sprintf in
82 | Invalid_syntax pos ->
83 s "Invalid syntax in %s" (Pos.to_string pos)
84 | Break_outside_loop pos ->
85 s "Break statement is not within a loop. In %s" (Pos.to_string pos)
86 | Cycle_in_type_decs {from_id; from_pos; to_id; to_pos} ->
87 s ( "Circular type declaration between %S and %S."
88 ^^"Locations: %S (in %s), %S (in %s).")
89 (Sym.to_string from_id)
91 (Sym.to_string from_id)
92 (Pos.to_string from_pos)
94 (Pos.to_string to_pos)
95 | Unknown_id {id; pos} ->
96 s "Unknown identifier %S in %s" (Sym.to_string id) (Pos.to_string pos)
97 | Unknown_type {ty_id; pos} ->
98 s "Unknown type %S in %s" (Sym.to_string ty_id) (Pos.to_string pos)
99 | Id_is_a_function {id; pos} ->
100 s "Identifier %S is a function, it cannot be used as a variable in %s"
101 (Sym.to_string id) (Pos.to_string pos)
102 | Id_not_a_function {id; pos} ->
103 s "Identifier %S is not a function, it cannot be called in %s"
104 (Sym.to_string id) (Pos.to_string pos)
105 | No_such_field_in_record {field; record; pos} ->
106 s "No field %S in record %S in %s"
107 (Sym.to_string field) (Typ.to_string record) (Pos.to_string pos)
108 | Exp_not_a_record {ty; pos} ->
109 s ( "The expression of type %S is not a record, it cannot be"
111 (Typ.to_string ty) (Pos.to_string pos)
112 | Exp_not_an_array {ty; pos} ->
113 s ( "The expression of type %S is not an array, it cannot be"
115 (Typ.to_string ty) (Pos.to_string pos)
116 | Wrong_type {expected; given; pos} ->
117 s "Type error: expected: %S, but given: %S, in %s"
118 (Typ.to_string expected)
119 (Typ.to_string given)
121 | Wrong_type_of_expression_in_var_dec {var_id; expected; given; pos} ->
122 s ( "Wrong type of expression in declaration of %S. "
123 ^^"Expected: %S, given: %S. In %s")
124 (Sym.to_string var_id)
125 (Typ.to_string expected)
126 (Typ.to_string given)
128 | Wrong_type_used_as_array {ty_id; ty; pos} ->
129 s ( "Identifier %S is bound to type %S, not an array. "
130 ^^"It cannot be used in %s")
131 (Sym.to_string ty_id) (Typ.to_string ty) (Pos.to_string pos)
132 | Wrong_type_used_as_record {ty_id; ty; pos} ->
133 s ( "Identifier %S is bound to type %S, not a record. "
134 ^^"It cannot be used in %s")
135 (Sym.to_string ty_id) (Typ.to_string ty) (Pos.to_string pos)
136 | Wrong_type_of_field_value {field_id; expected; given; pos} ->
137 s ( "Field %S is declared to be of type %S, but is bound to expression "
138 ^^"of type %S in %s")
139 (Sym.to_string field_id)
140 (Typ.to_string expected)
141 (Typ.to_string given)
143 | Wrong_type_of_arg {func; expected; given; pos} ->
144 s ( "Incorrect type of argument to function %S, expected: %S, given: %S,"
147 (Typ.to_string expected)
148 (Typ.to_string given)
150 | Wrong_number_of_args {func; expected; given; pos} ->
151 s ( "Incorrect number of arguments to function %S, "
152 ^^"expected: %d, given: %d,"
154 (Sym.to_string func) expected given (Pos.to_string pos)
155 | Invalid_operand_type {oper; valid; given; pos} ->
156 s ( "Invalid operand type %S for operator %S, which expects only: %s"
158 (Typ.to_string given)
160 (String.concat ", " valid)
162 | Different_operand_types {oper; left; right; pos} ->
163 s "Operands of different types (%S %S %S) given in %s"
166 (Typ.to_string right)
169 let is_unknown_id t =
173 | Break_outside_loop _
174 | Cycle_in_type_decs _
178 | Id_not_a_function _
179 | No_such_field_in_record _
183 | Wrong_type_of_expression_in_var_dec _
184 | Wrong_type_used_as_array _
185 | Wrong_type_used_as_record _
186 | Wrong_type_of_field_value _
187 | Wrong_type_of_arg _
188 | Wrong_number_of_args _
189 | Invalid_operand_type _
190 | Different_operand_types _ ->
193 let is_unknown_type t =
197 | Break_outside_loop _
198 | Cycle_in_type_decs _
202 | Id_not_a_function _
203 | No_such_field_in_record _
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 _ ->
217 let is_wrong_type t =
221 | Break_outside_loop _
222 | Cycle_in_type_decs _
227 | Id_not_a_function _
228 | No_such_field_in_record _
231 | Wrong_type_of_expression_in_var_dec _
232 | Wrong_type_used_as_array _
233 | Wrong_type_used_as_record _
234 | Wrong_type_of_field_value _
235 | Wrong_type_of_arg _
236 | Wrong_number_of_args _
237 | Invalid_operand_type _
238 | Different_operand_types _ ->
241 let is_wrong_number_of_args t =
243 | Wrong_number_of_args _ ->
245 | Break_outside_loop _
246 | Cycle_in_type_decs _
252 | Id_not_a_function _
253 | No_such_field_in_record _
256 | Wrong_type_of_expression_in_var_dec _
257 | Wrong_type_used_as_array _
258 | Wrong_type_used_as_record _
259 | Wrong_type_of_field_value _
260 | Wrong_type_of_arg _
261 | Invalid_operand_type _
262 | Different_operand_types _ ->
265 let is_invalid_syntax t =
267 | Invalid_syntax _ ->
269 | Break_outside_loop _
270 | Cycle_in_type_decs _
275 | Id_not_a_function _
276 | No_such_field_in_record _
279 | Wrong_type_of_expression_in_var_dec _
280 | Wrong_type_used_as_array _
281 | Wrong_type_used_as_record _
282 | Wrong_type_of_field_value _
283 | Wrong_type_of_arg _
284 | Wrong_number_of_args _
285 | Invalid_operand_type _
286 | Different_operand_types _ ->
289 let is_not_a_record t =
291 | Exp_not_a_record _ ->
293 | Break_outside_loop _
294 | Cycle_in_type_decs _
300 | Id_not_a_function _
301 | No_such_field_in_record _
303 | Wrong_type_of_expression_in_var_dec _
304 | Wrong_type_used_as_array _
305 | Wrong_type_used_as_record _
306 | Wrong_type_of_field_value _
307 | Wrong_type_of_arg _
308 | Wrong_number_of_args _
309 | Invalid_operand_type _
310 | Different_operand_types _ ->
313 let is_not_an_array t =
315 | Exp_not_an_array _ ->
317 | Break_outside_loop _
318 | Cycle_in_type_decs _
325 | Id_not_a_function _
326 | No_such_field_in_record _
327 | Wrong_type_of_expression_in_var_dec _
328 | Wrong_type_used_as_array _
329 | Wrong_type_used_as_record _
330 | Wrong_type_of_field_value _
331 | Wrong_type_of_arg _
332 | Wrong_number_of_args _
333 | Invalid_operand_type _
334 | Different_operand_types _ ->
337 let is_no_such_field_in_record t =
339 | No_such_field_in_record _ ->
341 | Break_outside_loop _
342 | Cycle_in_type_decs _
350 | Id_not_a_function _
351 | Wrong_type_of_expression_in_var_dec _
352 | Wrong_type_used_as_array _
353 | Wrong_type_used_as_record _
354 | Wrong_type_of_field_value _
355 | Wrong_type_of_arg _
356 | Wrong_number_of_args _
357 | Invalid_operand_type _
358 | Different_operand_types _ ->
361 let is_cycle_in_type_dec t =
363 | Cycle_in_type_decs _ ->
365 | Break_outside_loop _
366 | No_such_field_in_record _
374 | Id_not_a_function _
375 | Wrong_type_of_expression_in_var_dec _
376 | Wrong_type_used_as_array _
377 | Wrong_type_used_as_record _
378 | Wrong_type_of_field_value _
379 | Wrong_type_of_arg _
380 | Wrong_number_of_args _
381 | Invalid_operand_type _
382 | Different_operand_types _ ->
385 let is_break_outside_loop t =
387 | Break_outside_loop _ ->
389 | Cycle_in_type_decs _
390 | No_such_field_in_record _
398 | Id_not_a_function _
399 | Wrong_type_of_expression_in_var_dec _
400 | Wrong_type_used_as_array _
401 | Wrong_type_used_as_record _
402 | Wrong_type_of_field_value _
403 | Wrong_type_of_arg _
404 | Wrong_number_of_args _
405 | Invalid_operand_type _
406 | Different_operand_types _ ->