| 1 | module List = ListLabels |
| 2 | |
| 3 | module A = Tiger_absyn |
| 4 | module Env = Tiger_env |
| 5 | module E = Tiger_error |
| 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 | |
| 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 |
| 39 | |
| 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 | |
| 59 | (* TODO: actual_ty *) |
| 60 | (* TODO: mutual recursion *) |
| 61 | |
| 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 |
| 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 | ) |
| 81 | | A.OpExp {oper; left; right; pos} -> |
| 82 | trop oper ~left ~right ~pos |
| 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 |
| 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 | (* FIXME: Return type of last expression, not unit. *) |
| 107 | return_unit |
| 108 | | A.AssignExp {var; exp; pos} -> |
| 109 | check_same (trvar var) (trexp exp) ~pos; |
| 110 | (* TODO: Add var->exp to val env? *) |
| 111 | return_unit |
| 112 | | A.IfExp {test; then'; else'; pos} -> |
| 113 | (* test : must be int, because we have no bool *) |
| 114 | (* then : must equal else *) |
| 115 | (* else : must equal then or be None *) |
| 116 | check_int (trexp test) ~pos; |
| 117 | (match (trexp then', else') with |
| 118 | | expty_then, None -> |
| 119 | expty_then |
| 120 | | expty_then, Some else' -> |
| 121 | let expty_else = trexp else' in |
| 122 | check_same expty_then expty_else ~pos; |
| 123 | expty_then |
| 124 | ) |
| 125 | | A.WhileExp {test; body; pos} -> |
| 126 | (* test : must be int, because we have no bool *) |
| 127 | check_int (trexp test) ~pos; |
| 128 | ignore (trexp body); (* Only care if a type-error is raised *) |
| 129 | return_unit |
| 130 | | A.ForExp {var; lo; hi; body; pos; escape=_} -> |
| 131 | check_int (trexp lo) ~pos; |
| 132 | check_int (trexp hi) ~pos; |
| 133 | (* Only care if a type-error is raised *) |
| 134 | ignore (transExp ~env:(Env.set_typ env var Type.Int) body); |
| 135 | return_unit |
| 136 | | A.BreakExp _ -> |
| 137 | return_unit |
| 138 | | A.LetExp {decs; body; pos=_} -> |
| 139 | (* (1) decs augment env *) |
| 140 | (* (2) body checked against the new env *) |
| 141 | let env = |
| 142 | List.fold_left decs ~init:env ~f:(fun env dec -> transDec dec ~env) |
| 143 | in |
| 144 | transExp body ~env |
| 145 | | A.ArrayExp {typ; size; init; pos} -> |
| 146 | check_int (trexp size) ~pos; |
| 147 | let ty = env_get_typ ~sym:typ ~env ~pos in |
| 148 | Type.if_array |
| 149 | ty |
| 150 | ~f:(fun ty_elements -> |
| 151 | check_same {exp=(); ty=ty_elements} (trexp init) ~pos |
| 152 | ) |
| 153 | ~otherwise:(fun () -> |
| 154 | E.raise (E.Wrong_type_used_as_array {ty_id=typ; ty; pos}) |
| 155 | ); |
| 156 | return ty |
| 157 | | A.VarExp var -> |
| 158 | trvar var |
| 159 | ) |
| 160 | and trvar = |
| 161 | (function |
| 162 | | A.SimpleVar {symbol=sym; pos} -> |
| 163 | (match env_get_val ~sym ~env ~pos with |
| 164 | | Value.Fun _ -> E.raise (E.Id_is_a_function {id=sym; pos}) |
| 165 | | Value.Var {ty} -> return ty |
| 166 | ) |
| 167 | | A.FieldVar {var; symbol; pos} -> |
| 168 | let {exp=_; ty} = trvar var in |
| 169 | Type.if_record |
| 170 | ty |
| 171 | ~f:(fun fields -> |
| 172 | (match List.assoc_opt symbol fields with |
| 173 | | None -> |
| 174 | E.raise |
| 175 | (E.No_such_field_in_record {field=symbol; record=ty; pos}) |
| 176 | | Some ty -> |
| 177 | return ty |
| 178 | ) |
| 179 | ) |
| 180 | ~otherwise:(fun () -> E.raise (E.Exp_not_a_record {ty; pos})) |
| 181 | | A.SubscriptVar {var; exp; pos} -> |
| 182 | let {exp=_; ty} = trvar var in |
| 183 | check_int (trexp exp) ~pos; |
| 184 | Type.if_array |
| 185 | ty |
| 186 | ~f:(fun ty_elements -> return ty_elements) |
| 187 | ~otherwise:(fun () -> E.raise (E.Exp_not_an_array {ty; pos})) |
| 188 | ) |
| 189 | and trop oper ~left ~right ~pos = |
| 190 | let expty_left = trexp left in |
| 191 | let expty_right = trexp right in |
| 192 | check_same expty_left expty_right ~pos; |
| 193 | let {exp=_; ty} = expty_left in |
| 194 | let module T = Type in |
| 195 | (match oper with |
| 196 | (* Arithmetic: int *) |
| 197 | | A.PlusOp |
| 198 | | A.MinusOp |
| 199 | | A.TimesOp |
| 200 | | A.DivideOp -> |
| 201 | check_int expty_left ~pos; |
| 202 | return_int |
| 203 | (* Equality: int, string, array, record *) |
| 204 | | A.EqOp |
| 205 | | A.NeqOp -> |
| 206 | if (T.is_int ty) |
| 207 | || (T.is_string ty) |
| 208 | || (T.is_array ty) |
| 209 | || (T.is_record ty) |
| 210 | then |
| 211 | return ty |
| 212 | else |
| 213 | E.raise (E.Invalid_operand_type |
| 214 | { oper |
| 215 | ; valid = ["int"; "string"; "array"; "record"] |
| 216 | ; given = ty |
| 217 | ; pos |
| 218 | }) |
| 219 | (* Order: int, string *) |
| 220 | | A.LtOp |
| 221 | | A.LeOp |
| 222 | | A.GtOp |
| 223 | | A.GeOp -> |
| 224 | if (T.is_int ty) |
| 225 | || (T.is_string ty) |
| 226 | then |
| 227 | return ty |
| 228 | else |
| 229 | E.raise (E.Invalid_operand_type |
| 230 | { oper |
| 231 | ; valid = ["int"; "string"] |
| 232 | ; given = ty |
| 233 | ; pos |
| 234 | }) |
| 235 | ) |
| 236 | in |
| 237 | trexp exp |
| 238 | and transDec ~env dec = |
| 239 | (match dec with |
| 240 | | A.VarDec {name; typ=typ_opt; init; pos=pos_outter; escape=_} -> |
| 241 | let ty = |
| 242 | (match (typ_opt, transExp ~env init) with |
| 243 | | None, {ty; exp=()} -> |
| 244 | ty |
| 245 | | Some (sym, pos_inner), expty_init -> |
| 246 | let ty = env_get_typ ~sym ~env ~pos:pos_inner in |
| 247 | check_same {exp=(); ty} expty_init ~pos:pos_outter; |
| 248 | ty |
| 249 | ) |
| 250 | in |
| 251 | Env.set_val env name (Value.Var {ty}) |
| 252 | | A.TypeDecs typedecs -> |
| 253 | List.fold_left typedecs ~init:env ~f:( |
| 254 | fun env (A.TypeDec {name; ty; pos=_}) -> |
| 255 | let ty = transTy ~env ty in |
| 256 | Env.set_typ env name ty |
| 257 | ) |
| 258 | | A.FunDecs fundecs -> |
| 259 | List.fold_left fundecs ~init:env ~f:( |
| 260 | fun env (A.FunDec {name; params; result; body; pos=_}) -> |
| 261 | let (env_for_body, formals_in_reverse_order) = |
| 262 | List.fold_left params ~init:(env, []) ~f:( |
| 263 | fun (env, formals) (A.Field {name; escape=_; typ; pos}) -> |
| 264 | let ty = env_get_typ ~env ~sym:typ ~pos in |
| 265 | let env = Env.set_val env name (Value.Var {ty}) in |
| 266 | (env, ty :: formals) |
| 267 | ) |
| 268 | in |
| 269 | (* ignore because we only care if an exception is raised *) |
| 270 | ignore (transExp ~env:env_for_body body); |
| 271 | let formals = List.rev formals_in_reverse_order in |
| 272 | let result = |
| 273 | match result with |
| 274 | | None -> |
| 275 | Type.Unit |
| 276 | | Some (sym, pos) -> |
| 277 | env_get_typ ~sym ~env ~pos |
| 278 | in |
| 279 | Env.set_val env name (Value.Fun {formals; result}) |
| 280 | ) |
| 281 | ) |
| 282 | and transTy ~env typ = |
| 283 | (match typ with |
| 284 | | A.NameTy {symbol=sym; pos} -> |
| 285 | env_get_typ ~sym ~env ~pos |
| 286 | | A.RecordTy fields -> |
| 287 | let fields = |
| 288 | List.map fields ~f:(fun (A.Field {name; escape=_; typ; pos}) -> |
| 289 | let ty = env_get_typ ~sym:typ ~env ~pos in |
| 290 | (name, ty) |
| 291 | ) |
| 292 | in |
| 293 | Type.new_record fields |
| 294 | | A.ArrayTy {symbol=sym; pos} -> |
| 295 | let element_ty = env_get_typ ~sym ~env ~pos in |
| 296 | Type.new_array element_ty |
| 297 | ) |
| 298 | |
| 299 | let transVar ~env:_ var = |
| 300 | (match var with |
| 301 | | A.SimpleVar {symbol=_; _} -> |
| 302 | unimplemented () |
| 303 | | A.FieldVar {var=_; symbol=_; _} -> |
| 304 | unimplemented () |
| 305 | | A.SubscriptVar {var=_; exp=_; _} -> |
| 306 | unimplemented () |
| 307 | ) |
| 308 | end |
| 309 | |
| 310 | open Semant |
| 311 | |
| 312 | let transProg absyn = |
| 313 | let {exp = _; ty = _} = transExp absyn ~env:Env.base in |
| 314 | () |