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 | |
862e5c05 SK |
22 | |
23 | (* transVar does not seem to be needed, as trvar handles all our cases. | |
24 | * Am I wrong? | |
25 | * | |
26 | * val transVar : env:Env.t -> A.var -> expty | |
27 | * | |
28 | *) | |
155ee327 SK |
29 | end = struct |
30 | type expty = | |
31 | { exp : Translate.exp | |
32 | ; ty : Type.t | |
33 | } | |
34 | ||
523e2b06 SK |
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 | |
155ee327 | 40 | |
523e2b06 SK |
41 | let env_get_typ ~sym ~env ~pos : Type.t = |
42 | match Env.get_typ env sym with | |
43 | | Some ty -> ty | |
44 | | None -> E.raise (E.Unknown_type {ty_id=sym; pos}) | |
45 | ||
46 | let env_get_val ~sym ~env ~pos : Value.t = | |
47 | match Env.get_val env sym with | |
48 | | Some ty -> ty | |
49 | | None -> E.raise (E.Unknown_id {id=sym; pos}) | |
50 | ||
51 | let check_same {exp=_; ty=ty_left} {exp=_; ty=ty_right} ~pos : unit = | |
52 | if Type.is_equal ty_left ty_right then | |
53 | () | |
54 | else | |
55 | E.raise (E.Wrong_type {expected=ty_left; given=ty_right; pos}) | |
56 | ||
57 | let check_int expty ~pos : unit = | |
58 | check_same {exp=(); ty=Type.Int} expty ~pos | |
59 | ||
978cb41c | 60 | (* TODO: actual_ty *) |
def58eef | 61 | (* TODO: mutual recursion *) |
978cb41c | 62 | |
523e2b06 SK |
63 | let rec transExp ~env exp = |
64 | let rec trexp exp = | |
65 | (match exp with | |
66 | | A.NilExp -> | |
67 | return_nil | |
68 | | A.IntExp _ -> | |
69 | return_int | |
70 | | A.StringExp {string=_; _} -> | |
71 | return_string | |
978cb41c SK |
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; | |
77 | ); | |
78 | return result | |
79 | | Value.Var _ -> | |
80 | E.raise (E.Id_not_a_function {id=func; pos}) | |
81 | ) | |
523e2b06 SK |
82 | | A.OpExp {oper; left; right; pos} -> |
83 | trop oper ~left ~right ~pos | |
0ed7a07c SK |
84 | | A.RecordExp {fields=field_exps; typ; pos} -> |
85 | let ty = env_get_typ ~sym:typ ~env ~pos in | |
86 | Type.if_record | |
87 | ty | |
88 | ~f:(fun field_tys -> | |
89 | List.iter field_exps ~f:(fun (field, exp, pos) -> | |
90 | (match List.assoc_opt field field_tys with | |
91 | | Some field_ty -> | |
92 | check_same {exp=(); ty=field_ty} (trexp exp) ~pos | |
93 | | None -> | |
94 | E.raise | |
95 | (E.No_such_field_in_record {field; record=ty; pos}) | |
96 | ) | |
97 | ) | |
98 | ) | |
99 | ~otherwise:(fun () -> | |
100 | E.raise (E.Wrong_type_used_as_record {ty_id=typ; ty; pos}) | |
101 | ); | |
102 | return ty | |
523e2b06 SK |
103 | | A.SeqExp exps -> |
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)); | |
76c771a7 | 107 | (* FIXME: Return type of last expression, not unit. *) |
523e2b06 SK |
108 | return_unit |
109 | | A.AssignExp {var; exp; pos} -> | |
110 | check_same (trvar var) (trexp exp) ~pos; | |
111 | (* TODO: Add var->exp to val env? *) | |
112 | return_unit | |
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 -> | |
120 | expty_then | |
121 | | expty_then, Some else' -> | |
122 | let expty_else = trexp else' in | |
123 | check_same expty_then expty_else ~pos; | |
124 | expty_then | |
125 | ) | |
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 *) | |
130 | return_unit | |
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 *) | |
85e08b69 SK |
135 | let env = Env.set_val env var (Value.Var {ty = Type.Int}) in |
136 | ignore (transExp ~env body); | |
523e2b06 SK |
137 | return_unit |
138 | | A.BreakExp _ -> | |
139 | return_unit | |
8744eb3a SK |
140 | | A.LetExp {decs; body; pos=_} -> |
141 | (* (1) decs augment env *) | |
142 | (* (2) body checked against the new env *) | |
143 | let env = | |
144 | List.fold_left decs ~init:env ~f:(fun env dec -> transDec dec ~env) | |
145 | in | |
146 | transExp body ~env | |
4c550cd5 SK |
147 | | A.ArrayExp {typ; size; init; pos} -> |
148 | check_int (trexp size) ~pos; | |
149 | let ty = env_get_typ ~sym:typ ~env ~pos in | |
150 | Type.if_array | |
151 | ty | |
152 | ~f:(fun ty_elements -> | |
153 | check_same {exp=(); ty=ty_elements} (trexp init) ~pos | |
154 | ) | |
155 | ~otherwise:(fun () -> | |
156 | E.raise (E.Wrong_type_used_as_array {ty_id=typ; ty; pos}) | |
157 | ); | |
158 | return ty | |
523e2b06 SK |
159 | | A.VarExp var -> |
160 | trvar var | |
161 | ) | |
162 | and trvar = | |
163 | (function | |
164 | | A.SimpleVar {symbol=sym; pos} -> | |
165 | (match env_get_val ~sym ~env ~pos with | |
166 | | Value.Fun _ -> E.raise (E.Id_is_a_function {id=sym; pos}) | |
167 | | Value.Var {ty} -> return ty | |
168 | ) | |
169 | | A.FieldVar {var; symbol; pos} -> | |
170 | let {exp=_; ty} = trvar var in | |
171 | Type.if_record | |
172 | ty | |
173 | ~f:(fun fields -> | |
174 | (match List.assoc_opt symbol fields with | |
175 | | None -> | |
176 | E.raise | |
177 | (E.No_such_field_in_record {field=symbol; record=ty; pos}) | |
178 | | Some ty -> | |
179 | return ty | |
180 | ) | |
181 | ) | |
182 | ~otherwise:(fun () -> E.raise (E.Exp_not_a_record {ty; pos})) | |
161a300d SK |
183 | | A.SubscriptVar {var; exp; pos} -> |
184 | let {exp=_; ty} = trvar var in | |
185 | check_int (trexp exp) ~pos; | |
186 | Type.if_array | |
187 | ty | |
188 | ~f:(fun ty_elements -> return ty_elements) | |
189 | ~otherwise:(fun () -> E.raise (E.Exp_not_an_array {ty; pos})) | |
523e2b06 SK |
190 | ) |
191 | and trop oper ~left ~right ~pos = | |
192 | let expty_left = trexp left in | |
193 | let expty_right = trexp right in | |
194 | check_same expty_left expty_right ~pos; | |
195 | let {exp=_; ty} = expty_left in | |
196 | let module T = Type in | |
197 | (match oper with | |
198 | (* Arithmetic: int *) | |
199 | | A.PlusOp | |
200 | | A.MinusOp | |
201 | | A.TimesOp | |
202 | | A.DivideOp -> | |
203 | check_int expty_left ~pos; | |
204 | return_int | |
205 | (* Equality: int, string, array, record *) | |
206 | | A.EqOp | |
207 | | A.NeqOp -> | |
208 | if (T.is_int ty) | |
209 | || (T.is_string ty) | |
210 | || (T.is_array ty) | |
211 | || (T.is_record ty) | |
212 | then | |
213 | return ty | |
214 | else | |
215 | E.raise (E.Invalid_operand_type | |
216 | { oper | |
217 | ; valid = ["int"; "string"; "array"; "record"] | |
218 | ; given = ty | |
219 | ; pos | |
220 | }) | |
221 | (* Order: int, string *) | |
222 | | A.LtOp | |
223 | | A.LeOp | |
224 | | A.GtOp | |
225 | | A.GeOp -> | |
226 | if (T.is_int ty) | |
227 | || (T.is_string ty) | |
228 | then | |
229 | return ty | |
230 | else | |
231 | E.raise (E.Invalid_operand_type | |
232 | { oper | |
233 | ; valid = ["int"; "string"] | |
234 | ; given = ty | |
235 | ; pos | |
236 | }) | |
237 | ) | |
238 | in | |
239 | trexp exp | |
862e5c05 | 240 | and transDec ~(env : Env.t) (dec : A.dec) : Env.t = |
8744eb3a SK |
241 | (match dec with |
242 | | A.VarDec {name; typ=typ_opt; init; pos=pos_outter; escape=_} -> | |
243 | let ty = | |
244 | (match (typ_opt, transExp ~env init) with | |
245 | | None, {ty; exp=()} -> | |
246 | ty | |
247 | | Some (sym, pos_inner), expty_init -> | |
248 | let ty = env_get_typ ~sym ~env ~pos:pos_inner in | |
249 | check_same {exp=(); ty} expty_init ~pos:pos_outter; | |
250 | ty | |
251 | ) | |
252 | in | |
253 | Env.set_val env name (Value.Var {ty}) | |
0324a942 SK |
254 | | A.TypeDecs typedecs -> |
255 | List.fold_left typedecs ~init:env ~f:( | |
256 | fun env (A.TypeDec {name; ty; pos=_}) -> | |
257 | let ty = transTy ~env ty in | |
258 | Env.set_typ env name ty | |
259 | ) | |
76c771a7 SK |
260 | | A.FunDecs fundecs -> |
261 | List.fold_left fundecs ~init:env ~f:( | |
262 | fun env (A.FunDec {name; params; result; body; pos=_}) -> | |
263 | let (env_for_body, formals_in_reverse_order) = | |
264 | List.fold_left params ~init:(env, []) ~f:( | |
265 | fun (env, formals) (A.Field {name; escape=_; typ; pos}) -> | |
266 | let ty = env_get_typ ~env ~sym:typ ~pos in | |
267 | let env = Env.set_val env name (Value.Var {ty}) in | |
268 | (env, ty :: formals) | |
269 | ) | |
270 | in | |
271 | (* ignore because we only care if an exception is raised *) | |
272 | ignore (transExp ~env:env_for_body body); | |
273 | let formals = List.rev formals_in_reverse_order in | |
274 | let result = | |
275 | match result with | |
276 | | None -> | |
277 | Type.Unit | |
278 | | Some (sym, pos) -> | |
279 | env_get_typ ~sym ~env ~pos | |
280 | in | |
281 | Env.set_val env name (Value.Fun {formals; result}) | |
282 | ) | |
8744eb3a | 283 | ) |
862e5c05 | 284 | and transTy ~(env : Env.t) (typ : A.ty) : Type.t = |
0324a942 SK |
285 | (match typ with |
286 | | A.NameTy {symbol=sym; pos} -> | |
287 | env_get_typ ~sym ~env ~pos | |
288 | | A.RecordTy fields -> | |
289 | let fields = | |
290 | List.map fields ~f:(fun (A.Field {name; escape=_; typ; pos}) -> | |
291 | let ty = env_get_typ ~sym:typ ~env ~pos in | |
292 | (name, ty) | |
293 | ) | |
294 | in | |
295 | Type.new_record fields | |
296 | | A.ArrayTy {symbol=sym; pos} -> | |
297 | let element_ty = env_get_typ ~sym ~env ~pos in | |
298 | Type.new_array element_ty | |
299 | ) | |
155ee327 SK |
300 | end |
301 | ||
8744eb3a SK |
302 | open Semant |
303 | ||
155ee327 | 304 | let transProg absyn = |
155ee327 SK |
305 | let {exp = _; ty = _} = transExp absyn ~env:Env.base in |
306 | () |