| 1 | module List = ListLabels |
| 2 | |
| 3 | module A = Tiger_absyn |
| 4 | module Dag = Tiger_dag |
| 5 | module Env = Tiger_env |
| 6 | module E = Tiger_error |
| 7 | module Escape = Tiger_semant_escape |
| 8 | module Pos = Tiger_position |
| 9 | module Sym = Tiger_symbol |
| 10 | module Temp = Tiger_temp |
| 11 | module Translate = Tiger_translate |
| 12 | module Type = Tiger_env_type |
| 13 | module Value = Tiger_env_value |
| 14 | |
| 15 | (* The only reason for having this seemingly-superfluous inner module is to |
| 16 | * have this nice signature as a summary of what each function does. *) |
| 17 | module Semant : sig |
| 18 | type expty = |
| 19 | { exp : Translate.exp |
| 20 | ; ty : Type.t |
| 21 | } |
| 22 | |
| 23 | (* Violating normal naming convention just to make it easier to follow |
| 24 | * Appel's |
| 25 | *) |
| 26 | val transExp : env:Env.t -> A.exp -> expty |
| 27 | |
| 28 | (* transVar does not seem to be needed, as trvar handles all our cases. |
| 29 | * Am I wrong? |
| 30 | * |
| 31 | * val transVar : env:Env.t -> A.var -> expty |
| 32 | * |
| 33 | *) |
| 34 | end = struct |
| 35 | type expty = |
| 36 | { exp : Translate.exp |
| 37 | ; ty : Type.t |
| 38 | } |
| 39 | |
| 40 | let rec actual_ty ty ~pos = |
| 41 | match ty with |
| 42 | | Type.Name (name, ty_opt_ref) -> |
| 43 | (match !ty_opt_ref with |
| 44 | | None -> |
| 45 | E.raise (E.Unknown_type {ty_id=name; pos}) |
| 46 | | Some ty -> |
| 47 | actual_ty ty ~pos |
| 48 | ) |
| 49 | | Type.Unit |
| 50 | | Type.Nil |
| 51 | | Type.Int |
| 52 | | Type.String |
| 53 | | Type.Record _ |
| 54 | | Type.Array _ -> |
| 55 | ty |
| 56 | |
| 57 | let return ty = {exp = (); ty} |
| 58 | let return_unit = return Type.Unit |
| 59 | let return_nil = return Type.Nil |
| 60 | let return_int = return Type.Int |
| 61 | let return_string = return Type.String |
| 62 | |
| 63 | let env_get_typ ~sym ~env ~pos : Type.t = |
| 64 | match Env.get_typ env sym with |
| 65 | | Some ty -> ty |
| 66 | | None -> E.raise (E.Unknown_type {ty_id=sym; pos}) |
| 67 | |
| 68 | let env_get_typ_actual ~sym ~env ~pos : Type.t = |
| 69 | actual_ty (env_get_typ ~sym ~env ~pos) ~pos |
| 70 | |
| 71 | let env_get_val ~sym ~env ~pos : Value.t = |
| 72 | match Env.get_val env sym with |
| 73 | | Some ty -> ty |
| 74 | | None -> E.raise (E.Unknown_id {id=sym; pos}) |
| 75 | |
| 76 | let check_same {exp=_; ty=ty_left} {exp=_; ty=ty_right} ~pos : unit = |
| 77 | if Type.is_equal ty_left ty_right then |
| 78 | () |
| 79 | else |
| 80 | E.raise (E.Wrong_type {expected=ty_left; given=ty_right; pos}) |
| 81 | |
| 82 | let check_int expty ~pos : unit = |
| 83 | check_same return_int expty ~pos |
| 84 | |
| 85 | let paths_of_typedecs typedecs : (Sym.t * Sym.t * Pos.t) list list = |
| 86 | let (path, paths) = |
| 87 | List.fold_left typedecs ~init:([], []) ~f:( |
| 88 | fun (path, paths) (A.TypeDec {name=child; ty; pos}) -> |
| 89 | match ty with |
| 90 | | A.NameTy {symbol=parent; _} -> |
| 91 | (((parent, child, pos) :: path), paths) |
| 92 | | A.RecordTy _ |
| 93 | | A.ArrayTy _ -> |
| 94 | ([], path :: paths) |
| 95 | ) |
| 96 | in |
| 97 | List.map (path :: paths) ~f:List.rev |
| 98 | |
| 99 | let check_cycles (typedecs : A.typedec list) : unit = |
| 100 | let non_empty_paths = |
| 101 | List.filter |
| 102 | (paths_of_typedecs typedecs) |
| 103 | ~f:(function [] -> false | _ -> true) |
| 104 | in |
| 105 | List.iter non_empty_paths ~f:( |
| 106 | fun path -> |
| 107 | match Dag.of_list (List.map path ~f:(fun (p, c, _) -> (p, c))) with |
| 108 | | Ok _ -> |
| 109 | () |
| 110 | | Error `Cycle -> |
| 111 | let (_, from_id, from_pos) = List.hd path in |
| 112 | let (_, to_id, to_pos) = List.hd (List.rev path) in |
| 113 | E.raise (E.Cycle_in_type_decs {from_id; from_pos; to_id; to_pos}) |
| 114 | ) |
| 115 | |
| 116 | let rec transExp ~env exp = |
| 117 | let rec trexp exp = |
| 118 | (match exp with |
| 119 | | A.NilExp -> |
| 120 | return_nil |
| 121 | | A.IntExp _ -> |
| 122 | return_int |
| 123 | | A.StringExp {string=_; _} -> |
| 124 | return_string |
| 125 | | A.CallExp {func; args; pos} -> |
| 126 | (match env_get_val ~sym:func ~env ~pos with |
| 127 | | Value.Fun {formals; result; level=_; label=_} -> |
| 128 | let expected = List.length formals in |
| 129 | let given = List.length args in |
| 130 | if given = expected then |
| 131 | begin |
| 132 | List.iter2 formals args ~f:(fun ty_expected exp_given -> |
| 133 | check_same |
| 134 | (return (actual_ty ~pos ty_expected)) |
| 135 | (trexp exp_given) |
| 136 | ~pos; |
| 137 | ); |
| 138 | return (actual_ty ~pos result) |
| 139 | end |
| 140 | else |
| 141 | E.raise (E.Wrong_number_of_args {func; expected; given; pos}) |
| 142 | | Value.Var _ -> |
| 143 | E.raise (E.Id_not_a_function {id=func; pos}) |
| 144 | ) |
| 145 | | A.OpExp {oper; left; right; pos} -> |
| 146 | trop oper ~left ~right ~pos |
| 147 | | A.RecordExp {fields=field_exps; typ; pos} -> |
| 148 | let ty = env_get_typ_actual ~sym:typ ~env ~pos in |
| 149 | Type.if_record |
| 150 | ty |
| 151 | ~f:(fun field_tys -> |
| 152 | List.iter field_exps ~f:(fun (field, exp, pos) -> |
| 153 | (match List.assoc_opt field field_tys with |
| 154 | | Some field_ty -> |
| 155 | check_same (return (actual_ty ~pos field_ty)) (trexp exp) ~pos |
| 156 | | None -> |
| 157 | E.raise |
| 158 | (E.No_such_field_in_record {field; record=ty; pos}) |
| 159 | ) |
| 160 | ) |
| 161 | ) |
| 162 | ~otherwise:(fun () -> |
| 163 | E.raise (E.Wrong_type_used_as_record {ty_id=typ; ty; pos}) |
| 164 | ); |
| 165 | return (actual_ty ~pos ty) |
| 166 | | A.SeqExp [] -> |
| 167 | return_unit |
| 168 | | A.SeqExp exps -> |
| 169 | let last xs = |
| 170 | xs |
| 171 | |> List.rev (* Yes, redundant, but clean-looking ;-P *) |
| 172 | |> List.hd (* Empty is matched in above SeqExp match case *) |
| 173 | in |
| 174 | exps |
| 175 | |> List.map ~f:(fun (exp, _) -> trexp exp) |
| 176 | |> last |
| 177 | | A.AssignExp {var; exp; pos} -> |
| 178 | check_same (trvar var) (trexp exp) ~pos; |
| 179 | (* TODO: Add var->exp to val env? *) |
| 180 | return_unit |
| 181 | | A.IfExp {test; then'; else'; pos} -> |
| 182 | (* test : must be int, because we have no bool *) |
| 183 | (* then : must equal else *) |
| 184 | (* else : must equal then or be None *) |
| 185 | check_int (trexp test) ~pos; |
| 186 | (match (trexp then', else') with |
| 187 | | expty_then, None -> |
| 188 | expty_then |
| 189 | | expty_then, Some else' -> |
| 190 | let expty_else = trexp else' in |
| 191 | check_same expty_then expty_else ~pos; |
| 192 | expty_then |
| 193 | ) |
| 194 | | A.WhileExp {test; body; pos} -> |
| 195 | (* test : must be int, because we have no bool *) |
| 196 | check_int (trexp test) ~pos; |
| 197 | let (loop, env) = Env.loop_begin env in |
| 198 | (* Only care if an error is raised *) |
| 199 | ignore (transExp ~env body); |
| 200 | ignore (Env.loop_end env loop); |
| 201 | return_unit |
| 202 | | A.ForExp {var; lo; hi; body; pos; escape=_} -> |
| 203 | check_int (trexp lo) ~pos; |
| 204 | check_int (trexp hi) ~pos; |
| 205 | let (loop, env) = Env.loop_begin env in |
| 206 | let level = Env.level_get env in |
| 207 | (* Assuming all escape, for now *) |
| 208 | let access = Translate.alloc_local ~level ~escapes:true in |
| 209 | let env = Env.set_val env var (Value.Var {ty = Type.Int; access}) in |
| 210 | (* Only care if an error is raised *) |
| 211 | ignore (transExp ~env body); |
| 212 | ignore (Env.loop_end env loop); |
| 213 | return_unit |
| 214 | | A.BreakExp pos -> |
| 215 | (match Env.loop_current env with |
| 216 | | Some _ -> () |
| 217 | | None -> E.raise (E.Break_outside_loop pos) |
| 218 | ); |
| 219 | return_unit |
| 220 | | A.LetExp {decs; body; pos=_} -> |
| 221 | (* (1) decs augment env *) |
| 222 | (* (2) body checked against the new env *) |
| 223 | let env = |
| 224 | List.fold_left decs ~init:env ~f:(fun env dec -> transDec dec ~env) |
| 225 | in |
| 226 | transExp body ~env |
| 227 | | A.ArrayExp {typ; size; init; pos} -> |
| 228 | check_int (trexp size) ~pos; |
| 229 | let ty = env_get_typ_actual ~sym:typ ~env ~pos in |
| 230 | Type.if_array |
| 231 | ty |
| 232 | ~f:(fun ty_elements -> |
| 233 | check_same (return (actual_ty ~pos ty_elements)) (trexp init) ~pos |
| 234 | ) |
| 235 | ~otherwise:(fun () -> |
| 236 | E.raise (E.Wrong_type_used_as_array {ty_id=typ; ty; pos}) |
| 237 | ); |
| 238 | return (actual_ty ~pos ty) |
| 239 | | A.VarExp var -> |
| 240 | trvar var |
| 241 | ) |
| 242 | and trvar = |
| 243 | (function |
| 244 | | A.SimpleVar {symbol=sym; pos} -> |
| 245 | (match env_get_val ~sym ~env ~pos with |
| 246 | | Value.Fun _ -> |
| 247 | E.raise (E.Id_is_a_function {id=sym; pos}) |
| 248 | | Value.Var {ty; access=_} -> |
| 249 | return (actual_ty ~pos ty) |
| 250 | ) |
| 251 | | A.FieldVar {var; symbol; pos} -> |
| 252 | let {exp=_; ty} = trvar var in |
| 253 | Type.if_record |
| 254 | ty |
| 255 | ~f:(fun fields -> |
| 256 | (match List.assoc_opt symbol fields with |
| 257 | | None -> |
| 258 | E.raise |
| 259 | (E.No_such_field_in_record {field=symbol; record=ty; pos}) |
| 260 | | Some ty -> |
| 261 | return (actual_ty ~pos ty) |
| 262 | ) |
| 263 | ) |
| 264 | ~otherwise:(fun () -> E.raise (E.Exp_not_a_record {ty; pos})) |
| 265 | | A.SubscriptVar {var; exp; pos} -> |
| 266 | let {exp=_; ty} = trvar var in |
| 267 | check_int (trexp exp) ~pos; |
| 268 | Type.if_array |
| 269 | ty |
| 270 | ~f:(fun ty_elements -> return (actual_ty ~pos ty_elements)) |
| 271 | ~otherwise:(fun () -> E.raise (E.Exp_not_an_array {ty; pos})) |
| 272 | ) |
| 273 | and trop oper ~left ~right ~pos = |
| 274 | (* TODO: Refactor trop - all opers return bool/int *) |
| 275 | let expty_left = trexp left in |
| 276 | let expty_right = trexp right in |
| 277 | check_same expty_left expty_right ~pos; |
| 278 | let {exp=_; ty} = expty_left in |
| 279 | let module T = Type in |
| 280 | (match oper with |
| 281 | (* Arithmetic: int *) |
| 282 | | A.PlusOp |
| 283 | | A.MinusOp |
| 284 | | A.TimesOp |
| 285 | | A.DivideOp -> |
| 286 | check_int expty_left ~pos; |
| 287 | return_int |
| 288 | (* Equality: int, string, array, record *) |
| 289 | | A.EqOp |
| 290 | | A.NeqOp -> |
| 291 | if (T.is_int ty) |
| 292 | || (T.is_string ty) |
| 293 | || (T.is_array ty) |
| 294 | || (T.is_record ty) |
| 295 | then |
| 296 | return_int (* Because we have no bool type *) |
| 297 | else |
| 298 | E.raise (E.Invalid_operand_type |
| 299 | { oper |
| 300 | ; valid = ["int"; "string"; "array"; "record"] |
| 301 | ; given = ty |
| 302 | ; pos |
| 303 | }) |
| 304 | (* Order: int, string *) |
| 305 | | A.LtOp |
| 306 | | A.LeOp |
| 307 | | A.GtOp |
| 308 | | A.GeOp -> |
| 309 | if (T.is_int ty) |
| 310 | || (T.is_string ty) |
| 311 | then |
| 312 | return_int (* Because we have no bool type *) |
| 313 | else |
| 314 | E.raise (E.Invalid_operand_type |
| 315 | { oper |
| 316 | ; valid = ["int"; "string"] |
| 317 | ; given = ty |
| 318 | ; pos |
| 319 | }) |
| 320 | ) |
| 321 | in |
| 322 | trexp exp |
| 323 | and transDec ~(env : Env.t) (dec : A.dec) : Env.t = |
| 324 | (match dec with |
| 325 | | A.VarDec {name; typ=typ_opt; init; pos=pos_outter; escape=_} -> |
| 326 | let ty = |
| 327 | (match (typ_opt, transExp ~env init) with |
| 328 | | None, {ty; exp=()} -> |
| 329 | ty |
| 330 | | Some (sym, pos_inner), expty_init -> |
| 331 | let ty = env_get_typ_actual ~sym ~env ~pos:pos_inner in |
| 332 | check_same (return ty) expty_init ~pos:pos_outter; |
| 333 | ty |
| 334 | ) |
| 335 | in |
| 336 | let access = |
| 337 | Translate.alloc_local |
| 338 | ~level:(Env.level_get env) |
| 339 | ~escapes:true (* Assuming all escape, for now... *) |
| 340 | in |
| 341 | Env.set_val env name (Value.Var {ty; access}) |
| 342 | | A.TypeDecs typedecs -> |
| 343 | check_cycles typedecs; |
| 344 | let env = |
| 345 | List.fold_left typedecs ~init:env ~f:( |
| 346 | fun env (A.TypeDec {name; ty=_; pos=_}) -> |
| 347 | Env.set_typ env name (Type.Name (name, ref None)) |
| 348 | ) |
| 349 | in |
| 350 | List.iter typedecs ~f:(fun (A.TypeDec {name=ty_name; ty=ty_exp; pos}) -> |
| 351 | let ty = transTy ~env ~ty_name ~ty_exp in |
| 352 | (match env_get_typ ~sym:ty_name ~env ~pos with |
| 353 | | Type.Name (_, ty_opt_ref) -> |
| 354 | ty_opt_ref := Some ty |
| 355 | | Type.Unit |
| 356 | | Type.Nil |
| 357 | | Type.Int |
| 358 | | Type.String |
| 359 | | Type.Record _ |
| 360 | | Type.Array _ -> |
| 361 | () |
| 362 | ) |
| 363 | ); |
| 364 | env |
| 365 | | A.FunDecs fundecs -> |
| 366 | let env_with_fun_heads_only = |
| 367 | List.fold_left fundecs ~init:env ~f:( |
| 368 | fun env (A.FunDec {name; params; result; body=_; pos=_}) -> |
| 369 | let formals = |
| 370 | List.map params ~f:( |
| 371 | fun (A.Field {name=_; typ; pos; escape=_}) -> |
| 372 | env_get_typ_actual ~env ~sym:typ ~pos |
| 373 | ) |
| 374 | in |
| 375 | let result = |
| 376 | match result with |
| 377 | | Some (s, p) -> env_get_typ_actual ~sym:s ~env ~pos:p |
| 378 | | None -> Type.Unit |
| 379 | in |
| 380 | let label = Temp.Label.gen () in |
| 381 | let level = |
| 382 | Translate.Level.next |
| 383 | (Env.level_get env) |
| 384 | ~name:label |
| 385 | (* Assuming all escape (for now) *) |
| 386 | ~formals:(List.map formals ~f:(fun _ -> true)) |
| 387 | in |
| 388 | let env = Env.level_set env level in |
| 389 | Env.set_val env name (Value.Fun {formals; result; level; label}) |
| 390 | ) |
| 391 | in |
| 392 | List.iter fundecs ~f:( |
| 393 | fun (A.FunDec {name=_; params; result=_; body; pos=_}) -> |
| 394 | let env_with_fun_heads_and_local_vars = |
| 395 | List.fold_left params ~init:env_with_fun_heads_only ~f:( |
| 396 | fun env (A.Field {name=var_name; escape=_; typ; pos}) -> |
| 397 | let var_ty = env_get_typ_actual ~env ~sym:typ ~pos in |
| 398 | let level = Env.level_get env in |
| 399 | (* Assuming all escape, for now *) |
| 400 | let access = Translate.alloc_local ~level ~escapes:true in |
| 401 | Env.set_val |
| 402 | env |
| 403 | var_name |
| 404 | (Value.Var {ty = var_ty; access}) |
| 405 | ) |
| 406 | in |
| 407 | (* we only care if an exception is raised *) |
| 408 | ignore (transExp ~env:env_with_fun_heads_and_local_vars body); |
| 409 | ); |
| 410 | env_with_fun_heads_only |
| 411 | ) |
| 412 | and transTy ~(env : Env.t) ~ty_name ~(ty_exp : A.ty) : Type.t = |
| 413 | (match ty_exp with |
| 414 | | A.NameTy {symbol=sym; pos} -> |
| 415 | env_get_typ ~sym ~env ~pos |
| 416 | | A.RecordTy fields -> |
| 417 | let fields = |
| 418 | List.map fields ~f:(fun (A.Field {name; escape=_; typ; pos}) -> |
| 419 | let ty = env_get_typ ~sym:typ ~env ~pos in |
| 420 | (name, ty) |
| 421 | ) |
| 422 | in |
| 423 | Type.new_record ~name:ty_name ~fields |
| 424 | | A.ArrayTy {symbol=sym; pos} -> |
| 425 | let element_ty = env_get_typ ~sym ~env ~pos in |
| 426 | Type.new_array ~name:ty_name ~ty:element_ty |
| 427 | ) |
| 428 | end |
| 429 | |
| 430 | open Semant |
| 431 | |
| 432 | let transProg absyn = |
| 433 | Escape.find ~prog:absyn; |
| 434 | let {exp = _; ty = _} = transExp absyn ~env:Env.base in |
| 435 | () |