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 return ty = {exp = (); ty}
36 let return_unit = return Type.Unit
37 let return_nil = return Type.Nil
38 let return_int = return Type.Int
39 let return_string = return Type.String
41 let env_get_typ ~sym ~env ~pos : Type.t =
42 match Env.get_typ env sym with
44 | None -> E.raise (E.Unknown_type {ty_id=sym; pos})
46 let env_get_val ~sym ~env ~pos : Value.t =
47 match Env.get_val env sym with
49 | None -> E.raise (E.Unknown_id {id=sym; pos})
51 let check_same {exp=_; ty=ty_left} {exp=_; ty=ty_right} ~pos : unit =
52 if Type.is_equal ty_left ty_right then
55 E.raise (E.Wrong_type {expected=ty_left; given=ty_right; pos})
57 let check_int expty ~pos : unit =
58 check_same {exp=(); ty=Type.Int} expty ~pos
61 (* TODO: mutual recursion *)
63 let rec transExp ~env exp =
70 | A.StringExp {string=_; _} ->
72 | A.CallExp {func; args; pos} ->
73 (match env_get_val ~sym:func ~env ~pos with
74 | Value.Fun {formals; result} ->
75 List.iter2 formals args ~f:(fun ty_expected exp_given ->
76 check_same {exp=(); ty = ty_expected} (trexp exp_given) ~pos;
80 E.raise (E.Id_not_a_function {id=func; pos})
82 | A.OpExp {oper; left; right; pos} ->
83 trop oper ~left ~right ~pos
84 | A.RecordExp {fields=field_exps; typ; pos} ->
85 let ty = env_get_typ ~sym:typ ~env ~pos in
89 List.iter field_exps ~f:(fun (field, exp, pos) ->
90 (match List.assoc_opt field field_tys with
92 check_same {exp=(); ty=field_ty} (trexp exp) ~pos
95 (E.No_such_field_in_record {field; record=ty; pos})
100 E.raise (E.Wrong_type_used_as_record {ty_id=typ; ty; pos})
104 (* Ignoring value because we only care if a type-checking exception
105 * is raised in one of trexp calls: *)
106 List.iter exps ~f:(fun (exp, _) -> ignore (trexp exp));
107 (* FIXME: Return type of last expression, not unit. *)
109 | A.AssignExp {var; exp; pos} ->
110 check_same (trvar var) (trexp exp) ~pos;
111 (* TODO: Add var->exp to val env? *)
113 | A.IfExp {test; then'; else'; pos} ->
114 (* test : must be int, because we have no bool *)
115 (* then : must equal else *)
116 (* else : must equal then or be None *)
117 check_int (trexp test) ~pos;
118 (match (trexp then', else') with
119 | expty_then, None ->
121 | expty_then, Some else' ->
122 let expty_else = trexp else' in
123 check_same expty_then expty_else ~pos;
126 | A.WhileExp {test; body; pos} ->
127 (* test : must be int, because we have no bool *)
128 check_int (trexp test) ~pos;
129 ignore (trexp body); (* Only care if a type-error is raised *)
131 | A.ForExp {var; lo; hi; body; pos; escape=_} ->
132 check_int (trexp lo) ~pos;
133 check_int (trexp hi) ~pos;
134 (* Only care if a type-error is raised *)
135 ignore (transExp ~env:(Env.set_typ env var Type.Int) body);
139 | A.LetExp {decs; body; pos=_} ->
140 (* (1) decs augment env *)
141 (* (2) body checked against the new env *)
143 List.fold_left decs ~init:env ~f:(fun env dec -> transDec dec ~env)
146 | A.ArrayExp {typ; size; init; pos} ->
147 check_int (trexp size) ~pos;
148 let ty = env_get_typ ~sym:typ ~env ~pos in
151 ~f:(fun ty_elements ->
152 check_same {exp=(); ty=ty_elements} (trexp init) ~pos
154 ~otherwise:(fun () ->
155 E.raise (E.Wrong_type_used_as_array {ty_id=typ; ty; pos})
163 | A.SimpleVar {symbol=sym; pos} ->
164 (match env_get_val ~sym ~env ~pos with
165 | Value.Fun _ -> E.raise (E.Id_is_a_function {id=sym; pos})
166 | Value.Var {ty} -> return ty
168 | A.FieldVar {var; symbol; pos} ->
169 let {exp=_; ty} = trvar var in
173 (match List.assoc_opt symbol fields with
176 (E.No_such_field_in_record {field=symbol; record=ty; pos})
181 ~otherwise:(fun () -> E.raise (E.Exp_not_a_record {ty; pos}))
182 | A.SubscriptVar {var; exp; pos} ->
183 let {exp=_; ty} = trvar var in
184 check_int (trexp exp) ~pos;
187 ~f:(fun ty_elements -> return ty_elements)
188 ~otherwise:(fun () -> E.raise (E.Exp_not_an_array {ty; pos}))
190 and trop oper ~left ~right ~pos =
191 let expty_left = trexp left in
192 let expty_right = trexp right in
193 check_same expty_left expty_right ~pos;
194 let {exp=_; ty} = expty_left in
195 let module T = Type in
197 (* Arithmetic: int *)
202 check_int expty_left ~pos;
204 (* Equality: int, string, array, record *)
214 E.raise (E.Invalid_operand_type
216 ; valid = ["int"; "string"; "array"; "record"]
220 (* Order: int, string *)
230 E.raise (E.Invalid_operand_type
232 ; valid = ["int"; "string"]
239 and transDec ~(env : Env.t) (dec : A.dec) : Env.t =
241 | A.VarDec {name; typ=typ_opt; init; pos=pos_outter; escape=_} ->
243 (match (typ_opt, transExp ~env init) with
244 | None, {ty; exp=()} ->
246 | Some (sym, pos_inner), expty_init ->
247 let ty = env_get_typ ~sym ~env ~pos:pos_inner in
248 check_same {exp=(); ty} expty_init ~pos:pos_outter;
252 Env.set_val env name (Value.Var {ty})
253 | A.TypeDecs typedecs ->
254 List.fold_left typedecs ~init:env ~f:(
255 fun env (A.TypeDec {name; ty; pos=_}) ->
256 let ty = transTy ~env ty in
257 Env.set_typ env name ty
259 | A.FunDecs fundecs ->
260 List.fold_left fundecs ~init:env ~f:(
261 fun env (A.FunDec {name; params; result; body; pos=_}) ->
262 let (env_for_body, formals_in_reverse_order) =
263 List.fold_left params ~init:(env, []) ~f:(
264 fun (env, formals) (A.Field {name; escape=_; typ; pos}) ->
265 let ty = env_get_typ ~env ~sym:typ ~pos in
266 let env = Env.set_val env name (Value.Var {ty}) in
270 (* ignore because we only care if an exception is raised *)
271 ignore (transExp ~env:env_for_body body);
272 let formals = List.rev formals_in_reverse_order in
278 env_get_typ ~sym ~env ~pos
280 Env.set_val env name (Value.Fun {formals; result})
283 and transTy ~(env : Env.t) (typ : A.ty) : Type.t =
285 | A.NameTy {symbol=sym; pos} ->
286 env_get_typ ~sym ~env ~pos
287 | A.RecordTy fields ->
289 List.map fields ~f:(fun (A.Field {name; escape=_; typ; pos}) ->
290 let ty = env_get_typ ~sym:typ ~env ~pos in
294 Type.new_record fields
295 | A.ArrayTy {symbol=sym; pos} ->
296 let element_ty = env_get_typ ~sym ~env ~pos in
297 Type.new_array element_ty
303 let transProg absyn =
304 let {exp = _; ty = _} = transExp absyn ~env:Env.base in