summary refs log tree commit diff
path: root/source/xi_lib/typechecker_errors.ml
blob: 4799a6bb040da073320e692ac5892a11bc6e3c2b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
open Ast
open Types

type type_checking_error =

    (* Ogólny błąd, że oczekiwany typ się nie zgadza z rzeczywistym *)
  | TCERR_TypeMismatch of
    { loc: location
    ; expected: normal_type
    ; actual: normal_type
    }

    (* Błąd typu przy multireturn *)
  | TCERR_BindingTypeMismatch of
    { loc: location
    ; expected: normal_type
    ; actual: normal_type
    ; id: identifier
    }

    (* Wywołaliśmy procedurę/funkcję ze złą liczbą argumentów *)
  | TCERR_BadNumberOfActualArguments of
    { loc: location
    ; expected: int
    ; actual: int
    }

    (* Return dostał złą liczbę parametrów *)
  | TCERR_BadNumberOfReturnValues of
    { loc: location
    ; expected: int
    ; actual: int
    }

    (* Nieznany identyfikator *)
  | TCERR_UnknownIdentifier of
    { loc: location
    ; id: identifier
    }

    (* Identyfikator nie jest zmienną (a np jest funkcją) *)
  | TCERR_IdentifierIsNotVariable of
    { loc: location
    ; id: identifier
    }

    (* Generyczny błąd gdyby inne nie pasowały *)
  | TCERR_OtherError of
    { loc: location
    ; descr: string
    }

    (* Wywołujemy identyfikator, który nie jest funkcją/procedurą *)
  | TCERR_IdentifierIsNotCallable of
    { loc: location
    ; id: identifier
    }

    (* Brakuje return gdzieś *)
  | TCERR_NotAllControlPathsReturnValue of
    { loc: location
    ; id: identifier
    }

    (* Użyliśmy w wyrażeniu multireturnowej funkcji *)
  | TCERR_ExpectedFunctionReturningOneValue of
    { loc: location
    ; id: identifier
    }

    (* Użyliśmy w multireturnie zywkłej funkcji *)
  | TCERR_ExpectedFunctionReturningManyValues of
    { loc: location
    ; expected: int
    ; actual: int
    ; id: identifier
    }

    (* Return w procedurze *)
  | TCERR_ProcedureCannotReturnValue of
    { loc: location
    }

    (* Brakuje parametru w return *)
  | TCERR_FunctionMustReturnValue of
    { loc: location
    }
  
    (* Typ miał być tablicą *)
  | TCERR_ExpectedArray of
    { loc: location
    ; actual: normal_type
    }

    (* Przykryliśmy nazwę *)
  | TCERR_ShadowsPreviousDefinition of
    { loc: location
    ; id: identifier
    }

    (* W wyrażeniu typowym pojawiło się wyrażenie oznaczające rozmiar *)
  | TCERR_ArrayInitializationForbidden of
    { loc: location }

    (* Generyczny błąd, że nie dało się zrekonstruować typu *)
  | TCERR_CannotInferType of
    { loc: location }

let string_of_type_checking_error = function
  | TCERR_TypeMismatch {loc; actual; expected} ->
    Format.sprintf "%s: error: type mismatch: expected %s; got %s"
      (string_of_location loc)
      (string_of_normal_type expected)
      (string_of_normal_type actual)

  | TCERR_BindingTypeMismatch {loc; actual; expected; id} ->
    Format.sprintf "%s: error: type mismatch: expected %s; got %s; binding %s"
      (string_of_location loc)
      (string_of_normal_type expected)
      (string_of_normal_type actual)
      (string_of_identifier id)

  | TCERR_BadNumberOfActualArguments {loc; actual; expected} ->
    Format.sprintf "%s: error: bad number of actual arguments: expected %u; got %u"
      (string_of_location loc)
      (expected)
      (actual)

  | TCERR_BadNumberOfReturnValues {loc; actual; expected} ->
    Format.sprintf "%s: error: bad number of return values: expected %u; got %u"
      (string_of_location loc)
      (expected)
      (actual)

  | TCERR_UnknownIdentifier {loc; id} ->
    Format.sprintf "%s: unknown identifier: %s"
      (string_of_location loc)
      (string_of_identifier id)

  | TCERR_IdentifierIsNotVariable {loc; id} ->
    Format.sprintf "%s: identifier is not a variable: %s"
      (string_of_location loc)
      (string_of_identifier id)

  | TCERR_IdentifierIsNotCallable {loc; id} ->
    Format.sprintf "%s: identifier is not callable: %s"
      (string_of_location loc)
      (string_of_identifier id)

  | TCERR_OtherError {loc; descr} ->
    Format.sprintf "%s: error: %s"
      (string_of_location loc)
      descr

  | TCERR_NotAllControlPathsReturnValue {loc; id} ->
    Format.sprintf "%s: not all control paths return value: %s"
      (string_of_location loc)
      (string_of_identifier id)

  | TCERR_ExpectedFunctionReturningOneValue {loc; id} ->
    Format.sprintf "%s: expected function returning exactly one value: %s"
      (string_of_location loc)
      (string_of_identifier id)

  | TCERR_ExpectedFunctionReturningManyValues {loc; id; expected; actual} ->
    Format.sprintf "%s: expected function returning %u values, not %u: %s"
      (string_of_location loc)
      expected actual
      (string_of_identifier id)

  | TCERR_ExpectedArray {loc; actual} ->
    Format.sprintf "%s: expected array, not: %s"
      (string_of_location loc)
      (string_of_normal_type actual)

  | TCERR_FunctionMustReturnValue {loc} ->
    Format.sprintf "%s: function must return something"
      (string_of_location loc)

  | TCERR_ProcedureCannotReturnValue {loc} ->
    Format.sprintf "%s: procedure cannot return value"
      (string_of_location loc)

  | TCERR_ShadowsPreviousDefinition {loc; id} ->
    Format.sprintf "%s: shadows previous definition: %s"
      (string_of_location loc)
      (string_of_identifier id)

  | TCERR_ArrayInitializationForbidden {loc} ->
    Format.sprintf "%s: array initialization is forbidden here"
      (string_of_location loc)

  | TCERR_CannotInferType {loc} ->
    Format.sprintf "%s: cannot infer type"
      (string_of_location loc)




  module MakeCollectingErrorReporter () = struct

    let r = ref []

    let add e = r := e :: !r

    let wrap f x =
      let result = f x in
      let errors = List.rev !r in
      r := [];
      match errors with
      | [] -> Ok result
      | xs -> Error xs

    let report_type_mismatch ~loc ~expected ~actual =
      add @@ TCERR_TypeMismatch {loc;expected;actual}

    let report_binding_type_mismatch ~loc ~expected ~actual ~id =
      add @@ TCERR_BindingTypeMismatch {loc;expected;actual; id}

    let report_other_error ~loc ~descr =
      add @@ TCERR_OtherError {loc; descr}

    let report_identifier_is_not_variable ~loc ~id =
      add @@ TCERR_IdentifierIsNotVariable {loc; id}

    let report_unknown_identifier ~loc ~id =
      add @@ TCERR_UnknownIdentifier {loc; id}

    let report_identifier_is_not_callable ~loc ~id =
      add @@ TCERR_IdentifierIsNotCallable {loc; id}

    let report_bad_number_of_arguments ~loc ~expected ~actual =
      add @@ TCERR_BadNumberOfActualArguments {loc; expected; actual}

    let report_bad_number_of_return_values ~loc ~expected ~actual =
      add @@ TCERR_BadNumberOfReturnValues {loc; expected; actual}

    let report_expected_function_returning_one_value ~loc ~id =
      add @@ TCERR_ExpectedFunctionReturningOneValue {loc;id}

    let report_expected_function_returning_many_values ~loc ~id ~expected ~actual =
      add @@ TCERR_ExpectedFunctionReturningManyValues {loc;id; expected;actual}

    let report_function_must_return_something ~loc =
      add @@ TCERR_FunctionMustReturnValue {loc}

    let report_procedure_cannot_return_value ~loc =
      add @@ TCERR_ProcedureCannotReturnValue {loc}

    let report_expected_array ~loc ~actual =
      add @@ TCERR_ExpectedArray {loc; actual}

    let report_not_all_control_paths_return_value ~loc ~id =
      add @@ TCERR_NotAllControlPathsReturnValue {loc; id}

    let report_shadows_previous_definition ~loc ~id =
      add @@ TCERR_ShadowsPreviousDefinition {loc; id}

    let report_array_initialization_forbidden ~loc =
      add @@ TCERR_ArrayInitializationForbidden {loc}

    let report_cannot_infer ~loc =
      add @@ TCERR_CannotInferType {loc}


  end

  (* Kopia poniewaz chcemy aby typ wyniku funkcji `add` był dowolny bo sterowanie się kończy przez raise *)
  module MakeOneShotErrorReporter() = struct 

    exception TypeCheckingError of type_checking_error

    let add e = raise (TypeCheckingError e)

    let wrap f x =
      try
        Ok (f x)
      with (TypeCheckingError e) ->
        Error [e]

    let report_type_mismatch ~loc ~expected ~actual =
      add @@ TCERR_TypeMismatch {loc;expected;actual}

    let report_binding_type_mismatch ~loc ~expected ~actual ~id =
      add @@ TCERR_BindingTypeMismatch {loc;expected;actual; id}

    let report_other_error ~loc ~descr =
      add @@ TCERR_OtherError {loc; descr}

    let report_identifier_is_not_variable ~loc ~id =
      add @@ TCERR_IdentifierIsNotVariable {loc; id}

    let report_unknown_identifier ~loc ~id =
      add @@ TCERR_UnknownIdentifier {loc; id}

    let report_identifier_is_not_callable ~loc ~id =
      add @@ TCERR_IdentifierIsNotCallable {loc; id}

    let report_bad_number_of_arguments ~loc ~expected ~actual =
      add @@ TCERR_BadNumberOfActualArguments {loc; expected; actual}

    let report_bad_number_of_return_values ~loc ~expected ~actual =
      add @@ TCERR_BadNumberOfReturnValues {loc; expected; actual}

    let report_expected_function_returning_one_value ~loc ~id =
      add @@ TCERR_ExpectedFunctionReturningOneValue {loc;id}

    let report_expected_function_returning_many_values ~loc ~id ~expected ~actual =
      add @@ TCERR_ExpectedFunctionReturningManyValues {loc;id; expected;actual}

    let report_function_must_return_something ~loc =
      add @@ TCERR_FunctionMustReturnValue {loc}

    let report_procedure_cannot_return_value ~loc =
      add @@ TCERR_ProcedureCannotReturnValue {loc}

    let report_expected_array ~loc ~actual =
      add @@ TCERR_ExpectedArray {loc; actual}

    let report_not_all_control_paths_return_value ~loc ~id =
      add @@ TCERR_NotAllControlPathsReturnValue {loc; id}

    let report_shadows_previous_definition ~loc ~id =
      add @@ TCERR_ShadowsPreviousDefinition {loc; id}

    let report_array_initialization_forbidden ~loc =
      add @@ TCERR_ArrayInitializationForbidden {loc}

    let report_cannot_infer ~loc =
      add @@ TCERR_CannotInferType {loc}
  
  end