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 SK |
59 | (* TODO: actual_ty *) |
60 | ||
523e2b06 SK |
61 | let rec transExp ~env exp = |
62 | let rec trexp exp = | |
63 | (match exp with | |
64 | | A.NilExp -> | |
65 | return_nil | |
66 | | A.IntExp _ -> | |
67 | return_int | |
68 | | A.StringExp {string=_; _} -> | |
69 | return_string | |
978cb41c SK |
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; | |
75 | ); | |
76 | return result | |
77 | | Value.Var _ -> | |
78 | E.raise (E.Id_not_a_function {id=func; pos}) | |
79 | ) | |
523e2b06 SK |
80 | | A.OpExp {oper; left; right; pos} -> |
81 | trop oper ~left ~right ~pos | |
0ed7a07c SK |
82 | | A.RecordExp {fields=field_exps; typ; pos} -> |
83 | let ty = env_get_typ ~sym:typ ~env ~pos in | |
84 | Type.if_record | |
85 | ty | |
86 | ~f:(fun field_tys -> | |
87 | List.iter field_exps ~f:(fun (field, exp, pos) -> | |
88 | (match List.assoc_opt field field_tys with | |
89 | | Some field_ty -> | |
90 | check_same {exp=(); ty=field_ty} (trexp exp) ~pos | |
91 | | None -> | |
92 | E.raise | |
93 | (E.No_such_field_in_record {field; record=ty; pos}) | |
94 | ) | |
95 | ) | |
96 | ) | |
97 | ~otherwise:(fun () -> | |
98 | E.raise (E.Wrong_type_used_as_record {ty_id=typ; ty; pos}) | |
99 | ); | |
100 | return ty | |
523e2b06 SK |
101 | | A.SeqExp exps -> |
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)); | |
105 | return_unit | |
106 | | A.AssignExp {var; exp; pos} -> | |
107 | check_same (trvar var) (trexp exp) ~pos; | |
108 | (* TODO: Add var->exp to val env? *) | |
109 | return_unit | |
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 -> | |
117 | expty_then | |
118 | | expty_then, Some else' -> | |
119 | let expty_else = trexp else' in | |
120 | check_same expty_then expty_else ~pos; | |
121 | expty_then | |
122 | ) | |
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 *) | |
127 | return_unit | |
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); | |
133 | return_unit | |
134 | | A.BreakExp _ -> | |
135 | return_unit | |
8744eb3a SK |
136 | | A.LetExp {decs; body; pos=_} -> |
137 | (* (1) decs augment env *) | |
138 | (* (2) body checked against the new env *) | |
139 | let env = | |
140 | List.fold_left decs ~init:env ~f:(fun env dec -> transDec dec ~env) | |
141 | in | |
142 | transExp body ~env | |
4c550cd5 SK |
143 | | A.ArrayExp {typ; size; init; pos} -> |
144 | check_int (trexp size) ~pos; | |
145 | let ty = env_get_typ ~sym:typ ~env ~pos in | |
146 | Type.if_array | |
147 | ty | |
148 | ~f:(fun ty_elements -> | |
149 | check_same {exp=(); ty=ty_elements} (trexp init) ~pos | |
150 | ) | |
151 | ~otherwise:(fun () -> | |
152 | E.raise (E.Wrong_type_used_as_array {ty_id=typ; ty; pos}) | |
153 | ); | |
154 | return ty | |
523e2b06 SK |
155 | | A.VarExp var -> |
156 | trvar var | |
157 | ) | |
158 | and trvar = | |
159 | (function | |
160 | | A.SimpleVar {symbol=sym; pos} -> | |
161 | (match env_get_val ~sym ~env ~pos with | |
162 | | Value.Fun _ -> E.raise (E.Id_is_a_function {id=sym; pos}) | |
163 | | Value.Var {ty} -> return ty | |
164 | ) | |
165 | | A.FieldVar {var; symbol; pos} -> | |
166 | let {exp=_; ty} = trvar var in | |
167 | Type.if_record | |
168 | ty | |
169 | ~f:(fun fields -> | |
170 | (match List.assoc_opt symbol fields with | |
171 | | None -> | |
172 | E.raise | |
173 | (E.No_such_field_in_record {field=symbol; record=ty; pos}) | |
174 | | Some ty -> | |
175 | return ty | |
176 | ) | |
177 | ) | |
178 | ~otherwise:(fun () -> E.raise (E.Exp_not_a_record {ty; pos})) | |
161a300d SK |
179 | | A.SubscriptVar {var; exp; pos} -> |
180 | let {exp=_; ty} = trvar var in | |
181 | check_int (trexp exp) ~pos; | |
182 | Type.if_array | |
183 | ty | |
184 | ~f:(fun ty_elements -> return ty_elements) | |
185 | ~otherwise:(fun () -> E.raise (E.Exp_not_an_array {ty; pos})) | |
523e2b06 SK |
186 | ) |
187 | and trop oper ~left ~right ~pos = | |
188 | let expty_left = trexp left in | |
189 | let expty_right = trexp right in | |
190 | check_same expty_left expty_right ~pos; | |
191 | let {exp=_; ty} = expty_left in | |
192 | let module T = Type in | |
193 | (match oper with | |
194 | (* Arithmetic: int *) | |
195 | | A.PlusOp | |
196 | | A.MinusOp | |
197 | | A.TimesOp | |
198 | | A.DivideOp -> | |
199 | check_int expty_left ~pos; | |
200 | return_int | |
201 | (* Equality: int, string, array, record *) | |
202 | | A.EqOp | |
203 | | A.NeqOp -> | |
204 | if (T.is_int ty) | |
205 | || (T.is_string ty) | |
206 | || (T.is_array ty) | |
207 | || (T.is_record ty) | |
208 | then | |
209 | return ty | |
210 | else | |
211 | E.raise (E.Invalid_operand_type | |
212 | { oper | |
213 | ; valid = ["int"; "string"; "array"; "record"] | |
214 | ; given = ty | |
215 | ; pos | |
216 | }) | |
217 | (* Order: int, string *) | |
218 | | A.LtOp | |
219 | | A.LeOp | |
220 | | A.GtOp | |
221 | | A.GeOp -> | |
222 | if (T.is_int ty) | |
223 | || (T.is_string ty) | |
224 | then | |
225 | return ty | |
226 | else | |
227 | E.raise (E.Invalid_operand_type | |
228 | { oper | |
229 | ; valid = ["int"; "string"] | |
230 | ; given = ty | |
231 | ; pos | |
232 | }) | |
233 | ) | |
234 | in | |
235 | trexp exp | |
8744eb3a SK |
236 | and transDec ~env dec = |
237 | (match dec with | |
238 | | A.VarDec {name; typ=typ_opt; init; pos=pos_outter; escape=_} -> | |
239 | let ty = | |
240 | (match (typ_opt, transExp ~env init) with | |
241 | | None, {ty; exp=()} -> | |
242 | ty | |
243 | | Some (sym, pos_inner), expty_init -> | |
244 | let ty = env_get_typ ~sym ~env ~pos:pos_inner in | |
245 | check_same {exp=(); ty} expty_init ~pos:pos_outter; | |
246 | ty | |
247 | ) | |
248 | in | |
249 | Env.set_val env name (Value.Var {ty}) | |
250 | | A.TypeDecs _ -> | |
251 | unimplemented () | |
252 | | A.FunDecs _ -> | |
253 | unimplemented () | |
254 | ) | |
155ee327 SK |
255 | |
256 | let transVar ~env:_ var = | |
257 | (match var with | |
258 | | A.SimpleVar {symbol=_; _} -> | |
259 | unimplemented () | |
260 | | A.FieldVar {var=_; symbol=_; _} -> | |
261 | unimplemented () | |
262 | | A.SubscriptVar {var=_; exp=_; _} -> | |
263 | unimplemented () | |
264 | ) | |
265 | ||
155ee327 SK |
266 | let transTy ~env:_ typ = |
267 | (match typ with | |
268 | | A.NameTy {symbol = _; pos = _} -> | |
269 | unimplemented () | |
270 | | A.RecordTy _ -> | |
271 | unimplemented () | |
272 | | A.ArrayTy {symbol = _; pos = _} -> | |
273 | unimplemented () | |
274 | ) | |
275 | end | |
276 | ||
8744eb3a SK |
277 | open Semant |
278 | ||
155ee327 | 279 | let transProg absyn = |
155ee327 SK |
280 | let {exp = _; ty = _} = transExp absyn ~env:Env.base in |
281 | () |