Skip to content

Commit 3d43fee

Browse files
authored
Merge pull request #38 from Copilot-Language/T36-move-shared-to-src
Move `Copilot.Compile.Bluespec.External` out of `shared` directory. Refs #36.
2 parents d9e787a + 49c94c1 commit 3d43fee

5 files changed

Lines changed: 76 additions & 9 deletions

File tree

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2025-03-20
2+
* Move Copilot.Compile.Bluespec.External out of shared directory. (#36)
3+
14
2025-03-10
25
* Version bump (4.3). (#34)
36

copilot-bluespec.cabal

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ source-repository head
3636

3737
library
3838
default-language : Haskell2010
39-
hs-source-dirs : src, shared
39+
hs-source-dirs : src
4040

4141
ghc-options : -Wall
4242
build-depends : base >= 4.9 && < 5
@@ -70,7 +70,7 @@ test-suite tests
7070
other-modules:
7171
Test.Copilot.Compile.Bluespec
7272

73-
Copilot.Compile.Bluespec.External
73+
Test.Copilot.Compile.Bluespec.External
7474

7575
build-depends:
7676
base
@@ -91,7 +91,7 @@ test-suite tests
9191
, copilot-bluespec
9292

9393
hs-source-dirs:
94-
tests, shared
94+
tests
9595

9696
default-language:
9797
Haskell2010

shared/Copilot/Compile/Bluespec/External.hs renamed to src/Copilot/Compile/Bluespec/External.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ data External = forall a. External
2222
-- | Collect all external variables from the streams and triggers.
2323
--
2424
-- Although Copilot specifications can contain also properties and theorems,
25-
-- the C99 backend currently only generates code for streams and triggers.
25+
-- the Bluespec backend currently only generates code for streams and triggers.
2626
gatherExts :: [Stream] -> [Trigger] -> [External]
2727
gatherExts streams triggers = streamsExts `extUnion` triggersExts
2828
where

tests/Test/Copilot/Compile/Bluespec.hs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,12 @@ import Text.ParserCombinators.ReadPrec (minPrec)
4747
-- External imports: Copilot
4848
import Copilot.Core hiding (Property)
4949

50-
-- External imports: Modules being tested
51-
import Copilot.Compile.Bluespec (bluespecSettingsOutputDirectory,
52-
compile, compileWith,
53-
mkDefaultBluespecSettings)
54-
import Copilot.Compile.Bluespec.External (External (extName), gatherExts)
50+
-- External imports
51+
import Copilot.Compile.Bluespec (bluespecSettingsOutputDirectory, compile,
52+
compileWith, mkDefaultBluespecSettings)
53+
54+
-- Internal imports
55+
import Test.Copilot.Compile.Bluespec.External (External (extName), gatherExts)
5556

5657
-- * Constants
5758

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{-# LANGUAGE ExistentialQuantification #-}
2+
3+
-- | This is a duplicate version of @Copilot.Compile.Bluespec.External@ that is
4+
-- specific to the test suite. Ideally, we would move this into a common library
5+
-- that is shared between both @copilot-bluespec@ and @copilot-c99@ so that we
6+
-- can avoid this duplication. See
7+
-- https://github.com/Copilot-Language/copilot-bluespec/issues/3.
8+
--
9+
-- Represent information about externs needed in the generation of Bluespec
10+
-- code for stream declarations and triggers.
11+
module Test.Copilot.Compile.Bluespec.External
12+
( External(..)
13+
, gatherExts
14+
) where
15+
16+
-- External imports
17+
import Data.List (unionBy)
18+
19+
-- External imports: Copilot
20+
import Copilot.Core ( Expr (..), Stream (..), Trigger (..), Type, UExpr (..) )
21+
22+
-- | Representation of external variables.
23+
data External = forall a. External
24+
{ extName :: String
25+
, extType :: Type a
26+
}
27+
28+
-- | Collect all external variables from the streams and triggers.
29+
--
30+
-- Although Copilot specifications can contain also properties and theorems,
31+
-- the Bluespec backend currently only generates code for streams and triggers.
32+
gatherExts :: [Stream] -> [Trigger] -> [External]
33+
gatherExts streams triggers = streamsExts `extUnion` triggersExts
34+
where
35+
streamsExts = foldr (extUnion . streamExts) mempty streams
36+
triggersExts = foldr (extUnion . triggerExts) mempty triggers
37+
38+
streamExts :: Stream -> [External]
39+
streamExts (Stream _ _ expr _) = exprExts expr
40+
41+
triggerExts :: Trigger -> [External]
42+
triggerExts (Trigger _ guard args) = guardExts `extUnion` argExts
43+
where
44+
guardExts = exprExts guard
45+
argExts = concatMap uExprExts args
46+
47+
uExprExts :: UExpr -> [External]
48+
uExprExts (UExpr _ expr) = exprExts expr
49+
50+
exprExts :: Expr a -> [External]
51+
exprExts (Local _ _ _ e1 e2) = exprExts e1 `extUnion` exprExts e2
52+
exprExts (ExternVar ty name _) = [External name ty]
53+
exprExts (Op1 _ e) = exprExts e
54+
exprExts (Op2 _ e1 e2) = exprExts e1 `extUnion` exprExts e2
55+
exprExts (Op3 _ e1 e2 e3) = exprExts e1 `extUnion` exprExts e2
56+
`extUnion` exprExts e3
57+
exprExts (Label _ _ e) = exprExts e
58+
exprExts _ = []
59+
60+
-- | Union over lists of External, we solely base the equality on the
61+
-- extName's.
62+
extUnion :: [External] -> [External] -> [External]
63+
extUnion = unionBy (\a b -> extName a == extName b)

0 commit comments

Comments
 (0)