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
|
open Xi_lib
open Ast
(* W Xi_lib.Types są definicje typów i środowiska typowego *)
open Types
module Make() = struct
(* Logger: używa się go jak Format.printf *)
let logf fmt = Logger.make_logf __MODULE__ fmt
module Check () = struct
(* Zgłaszaczka błędów *)
module ErrorReporter = Typechecker_errors.MakeOneShotErrorReporter ()
(* Hashtablica którą zwracamy jak wszystko jest OK.
* Mapuje znacznik węzła na przypisany typ. Dzięki tej tablicy
* późniejsze etapy kompilatora będą miały dostęp do policzonych
* typów wyrażeń
*
* Jeżeli typowanie się powiedzie to zawartość tablicy wydrukuje się
* do pliku xilog/004.typechecking.types
*)
let node2type_map = Hashtbl.create 513
let rec te2type = function
| TEXPR_Int _ -> TP_Int
| TEXPR_Bool _ -> TP_Bool
| TEXPR_Array {sub;_} -> TP_Array (te2type sub)
let add_var_decl env (VarDecl {loc;id;tp}) =
match TypingEnvironment.add id (ENVTP_Var (te2type tp)) env with
| (env,true) -> env
| (env,false) -> ErrorReporter.report_shadows_previous_definition ~loc ~id
(* --------------------------------------------------- *)
(* Funkcja nakładka na inferencję, jej zadanie to uzupełniać hashtablicę node2type_map *)
let rec infer_expression env e =
let tp = _infer_expression env e in
Hashtbl.replace node2type_map (tag_of_expression e) tp;
logf "%s: inferred type %s"
(string_of_location
(location_of_expression e))
(string_of_normal_type tp);
tp
(* --------------------------------------------------- *)
(* Oddolna strategia *)
and _infer_expression env = function
| EXPR_Id {id; loc; _} ->
begin
match TypingEnvironment.lookup id env with
| None -> ErrorReporter.report_unknown_identifier ~loc ~id
| Some tp -> match tp with
| ENVTP_Var tp -> tp
| ENVTP_Fn (_,_) -> ErrorReporter.report_identifier_is_not_variable ~loc ~id
end
| EXPR_Int _ ->
TP_Int
| EXPR_Char _ ->
TP_Int
| EXPR_Bool _ ->
TP_Bool
| EXPR_Index {expr;index;loc; _} ->
check_expression env TP_Int index;
begin
match infer_expression env expr with
| (TP_Array _) as tp -> tp
| TP_Int -> ErrorReporter.report_expected_array ~loc ~actual:TP_Int
| TP_Bool -> ErrorReporter.report_expected_array ~loc ~actual:TP_Bool
end
| EXPR_Call call ->
check_function_call env call
| EXPR_Length {arg;loc;_} ->
begin
match infer_expression env arg with
| TP_Array _ -> TP_Int
| TP_Int -> ErrorReporter.report_expected_array ~loc ~actual:TP_Int
| TP_Bool -> ErrorReporter.report_expected_array ~loc ~actual:TP_Bool
end
| EXPR_Relation {lhs; rhs; op=RELOP_Ge; _}
| EXPR_Relation {lhs; rhs; op=RELOP_Gt; _}
| EXPR_Relation {lhs; rhs; op=RELOP_Lt; _}
| EXPR_Relation {lhs; rhs; op=RELOP_Le; _} ->
check_expression env TP_Int lhs;
check_expression env TP_Int rhs;
TP_Bool
| EXPR_Relation {lhs; rhs; op=RELOP_Eq; _}
| EXPR_Relation {lhs; rhs; op=RELOP_Ne; _} ->
let tp = infer_expression env lhs in
check_expression env tp rhs;
TP_Bool
(* Consider backtracking *)
(* Reguła dla dodawania, jak w treści zadania *)
| EXPR_Binop {loc; lhs; rhs; op=BINOP_Add; _} ->
begin match infer_expression env lhs with
| (TP_Array _) as tp
| (TP_Int as tp) ->
check_expression env tp rhs;
tp
| _ ->
let descr = "operator + expects integer or array" in
ErrorReporter.report_other_error ~loc ~descr
end
| EXPR_Binop {lhs; rhs; op=BINOP_And;_}
| EXPR_Binop {lhs; rhs; op=BINOP_Or; _} ->
check_expression env TP_Bool lhs;
check_expression env TP_Bool rhs;
TP_Bool
| EXPR_Binop {lhs; rhs; op=BINOP_Sub;_}
| EXPR_Binop {lhs; rhs; op=BINOP_Rem;_}
| EXPR_Binop {lhs; rhs; op=BINOP_Mult;_}
| EXPR_Binop {lhs; rhs; op=BINOP_Div; _} ->
check_expression env TP_Int lhs;
check_expression env TP_Int rhs;
TP_Int
| EXPR_Unop {op=UNOP_Neg; sub; _} ->
check_expression env TP_Int sub;
TP_Int
| EXPR_Unop {op=UNOP_Not; sub; _} ->
check_expression env TP_Bool sub;
TP_Bool
| EXPR_String _ ->
TP_Array TP_Int
| EXPR_Struct {elements=[]; loc; _} ->
ErrorReporter.report_cannot_infer ~loc
| EXPR_Struct {elements=x::xs; _} ->
failwith "not yet implemented"
and check_function_call env call =
failwith "not yet implemented"
(* --------------------------------------------------- *)
(* Odgórna strategia: zapish w node2type_map oczekiwanie a następnie
* sprawdź czy nie zachodzi specjalny przypadek. *)
and check_expression env expected e =
logf "%s: checking expression against %s"
(string_of_location (location_of_expression e))
(string_of_normal_type expected);
Hashtbl.replace node2type_map (tag_of_expression e) expected;
(* Sprawdzamy specjalne przypadki *)
match e, expected with
(* Mamy sprawdzić `{elements...}` kontra `tp[]`, czyli sprawdzamy
* elementy kontra typ elementu tablicy `tp` *)
| EXPR_Struct {elements; _}, TP_Array tp ->
List.iter (check_expression env tp) elements
(* ========== !! Zaimplementuj pozostale przypadki !! ========= *)
(* Fallback do strategii oddolnej *)
| _ ->
let actual = infer_expression env e in
if actual <> expected then
ErrorReporter.report_type_mismatch
~loc:(location_of_expression e)
~actual
~expected
(* --------------------------------------------------- *)
(* Pomocnicza funkcja do sprawdzania wywołania procedury *)
let check_procedure_call env call : unit =
failwith "not yet implemented"
(* --------------------------------------------------- *)
(* Rekonstrukcja typu dla lvalue *)
let infer_lvalue env = function
| LVALUE_Id {id;loc;_} ->
failwith "not yet implemented"
| LVALUE_Index {index; sub; loc} ->
failwith "not yet implemented"
(* --------------------------------------------------- *)
(* Sprawdzanie statementów *)
let rec check_statement env = function
(* Proste, wyinferuj typ `lhs` i sprawdź `rhs` kontra wynik *)
| STMT_Assign {lhs; rhs; _} ->
let lhs_tp = infer_lvalue env lhs in
check_expression env lhs_tp rhs;
env, RT_Unit
| STMT_MultiVarDecl {vars; init; _} ->
failwith "not yet implemented"
| STMT_Block body ->
check_statement_block env body
| STMT_Call call ->
check_procedure_call env call;
env, RT_Unit
| STMT_If {cond;then_branch;else_branch; _} ->
failwith "not yet implemented"
| STMT_Return {values;loc} ->
failwith "not yet implemented"
| STMT_VarDecl {var; init} ->
begin
match var with
| VarDecl {tp;_} ->
let t=te2type (tp) in
begin
match init with
| Some init -> check_expression env t init
| None -> ()
end;
(add_var_decl env var),RT_Unit
end
| STMT_While {cond; body; _} ->
failwith "not yet implemented"
and check_statement_block env (STMTBlock {body; _}) =
failwith "not yet implemented"
(* --------------------------------------------------- *)
(* Top-level funkcje *)
let check_global_declaration env = function
| GDECL_Function {formal_parameters; return_types; body; loc; id; _} ->
(* Sprawdź wszystko *)
let env = List.fold_left add_var_decl env formal_parameters in
match body with
| Some body -> begin
match check_statement_block (TypingEnvironment.set_return env (List.map te2type return_types)) body with
| _, RT_Unit -> ErrorReporter.report_not_all_control_paths_return_value ~loc ~id
| _, RT_Void -> ()
end
| None -> ()
let scan_global_declaration env = function
| GDECL_Function {id; formal_parameters; return_types; loc; _} ->
match TypingEnvironment.add id (ENVTP_Fn ((List.map (fun (VarDecl x) -> te2type(x.tp)) formal_parameters),(List.map te2type return_types)) ) env with
| (env,true) -> env
| (env,false) -> ErrorReporter.report_shadows_previous_definition ~loc ~id
let scan_module env (ModuleDefinition {global_declarations; _}) =
List.fold_left scan_global_declaration env global_declarations
let check_module env (ModuleDefinition {global_declarations; _}) =
List.iter (check_global_declaration env) global_declarations
(* --------------------------------------------------- *)
(* Przetwórz moduł *)
let process_module env mdef =
(* Wpierw przeskanuj globalne definicje aby uzupełnić środowisko *)
let env = scan_module env mdef in
(* Zweryfikuj wszystko *)
check_module env mdef
let computation mdef =
(* Zaczynamy z pustym środowiskiem *)
let env = TypingEnvironment.empty in
process_module env mdef;
node2type_map
end
(* --------------------------------------------------- *)
(* Procedura wejściowa *)
let check_module mdef =
(* Stwórz instancję typecheckera i ją odpal *)
let module M = Check() in
M.ErrorReporter.wrap M.computation mdef
end
|