1 module List = ListLabels
6 module Symbol = Tiger_symbol
7 module Translate = Tiger_translate
8 module Type = Tiger_env_type
9 module Value = Tiger_env_value
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. *)
19 (* Violating normal naming convention just to make it easier to follow
22 val transExp : env:Env.t -> A.exp -> expty
24 (* transVar does not seem to be needed, as trvar handles all our cases.
27 * val transVar : env:Env.t -> A.var -> expty
36 let rec actual_ty ty ~pos =
38 | Type.Name (name, ty_opt_ref) ->
39 (match !ty_opt_ref with
41 E.raise (E.Unknown_type {ty_id=name; pos})
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
59 let env_get_typ ~sym ~env ~pos : Type.t =
60 match Env.get_typ env sym with
62 | None -> E.raise (E.Unknown_type {ty_id=sym; pos})
64 let env_get_typ_actual ~sym ~env ~pos : Type.t =
65 actual_ty (env_get_typ ~sym ~env ~pos) ~pos
67 let env_get_val ~sym ~env ~pos : Value.t =
68 match Env.get_val env sym with
70 | None -> E.raise (E.Unknown_id {id=sym; pos})
72 let check_same {exp=_; ty=ty_left} {exp=_; ty=ty_right} ~pos : unit =
73 if Type.is_equal ty_left ty_right then
76 E.raise (E.Wrong_type {expected=ty_left; given=ty_right; pos})
78 let check_int expty ~pos : unit =
79 check_same return_int expty ~pos
81 let rec transExp ~env exp =
88 | A.StringExp {string=_; _} ->
90 | A.CallExp {func; args; pos} ->
91 (match env_get_val ~sym:func ~env ~pos with
92 | Value.Fun {formals; result} ->
93 let expected = List.length formals in
94 let given = List.length args in
95 if given = expected then
97 List.iter2 formals args ~f:(fun ty_expected exp_given ->
99 (return (actual_ty ~pos ty_expected))
103 return (actual_ty ~pos result)
106 E.raise (E.Wrong_number_of_args {func; expected; given; pos})
108 E.raise (E.Id_not_a_function {id=func; pos})
110 | A.OpExp {oper; left; right; pos} ->
111 trop oper ~left ~right ~pos
112 | A.RecordExp {fields=field_exps; typ; pos} ->
113 let ty = env_get_typ_actual ~sym:typ ~env ~pos in
117 List.iter field_exps ~f:(fun (field, exp, pos) ->
118 (match List.assoc_opt field field_tys with
120 check_same (return (actual_ty ~pos field_ty)) (trexp exp) ~pos
123 (E.No_such_field_in_record {field; record=ty; pos})
127 ~otherwise:(fun () ->
128 E.raise (E.Wrong_type_used_as_record {ty_id=typ; ty; pos})
130 return (actual_ty ~pos ty)
136 |> List.rev (* Yes, redundant, but clean-looking ;-P *)
137 |> List.hd (* Empty is matched in above SeqExp match case *)
140 |> List.map ~f:(fun (exp, _) -> trexp exp)
142 | A.AssignExp {var; exp; pos} ->
143 check_same (trvar var) (trexp exp) ~pos;
144 (* TODO: Add var->exp to val env? *)
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 ->
154 | expty_then, Some else' ->
155 let expty_else = trexp else' in
156 check_same expty_then expty_else ~pos;
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 *)
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 *)
168 let env = Env.set_val env var (Value.Var {ty = Type.Int}) in
169 ignore (transExp ~env body);
173 | A.LetExp {decs; body; pos=_} ->
174 (* (1) decs augment env *)
175 (* (2) body checked against the new env *)
177 List.fold_left decs ~init:env ~f:(fun env dec -> transDec dec ~env)
180 | A.ArrayExp {typ; size; init; pos} ->
181 check_int (trexp size) ~pos;
182 let ty = env_get_typ_actual ~sym:typ ~env ~pos in
185 ~f:(fun ty_elements ->
186 check_same (return (actual_ty ~pos ty_elements)) (trexp init) ~pos
188 ~otherwise:(fun () ->
189 E.raise (E.Wrong_type_used_as_array {ty_id=typ; ty; pos})
191 return (actual_ty ~pos ty)
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})
200 | Value.Var {ty} -> return (actual_ty ~pos ty)
202 | A.FieldVar {var; symbol; pos} ->
203 let {exp=_; ty} = trvar var in
207 (match List.assoc_opt symbol fields with
210 (E.No_such_field_in_record {field=symbol; record=ty; pos})
212 return (actual_ty ~pos ty)
215 ~otherwise:(fun () -> E.raise (E.Exp_not_a_record {ty; pos}))
216 | A.SubscriptVar {var; exp; pos} ->
217 let {exp=_; ty} = trvar var in
218 check_int (trexp exp) ~pos;
221 ~f:(fun ty_elements -> return (actual_ty ~pos ty_elements))
222 ~otherwise:(fun () -> E.raise (E.Exp_not_an_array {ty; pos}))
224 and trop oper ~left ~right ~pos =
225 (* TODO: Refactor trop - all opers return bool/int *)
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
232 (* Arithmetic: int *)
237 check_int expty_left ~pos;
239 (* Equality: int, string, array, record *)
247 return_int (* Because we have no bool type *)
249 E.raise (E.Invalid_operand_type
251 ; valid = ["int"; "string"; "array"; "record"]
255 (* Order: int, string *)
263 return_int (* Because we have no bool type *)
265 E.raise (E.Invalid_operand_type
267 ; valid = ["int"; "string"]
274 and transDec ~(env : Env.t) (dec : A.dec) : Env.t =
276 | A.VarDec {name; typ=typ_opt; init; pos=pos_outter; escape=_} ->
278 (match (typ_opt, transExp ~env init) with
279 | None, {ty; exp=()} ->
281 | Some (sym, pos_inner), expty_init ->
282 let ty = env_get_typ_actual ~sym ~env ~pos:pos_inner in
283 check_same (return ty) expty_init ~pos:pos_outter;
287 Env.set_val env name (Value.Var {ty})
288 | A.TypeDecs typedecs ->
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))
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
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}) ->
316 let ty = env_get_typ_actual ~env ~sym:typ ~pos in
317 let env = Env.set_val env name (Value.Var {ty}) 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
329 env_get_typ_actual ~sym ~env ~pos
331 Env.set_val env name (Value.Fun {formals; result})
334 and transTy ~(env : Env.t) (ty_exp : A.ty) : Type.t =
336 | A.NameTy {symbol=sym; pos} ->
337 env_get_typ ~sym ~env ~pos
338 | A.RecordTy fields ->
340 List.map fields ~f:(fun (A.Field {name; escape=_; typ; pos}) ->
341 let ty = env_get_typ ~sym:typ ~env ~pos 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
354 let transProg absyn =
355 let {exp = _; ty = _} = transExp absyn ~env:Env.base in