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 | |
82 | | A.RecordExp {fields=_; typ=_; pos=_} -> | |
83 | unimplemented () | |
84 | | A.SeqExp exps -> | |
85 | (* Ignoring value because we only care if a type-checking exception | |
86 | * is raised in one of trexp calls: *) | |
87 | List.iter exps ~f:(fun (exp, _) -> ignore (trexp exp)); | |
88 | return_unit | |
89 | | A.AssignExp {var; exp; pos} -> | |
90 | check_same (trvar var) (trexp exp) ~pos; | |
91 | (* TODO: Add var->exp to val env? *) | |
92 | return_unit | |
93 | | A.IfExp {test; then'; else'; pos} -> | |
94 | (* test : must be int, because we have no bool *) | |
95 | (* then : must equal else *) | |
96 | (* else : must equal then or be None *) | |
97 | check_int (trexp test) ~pos; | |
98 | (match (trexp then', else') with | |
99 | | expty_then, None -> | |
100 | expty_then | |
101 | | expty_then, Some else' -> | |
102 | let expty_else = trexp else' in | |
103 | check_same expty_then expty_else ~pos; | |
104 | expty_then | |
105 | ) | |
106 | | A.WhileExp {test; body; pos} -> | |
107 | (* test : must be int, because we have no bool *) | |
108 | check_int (trexp test) ~pos; | |
109 | ignore (trexp body); (* Only care if a type-error is raised *) | |
110 | return_unit | |
111 | | A.ForExp {var; lo; hi; body; pos; escape=_} -> | |
112 | check_int (trexp lo) ~pos; | |
113 | check_int (trexp hi) ~pos; | |
114 | (* Only care if a type-error is raised *) | |
115 | ignore (transExp ~env:(Env.set_typ env var Type.Int) body); | |
116 | return_unit | |
117 | | A.BreakExp _ -> | |
118 | return_unit | |
119 | | A.LetExp {decs=_; body=_; _} -> | |
120 | unimplemented () | |
121 | | A.ArrayExp {typ=_; size=_; init=_; _} -> | |
122 | unimplemented () | |
123 | | A.VarExp var -> | |
124 | trvar var | |
125 | ) | |
126 | and trvar = | |
127 | (function | |
128 | | A.SimpleVar {symbol=sym; pos} -> | |
129 | (match env_get_val ~sym ~env ~pos with | |
130 | | Value.Fun _ -> E.raise (E.Id_is_a_function {id=sym; pos}) | |
131 | | Value.Var {ty} -> return ty | |
132 | ) | |
133 | | A.FieldVar {var; symbol; pos} -> | |
134 | let {exp=_; ty} = trvar var in | |
135 | Type.if_record | |
136 | ty | |
137 | ~f:(fun fields -> | |
138 | (match List.assoc_opt symbol fields with | |
139 | | None -> | |
140 | E.raise | |
141 | (E.No_such_field_in_record {field=symbol; record=ty; pos}) | |
142 | | Some ty -> | |
143 | return ty | |
144 | ) | |
145 | ) | |
146 | ~otherwise:(fun () -> E.raise (E.Exp_not_a_record {ty; pos})) | |
161a300d SK |
147 | | A.SubscriptVar {var; exp; pos} -> |
148 | let {exp=_; ty} = trvar var in | |
149 | check_int (trexp exp) ~pos; | |
150 | Type.if_array | |
151 | ty | |
152 | ~f:(fun ty_elements -> return ty_elements) | |
153 | ~otherwise:(fun () -> E.raise (E.Exp_not_an_array {ty; pos})) | |
523e2b06 SK |
154 | ) |
155 | and trop oper ~left ~right ~pos = | |
156 | let expty_left = trexp left in | |
157 | let expty_right = trexp right in | |
158 | check_same expty_left expty_right ~pos; | |
159 | let {exp=_; ty} = expty_left in | |
160 | let module T = Type in | |
161 | (match oper with | |
162 | (* Arithmetic: int *) | |
163 | | A.PlusOp | |
164 | | A.MinusOp | |
165 | | A.TimesOp | |
166 | | A.DivideOp -> | |
167 | check_int expty_left ~pos; | |
168 | return_int | |
169 | (* Equality: int, string, array, record *) | |
170 | | A.EqOp | |
171 | | A.NeqOp -> | |
172 | if (T.is_int ty) | |
173 | || (T.is_string ty) | |
174 | || (T.is_array ty) | |
175 | || (T.is_record ty) | |
176 | then | |
177 | return ty | |
178 | else | |
179 | E.raise (E.Invalid_operand_type | |
180 | { oper | |
181 | ; valid = ["int"; "string"; "array"; "record"] | |
182 | ; given = ty | |
183 | ; pos | |
184 | }) | |
185 | (* Order: int, string *) | |
186 | | A.LtOp | |
187 | | A.LeOp | |
188 | | A.GtOp | |
189 | | A.GeOp -> | |
190 | if (T.is_int ty) | |
191 | || (T.is_string ty) | |
192 | then | |
193 | return ty | |
194 | else | |
195 | E.raise (E.Invalid_operand_type | |
196 | { oper | |
197 | ; valid = ["int"; "string"] | |
198 | ; given = ty | |
199 | ; pos | |
200 | }) | |
201 | ) | |
202 | in | |
203 | trexp exp | |
155ee327 SK |
204 | |
205 | let transVar ~env:_ var = | |
206 | (match var with | |
207 | | A.SimpleVar {symbol=_; _} -> | |
208 | unimplemented () | |
209 | | A.FieldVar {var=_; symbol=_; _} -> | |
210 | unimplemented () | |
211 | | A.SubscriptVar {var=_; exp=_; _} -> | |
212 | unimplemented () | |
213 | ) | |
214 | ||
215 | let transDec ~env:_ dec = | |
216 | (match dec with | |
217 | | A.VarDec {name=_; typ=_; init=_; pos=_; escape=_} -> | |
218 | unimplemented () | |
219 | | A.TypeDecs _ -> | |
220 | unimplemented () | |
221 | | A.FunDecs _ -> | |
222 | unimplemented () | |
223 | ) | |
224 | ||
225 | let transTy ~env:_ typ = | |
226 | (match typ with | |
227 | | A.NameTy {symbol = _; pos = _} -> | |
228 | unimplemented () | |
229 | | A.RecordTy _ -> | |
230 | unimplemented () | |
231 | | A.ArrayTy {symbol = _; pos = _} -> | |
232 | unimplemented () | |
233 | ) | |
234 | end | |
235 | ||
236 | let transProg absyn = | |
237 | let open Semant in | |
238 | let {exp = _; ty = _} = transExp absyn ~env:Env.base in | |
239 | () |