X-Git-Url: https://ruin.nu/git/?p=proglang.git;a=blobdiff_plain;f=Typecheck.hs;h=70aad706a88cfc5b38a1863744ccce563772e490;hp=cdba298976ad2aefcecaf7e71a52b74ebb6415f0;hb=fd3bc0ef4542783846af4e2c4f986e94f96c74ff;hpb=fe6d5e0ac088e6b648a5029b101402c017fd04de diff --git a/Typecheck.hs b/Typecheck.hs index cdba298..70aad70 100644 --- a/Typecheck.hs +++ b/Typecheck.hs @@ -75,13 +75,18 @@ typeCheckStm (SIf e s s') = do typeCheckStm (SWhile e s) = do TBool <- typeCheckExp e pushAndPop $ typeCheckStm s +typeCheckStm (SDeclD t i) = addVariable i t typeCheckStm (SDecl t i e) = do t2 <- typeCheckExp e assert (t == t2) $ "Illegal to assign an expression of type "++show t2++" to variable "++show i++" of type "++show t + addVariable i t +typeCheckStm (SPrint e) = do + typeCheckExp e + return () + +addVariable :: (MonadState Types m) => Ident -> Type -> m () +addVariable i t = do (m:ms) <- get case insertLookupWithKey (\k a1 a2 -> a1) i t m of (Nothing,m') -> put (m':ms) _ -> fail $ "Duplicate variable declaration: "++show i -typeCheckStm (SPrint e) = do - typeCheckExp e - return ()