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