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 List.iter2 formals args ~f:(fun ty_expected exp_given ->
94 check_same (return (actual_ty ~pos ty_expected)) (trexp exp_given) ~pos;
96 return (actual_ty ~pos result)
98 E.raise (E.Id_not_a_function {id=func; pos})
100 | A.OpExp {oper; left; right; pos} ->
101 trop oper ~left ~right ~pos
102 | A.RecordExp {fields=field_exps; typ; pos} ->
103 let ty = env_get_typ_actual ~sym:typ ~env ~pos in
107 List.iter field_exps ~f:(fun (field, exp, pos) ->
108 (match List.assoc_opt field field_tys with
110 check_same (return (actual_ty ~pos field_ty)) (trexp exp) ~pos
113 (E.No_such_field_in_record {field; record=ty; pos})
117 ~otherwise:(fun () ->
118 E.raise (E.Wrong_type_used_as_record {ty_id=typ; ty; pos})
120 return (actual_ty ~pos ty)
126 |> List.rev (* Yes, redundant, but clean-looking ;-P *)
127 |> List.hd (* Empty is matched in above SeqExp match case *)
130 |> List.map ~f:(fun (exp, _) -> trexp exp)
132 | A.AssignExp {var; exp; pos} ->
133 check_same (trvar var) (trexp exp) ~pos;
134 (* TODO: Add var->exp to val env? *)
136 | A.IfExp {test; then'; else'; pos} ->
137 (* test : must be int, because we have no bool *)
138 (* then : must equal else *)
139 (* else : must equal then or be None *)
140 check_int (trexp test) ~pos;
141 (match (trexp then', else') with
142 | expty_then, None ->
144 | expty_then, Some else' ->
145 let expty_else = trexp else' in
146 check_same expty_then expty_else ~pos;
149 | A.WhileExp {test; body; pos} ->
150 (* test : must be int, because we have no bool *)
151 check_int (trexp test) ~pos;
152 ignore (trexp body); (* Only care if a type-error is raised *)
154 | A.ForExp {var; lo; hi; body; pos; escape=_} ->
155 check_int (trexp lo) ~pos;
156 check_int (trexp hi) ~pos;
157 (* Only care if a type-error is raised *)
158 let env = Env.set_val env var (Value.Var {ty = Type.Int}) in
159 ignore (transExp ~env body);
163 | A.LetExp {decs; body; pos=_} ->
164 (* (1) decs augment env *)
165 (* (2) body checked against the new env *)
167 List.fold_left decs ~init:env ~f:(fun env dec -> transDec dec ~env)
170 | A.ArrayExp {typ; size; init; pos} ->
171 check_int (trexp size) ~pos;
172 let ty = env_get_typ_actual ~sym:typ ~env ~pos in
175 ~f:(fun ty_elements ->
176 check_same (return (actual_ty ~pos ty_elements)) (trexp init) ~pos
178 ~otherwise:(fun () ->
179 E.raise (E.Wrong_type_used_as_array {ty_id=typ; ty; pos})
181 return (actual_ty ~pos ty)
187 | A.SimpleVar {symbol=sym; pos} ->
188 (match env_get_val ~sym ~env ~pos with
189 | Value.Fun _ -> E.raise (E.Id_is_a_function {id=sym; pos})
190 | Value.Var {ty} -> return (actual_ty ~pos ty)
192 | A.FieldVar {var; symbol; pos} ->
193 let {exp=_; ty} = trvar var in
197 (match List.assoc_opt symbol fields with
200 (E.No_such_field_in_record {field=symbol; record=ty; pos})
202 return (actual_ty ~pos ty)
205 ~otherwise:(fun () -> E.raise (E.Exp_not_a_record {ty; pos}))
206 | A.SubscriptVar {var; exp; pos} ->
207 let {exp=_; ty} = trvar var in
208 check_int (trexp exp) ~pos;
211 ~f:(fun ty_elements -> return (actual_ty ~pos ty_elements))
212 ~otherwise:(fun () -> E.raise (E.Exp_not_an_array {ty; pos}))
214 and trop oper ~left ~right ~pos =
215 (* TODO: Refactor trop - all opers return bool/int *)
216 let expty_left = trexp left in
217 let expty_right = trexp right in
218 check_same expty_left expty_right ~pos;
219 let {exp=_; ty} = expty_left in
220 let module T = Type in
222 (* Arithmetic: int *)
227 check_int expty_left ~pos;
229 (* Equality: int, string, array, record *)
237 return_int (* Because we have no bool type *)
239 E.raise (E.Invalid_operand_type
241 ; valid = ["int"; "string"; "array"; "record"]
245 (* Order: int, string *)
253 return_int (* Because we have no bool type *)
255 E.raise (E.Invalid_operand_type
257 ; valid = ["int"; "string"]
264 and transDec ~(env : Env.t) (dec : A.dec) : Env.t =
266 | A.VarDec {name; typ=typ_opt; init; pos=pos_outter; escape=_} ->
268 (match (typ_opt, transExp ~env init) with
269 | None, {ty; exp=()} ->
271 | Some (sym, pos_inner), expty_init ->
272 let ty = env_get_typ_actual ~sym ~env ~pos:pos_inner in
273 check_same (return ty) expty_init ~pos:pos_outter;
277 Env.set_val env name (Value.Var {ty})
278 | A.TypeDecs typedecs ->
280 List.fold_left typedecs ~init:env ~f:(
281 fun env (A.TypeDec {name; ty=_; pos=_}) ->
282 Env.set_typ env name (Type.Name (name, ref None))
285 List.iter typedecs ~f:(fun (A.TypeDec {name; ty=ty_exp; pos}) ->
286 let ty = transTy ~env ty_exp in
287 (match env_get_typ ~sym:name ~env ~pos with
288 | Type.Name (name, ty_opt_ref) ->
289 ty_opt_ref := Some ty
300 | A.FunDecs fundecs ->
301 List.fold_left fundecs ~init:env ~f:(
302 fun env (A.FunDec {name; params; result; body; pos=_}) ->
303 let (env_for_body, formals_in_reverse_order) =
304 List.fold_left params ~init:(env, []) ~f:(
305 fun (env, formals) (A.Field {name; escape=_; typ; pos}) ->
306 let ty = env_get_typ_actual ~env ~sym:typ ~pos in
307 let env = Env.set_val env name (Value.Var {ty}) in
311 (* ignore because we only care if an exception is raised *)
312 ignore (transExp ~env:env_for_body body);
313 let formals = List.rev formals_in_reverse_order in
319 env_get_typ_actual ~sym ~env ~pos
321 Env.set_val env name (Value.Fun {formals; result})
324 and transTy ~(env : Env.t) (ty_exp : A.ty) : Type.t =
326 | A.NameTy {symbol=sym; pos} ->
327 env_get_typ ~sym ~env ~pos
328 | A.RecordTy fields ->
330 List.map fields ~f:(fun (A.Field {name; escape=_; typ; pos}) ->
331 let ty = env_get_typ ~sym:typ ~env ~pos in
335 Type.new_record fields
336 | A.ArrayTy {symbol=sym; pos} ->
337 let element_ty = env_get_typ ~sym ~env ~pos in
338 Type.new_array element_ty
344 let transProg absyn =
345 let {exp = _; ty = _} = transExp absyn ~env:Env.base in