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
22 val transVar : env:Env.t -> A.var -> expty
23 val transDec : env:Env.t -> A.dec -> Env.t
24 val transTy : env:Env.t -> A.ty -> Type.t (* needs only type env *)
31 let unimplemented () =
32 failwith "unimplemented"
34 let return ty = {exp = (); ty}
35 let return_unit = return Type.Unit
36 let return_nil = return Type.Nil
37 let return_int = return Type.Int
38 let return_string = return Type.String
40 let env_get_typ ~sym ~env ~pos : Type.t =
41 match Env.get_typ env sym with
43 | None -> E.raise (E.Unknown_type {ty_id=sym; pos})
45 let env_get_val ~sym ~env ~pos : Value.t =
46 match Env.get_val env sym with
48 | None -> E.raise (E.Unknown_id {id=sym; pos})
50 let check_same {exp=_; ty=ty_left} {exp=_; ty=ty_right} ~pos : unit =
51 if Type.is_equal ty_left ty_right then
54 E.raise (E.Wrong_type {expected=ty_left; given=ty_right; pos})
56 let check_int expty ~pos : unit =
57 check_same {exp=(); ty=Type.Int} expty ~pos
61 let rec transExp ~env exp =
68 | A.StringExp {string=_; _} ->
70 | A.CallExp {func; args; pos} ->
71 (match env_get_val ~sym:func ~env ~pos with
72 | Value.Fun {formals; result} ->
73 List.iter2 formals args ~f:(fun ty_expected exp_given ->
74 check_same {exp=(); ty = ty_expected} (trexp exp_given) ~pos;
78 E.raise (E.Id_not_a_function {id=func; pos})
80 | A.OpExp {oper; left; right; pos} ->
81 trop oper ~left ~right ~pos
82 | A.RecordExp {fields=field_exps; typ; pos} ->
83 let ty = env_get_typ ~sym:typ ~env ~pos in
87 List.iter field_exps ~f:(fun (field, exp, pos) ->
88 (match List.assoc_opt field field_tys with
90 check_same {exp=(); ty=field_ty} (trexp exp) ~pos
93 (E.No_such_field_in_record {field; record=ty; pos})
98 E.raise (E.Wrong_type_used_as_record {ty_id=typ; ty; pos})
102 (* Ignoring value because we only care if a type-checking exception
103 * is raised in one of trexp calls: *)
104 List.iter exps ~f:(fun (exp, _) -> ignore (trexp exp));
106 | A.AssignExp {var; exp; pos} ->
107 check_same (trvar var) (trexp exp) ~pos;
108 (* TODO: Add var->exp to val env? *)
110 | A.IfExp {test; then'; else'; pos} ->
111 (* test : must be int, because we have no bool *)
112 (* then : must equal else *)
113 (* else : must equal then or be None *)
114 check_int (trexp test) ~pos;
115 (match (trexp then', else') with
116 | expty_then, None ->
118 | expty_then, Some else' ->
119 let expty_else = trexp else' in
120 check_same expty_then expty_else ~pos;
123 | A.WhileExp {test; body; pos} ->
124 (* test : must be int, because we have no bool *)
125 check_int (trexp test) ~pos;
126 ignore (trexp body); (* Only care if a type-error is raised *)
128 | A.ForExp {var; lo; hi; body; pos; escape=_} ->
129 check_int (trexp lo) ~pos;
130 check_int (trexp hi) ~pos;
131 (* Only care if a type-error is raised *)
132 ignore (transExp ~env:(Env.set_typ env var Type.Int) body);
136 | A.LetExp {decs=_; body=_; _} ->
138 | A.ArrayExp {typ; size; init; pos} ->
139 check_int (trexp size) ~pos;
140 let ty = env_get_typ ~sym:typ ~env ~pos in
143 ~f:(fun ty_elements ->
144 check_same {exp=(); ty=ty_elements} (trexp init) ~pos
146 ~otherwise:(fun () ->
147 E.raise (E.Wrong_type_used_as_array {ty_id=typ; ty; pos})
155 | A.SimpleVar {symbol=sym; pos} ->
156 (match env_get_val ~sym ~env ~pos with
157 | Value.Fun _ -> E.raise (E.Id_is_a_function {id=sym; pos})
158 | Value.Var {ty} -> return ty
160 | A.FieldVar {var; symbol; pos} ->
161 let {exp=_; ty} = trvar var in
165 (match List.assoc_opt symbol fields with
168 (E.No_such_field_in_record {field=symbol; record=ty; pos})
173 ~otherwise:(fun () -> E.raise (E.Exp_not_a_record {ty; pos}))
174 | A.SubscriptVar {var; exp; pos} ->
175 let {exp=_; ty} = trvar var in
176 check_int (trexp exp) ~pos;
179 ~f:(fun ty_elements -> return ty_elements)
180 ~otherwise:(fun () -> E.raise (E.Exp_not_an_array {ty; pos}))
182 and trop oper ~left ~right ~pos =
183 let expty_left = trexp left in
184 let expty_right = trexp right in
185 check_same expty_left expty_right ~pos;
186 let {exp=_; ty} = expty_left in
187 let module T = Type in
189 (* Arithmetic: int *)
194 check_int expty_left ~pos;
196 (* Equality: int, string, array, record *)
206 E.raise (E.Invalid_operand_type
208 ; valid = ["int"; "string"; "array"; "record"]
212 (* Order: int, string *)
222 E.raise (E.Invalid_operand_type
224 ; valid = ["int"; "string"]
232 let transVar ~env:_ var =
234 | A.SimpleVar {symbol=_; _} ->
236 | A.FieldVar {var=_; symbol=_; _} ->
238 | A.SubscriptVar {var=_; exp=_; _} ->
242 let transDec ~env:_ dec =
244 | A.VarDec {name=_; typ=_; init=_; pos=_; escape=_} ->
252 let transTy ~env:_ typ =
254 | A.NameTy {symbol = _; pos = _} ->
258 | A.ArrayTy {symbol = _; pos = _} ->
263 let transProg absyn =
265 let {exp = _; ty = _} = transExp absyn ~env:Env.base in