-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReturnChecker.hs
78 lines (66 loc) · 2.35 KB
/
ReturnChecker.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
module ReturnChecker (checkReturnPaths) where
import Control.Monad.Except
import ProgramStructure
type InnerMonad = Except (String, Position)
checkReturnPaths :: Program Position -> InnerMonad (Program Position)
checkReturnPaths (Program a defs) = do
ndefs <- mapM checkD defs
return (Program a ndefs)
checkD (FunctionDef a (VoidT tp) n as b) = do
let rb = removePastReturn b
nb <- checkVoidBlock rb
return (FunctionDef a (VoidT tp) n as nb)
checkD (FunctionDef a t n as b) = do
let rb = removePastReturn b
checkFB rb
return (FunctionDef a t n as rb)
checkD (ClassDef a c p decls) = do
ndecls <- mapM checkDecl decls
return (ClassDef a c p ndecls)
checkDecl (MethodDecl a (VoidT tp) n as b) = do
let rb = removePastReturn b
nb <- checkVoidBlock rb
return (MethodDecl a (VoidT tp) n as nb)
checkDecl (MethodDecl a t n as b) = do
let rb = removePastReturn b
checkFB rb
return (MethodDecl a t n as rb)
checkDecl d = return d
checkFB :: Block Position -> InnerMonad ()
checkFB b@(Block pos _) = do
c <- checkB b
if c then return ()
else throwError ("Not all paths return a value", pos)
checkB (Block _ stmts) =
innerCheck stmts
where
innerCheck (s:ss) = do
s <- checkS s
if s then return True
else innerCheck ss
innerCheck [] = return False
checkS (ReturnValue _ _) = return True
checkS (IfElse _ (Lit _ (Bool _ True)) s1 _) = checkS s1
checkS (IfElse _ (Lit _ (Bool _ False)) _ s2) = checkS s2
checkS (IfElse _ _ s1 s2) = do
b1 <- checkS s1
b2 <- checkS s2
return (b1 && b2)
checkS (While _ (Lit _ (Bool _ True)) _) = return True
checkS (While _ _ s) = checkS s
checkS (BlockStmt _ b) = checkB b
checkS _ = return False
checkVoidBlock b@(Block a stmts) =
if stmts == [] then return (Block a [ReturnVoid BuiltIn])
else
case last stmts of
ReturnVoid _ -> return b
_ -> return (Block a (stmts ++ [ReturnVoid BuiltIn]))
removePastReturn (Block a stmts) = (Block a (firstUntilReturn stmts))
where
firstUntilReturn (h:t) = case h of
ReturnVoid _ -> [h]
ReturnValue _ _ -> [h]
BlockStmt a b -> BlockStmt a (removePastReturn b) : firstUntilReturn t
_ -> h : firstUntilReturn t
firstUntilReturn [] = []