From 0324a942124e962cba76dbb9985f49ff1fa0960a Mon Sep 17 00:00:00 2001 From: Siraaj Khandkar Date: Fri, 14 Sep 2018 09:36:42 -0400 Subject: [PATCH] WIP type-checking -- check type declarations --- compiler/src/lib/tiger/tiger_semant.ml | 34 +++++++++++++++++--------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/compiler/src/lib/tiger/tiger_semant.ml b/compiler/src/lib/tiger/tiger_semant.ml index 0258351..3a80f21 100644 --- a/compiler/src/lib/tiger/tiger_semant.ml +++ b/compiler/src/lib/tiger/tiger_semant.ml @@ -247,11 +247,31 @@ end = struct ) in Env.set_val env name (Value.Var {ty}) - | A.TypeDecs _ -> - unimplemented () + | 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 @@ -262,16 +282,6 @@ end = struct | A.SubscriptVar {var=_; exp=_; _} -> unimplemented () ) - - let transTy ~env:_ typ = - (match typ with - | A.NameTy {symbol = _; pos = _} -> - unimplemented () - | A.RecordTy _ -> - unimplemented () - | A.ArrayTy {symbol = _; pos = _} -> - unimplemented () - ) end open Semant -- 2.20.1