From: pimjager Date: Tue, 12 Apr 2016 22:44:22 +0000 (+0200) Subject: Added some expression typechecking X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=a9c9d183b4bd43472ca0e40ec8b5f044a398c327;hp=-c;p=cc1516.git Added some expression typechecking --- a9c9d183b4bd43472ca0e40ec8b5f044a398c327 diff --git a/examples/varEx.spl b/examples/varEx.spl index 2336e05..f3fa5fa 100644 --- a/examples/varEx.spl +++ b/examples/varEx.spl @@ -6,6 +6,15 @@ var d = True; var e = 4; //Int f = 4 + True; +var g = True == True; +var h = 1 != 3; +//var i = True == 1; + +var j = 1 < 3; +//var k = True < 3; + +var l = 1:2:[]; + facR(n) :: Int -> Int { return 5; } \ No newline at end of file diff --git a/sem.icl b/sem.icl index 202f2ad..6e177ec 100644 --- a/sem.icl +++ b/sem.icl @@ -95,23 +95,25 @@ typeExpr (Op2Expr p e1 BiTimes e2) = unify IntType e1 >>| unify IntType e2 typeExpr (Op2Expr p e1 BiDivide e2) = unify IntType e1 >>| unify IntType e2 typeExpr (Op2Expr p e1 BiMod e2) = unify IntType e1 >>| unify IntType e2 //bool, char of int -typeExpr (Op2Expr p e1 BiEquals e2) = undef -typeExpr (Op2Expr p e1 BiUnEqual e2) = undef +typeExpr (Op2Expr p e1 BiEquals e2) = typeExpr e1 >>= \t1 -> unify t1 e2 + >>| pure BoolType //todo, actually check t1 in Char,Bool,Int +typeExpr (Op2Expr p e1 BiUnEqual e2) = typeExpr (Op2Expr p e1 BiEquals e2) //char of int -typeExpr (Op2Expr p e1 BiLesser e2) = undef -typeExpr (Op2Expr p e1 BiGreater e2) = undef -typeExpr (Op2Expr p e1 BiLesserEq e2) = undef -typeExpr (Op2Expr p e1 BiGreaterEq e2) = undef +typeExpr (Op2Expr p e1 BiLesser e2) = typeExpr e1 >>= \t1 -> unify t1 e2 + >>| pure BoolType //todo, actually check t1 in Char, Int +typeExpr (Op2Expr p e1 BiGreater e2) = typeExpr (Op2Expr p e1 BiLesser e2) +typeExpr (Op2Expr p e1 BiLesserEq e2) = typeExpr (Op2Expr p e1 BiLesser e2) +typeExpr (Op2Expr p e1 BiGreaterEq e2) = typeExpr (Op2Expr p e1 BiLesser e2) //bool -typeExpr (Op2Expr p e1 BiAnd e2) = undef -typeExpr (Op2Expr p e1 BiOr e2) = undef +typeExpr (Op2Expr p e1 BiAnd e2) = unify BoolType e1 >>| unify BoolType e2 +typeExpr (Op2Expr p e1 BiOr e2) = unify BoolType e1 >>| unify BoolType e2 //a -typeExpr (Op2Expr p e1 BiCons e2) = undef -//typeExpr (FunExpr Pos FunCall) = undef -//typeExpr (EmptyListExpr Pos) = undef +typeExpr (Op2Expr p e1 BiCons e2) = typeExpr e1 >>= \t1-> typeExpr e2 + >>= \t2-> unify (ListType t1) t2 +//typeExpr (FunExpr p FunCall) = undef +typeExpr (EmptyListExpr p) = pure $ ListType IntType //we'll need quantified types //typeExpr (VarExpr Pos VarDef) = undef //when checking var-expr, be sure to -//put the infered type - //in the context + //put the infered type in the context class unify a :: Type a -> Env Type @@ -133,6 +135,7 @@ instance unify Type where unify IntType IntType = pure IntType unify BoolType BoolType = pure BoolType unify CharType CharType = pure CharType + unify (ListType t1) (ListType t2) = unify t1 t2 unify t1 t2 = liftT $ Left $ UnifyError zero t1 t2 instance zero Pos where @@ -156,4 +159,4 @@ extrPos (CharExpr p _) = p extrPos (BoolExpr p _) = p extrPos (FunExpr p _) = p extrPos (EmptyListExpr p) = p -extrPos (TupleExpr p _) = p +extrPos (TupleExpr p _) = p \ No newline at end of file