1 module List = ListLabels
7 module Escape = Tiger_semant_escape
8 module Pos = Tiger_position
9 module Sym = Tiger_symbol
10 module Translate = Tiger_translate
11 module Type = Tiger_env_type
12 module Value = Tiger_env_value
14 (* The only reason for having this seemingly-superfluous inner module is to
15 * have this nice signature as a summary of what each function does. *)
22 (* Violating normal naming convention just to make it easier to follow
25 val transExp : env:Env.t -> A.exp -> expty
27 (* transVar does not seem to be needed, as trvar handles all our cases.
30 * val transVar : env:Env.t -> A.var -> expty
39 let rec actual_ty ty ~pos =
41 | Type.Name (name, ty_opt_ref) ->
42 (match !ty_opt_ref with
44 E.raise (E.Unknown_type {ty_id=name; pos})
56 let return ty = {exp = (); ty}
57 let return_unit = return Type.Unit
58 let return_nil = return Type.Nil
59 let return_int = return Type.Int
60 let return_string = return Type.String
62 let env_get_typ ~sym ~env ~pos : Type.t =
63 match Env.get_typ env sym with
65 | None -> E.raise (E.Unknown_type {ty_id=sym; pos})
67 let env_get_typ_actual ~sym ~env ~pos : Type.t =
68 actual_ty (env_get_typ ~sym ~env ~pos) ~pos
70 let env_get_val ~sym ~env ~pos : Value.t =
71 match Env.get_val env sym with
73 | None -> E.raise (E.Unknown_id {id=sym; pos})
75 let check_same {exp=_; ty=ty_left} {exp=_; ty=ty_right} ~pos : unit =
76 if Type.is_equal ty_left ty_right then
79 E.raise (E.Wrong_type {expected=ty_left; given=ty_right; pos})
81 let check_int expty ~pos : unit =
82 check_same return_int expty ~pos
84 let paths_of_typedecs typedecs : (Sym.t * Sym.t * Pos.t) list list =
86 List.fold_left typedecs ~init:([], []) ~f:(
87 fun (path, paths) (A.TypeDec {name=child; ty; pos}) ->
89 | A.NameTy {symbol=parent; _} ->
90 (((parent, child, pos) :: path), paths)
96 List.map (path :: paths) ~f:List.rev
98 let check_cycles (typedecs : A.typedec list) : unit =
101 (paths_of_typedecs typedecs)
102 ~f:(function [] -> false | _ -> true)
104 List.iter non_empty_paths ~f:(
106 match Dag.of_list (List.map path ~f:(fun (p, c, _) -> (p, c))) with
110 let (_, from_id, from_pos) = List.hd path in
111 let (_, to_id, to_pos) = List.hd (List.rev path) in
112 E.raise (E.Cycle_in_type_decs {from_id; from_pos; to_id; to_pos})
115 let rec transExp ~env exp =
122 | A.StringExp {string=_; _} ->
124 | A.CallExp {func; args; pos} ->
125 (match env_get_val ~sym:func ~env ~pos with
126 | Value.Fun {formals; result} ->
127 let expected = List.length formals in
128 let given = List.length args in
129 if given = expected then
131 List.iter2 formals args ~f:(fun ty_expected exp_given ->
133 (return (actual_ty ~pos ty_expected))
137 return (actual_ty ~pos result)
140 E.raise (E.Wrong_number_of_args {func; expected; given; pos})
142 E.raise (E.Id_not_a_function {id=func; pos})
144 | A.OpExp {oper; left; right; pos} ->
145 trop oper ~left ~right ~pos
146 | A.RecordExp {fields=field_exps; typ; pos} ->
147 let ty = env_get_typ_actual ~sym:typ ~env ~pos in
151 List.iter field_exps ~f:(fun (field, exp, pos) ->
152 (match List.assoc_opt field field_tys with
154 check_same (return (actual_ty ~pos field_ty)) (trexp exp) ~pos
157 (E.No_such_field_in_record {field; record=ty; pos})
161 ~otherwise:(fun () ->
162 E.raise (E.Wrong_type_used_as_record {ty_id=typ; ty; pos})
164 return (actual_ty ~pos ty)
170 |> List.rev (* Yes, redundant, but clean-looking ;-P *)
171 |> List.hd (* Empty is matched in above SeqExp match case *)
174 |> List.map ~f:(fun (exp, _) -> trexp exp)
176 | A.AssignExp {var; exp; pos} ->
177 check_same (trvar var) (trexp exp) ~pos;
178 (* TODO: Add var->exp to val env? *)
180 | A.IfExp {test; then'; else'; pos} ->
181 (* test : must be int, because we have no bool *)
182 (* then : must equal else *)
183 (* else : must equal then or be None *)
184 check_int (trexp test) ~pos;
185 (match (trexp then', else') with
186 | expty_then, None ->
188 | expty_then, Some else' ->
189 let expty_else = trexp else' in
190 check_same expty_then expty_else ~pos;
193 | A.WhileExp {test; body; pos} ->
194 (* test : must be int, because we have no bool *)
195 check_int (trexp test) ~pos;
196 let (loop, env) = Env.loop_begin env in
197 (* Only care if an error is raised *)
198 ignore (transExp ~env body);
199 ignore (Env.loop_end env loop);
201 | A.ForExp {var; lo; hi; body; pos; escape=_} ->
202 check_int (trexp lo) ~pos;
203 check_int (trexp hi) ~pos;
204 let (loop, env) = Env.loop_begin env in
205 let env = Env.set_val env var (Value.Var {ty = Type.Int}) in
206 (* Only care if an error is raised *)
207 ignore (transExp ~env body);
208 ignore (Env.loop_end env loop);
211 (match Env.loop_current env with
213 | None -> E.raise (E.Break_outside_loop pos)
216 | A.LetExp {decs; body; pos=_} ->
217 (* (1) decs augment env *)
218 (* (2) body checked against the new env *)
220 List.fold_left decs ~init:env ~f:(fun env dec -> transDec dec ~env)
223 | A.ArrayExp {typ; size; init; pos} ->
224 check_int (trexp size) ~pos;
225 let ty = env_get_typ_actual ~sym:typ ~env ~pos in
228 ~f:(fun ty_elements ->
229 check_same (return (actual_ty ~pos ty_elements)) (trexp init) ~pos
231 ~otherwise:(fun () ->
232 E.raise (E.Wrong_type_used_as_array {ty_id=typ; ty; pos})
234 return (actual_ty ~pos ty)
240 | A.SimpleVar {symbol=sym; pos} ->
241 (match env_get_val ~sym ~env ~pos with
242 | Value.Fun _ -> E.raise (E.Id_is_a_function {id=sym; pos})
243 | Value.Var {ty} -> return (actual_ty ~pos ty)
245 | A.FieldVar {var; symbol; pos} ->
246 let {exp=_; ty} = trvar var in
250 (match List.assoc_opt symbol fields with
253 (E.No_such_field_in_record {field=symbol; record=ty; pos})
255 return (actual_ty ~pos ty)
258 ~otherwise:(fun () -> E.raise (E.Exp_not_a_record {ty; pos}))
259 | A.SubscriptVar {var; exp; pos} ->
260 let {exp=_; ty} = trvar var in
261 check_int (trexp exp) ~pos;
264 ~f:(fun ty_elements -> return (actual_ty ~pos ty_elements))
265 ~otherwise:(fun () -> E.raise (E.Exp_not_an_array {ty; pos}))
267 and trop oper ~left ~right ~pos =
268 (* TODO: Refactor trop - all opers return bool/int *)
269 let expty_left = trexp left in
270 let expty_right = trexp right in
271 check_same expty_left expty_right ~pos;
272 let {exp=_; ty} = expty_left in
273 let module T = Type in
275 (* Arithmetic: int *)
280 check_int expty_left ~pos;
282 (* Equality: int, string, array, record *)
290 return_int (* Because we have no bool type *)
292 E.raise (E.Invalid_operand_type
294 ; valid = ["int"; "string"; "array"; "record"]
298 (* Order: int, string *)
306 return_int (* Because we have no bool type *)
308 E.raise (E.Invalid_operand_type
310 ; valid = ["int"; "string"]
317 and transDec ~(env : Env.t) (dec : A.dec) : Env.t =
319 | A.VarDec {name; typ=typ_opt; init; pos=pos_outter; escape=_} ->
321 (match (typ_opt, transExp ~env init) with
322 | None, {ty; exp=()} ->
324 | Some (sym, pos_inner), expty_init ->
325 let ty = env_get_typ_actual ~sym ~env ~pos:pos_inner in
326 check_same (return ty) expty_init ~pos:pos_outter;
330 Env.set_val env name (Value.Var {ty})
331 | A.TypeDecs typedecs ->
332 check_cycles typedecs;
334 List.fold_left typedecs ~init:env ~f:(
335 fun env (A.TypeDec {name; ty=_; pos=_}) ->
336 Env.set_typ env name (Type.Name (name, ref None))
339 List.iter typedecs ~f:(fun (A.TypeDec {name=ty_name; ty=ty_exp; pos}) ->
340 let ty = transTy ~env ~ty_name ~ty_exp in
341 (match env_get_typ ~sym:ty_name ~env ~pos with
342 | Type.Name (_, ty_opt_ref) ->
343 ty_opt_ref := Some ty
354 | A.FunDecs fundecs ->
355 let env_with_fun_heads_only =
356 List.fold_left fundecs ~init:env ~f:(
357 fun env (A.FunDec {name; params; result; body=_; pos=_}) ->
360 fun (A.Field {name=_; typ; pos; escape=_}) ->
361 env_get_typ_actual ~env ~sym:typ ~pos
366 | Some (s, p) -> env_get_typ_actual ~sym:s ~env ~pos:p
369 Env.set_val env name (Value.Fun {formals; result})
372 List.iter fundecs ~f:(
373 fun (A.FunDec {name=_; params; result=_; body; pos=_}) ->
374 let env_with_fun_heads_and_local_vars =
375 List.fold_left params ~init:env_with_fun_heads_only ~f:(
376 fun env (A.Field {name=var_name; escape=_; typ; pos}) ->
377 let var_ty = env_get_typ_actual ~env ~sym:typ ~pos in
378 Env.set_val env var_name (Value.Var {ty = var_ty})
381 (* we only care if an exception is raised *)
382 ignore (transExp ~env:env_with_fun_heads_and_local_vars body);
384 env_with_fun_heads_only
386 and transTy ~(env : Env.t) ~ty_name ~(ty_exp : A.ty) : Type.t =
388 | A.NameTy {symbol=sym; pos} ->
389 env_get_typ ~sym ~env ~pos
390 | A.RecordTy fields ->
392 List.map fields ~f:(fun (A.Field {name; escape=_; typ; pos}) ->
393 let ty = env_get_typ ~sym:typ ~env ~pos in
397 Type.new_record ~name:ty_name ~fields
398 | A.ArrayTy {symbol=sym; pos} ->
399 let element_ty = env_get_typ ~sym ~env ~pos in
400 Type.new_array ~name:ty_name ~ty:element_ty
406 let transProg absyn =
408 let {exp = _; ty = _} = transExp absyn ~env:Env.base in