1 module List = ListLabels
6 module Translate = Tiger_translate
7 module Type = Tiger_env_type
8 module Value = Tiger_env_value
10 (* The only reason for having this seemingly-superfluous inner module is to
11 * have this nice signature as a summary of what each function does. *)
18 (* Violating normal naming convention just to make it easier to follow
21 val transExp : env:Env.t -> A.exp -> expty
23 (* transVar does not seem to be needed, as trvar handles all our cases.
26 * val transVar : env:Env.t -> A.var -> expty
35 let rec actual_ty ty ~pos =
37 | Type.Name (name, ty_opt_ref) ->
38 (match !ty_opt_ref with
40 E.raise (E.Unknown_type {ty_id=name; pos})
52 let return ty = {exp = (); ty}
53 let return_unit = return Type.Unit
54 let return_nil = return Type.Nil
55 let return_int = return Type.Int
56 let return_string = return Type.String
58 let env_get_typ ~sym ~env ~pos : Type.t =
59 match Env.get_typ env sym with
61 | None -> E.raise (E.Unknown_type {ty_id=sym; pos})
63 let env_get_typ_actual ~sym ~env ~pos : Type.t =
64 actual_ty (env_get_typ ~sym ~env ~pos) ~pos
66 let env_get_val ~sym ~env ~pos : Value.t =
67 match Env.get_val env sym with
69 | None -> E.raise (E.Unknown_id {id=sym; pos})
71 let check_same {exp=_; ty=ty_left} {exp=_; ty=ty_right} ~pos : unit =
72 if Type.is_equal ty_left ty_right then
75 E.raise (E.Wrong_type {expected=ty_left; given=ty_right; pos})
77 let check_int expty ~pos : unit =
78 check_same return_int expty ~pos
80 let rec transExp ~env exp =
87 | A.StringExp {string=_; _} ->
89 | A.CallExp {func; args; pos} ->
90 (match env_get_val ~sym:func ~env ~pos with
91 | Value.Fun {formals; result} ->
92 let expected = List.length formals in
93 let given = List.length args in
94 if given = expected then
96 List.iter2 formals args ~f:(fun ty_expected exp_given ->
98 (return (actual_ty ~pos ty_expected))
102 return (actual_ty ~pos result)
105 E.raise (E.Wrong_number_of_args {func; expected; given; pos})
107 E.raise (E.Id_not_a_function {id=func; pos})
109 | A.OpExp {oper; left; right; pos} ->
110 trop oper ~left ~right ~pos
111 | A.RecordExp {fields=field_exps; typ; pos} ->
112 let ty = env_get_typ_actual ~sym:typ ~env ~pos in
116 List.iter field_exps ~f:(fun (field, exp, pos) ->
117 (match List.assoc_opt field field_tys with
119 check_same (return (actual_ty ~pos field_ty)) (trexp exp) ~pos
122 (E.No_such_field_in_record {field; record=ty; pos})
126 ~otherwise:(fun () ->
127 E.raise (E.Wrong_type_used_as_record {ty_id=typ; ty; pos})
129 return (actual_ty ~pos ty)
135 |> List.rev (* Yes, redundant, but clean-looking ;-P *)
136 |> List.hd (* Empty is matched in above SeqExp match case *)
139 |> List.map ~f:(fun (exp, _) -> trexp exp)
141 | A.AssignExp {var; exp; pos} ->
142 check_same (trvar var) (trexp exp) ~pos;
143 (* TODO: Add var->exp to val env? *)
145 | A.IfExp {test; then'; else'; pos} ->
146 (* test : must be int, because we have no bool *)
147 (* then : must equal else *)
148 (* else : must equal then or be None *)
149 check_int (trexp test) ~pos;
150 (match (trexp then', else') with
151 | expty_then, None ->
153 | expty_then, Some else' ->
154 let expty_else = trexp else' in
155 check_same expty_then expty_else ~pos;
158 | A.WhileExp {test; body; pos} ->
159 (* test : must be int, because we have no bool *)
160 check_int (trexp test) ~pos;
161 ignore (trexp body); (* Only care if a type-error is raised *)
163 | A.ForExp {var; lo; hi; body; pos; escape=_} ->
164 check_int (trexp lo) ~pos;
165 check_int (trexp hi) ~pos;
166 (* Only care if a type-error is raised *)
167 let env = Env.set_val env var (Value.Var {ty = Type.Int}) in
168 ignore (transExp ~env body);
172 | A.LetExp {decs; body; pos=_} ->
173 (* (1) decs augment env *)
174 (* (2) body checked against the new env *)
176 List.fold_left decs ~init:env ~f:(fun env dec -> transDec dec ~env)
179 | A.ArrayExp {typ; size; init; pos} ->
180 check_int (trexp size) ~pos;
181 let ty = env_get_typ_actual ~sym:typ ~env ~pos in
184 ~f:(fun ty_elements ->
185 check_same (return (actual_ty ~pos ty_elements)) (trexp init) ~pos
187 ~otherwise:(fun () ->
188 E.raise (E.Wrong_type_used_as_array {ty_id=typ; ty; pos})
190 return (actual_ty ~pos ty)
196 | A.SimpleVar {symbol=sym; pos} ->
197 (match env_get_val ~sym ~env ~pos with
198 | Value.Fun _ -> E.raise (E.Id_is_a_function {id=sym; pos})
199 | Value.Var {ty} -> return (actual_ty ~pos ty)
201 | A.FieldVar {var; symbol; pos} ->
202 let {exp=_; ty} = trvar var in
206 (match List.assoc_opt symbol fields with
209 (E.No_such_field_in_record {field=symbol; record=ty; pos})
211 return (actual_ty ~pos ty)
214 ~otherwise:(fun () -> E.raise (E.Exp_not_a_record {ty; pos}))
215 | A.SubscriptVar {var; exp; pos} ->
216 let {exp=_; ty} = trvar var in
217 check_int (trexp exp) ~pos;
220 ~f:(fun ty_elements -> return (actual_ty ~pos ty_elements))
221 ~otherwise:(fun () -> E.raise (E.Exp_not_an_array {ty; pos}))
223 and trop oper ~left ~right ~pos =
224 (* TODO: Refactor trop - all opers return bool/int *)
225 let expty_left = trexp left in
226 let expty_right = trexp right in
227 check_same expty_left expty_right ~pos;
228 let {exp=_; ty} = expty_left in
229 let module T = Type in
231 (* Arithmetic: int *)
236 check_int expty_left ~pos;
238 (* Equality: int, string, array, record *)
246 return_int (* Because we have no bool type *)
248 E.raise (E.Invalid_operand_type
250 ; valid = ["int"; "string"; "array"; "record"]
254 (* Order: int, string *)
262 return_int (* Because we have no bool type *)
264 E.raise (E.Invalid_operand_type
266 ; valid = ["int"; "string"]
273 and transDec ~(env : Env.t) (dec : A.dec) : Env.t =
275 | A.VarDec {name; typ=typ_opt; init; pos=pos_outter; escape=_} ->
277 (match (typ_opt, transExp ~env init) with
278 | None, {ty; exp=()} ->
280 | Some (sym, pos_inner), expty_init ->
281 let ty = env_get_typ_actual ~sym ~env ~pos:pos_inner in
282 check_same (return ty) expty_init ~pos:pos_outter;
286 Env.set_val env name (Value.Var {ty})
287 | A.TypeDecs typedecs ->
289 List.fold_left typedecs ~init:env ~f:(
290 fun env (A.TypeDec {name; ty=_; pos=_}) ->
291 Env.set_typ env name (Type.Name (name, ref None))
294 List.iter typedecs ~f:(fun (A.TypeDec {name; ty=ty_exp; pos}) ->
295 let ty = transTy ~env ty_exp in
296 (match env_get_typ ~sym:name ~env ~pos with
297 | Type.Name (_, ty_opt_ref) ->
298 ty_opt_ref := Some ty
309 | A.FunDecs fundecs ->
310 let env_with_fun_heads_only =
311 List.fold_left fundecs ~init:env ~f:(
312 fun env (A.FunDec {name; params; result; body=_; pos=_}) ->
315 fun (A.Field {name=_; typ; pos; escape=_}) ->
316 env_get_typ_actual ~env ~sym:typ ~pos
321 | Some (s, p) -> env_get_typ_actual ~sym:s ~env ~pos:p
324 Env.set_val env name (Value.Fun {formals; result})
327 List.iter fundecs ~f:(
328 fun (A.FunDec {name=_; params; result=_; body; pos=_}) ->
329 let env_with_fun_heads_and_local_vars =
330 List.fold_left params ~init:env_with_fun_heads_only ~f:(
331 fun env (A.Field {name=var_name; escape=_; typ; pos}) ->
332 let var_ty = env_get_typ_actual ~env ~sym:typ ~pos in
333 Env.set_val env var_name (Value.Var {ty = var_ty})
336 (* we only care if an exception is raised *)
337 ignore (transExp ~env:env_with_fun_heads_and_local_vars body);
339 env_with_fun_heads_only
341 and transTy ~(env : Env.t) (ty_exp : A.ty) : Type.t =
343 | A.NameTy {symbol=sym; pos} ->
344 env_get_typ ~sym ~env ~pos
345 | A.RecordTy fields ->
347 List.map fields ~f:(fun (A.Field {name; escape=_; typ; pos}) ->
348 let ty = env_get_typ ~sym:typ ~env ~pos in
352 Type.new_record fields
353 | A.ArrayTy {symbol=sym; pos} ->
354 let element_ty = env_get_typ ~sym ~env ~pos in
355 Type.new_array element_ty
361 let transProg absyn =
362 let {exp = _; ty = _} = transExp absyn ~env:Env.base in