Check scope of break statements
[tiger.ml.git] / compiler / src / lib / tiger / tiger_error.ml
... / ...
CommitLineData
1module Abs = Tiger_absyn
2module Pos = Tiger_position
3module Sym = Tiger_symbol
4module Typ = Tiger_env_type
5
6type t =
7 | Cycle_in_type_decs of
8 { from_id : Sym.t
9 ; from_pos : Pos.t
10 ; to_id : Sym.t
11 ; to_pos : Pos.t
12 }
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}
22 | Wrong_type of
23 { expected : Typ.t
24 ; given : Typ.t
25 ; pos : Pos.t
26 }
27 | Wrong_type_of_expression_in_var_dec of
28 { var_id : Sym.t
29 ; expected : Typ.t
30 ; given : Typ.t
31 ; pos : Pos.t
32 }
33 | Wrong_type_used_as_record of
34 { ty_id : Sym.t
35 ; ty : Typ.t
36 ; pos : Pos.t
37 }
38 | Wrong_type_used_as_array of
39 { ty_id : Sym.t
40 ; ty : Typ.t
41 ; pos : Pos.t
42 }
43 | Wrong_type_of_field_value of
44 { field_id : Sym.t
45 ; expected : Typ.t
46 ; given : Typ.t
47 ; pos : Pos.t
48 }
49 | Wrong_type_of_arg of
50 { func : Sym.t
51 ; expected : Typ.t
52 ; given : Typ.t
53 ; pos : Pos.t
54 }
55 | Wrong_number_of_args of
56 { func : Sym.t
57 ; expected : int
58 ; given : int
59 ; pos : Pos.t
60 }
61 | Invalid_operand_type of
62 { oper : Abs.oper
63 ; valid : string list
64 ; given : Typ.t
65 ; pos : Pos.t
66 }
67 | Different_operand_types of
68 { oper : Abs.oper
69 ; left : Typ.t
70 ; right : Typ.t
71 ; pos : Pos.t
72 }
73
74exception T of t
75
76let raise t =
77 raise (T t)
78
79let to_string =
80 let s = Printf.sprintf in
81 function
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)
90 (Sym.to_string to_id)
91 (Sym.to_string from_id)
92 (Pos.to_string from_pos)
93 (Sym.to_string to_id)
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"
110 ^^"accessed in %s")
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"
114 ^^"accessed in %s")
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)
120 (Pos.to_string pos)
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)
127 (Pos.to_string pos)
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)
142 (Pos.to_string pos)
143 | Wrong_type_of_arg {func; expected; given; pos} ->
144 s ( "Incorrect type of argument to function %S, expected: %S, given: %S,"
145 ^^" in %s")
146 (Sym.to_string func)
147 (Typ.to_string expected)
148 (Typ.to_string given)
149 (Pos.to_string pos)
150 | Wrong_number_of_args {func; expected; given; pos} ->
151 s ( "Incorrect number of arguments to function %S, "
152 ^^"expected: %d, given: %d,"
153 ^^" in %s")
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"
157 ^^". In %s")
158 (Typ.to_string given)
159 (Abs.op_show oper)
160 (String.concat ", " valid)
161 (Pos.to_string pos)
162 | Different_operand_types {oper; left; right; pos} ->
163 s "Operands of different types (%S %S %S) given in %s"
164 (Typ.to_string left)
165 (Abs.op_show oper)
166 (Typ.to_string right)
167 (Pos.to_string pos)
168
169let is_unknown_id t =
170 match t with
171 | Unknown_id _ ->
172 true
173 | Break_outside_loop _
174 | Cycle_in_type_decs _
175 | Invalid_syntax _
176 | Unknown_type _
177 | Id_is_a_function _
178 | Id_not_a_function _
179 | No_such_field_in_record _
180 | Exp_not_a_record _
181 | Exp_not_an_array _
182 | Wrong_type _
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 _ ->
191 false
192
193let is_unknown_type t =
194 match t with
195 | Unknown_type _ ->
196 true
197 | Break_outside_loop _
198 | Cycle_in_type_decs _
199 | Unknown_id _
200 | Invalid_syntax _
201 | Id_is_a_function _
202 | Id_not_a_function _
203 | No_such_field_in_record _
204 | Exp_not_a_record _
205 | Exp_not_an_array _
206 | Wrong_type _
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
217let is_wrong_type t =
218 match t with
219 | Wrong_type _ ->
220 true
221 | Break_outside_loop _
222 | Cycle_in_type_decs _
223 | Unknown_type _
224 | Unknown_id _
225 | Invalid_syntax _
226 | Id_is_a_function _
227 | Id_not_a_function _
228 | No_such_field_in_record _
229 | Exp_not_a_record _
230 | Exp_not_an_array _
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 _ ->
239 false
240
241let is_wrong_number_of_args t =
242 match t with
243 | Wrong_number_of_args _ ->
244 true
245 | Break_outside_loop _
246 | Cycle_in_type_decs _
247 | Wrong_type _
248 | Unknown_type _
249 | Unknown_id _
250 | Invalid_syntax _
251 | Id_is_a_function _
252 | Id_not_a_function _
253 | No_such_field_in_record _
254 | Exp_not_a_record _
255 | Exp_not_an_array _
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 _ ->
263 false
264
265let is_invalid_syntax t =
266 match t with
267 | Invalid_syntax _ ->
268 true
269 | Break_outside_loop _
270 | Cycle_in_type_decs _
271 | Wrong_type _
272 | Unknown_type _
273 | Unknown_id _
274 | Id_is_a_function _
275 | Id_not_a_function _
276 | No_such_field_in_record _
277 | Exp_not_a_record _
278 | Exp_not_an_array _
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 _ ->
287 false
288
289let is_not_a_record t =
290 match t with
291 | Exp_not_a_record _ ->
292 true
293 | Break_outside_loop _
294 | Cycle_in_type_decs _
295 | Invalid_syntax _
296 | Wrong_type _
297 | Unknown_type _
298 | Unknown_id _
299 | Id_is_a_function _
300 | Id_not_a_function _
301 | No_such_field_in_record _
302 | Exp_not_an_array _
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 _ ->
311 false
312
313let is_not_an_array t =
314 match t with
315 | Exp_not_an_array _ ->
316 true
317 | Break_outside_loop _
318 | Cycle_in_type_decs _
319 | Exp_not_a_record _
320 | Invalid_syntax _
321 | Wrong_type _
322 | Unknown_type _
323 | Unknown_id _
324 | Id_is_a_function _
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 _ ->
335 false
336
337let is_no_such_field_in_record t =
338 match t with
339 | No_such_field_in_record _ ->
340 true
341 | Break_outside_loop _
342 | Cycle_in_type_decs _
343 | Exp_not_an_array _
344 | Exp_not_a_record _
345 | Invalid_syntax _
346 | Wrong_type _
347 | Unknown_type _
348 | Unknown_id _
349 | Id_is_a_function _
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 _ ->
359 false
360
361let is_cycle_in_type_dec t =
362 match t with
363 | Cycle_in_type_decs _ ->
364 true
365 | Break_outside_loop _
366 | No_such_field_in_record _
367 | Exp_not_an_array _
368 | Exp_not_a_record _
369 | Invalid_syntax _
370 | Wrong_type _
371 | Unknown_type _
372 | Unknown_id _
373 | Id_is_a_function _
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 _ ->
383 false
384
385let is_break_outside_loop t =
386 match t with
387 | Break_outside_loop _ ->
388 true
389 | Cycle_in_type_decs _
390 | No_such_field_in_record _
391 | Exp_not_an_array _
392 | Exp_not_a_record _
393 | Invalid_syntax _
394 | Wrong_type _
395 | Unknown_type _
396 | Unknown_id _
397 | Id_is_a_function _
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 _ ->
407 false
This page took 0.022937 seconds and 4 git commands to generate.