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 | Invalid_syntax of Pos.t
14 | Unknown_id of {id : Sym.t; pos : Pos.t}
15 | Unknown_type of {ty_id : Sym.t; pos : Pos.t}
16 | Id_is_a_function of {id : Sym.t; pos : Pos.t}
17 | Id_not_a_function of {id : Sym.t; pos : Pos.t}
18 | No_such_field_in_record of {field : Sym.t; record : Typ.t; pos : Pos.t}
19 | Exp_not_a_record of {ty : Typ.t; pos : Pos.t}
20 | Exp_not_an_array of {ty : Typ.t; pos : Pos.t}
26 | Wrong_type_of_expression_in_var_dec of
32 | Wrong_type_used_as_record of
37 | Wrong_type_used_as_array of
42 | Wrong_type_of_field_value of
48 | Wrong_type_of_arg of
54 | Wrong_number_of_args of
60 | Invalid_operand_type of
66 | Different_operand_types of
79 let s = Printf.sprintf in
81 | Invalid_syntax pos ->
82 s "Invalid syntax in %s" (Pos.to_string pos)
83 | Cycle_in_type_decs {from_id; from_pos; to_id; to_pos} ->
84 s ( "Circular type declaration between %S and %S."
85 ^^"Locations: %S (in %s), %S (in %s).")
86 (Sym.to_string from_id)
88 (Sym.to_string from_id)
89 (Pos.to_string from_pos)
91 (Pos.to_string to_pos)
92 | Unknown_id {id; pos} ->
93 s "Unknown identifier %S in %s" (Sym.to_string id) (Pos.to_string pos)
94 | Unknown_type {ty_id; pos} ->
95 s "Unknown type %S in %s" (Sym.to_string ty_id) (Pos.to_string pos)
96 | Id_is_a_function {id; pos} ->
97 s "Identifier %S is a function, it cannot be used as a variable in %s"
98 (Sym.to_string id) (Pos.to_string pos)
99 | Id_not_a_function {id; pos} ->
100 s "Identifier %S is not a function, it cannot be called in %s"
101 (Sym.to_string id) (Pos.to_string pos)
102 | No_such_field_in_record {field; record; pos} ->
103 s "No field %S in record %S in %s"
104 (Sym.to_string field) (Typ.to_string record) (Pos.to_string pos)
105 | Exp_not_a_record {ty; pos} ->
106 s ( "The expression of type %S is not a record, it cannot be"
108 (Typ.to_string ty) (Pos.to_string pos)
109 | Exp_not_an_array {ty; pos} ->
110 s ( "The expression of type %S is not an array, it cannot be"
112 (Typ.to_string ty) (Pos.to_string pos)
113 | Wrong_type {expected; given; pos} ->
114 s "Type error: expected: %S, but given: %S, in %s"
115 (Typ.to_string expected)
116 (Typ.to_string given)
118 | Wrong_type_of_expression_in_var_dec {var_id; expected; given; pos} ->
119 s ( "Wrong type of expression in declaration of %S. "
120 ^^"Expected: %S, given: %S. In %s")
121 (Sym.to_string var_id)
122 (Typ.to_string expected)
123 (Typ.to_string given)
125 | Wrong_type_used_as_array {ty_id; ty; pos} ->
126 s ( "Identifier %S is bound to type %S, not an array. "
127 ^^"It cannot be used in %s")
128 (Sym.to_string ty_id) (Typ.to_string ty) (Pos.to_string pos)
129 | Wrong_type_used_as_record {ty_id; ty; pos} ->
130 s ( "Identifier %S is bound to type %S, not a record. "
131 ^^"It cannot be used in %s")
132 (Sym.to_string ty_id) (Typ.to_string ty) (Pos.to_string pos)
133 | Wrong_type_of_field_value {field_id; expected; given; pos} ->
134 s ( "Field %S is declared to be of type %S, but is bound to expression "
135 ^^"of type %S in %s")
136 (Sym.to_string field_id)
137 (Typ.to_string expected)
138 (Typ.to_string given)
140 | Wrong_type_of_arg {func; expected; given; pos} ->
141 s ( "Incorrect type of argument to function %S, expected: %S, given: %S,"
144 (Typ.to_string expected)
145 (Typ.to_string given)
147 | Wrong_number_of_args {func; expected; given; pos} ->
148 s ( "Incorrect number of arguments to function %S, "
149 ^^"expected: %d, given: %d,"
151 (Sym.to_string func) expected given (Pos.to_string pos)
152 | Invalid_operand_type {oper; valid; given; pos} ->
153 s ( "Invalid operand type %S for operator %S, which expects only: %s"
155 (Typ.to_string given)
157 (String.concat ", " valid)
159 | Different_operand_types {oper; left; right; pos} ->
160 s "Operands of different types (%S %S %S) given in %s"
163 (Typ.to_string right)
166 let is_unknown_id t =
170 | Cycle_in_type_decs _
174 | Id_not_a_function _
175 | No_such_field_in_record _
179 | Wrong_type_of_expression_in_var_dec _
180 | Wrong_type_used_as_array _
181 | Wrong_type_used_as_record _
182 | Wrong_type_of_field_value _
183 | Wrong_type_of_arg _
184 | Wrong_number_of_args _
185 | Invalid_operand_type _
186 | Different_operand_types _ ->
189 let is_unknown_type t =
193 | Cycle_in_type_decs _
197 | Id_not_a_function _
198 | No_such_field_in_record _
202 | Wrong_type_of_expression_in_var_dec _
203 | Wrong_type_used_as_array _
204 | Wrong_type_used_as_record _
205 | Wrong_type_of_field_value _
206 | Wrong_type_of_arg _
207 | Wrong_number_of_args _
208 | Invalid_operand_type _
209 | Different_operand_types _ ->
212 let is_wrong_type t =
216 | Cycle_in_type_decs _
221 | Id_not_a_function _
222 | No_such_field_in_record _
225 | Wrong_type_of_expression_in_var_dec _
226 | Wrong_type_used_as_array _
227 | Wrong_type_used_as_record _
228 | Wrong_type_of_field_value _
229 | Wrong_type_of_arg _
230 | Wrong_number_of_args _
231 | Invalid_operand_type _
232 | Different_operand_types _ ->
235 let is_wrong_number_of_args t =
237 | Wrong_number_of_args _ ->
239 | Cycle_in_type_decs _
245 | Id_not_a_function _
246 | No_such_field_in_record _
249 | Wrong_type_of_expression_in_var_dec _
250 | Wrong_type_used_as_array _
251 | Wrong_type_used_as_record _
252 | Wrong_type_of_field_value _
253 | Wrong_type_of_arg _
254 | Invalid_operand_type _
255 | Different_operand_types _ ->
258 let is_invalid_syntax t =
260 | Invalid_syntax _ ->
262 | Cycle_in_type_decs _
267 | Id_not_a_function _
268 | No_such_field_in_record _
271 | Wrong_type_of_expression_in_var_dec _
272 | Wrong_type_used_as_array _
273 | Wrong_type_used_as_record _
274 | Wrong_type_of_field_value _
275 | Wrong_type_of_arg _
276 | Wrong_number_of_args _
277 | Invalid_operand_type _
278 | Different_operand_types _ ->
281 let is_not_a_record t =
283 | Exp_not_a_record _ ->
285 | Cycle_in_type_decs _
291 | Id_not_a_function _
292 | No_such_field_in_record _
294 | Wrong_type_of_expression_in_var_dec _
295 | Wrong_type_used_as_array _
296 | Wrong_type_used_as_record _
297 | Wrong_type_of_field_value _
298 | Wrong_type_of_arg _
299 | Wrong_number_of_args _
300 | Invalid_operand_type _
301 | Different_operand_types _ ->
304 let is_not_an_array t =
306 | Exp_not_an_array _ ->
308 | Cycle_in_type_decs _
315 | Id_not_a_function _
316 | No_such_field_in_record _
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 _ ->
327 let is_no_such_field_in_record t =
329 | No_such_field_in_record _ ->
331 | Cycle_in_type_decs _
339 | Id_not_a_function _
340 | Wrong_type_of_expression_in_var_dec _
341 | Wrong_type_used_as_array _
342 | Wrong_type_used_as_record _
343 | Wrong_type_of_field_value _
344 | Wrong_type_of_arg _
345 | Wrong_number_of_args _
346 | Invalid_operand_type _
347 | Different_operand_types _ ->
350 let is_cycle_in_type_dec t =
352 | Cycle_in_type_decs _ ->
354 | No_such_field_in_record _
362 | Id_not_a_function _
363 | Wrong_type_of_expression_in_var_dec _
364 | Wrong_type_used_as_array _
365 | Wrong_type_used_as_record _
366 | Wrong_type_of_field_value _
367 | Wrong_type_of_arg _
368 | Wrong_number_of_args _
369 | Invalid_operand_type _
370 | Different_operand_types _ ->