Skip to content

Commit 09228c1

Browse files
committed
flatter structure
1 parent b724024 commit 09228c1

File tree

13 files changed

+103
-66
lines changed

13 files changed

+103
-66
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.cabal-sandbox/*
2+
cabal.sandbox.config

ppa.cabal

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ extra-source-files: README.md
1717
cabal-version: >=1.10
1818

1919
library
20-
exposed-modules: PPA.Lang.While.Syntax PPA.Lang.While.Internal.Syntax PPA.Lang.While.Util
20+
exposed-modules: PPA.Lang.While PPA.Lang.While.Internal PPA.Lang.While.Util
2121
-- other-modules:
2222
other-extensions: FlexibleContexts
2323
build-depends: base == 4.8.*, parsec == 3.1.*, containers == 0.5.*

src/PPA/Lang/While.hs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module PPA.Lang.While (S.Stmt, S.showL) where
2+
3+
import PPA.Lang.While.Internal as S

src/PPA/Lang/While/Internal/Syntax.hs src/PPA/Lang/While/Internal.hs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{-# LANGUAGE FlexibleContexts #-}
22

3-
module PPA.Lang.While.Internal.Syntax where
3+
module PPA.Lang.While.Internal where
44

55
import Prelude hiding (Num, GT, LT)
6+
67
import Text.Parsec
7-
import qualified Text.ParserCombinators.Parsec.Char as Char
8-
import qualified Text.ParserCombinators.Parsec.Language as Language
9-
import qualified Text.ParserCombinators.Parsec.Token as Token
10-
import qualified Text.ParserCombinators.Parsec.Expr as Expr
8+
import qualified Text.Parsec.Char as Char
9+
import qualified Text.Parsec.Language as Language
10+
import qualified Text.Parsec.Token as Token
11+
import qualified Text.Parsec.Expr as Expr
1112

1213
import qualified Data.List as List
1314

src/PPA/Lang/While/Syntax.hs

-3
This file was deleted.

src/PPA/Lang/While/Util.hs

+29-21
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import Prelude hiding (init)
44

55
import qualified Data.Set as Set
66

7-
import PPA.Lang.While.Internal.Syntax
7+
import PPA.Lang.While.Internal
88

99
data Block = BAssign Var AExp Lab | BSkip Lab | BBExp BExp Lab deriving (Eq, Ord)
1010

1111
instance Show Block where
1212
show (BAssign x a l) = "(" ++ (show l) ++ ") " ++ (show $ SAssign x a l)
1313
show (BSkip l) = "(" ++ (show l) ++ ") " ++ (show $ SSkip l)
1414
show (BBExp b l) = "(" ++ (show l) ++ ") " ++ (show b)
15-
15+
-- 2.1, p. 36
1616
init :: Stmt -> Lab
1717
init (SAssign _ _ l) = l
1818
init (SSkip l) = l
@@ -23,6 +23,7 @@ init (SSeq ss) = init s_1
2323
init (SIf _ l _ _) = l
2424
init (SWhile _ l _) = l
2525

26+
-- 2.1, p. 36
2627
final :: Stmt -> Set.Set Lab
2728
final (SAssign _ _ l) = Set.singleton l
2829
final (SSkip l) = Set.singleton l
@@ -33,6 +34,7 @@ final (SSeq ss) = final s_n
3334
final (SIf _ _ s1 s2) = Set.unions [(final s1), (final s2)]
3435
final (SWhile _ l _) = Set.singleton l
3536

37+
-- 2.1, p. 36
3638
blocks :: Stmt -> Set.Set Block
3739
blocks (SAssign x a l) = Set.singleton $ BAssign x a l
3840
blocks (SSkip l) = Set.singleton $ BSkip l
@@ -43,32 +45,15 @@ blocks (SSeq ss) = Set.unions $ bs
4345
blocks (SIf b l s1 s2) = Set.unions [Set.singleton $ BBExp b l, blocks s1, blocks s2]
4446
blocks (SWhile b l s) = Set.unions [Set.singleton $ BBExp b l, blocks s]
4547

48+
-- 2.1, p. 37
4649
labels :: Stmt -> Set.Set Lab
4750
labels s = Set.map getLabel $ blocks s
4851
where
4952
getLabel (BAssign _ _ l) = l
5053
getLabel (BSkip l) = l
5154
getLabel (BBExp _ l) = l
5255

53-
fvA :: AExp -> Set.Set Var
54-
fvA (AVar x) = Set.singleton x
55-
fvA (ANum _) = Set.empty
56-
fvA (AOpA _ a1 a2) = Set.unions [fvA a1, fvA a2]
57-
58-
fvB :: BExp -> Set.Set Var
59-
fvB (BTrue) = Set.empty
60-
fvB (BFalse) = Set.empty
61-
fvB (BNot b) = fvB b
62-
fvB (BOpB _ b1 b2) = Set.unions [fvB b1, fvB b2]
63-
fvB (BOpR _ a1 a2) = Set.unions [fvA a1, fvA a2]
64-
65-
fv :: Stmt -> Set.Set Var
66-
fv (SAssign x a _) = Set.unions [Set.singleton x, fvA a]
67-
fv (SSkip _) = Set.empty
68-
fv (SSeq ss) = Set.unions $ map fv ss
69-
fv (SIf b _ s1 s2) = Set.unions [fvB b, fv s1, fv s2]
70-
fv (SWhile b _ s) = Set.unions [fvB b, fv s]
71-
56+
-- 2.1, p. 37
7257
flow :: Stmt -> Set.Set (Lab, Lab)
7358
flow (SAssign _ _ _) = Set.empty
7459
flow (SSkip _) = Set.empty
@@ -101,6 +86,29 @@ flow (SWhile _ l s) = Set.unions [subflows, bodyflows]
10186
bodyflows :: Set.Set (Lab, Lab)
10287
bodyflows = Set.unions [Set.singleton (l, init s), Set.map (\ l' -> (l', l)) $ final s]
10388

89+
-- 2.1, p. 38
10490
flowReverse :: Stmt -> Set.Set (Lab, Lab)
10591
flowReverse s = Set.map (\ (l, l') -> (l', l)) $ flow s
10692

93+
fvA :: AExp -> Set.Set Var
94+
fvA (AVar x) = Set.singleton x
95+
fvA (ANum _) = Set.empty
96+
fvA (AOpA _ a1 a2) = Set.unions [fvA a1, fvA a2]
97+
98+
fvB :: BExp -> Set.Set Var
99+
fvB (BTrue) = Set.empty
100+
fvB (BFalse) = Set.empty
101+
fvB (BNot b) = fvB b
102+
fvB (BOpB _ b1 b2) = Set.unions [fvB b1, fvB b2]
103+
fvB (BOpR _ a1 a2) = Set.unions [fvA a1, fvA a2]
104+
105+
-- 2.1, p. 38
106+
-- NOTE: Referenced, but not defined
107+
fv :: Stmt -> Set.Set Var
108+
fv (SAssign x a _) = Set.unions [Set.singleton x, fvA a]
109+
fv (SSkip _) = Set.empty
110+
fv (SSeq ss) = Set.unions $ map fv ss
111+
fv (SIf b _ s1 s2) = Set.unions [fvB b, fv s1, fv s2]
112+
fv (SWhile b _ s) = Set.unions [fvB b, fv s]
113+
114+

test/Main.hs

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ module Main (main) where
22

33
import Test.Hspec
44

5-
import qualified PPATest.Lang.While.SyntaxSpec
6-
import qualified PPATest.Lang.While.UtilSpec
5+
import qualified PPATest.Lang.While
6+
import qualified PPATest.Lang.While.Internal
7+
import qualified PPATest.Lang.While.Util
78

8-
import qualified PPATest.Lang.While.Internal.SyntaxSpec
99

1010
main :: IO ()
1111
main = hspec spec
1212

1313
spec :: Spec
1414
spec = do
15-
describe "Lang.While.SyntaxSpec" PPATest.Lang.While.SyntaxSpec.spec
16-
describe "Lang.While.UtilSpec" PPATest.Lang.While.UtilSpec.spec
17-
describe "Lang.While.Internal.SyntaxSpec" PPATest.Lang.While.Internal.SyntaxSpec.spec
15+
describe "Lang.While" PPATest.Lang.While.spec
16+
describe "Lang.While.Internal" PPATest.Lang.While.Internal.spec
17+
describe "Lang.While.Util" PPATest.Lang.While.Util.spec

test/PPATest/Common.hs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module PPATest.Common where
22

3+
4+
35
-- Progs
46

57
ifBasic :: String

test/PPATest/Lang/While.hs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
module PPATest.Lang.While (spec) where
2+
3+
import Test.Hspec
4+
5+
import PPATest.Common
6+
7+
import PPA.Lang.While
8+
9+
spec :: Spec
10+
spec = do
11+
describe "read . show" $ do
12+
it "-- ifBasic" $
13+
rs ifBasicS `shouldBe` ifBasicS
14+
it "-- ifTwice" $
15+
rs ifTwiceS `shouldBe` ifTwiceS
16+
it "-- power" $
17+
rs powerS `shouldBe` powerS
18+
it "-- factorial" $
19+
rs factorialS `shouldBe` factorialS
20+
it "-- available" $
21+
rs availableS `shouldBe` availableS
22+
where
23+
24+
rs :: Stmt -> Stmt
25+
rs = (read . show)
26+
27+
ifBasicS :: Stmt
28+
ifBasicS = read ifBasic
29+
30+
ifTwiceS :: Stmt
31+
ifTwiceS = read ifTwice
32+
33+
powerS :: Stmt
34+
powerS = read power
35+
36+
factorialS :: Stmt
37+
factorialS = read factorial
38+
39+
availableS :: Stmt
40+
availableS = read available
41+

test/PPATest/Lang/While/Internal.hs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module PPATest.Lang.While.Internal (spec) where
2+
3+
import Test.Hspec
4+
5+
spec :: Spec
6+
spec = return ()

test/PPATest/Lang/While/Internal/SyntaxSpec.hs

-6
This file was deleted.

test/PPATest/Lang/While/SyntaxSpec.hs

-21
This file was deleted.

test/PPATest/Lang/While/UtilSpec.hs test/PPATest/Lang/While/Util.hs

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module PPATest.Lang.While.UtilSpec (spec) where
1+
module PPATest.Lang.While.Util (spec) where
22

33
import Prelude hiding (init)
44

@@ -8,8 +8,8 @@ import PPATest.Common
88

99
import qualified Data.Set as Set
1010

11+
import PPA.Lang.While
1112
import PPA.Lang.While.Util
12-
import PPA.Lang.While.Syntax
1313

1414
spec :: Spec
1515
spec = do
@@ -24,6 +24,7 @@ spec = do
2424
(init $ factorialS) `shouldBe` 1
2525
it "-- available" $ do
2626
(init $ availableS) `shouldBe` 1
27+
2728
describe "final" $ do
2829
it "-- ifBasic" $ do
2930
(final $ ifBasicS) `shouldBe` Set.fromList [2, 3]
@@ -35,6 +36,7 @@ spec = do
3536
(final $ factorialS) `shouldBe` Set.fromList [6]
3637
it "-- available" $ do
3738
(final $ availableS) `shouldBe` Set.fromList [3]
39+
3840
describe "flow" $ do
3941
it "-- ifBasic" $
4042
(flow $ ifBasicS) `shouldBe` Set.fromList [(1,2), (1,3)]
@@ -44,11 +46,12 @@ spec = do
4446
(flow $ powerS) `shouldBe` Set.fromList [(1,2), (2,3), (3,4), (4,2)]
4547
it "-- factorial" $
4648
(flow $ factorialS) `shouldBe` Set.fromList [(1,2), (2,3), (3,4), (3,6), (4,5), (5,3)]
49+
4750
describe "free variables (fv)" $
4851
it "-- factorial" $
4952
(fv $ factorialS) `shouldBe` Set.fromList ["x", "y", "z"]
50-
5153
where
54+
5255
ifBasicS :: Stmt
5356
ifBasicS = read ifBasic
5457

@@ -63,3 +66,4 @@ spec = do
6366

6467
availableS :: Stmt
6568
availableS = read available
69+

0 commit comments

Comments
 (0)