X-Git-Url: https://git.martlubbers.net/?a=blobdiff_plain;f=sem.icl;h=667b6eb24db9eb4530845742c826be87a6dfdc03;hb=5b1c37a52f0d63d4972665f672df100a231a2f57;hp=99aa44a41f9b8cbf51d10dd8e3702fb74c222f13;hpb=8c4ec1c3287d6c6ddefd22ef608a4d97693e7e85;p=cc1516.git diff --git a/sem.icl b/sem.icl index 99aa44a..667b6eb 100644 --- a/sem.icl +++ b/sem.icl @@ -22,6 +22,15 @@ from parse import :: ParserOutput, :: Error :: Gamma :== 'Map'.Map String Type :: Env a :== (State Gamma (Either SemError a)) +get = state $ \s -> (s,s) + +putIdent :: String Type -> Env Void +putIdent i t = gets ('Map'.get i) >>= \mt -> case mt of + Nothing = pure <$> modify ('Map'.put i t) + Just t2 = unify t t2 >>= \r -> case r of + Left e = pure $ Left e + Right t3 = pure <$> modify ('Map'.put i t3) + instance toString SemError where toString (ParseError p e) = concat [ toString p,"SemError: ParseError: ", e] @@ -52,36 +61,42 @@ semFunDecl f = pure $ Right f semVarDecl :: VarDecl -> Env VarDecl semVarDecl v = pure $ Right v //Right v -//semVarDecl vd=:(VarDecl pos type ident expr) = case unify type expr of -// Left e = Left e +//semVarDecl vd=:(VarDecl pos type ident expr) = case unify type expr of // Left e = Left e // //TODO ident in de environment // Right e = Right $ pure vd -// -//typeExpr :: Expr -> Env Type -//typeExpr (IntExpr _ _) = Right $ pure IntType -//typeExpr (CharExpr _ _) = Right $ pure CharType -//typeExpr (BoolExpr _ _) = Right $ pure BoolType -//typeExpr (Op1Expr p UnNegation expr) = undef//typeExpr expr -//// >>= \exprtype->case exprtype of -//// Right BoolType = Right $ pure BoolType -//// t = Left $ UnifyError p BoolType exprtype -//typeExpr (Op1Expr p UnMinus expr) = undef// typeExpr expr -//// >>= \exprtype->case exprtype of -//// IntType = Right $ pure IntType -//// t = Left $ UnifyError p IntType exprtype -//// typeExpr (Op2Expr Pos Expr Op2 Expr) = undef -////typeExpr (FunExpr Pos FunCall -////typeExpr (EmptyListExpr Pos -////typeExpr (TupleExpr Pos (Expr, Expr) -////typeExpr (VarExpr Pos VarDef) = undef -//// -//class unify a :: Type a -> Env a -// -//instance unify Type where -// unify IntType IntType = Right $ pure IntType -// unify BoolType BoolType = Right $ pure BoolType -// unify CharType CharType = Right $ pure CharType -// unify _ _ = undef + +typeOp1 :: Pos Expr Type -> Env Type +typeOp1 p expr rtype = typeExpr expr >>= \exprtype->case exprtype of + Left e = pure $ Left e + Right rtype = pure $ Right rtype + Right (IdType ident) = putIdent ident rtype >>| pure (Right rtype) + Right t = pure $ Left $ UnifyError p rtype t + +typeExpr :: Expr -> Env Type +typeExpr (IntExpr _ _) = pure $ Right IntType +typeExpr (CharExpr _ _) = pure $ Right CharType +typeExpr (BoolExpr _ _) = pure $ Right BoolType +typeExpr (Op1Expr p UnNegation expr) = typeOp1 p expr BoolType +typeExpr (Op1Expr p UnMinus expr) = typeOp1 p expr IntType +typeExpr (TupleExpr p (e1, e2)) = typeExpr e1 + >>= \ete1->typeExpr e2 >>= \ete2->pure ( + ete1 >>= \te1->ete2 >>= \te2->Right $ TupleType (te1, te2)) +//typeExpr (Op1Expr p UnMinus expr) = typeExpr expr +// >>= \exprtype->case exprtype of +// IntType = pure $ Right IntType +// t = Left $ UnifyError p IntType exprtype +//typeExpr (Op2Expr Pos Expr Op2 Expr) = undef +//typeExpr (FunExpr Pos FunCall) = undef +//typeExpr (EmptyListExpr Pos) = undef +//typeExpr (VarExpr Pos VarDef) = undef + +class unify a :: Type a -> Env Type + +instance unify Type where + unify IntType IntType = pure $ Right IntType + unify BoolType BoolType = pure $ Right BoolType + unify CharType CharType = pure $ Right CharType + unify _ _ = undef // //instance unify Expr where // unify type expr = case type of @@ -97,14 +112,3 @@ semVarDecl v = pure $ Right v // BoolType = undef // CharType = undef // VarType = undef -// -//extrPos :: Expr -> Pos -//extrPos (VarExpr p _) = p -//extrPos (Op2Expr p _ _ _) = p -//extrPos (Op1Expr p _ _) = p -//extrPos (IntExpr p _) = p -//extrPos (CharExpr p _) = p -//extrPos (BoolExpr p _) = p -//extrPos (FunExpr p _) = p -//extrPos (EmptyListExpr p) = p -//extrPos (TupleExpr p _) = p