Commit | Line | Data |
---|---|---|
523e2b06 SK |
1 | module List = ListLabels |
2 | ||
155ee327 SK |
3 | module A = Tiger_absyn |
4 | module Env = Tiger_env | |
5 | module E = Tiger_error | |
155ee327 SK |
6 | module Translate = Tiger_translate |
7 | module Type = Tiger_env_type | |
8 | module Value = Tiger_env_value | |
9 | ||
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. *) | |
12 | module Semant : sig | |
13 | type expty = | |
14 | { exp : Translate.exp | |
15 | ; ty : Type.t | |
16 | } | |
17 | ||
18 | (* Violating normal naming convention just to make it easier to follow | |
19 | * Appel's | |
20 | *) | |
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 *) | |
25 | end = struct | |
26 | type expty = | |
27 | { exp : Translate.exp | |
28 | ; ty : Type.t | |
29 | } | |
30 | ||
31 | let unimplemented () = | |
32 | failwith "unimplemented" | |
33 | ||
523e2b06 SK |
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 | |
155ee327 | 39 | |
523e2b06 SK |
40 | let env_get_typ ~sym ~env ~pos : Type.t = |
41 | match Env.get_typ env sym with | |
42 | | Some ty -> ty | |
43 | | None -> E.raise (E.Unknown_type {ty_id=sym; pos}) | |
44 | ||
45 | let env_get_val ~sym ~env ~pos : Value.t = | |
46 | match Env.get_val env sym with | |
47 | | Some ty -> ty | |
48 | | None -> E.raise (E.Unknown_id {id=sym; pos}) | |
49 | ||
50 | let check_same {exp=_; ty=ty_left} {exp=_; ty=ty_right} ~pos : unit = | |
51 | if Type.is_equal ty_left ty_right then | |
52 | () | |
53 | else | |
54 | E.raise (E.Wrong_type {expected=ty_left; given=ty_right; pos}) | |
55 | ||
56 | let check_int expty ~pos : unit = | |
57 | check_same {exp=(); ty=Type.Int} expty ~pos | |
58 | ||
978cb41c | 59 | (* TODO: actual_ty *) |
def58eef | 60 | (* TODO: mutual recursion *) |
978cb41c | 61 | |
523e2b06 SK |
62 | let rec transExp ~env exp = |
63 | let rec trexp exp = | |
64 | (match exp with | |
65 | | A.NilExp -> | |
66 | return_nil | |
67 | | A.IntExp _ -> | |
68 | return_int | |
69 | | A.StringExp {string=_; _} -> | |
70 | return_string | |
978cb41c SK |
71 | | A.CallExp {func; args; pos} -> |
72 | (match env_get_val ~sym:func ~env ~pos with | |
73 | | Value.Fun {formals; result} -> | |
74 | List.iter2 formals args ~f:(fun ty_expected exp_given -> | |
75 | check_same {exp=(); ty = ty_expected} (trexp exp_given) ~pos; | |
76 | ); | |
77 | return result | |
78 | | Value.Var _ -> | |
79 | E.raise (E.Id_not_a_function {id=func; pos}) | |
80 | ) | |
523e2b06 SK |
81 | | A.OpExp {oper; left; right; pos} -> |
82 | trop oper ~left ~right ~pos | |
0ed7a07c SK |
83 | | A.RecordExp {fields=field_exps; typ; pos} -> |
84 | let ty = env_get_typ ~sym:typ ~env ~pos in | |
85 | Type.if_record | |
86 | ty | |
87 | ~f:(fun field_tys -> | |
88 | List.iter field_exps ~f:(fun (field, exp, pos) -> | |
89 | (match List.assoc_opt field field_tys with | |
90 | | Some field_ty -> | |
91 | check_same {exp=(); ty=field_ty} (trexp exp) ~pos | |
92 | | None -> | |
93 | E.raise | |
94 | (E.No_such_field_in_record {field; record=ty; pos}) | |
95 | ) | |
96 | ) | |
97 | ) | |
98 | ~otherwise:(fun () -> | |
99 | E.raise (E.Wrong_type_used_as_record {ty_id=typ; ty; pos}) | |
100 | ); | |
101 | return ty | |
523e2b06 SK |
102 | | A.SeqExp exps -> |
103 | (* Ignoring value because we only care if a type-checking exception | |
104 | * is raised in one of trexp calls: *) | |
105 | List.iter exps ~f:(fun (exp, _) -> ignore (trexp exp)); | |
106 | return_unit | |
107 | | A.AssignExp {var; exp; pos} -> | |
108 | check_same (trvar var) (trexp exp) ~pos; | |
109 | (* TODO: Add var->exp to val env? *) | |
110 | return_unit | |
111 | | A.IfExp {test; then'; else'; pos} -> | |
112 | (* test : must be int, because we have no bool *) | |
113 | (* then : must equal else *) | |
114 | (* else : must equal then or be None *) | |
115 | check_int (trexp test) ~pos; | |
116 | (match (trexp then', else') with | |
117 | | expty_then, None -> | |
118 | expty_then | |
119 | | expty_then, Some else' -> | |
120 | let expty_else = trexp else' in | |
121 | check_same expty_then expty_else ~pos; | |
122 | expty_then | |
123 | ) | |
124 | | A.WhileExp {test; body; pos} -> | |
125 | (* test : must be int, because we have no bool *) | |
126 | check_int (trexp test) ~pos; | |
127 | ignore (trexp body); (* Only care if a type-error is raised *) | |
128 | return_unit | |
129 | | A.ForExp {var; lo; hi; body; pos; escape=_} -> | |
130 | check_int (trexp lo) ~pos; | |
131 | check_int (trexp hi) ~pos; | |
132 | (* Only care if a type-error is raised *) | |
133 | ignore (transExp ~env:(Env.set_typ env var Type.Int) body); | |
134 | return_unit | |
135 | | A.BreakExp _ -> | |
136 | return_unit | |
8744eb3a SK |
137 | | A.LetExp {decs; body; pos=_} -> |
138 | (* (1) decs augment env *) | |
139 | (* (2) body checked against the new env *) | |
140 | let env = | |
141 | List.fold_left decs ~init:env ~f:(fun env dec -> transDec dec ~env) | |
142 | in | |
143 | transExp body ~env | |
4c550cd5 SK |
144 | | A.ArrayExp {typ; size; init; pos} -> |
145 | check_int (trexp size) ~pos; | |
146 | let ty = env_get_typ ~sym:typ ~env ~pos in | |
147 | Type.if_array | |
148 | ty | |
149 | ~f:(fun ty_elements -> | |
150 | check_same {exp=(); ty=ty_elements} (trexp init) ~pos | |
151 | ) | |
152 | ~otherwise:(fun () -> | |
153 | E.raise (E.Wrong_type_used_as_array {ty_id=typ; ty; pos}) | |
154 | ); | |
155 | return ty | |
523e2b06 SK |
156 | | A.VarExp var -> |
157 | trvar var | |
158 | ) | |
159 | and trvar = | |
160 | (function | |
161 | | A.SimpleVar {symbol=sym; pos} -> | |
162 | (match env_get_val ~sym ~env ~pos with | |
163 | | Value.Fun _ -> E.raise (E.Id_is_a_function {id=sym; pos}) | |
164 | | Value.Var {ty} -> return ty | |
165 | ) | |
166 | | A.FieldVar {var; symbol; pos} -> | |
167 | let {exp=_; ty} = trvar var in | |
168 | Type.if_record | |
169 | ty | |
170 | ~f:(fun fields -> | |
171 | (match List.assoc_opt symbol fields with | |
172 | | None -> | |
173 | E.raise | |
174 | (E.No_such_field_in_record {field=symbol; record=ty; pos}) | |
175 | | Some ty -> | |
176 | return ty | |
177 | ) | |
178 | ) | |
179 | ~otherwise:(fun () -> E.raise (E.Exp_not_a_record {ty; pos})) | |
161a300d SK |
180 | | A.SubscriptVar {var; exp; pos} -> |
181 | let {exp=_; ty} = trvar var in | |
182 | check_int (trexp exp) ~pos; | |
183 | Type.if_array | |
184 | ty | |
185 | ~f:(fun ty_elements -> return ty_elements) | |
186 | ~otherwise:(fun () -> E.raise (E.Exp_not_an_array {ty; pos})) | |
523e2b06 SK |
187 | ) |
188 | and trop oper ~left ~right ~pos = | |
189 | let expty_left = trexp left in | |
190 | let expty_right = trexp right in | |
191 | check_same expty_left expty_right ~pos; | |
192 | let {exp=_; ty} = expty_left in | |
193 | let module T = Type in | |
194 | (match oper with | |
195 | (* Arithmetic: int *) | |
196 | | A.PlusOp | |
197 | | A.MinusOp | |
198 | | A.TimesOp | |
199 | | A.DivideOp -> | |
200 | check_int expty_left ~pos; | |
201 | return_int | |
202 | (* Equality: int, string, array, record *) | |
203 | | A.EqOp | |
204 | | A.NeqOp -> | |
205 | if (T.is_int ty) | |
206 | || (T.is_string ty) | |
207 | || (T.is_array ty) | |
208 | || (T.is_record ty) | |
209 | then | |
210 | return ty | |
211 | else | |
212 | E.raise (E.Invalid_operand_type | |
213 | { oper | |
214 | ; valid = ["int"; "string"; "array"; "record"] | |
215 | ; given = ty | |
216 | ; pos | |
217 | }) | |
218 | (* Order: int, string *) | |
219 | | A.LtOp | |
220 | | A.LeOp | |
221 | | A.GtOp | |
222 | | A.GeOp -> | |
223 | if (T.is_int ty) | |
224 | || (T.is_string ty) | |
225 | then | |
226 | return ty | |
227 | else | |
228 | E.raise (E.Invalid_operand_type | |
229 | { oper | |
230 | ; valid = ["int"; "string"] | |
231 | ; given = ty | |
232 | ; pos | |
233 | }) | |
234 | ) | |
235 | in | |
236 | trexp exp | |
8744eb3a SK |
237 | and transDec ~env dec = |
238 | (match dec with | |
239 | | A.VarDec {name; typ=typ_opt; init; pos=pos_outter; escape=_} -> | |
240 | let ty = | |
241 | (match (typ_opt, transExp ~env init) with | |
242 | | None, {ty; exp=()} -> | |
243 | ty | |
244 | | Some (sym, pos_inner), expty_init -> | |
245 | let ty = env_get_typ ~sym ~env ~pos:pos_inner in | |
246 | check_same {exp=(); ty} expty_init ~pos:pos_outter; | |
247 | ty | |
248 | ) | |
249 | in | |
250 | Env.set_val env name (Value.Var {ty}) | |
0324a942 SK |
251 | | A.TypeDecs typedecs -> |
252 | List.fold_left typedecs ~init:env ~f:( | |
253 | fun env (A.TypeDec {name; ty; pos=_}) -> | |
254 | let ty = transTy ~env ty in | |
255 | Env.set_typ env name ty | |
256 | ) | |
8744eb3a SK |
257 | | A.FunDecs _ -> |
258 | unimplemented () | |
259 | ) | |
0324a942 SK |
260 | and transTy ~env typ = |
261 | (match typ with | |
262 | | A.NameTy {symbol=sym; pos} -> | |
263 | env_get_typ ~sym ~env ~pos | |
264 | | A.RecordTy fields -> | |
265 | let fields = | |
266 | List.map fields ~f:(fun (A.Field {name; escape=_; typ; pos}) -> | |
267 | let ty = env_get_typ ~sym:typ ~env ~pos in | |
268 | (name, ty) | |
269 | ) | |
270 | in | |
271 | Type.new_record fields | |
272 | | A.ArrayTy {symbol=sym; pos} -> | |
273 | let element_ty = env_get_typ ~sym ~env ~pos in | |
274 | Type.new_array element_ty | |
275 | ) | |
155ee327 SK |
276 | |
277 | let transVar ~env:_ var = | |
278 | (match var with | |
279 | | A.SimpleVar {symbol=_; _} -> | |
280 | unimplemented () | |
281 | | A.FieldVar {var=_; symbol=_; _} -> | |
282 | unimplemented () | |
283 | | A.SubscriptVar {var=_; exp=_; _} -> | |
284 | unimplemented () | |
285 | ) | |
155ee327 SK |
286 | end |
287 | ||
8744eb3a SK |
288 | open Semant |
289 | ||
155ee327 | 290 | let transProg absyn = |
155ee327 SK |
291 | let {exp = _; ty = _} = transExp absyn ~env:Env.base in |
292 | () |