Fix - check number of arguments in function calls
[tiger.ml.git] / compiler / src / lib / tiger / tiger_semant.ml
CommitLineData
523e2b06
SK
1module List = ListLabels
2
155ee327
SK
3module A = Tiger_absyn
4module Env = Tiger_env
5module E = Tiger_error
cbb4ffb6 6module Symbol = Tiger_symbol
155ee327
SK
7module Translate = Tiger_translate
8module Type = Tiger_env_type
9module Value = Tiger_env_value
10
11(* The only reason for having this seemingly-superfluous inner module is to
12 * have this nice signature as a summary of what each function does. *)
13module Semant : sig
14 type expty =
15 { exp : Translate.exp
16 ; ty : Type.t
17 }
18
19 (* Violating normal naming convention just to make it easier to follow
20 * Appel's
21 *)
22 val transExp : env:Env.t -> A.exp -> expty
862e5c05
SK
23
24 (* transVar does not seem to be needed, as trvar handles all our cases.
25 * Am I wrong?
26 *
27 * val transVar : env:Env.t -> A.var -> expty
28 *
29 *)
155ee327
SK
30end = struct
31 type expty =
32 { exp : Translate.exp
33 ; ty : Type.t
34 }
35
cbb4ffb6
SK
36 let rec actual_ty ty ~pos =
37 match ty with
38 | Type.Name (name, ty_opt_ref) ->
39 (match !ty_opt_ref with
40 | None ->
41 E.raise (E.Unknown_type {ty_id=name; pos})
42 | Some ty ->
43 actual_ty ty ~pos
44 )
45 | Type.Unit
46 | Type.Nil
47 | Type.Int
48 | Type.String
49 | Type.Record _
50 | Type.Array _ ->
51 ty
52
523e2b06
SK
53 let return ty = {exp = (); ty}
54 let return_unit = return Type.Unit
55 let return_nil = return Type.Nil
56 let return_int = return Type.Int
57 let return_string = return Type.String
155ee327 58
523e2b06
SK
59 let env_get_typ ~sym ~env ~pos : Type.t =
60 match Env.get_typ env sym with
61 | Some ty -> ty
62 | None -> E.raise (E.Unknown_type {ty_id=sym; pos})
63
cbb4ffb6
SK
64 let env_get_typ_actual ~sym ~env ~pos : Type.t =
65 actual_ty (env_get_typ ~sym ~env ~pos) ~pos
66
523e2b06
SK
67 let env_get_val ~sym ~env ~pos : Value.t =
68 match Env.get_val env sym with
69 | Some ty -> ty
70 | None -> E.raise (E.Unknown_id {id=sym; pos})
71
72 let check_same {exp=_; ty=ty_left} {exp=_; ty=ty_right} ~pos : unit =
73 if Type.is_equal ty_left ty_right then
74 ()
75 else
76 E.raise (E.Wrong_type {expected=ty_left; given=ty_right; pos})
77
78 let check_int expty ~pos : unit =
cbb4ffb6 79 check_same return_int expty ~pos
978cb41c 80
523e2b06
SK
81 let rec transExp ~env exp =
82 let rec trexp exp =
83 (match exp with
84 | A.NilExp ->
85 return_nil
86 | A.IntExp _ ->
87 return_int
88 | A.StringExp {string=_; _} ->
89 return_string
978cb41c
SK
90 | A.CallExp {func; args; pos} ->
91 (match env_get_val ~sym:func ~env ~pos with
92 | Value.Fun {formals; result} ->
54e838f4
SK
93 let expected = List.length formals in
94 let given = List.length args in
95 if given = expected then
96 begin
97 List.iter2 formals args ~f:(fun ty_expected exp_given ->
98 check_same
99 (return (actual_ty ~pos ty_expected))
100 (trexp exp_given)
101 ~pos;
102 );
103 return (actual_ty ~pos result)
104 end
105 else
106 E.raise (E.Wrong_number_of_args {func; expected; given; pos})
978cb41c
SK
107 | Value.Var _ ->
108 E.raise (E.Id_not_a_function {id=func; pos})
109 )
523e2b06
SK
110 | A.OpExp {oper; left; right; pos} ->
111 trop oper ~left ~right ~pos
0ed7a07c 112 | A.RecordExp {fields=field_exps; typ; pos} ->
cbb4ffb6 113 let ty = env_get_typ_actual ~sym:typ ~env ~pos in
0ed7a07c
SK
114 Type.if_record
115 ty
116 ~f:(fun field_tys ->
117 List.iter field_exps ~f:(fun (field, exp, pos) ->
118 (match List.assoc_opt field field_tys with
119 | Some field_ty ->
cbb4ffb6 120 check_same (return (actual_ty ~pos field_ty)) (trexp exp) ~pos
0ed7a07c
SK
121 | None ->
122 E.raise
123 (E.No_such_field_in_record {field; record=ty; pos})
124 )
125 )
126 )
127 ~otherwise:(fun () ->
128 E.raise (E.Wrong_type_used_as_record {ty_id=typ; ty; pos})
129 );
cbb4ffb6 130 return (actual_ty ~pos ty)
9d8471b2 131 | A.SeqExp [] ->
523e2b06 132 return_unit
9d8471b2
SK
133 | A.SeqExp exps ->
134 let last xs =
135 xs
136 |> List.rev (* Yes, redundant, but clean-looking ;-P *)
137 |> List.hd (* Empty is matched in above SeqExp match case *)
138 in
139 exps
cbb4ffb6 140 |> List.map ~f:(fun (exp, _) -> trexp exp)
9d8471b2 141 |> last
523e2b06
SK
142 | A.AssignExp {var; exp; pos} ->
143 check_same (trvar var) (trexp exp) ~pos;
144 (* TODO: Add var->exp to val env? *)
145 return_unit
146 | A.IfExp {test; then'; else'; pos} ->
147 (* test : must be int, because we have no bool *)
148 (* then : must equal else *)
149 (* else : must equal then or be None *)
150 check_int (trexp test) ~pos;
151 (match (trexp then', else') with
152 | expty_then, None ->
153 expty_then
154 | expty_then, Some else' ->
155 let expty_else = trexp else' in
156 check_same expty_then expty_else ~pos;
157 expty_then
158 )
159 | A.WhileExp {test; body; pos} ->
160 (* test : must be int, because we have no bool *)
161 check_int (trexp test) ~pos;
162 ignore (trexp body); (* Only care if a type-error is raised *)
163 return_unit
164 | A.ForExp {var; lo; hi; body; pos; escape=_} ->
165 check_int (trexp lo) ~pos;
166 check_int (trexp hi) ~pos;
167 (* Only care if a type-error is raised *)
85e08b69
SK
168 let env = Env.set_val env var (Value.Var {ty = Type.Int}) in
169 ignore (transExp ~env body);
523e2b06
SK
170 return_unit
171 | A.BreakExp _ ->
172 return_unit
8744eb3a
SK
173 | A.LetExp {decs; body; pos=_} ->
174 (* (1) decs augment env *)
175 (* (2) body checked against the new env *)
176 let env =
177 List.fold_left decs ~init:env ~f:(fun env dec -> transDec dec ~env)
178 in
179 transExp body ~env
4c550cd5
SK
180 | A.ArrayExp {typ; size; init; pos} ->
181 check_int (trexp size) ~pos;
cbb4ffb6 182 let ty = env_get_typ_actual ~sym:typ ~env ~pos in
4c550cd5
SK
183 Type.if_array
184 ty
185 ~f:(fun ty_elements ->
cbb4ffb6 186 check_same (return (actual_ty ~pos ty_elements)) (trexp init) ~pos
4c550cd5
SK
187 )
188 ~otherwise:(fun () ->
189 E.raise (E.Wrong_type_used_as_array {ty_id=typ; ty; pos})
190 );
cbb4ffb6 191 return (actual_ty ~pos ty)
523e2b06
SK
192 | A.VarExp var ->
193 trvar var
194 )
195 and trvar =
196 (function
197 | A.SimpleVar {symbol=sym; pos} ->
198 (match env_get_val ~sym ~env ~pos with
199 | Value.Fun _ -> E.raise (E.Id_is_a_function {id=sym; pos})
cbb4ffb6 200 | Value.Var {ty} -> return (actual_ty ~pos ty)
523e2b06
SK
201 )
202 | A.FieldVar {var; symbol; pos} ->
203 let {exp=_; ty} = trvar var in
204 Type.if_record
205 ty
206 ~f:(fun fields ->
207 (match List.assoc_opt symbol fields with
208 | None ->
209 E.raise
210 (E.No_such_field_in_record {field=symbol; record=ty; pos})
211 | Some ty ->
cbb4ffb6 212 return (actual_ty ~pos ty)
523e2b06
SK
213 )
214 )
215 ~otherwise:(fun () -> E.raise (E.Exp_not_a_record {ty; pos}))
161a300d
SK
216 | A.SubscriptVar {var; exp; pos} ->
217 let {exp=_; ty} = trvar var in
218 check_int (trexp exp) ~pos;
219 Type.if_array
220 ty
cbb4ffb6 221 ~f:(fun ty_elements -> return (actual_ty ~pos ty_elements))
161a300d 222 ~otherwise:(fun () -> E.raise (E.Exp_not_an_array {ty; pos}))
523e2b06
SK
223 )
224 and trop oper ~left ~right ~pos =
cbb4ffb6 225 (* TODO: Refactor trop - all opers return bool/int *)
523e2b06
SK
226 let expty_left = trexp left in
227 let expty_right = trexp right in
228 check_same expty_left expty_right ~pos;
229 let {exp=_; ty} = expty_left in
230 let module T = Type in
231 (match oper with
232 (* Arithmetic: int *)
233 | A.PlusOp
234 | A.MinusOp
235 | A.TimesOp
236 | A.DivideOp ->
237 check_int expty_left ~pos;
238 return_int
239 (* Equality: int, string, array, record *)
240 | A.EqOp
241 | A.NeqOp ->
242 if (T.is_int ty)
243 || (T.is_string ty)
244 || (T.is_array ty)
245 || (T.is_record ty)
246 then
cbb4ffb6 247 return_int (* Because we have no bool type *)
523e2b06
SK
248 else
249 E.raise (E.Invalid_operand_type
250 { oper
251 ; valid = ["int"; "string"; "array"; "record"]
252 ; given = ty
253 ; pos
254 })
255 (* Order: int, string *)
256 | A.LtOp
257 | A.LeOp
258 | A.GtOp
259 | A.GeOp ->
260 if (T.is_int ty)
261 || (T.is_string ty)
262 then
cbb4ffb6 263 return_int (* Because we have no bool type *)
523e2b06
SK
264 else
265 E.raise (E.Invalid_operand_type
266 { oper
267 ; valid = ["int"; "string"]
268 ; given = ty
269 ; pos
270 })
271 )
272 in
273 trexp exp
862e5c05 274 and transDec ~(env : Env.t) (dec : A.dec) : Env.t =
8744eb3a
SK
275 (match dec with
276 | A.VarDec {name; typ=typ_opt; init; pos=pos_outter; escape=_} ->
277 let ty =
278 (match (typ_opt, transExp ~env init) with
279 | None, {ty; exp=()} ->
280 ty
281 | Some (sym, pos_inner), expty_init ->
cbb4ffb6
SK
282 let ty = env_get_typ_actual ~sym ~env ~pos:pos_inner in
283 check_same (return ty) expty_init ~pos:pos_outter;
8744eb3a
SK
284 ty
285 )
286 in
287 Env.set_val env name (Value.Var {ty})
0324a942 288 | A.TypeDecs typedecs ->
cbb4ffb6
SK
289 let env =
290 List.fold_left typedecs ~init:env ~f:(
291 fun env (A.TypeDec {name; ty=_; pos=_}) ->
292 Env.set_typ env name (Type.Name (name, ref None))
293 )
294 in
295 List.iter typedecs ~f:(fun (A.TypeDec {name; ty=ty_exp; pos}) ->
296 let ty = transTy ~env ty_exp in
297 (match env_get_typ ~sym:name ~env ~pos with
298 | Type.Name (name, ty_opt_ref) ->
299 ty_opt_ref := Some ty
300 | Type.Unit
301 | Type.Nil
302 | Type.Int
303 | Type.String
304 | Type.Record _
305 | Type.Array _ ->
306 ()
307 )
308 );
309 env
76c771a7
SK
310 | A.FunDecs fundecs ->
311 List.fold_left fundecs ~init:env ~f:(
312 fun env (A.FunDec {name; params; result; body; pos=_}) ->
313 let (env_for_body, formals_in_reverse_order) =
314 List.fold_left params ~init:(env, []) ~f:(
315 fun (env, formals) (A.Field {name; escape=_; typ; pos}) ->
cbb4ffb6 316 let ty = env_get_typ_actual ~env ~sym:typ ~pos in
76c771a7
SK
317 let env = Env.set_val env name (Value.Var {ty}) in
318 (env, ty :: formals)
319 )
320 in
321 (* ignore because we only care if an exception is raised *)
322 ignore (transExp ~env:env_for_body body);
323 let formals = List.rev formals_in_reverse_order in
324 let result =
325 match result with
326 | None ->
327 Type.Unit
328 | Some (sym, pos) ->
cbb4ffb6 329 env_get_typ_actual ~sym ~env ~pos
76c771a7
SK
330 in
331 Env.set_val env name (Value.Fun {formals; result})
332 )
8744eb3a 333 )
cbb4ffb6
SK
334 and transTy ~(env : Env.t) (ty_exp : A.ty) : Type.t =
335 (match ty_exp with
0324a942
SK
336 | A.NameTy {symbol=sym; pos} ->
337 env_get_typ ~sym ~env ~pos
338 | A.RecordTy fields ->
339 let fields =
340 List.map fields ~f:(fun (A.Field {name; escape=_; typ; pos}) ->
341 let ty = env_get_typ ~sym:typ ~env ~pos in
342 (name, ty)
343 )
344 in
345 Type.new_record fields
346 | A.ArrayTy {symbol=sym; pos} ->
347 let element_ty = env_get_typ ~sym ~env ~pos in
348 Type.new_array element_ty
349 )
155ee327
SK
350end
351
8744eb3a
SK
352open Semant
353
155ee327 354let transProg absyn =
155ee327
SK
355 let {exp = _; ty = _} = transExp absyn ~env:Env.base in
356 ()
This page took 0.045898 seconds and 4 git commands to generate.