WIP type-checking -- check type declarations
[tiger.ml.git] / compiler / src / lib / tiger / tiger_semant.ml
index 40407ef..3a80f21 100644 (file)
@@ -79,8 +79,25 @@ end = struct
           )
       | A.OpExp {oper; left; right; pos} ->
           trop oper ~left ~right ~pos
-      | A.RecordExp {fields=_; typ=_; pos=_} ->
-          unimplemented ()
+      | A.RecordExp {fields=field_exps; typ; pos} ->
+          let ty = env_get_typ ~sym:typ ~env ~pos in
+          Type.if_record
+            ty
+            ~f:(fun field_tys ->
+              List.iter field_exps ~f:(fun (field, exp, pos) ->
+                (match List.assoc_opt field field_tys with
+                | Some field_ty ->
+                    check_same {exp=(); ty=field_ty} (trexp exp) ~pos
+                | None ->
+                    E.raise
+                      (E.No_such_field_in_record {field; record=ty; pos})
+                )
+              )
+            )
+            ~otherwise:(fun () ->
+              E.raise (E.Wrong_type_used_as_record {ty_id=typ; ty; pos})
+            );
+          return ty
       | A.SeqExp exps ->
           (* Ignoring value because we only care if a type-checking exception
            * is raised in one of trexp calls: *)
@@ -116,10 +133,25 @@ end = struct
           return_unit
       | A.BreakExp _ ->
           return_unit
-      | A.LetExp {decs=_; body=_; _} ->
-          unimplemented ()
-      | A.ArrayExp {typ=_; size=_; init=_; _} ->
-          unimplemented ()
+      | A.LetExp {decs; body; pos=_} ->
+          (* (1) decs augment env *)
+          (* (2) body checked against the new env *)
+          let env =
+            List.fold_left decs ~init:env ~f:(fun env dec -> transDec dec ~env)
+          in
+          transExp body ~env
+      | A.ArrayExp {typ; size; init; pos} ->
+          check_int (trexp size) ~pos;
+          let ty = env_get_typ ~sym:typ ~env ~pos in
+          Type.if_array
+            ty
+            ~f:(fun ty_elements ->
+              check_same {exp=(); ty=ty_elements} (trexp init) ~pos
+            )
+            ~otherwise:(fun () ->
+              E.raise (E.Wrong_type_used_as_array {ty_id=typ; ty; pos})
+            );
+          return ty
       | A.VarExp var ->
           trvar var
       )
@@ -201,6 +233,45 @@ end = struct
       )
     in
     trexp exp
+  and transDec ~env dec =
+    (match dec with
+    | A.VarDec {name; typ=typ_opt; init; pos=pos_outter; escape=_} ->
+        let ty =
+          (match (typ_opt, transExp ~env init) with
+          | None, {ty; exp=()} ->
+              ty
+          | Some (sym, pos_inner), expty_init ->
+              let ty = env_get_typ ~sym ~env ~pos:pos_inner in
+              check_same {exp=(); ty} expty_init ~pos:pos_outter;
+              ty
+          )
+        in
+        Env.set_val env name (Value.Var {ty})
+    | A.TypeDecs typedecs ->
+        List.fold_left typedecs ~init:env ~f:(
+          fun env (A.TypeDec {name; ty; pos=_}) ->
+            let ty = transTy ~env ty in
+            Env.set_typ env name ty
+        )
+    | A.FunDecs _ ->
+        unimplemented ()
+    )
+  and transTy ~env typ =
+    (match typ with
+    | A.NameTy {symbol=sym; pos} ->
+        env_get_typ ~sym ~env ~pos
+    | A.RecordTy fields ->
+        let fields =
+          List.map fields ~f:(fun (A.Field {name; escape=_; typ; pos}) ->
+            let ty = env_get_typ ~sym:typ ~env ~pos in
+            (name, ty)
+          )
+        in
+        Type.new_record fields
+    | A.ArrayTy {symbol=sym; pos} ->
+        let element_ty = env_get_typ ~sym ~env ~pos in
+        Type.new_array element_ty
+    )
 
   let transVar ~env:_ var =
     (match var with
@@ -211,29 +282,10 @@ end = struct
     | A.SubscriptVar {var=_; exp=_; _} ->
         unimplemented ()
     )
-
-  let transDec ~env:_ dec =
-    (match dec with
-    | A.VarDec {name=_; typ=_; init=_; pos=_; escape=_} ->
-        unimplemented ()
-    | A.TypeDecs _ ->
-        unimplemented ()
-    | A.FunDecs _ ->
-        unimplemented ()
-    )
-
-  let transTy ~env:_ typ =
-    (match typ with
-    | A.NameTy {symbol = _; pos = _} ->
-        unimplemented ()
-    | A.RecordTy _ ->
-        unimplemented ()
-    | A.ArrayTy {symbol = _; pos = _} ->
-        unimplemented ()
-    )
 end
 
+open Semant
+
 let transProg absyn =
-  let open Semant in
   let {exp = _; ty = _} = transExp absyn ~env:Env.base in
   ()
This page took 0.031488 seconds and 4 git commands to generate.