Skip to content

Commit 092f539

Browse files
Merge pull request #4722 from JacquesCarette/cleanup-main
Cleaned up GOOLTest File
2 parents f3a015a + 77a645d commit 092f539

File tree

4 files changed

+42
-52
lines changed

4 files changed

+42
-52
lines changed

code/drasil-code/lib/Language/Drasil/Code/FileData.hs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
{-# LANGUAGE PatternSynonyms #-}
12
{-# LANGUAGE MultiParamTypeClasses #-}
23
{-# LANGUAGE FlexibleContexts #-}
34
-- | Defines the underlying data types used in the package extension.
45
module Language.Drasil.Code.FileData (FileAndContents(filePath, fileDoc),
5-
fileAndContents, hasPathAndDocToFileAndContents, PackageData(packageProg,
6-
packageAux), packageData
7-
) where
6+
fileAndContents, hasPathAndDocToFileAndContents,
7+
PackageData(packageProg, packageAux), pattern PackageData
8+
) where
89

910
import Text.PrettyPrint.HughesPJ (Doc, isEmpty)
1011
import Utils.Drasil (HasPathAndDoc(..))
@@ -26,6 +27,8 @@ hasPathAndDocToFileAndContents file = fileAndContents (getPath file) (getDoc fil
2627
-- | The underlying data type for packages in all renderers.
2728
data PackageData a = PackD {packageProg :: a, packageAux :: [FileAndContents]}
2829

29-
-- | Constructor for package data.
30-
packageData :: a -> [FileAndContents] -> PackageData a
31-
packageData p as = PackD p (filter (not . isEmpty . fileDoc) as)
30+
pattern PackageData :: a -> [FileAndContents] -> PackageData a
31+
pattern PackageData prog aux <- PackD prog aux
32+
where
33+
PackageData prog aux = PackD prog (filter (not . isEmpty . fileDoc) aux)
34+
{-# COMPLETE PackageData #-}

code/drasil-code/lib/Language/Drasil/Code/Imperative/GOOL/ClassInterface.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{-# LANGUAGE TemplateHaskell #-}
1+
{-# LANGUAGE TemplateHaskell, PatternSynonyms #-}
22

33
-- | Defines a package extension for GOOL, with functions for pairing a GOOL
44
-- program with auxiliary, non-source-code files.
@@ -18,8 +18,8 @@ import Language.Drasil.Printers (PrintingInformation)
1818

1919
import Language.Drasil (Expr)
2020
import Language.Drasil.Code.DataDesc (DataDesc)
21-
import Language.Drasil.Code.FileData (FileAndContents(..), PackageData,
22-
fileAndContents, packageData)
21+
import Language.Drasil.Code.FileData (FileAndContents(..), fileAndContents,
22+
PackageData, pattern PackageData)
2323
import Language.Drasil.Code.FileNames (sampleInputName)
2424
import Language.Drasil.Choices (Comments, ImplementationType, Verbosity)
2525
import Language.Drasil.Code.Imperative.WriteInput (makeInputFile)
@@ -57,7 +57,7 @@ class AuxiliarySym r where
5757
auxHelperDoc :: r Doc -> Doc
5858

5959
package :: (Monad r) => ProgData -> [r FileAndContents] -> r (PackageData ProgData)
60-
package p = onCodeList (packageData p)
60+
package p = onCodeList (PackageData p)
6161

6262
sampleInput :: (Applicative r) => PrintingInformation -> DataDesc -> [Expr] ->
6363
r FileAndContents

code/drasil-code/lib/Language/Drasil/GOOL.hs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1+
{-# LANGUAGE PatternSynonyms #-}
2+
13
-- | Re-export code-related smart constructors for external code writing and generation.
24
module Language.Drasil.GOOL (
35
AuxiliarySym(..), package,
4-
FileAndContents(..), PackageData(..),
6+
FileAndContents(..), hasPathAndDocToFileAndContents,
7+
PackageData(..), pattern PackageData,
58
unPP, unJP, unCSP, unCPPP, unSP, unJLP
69
) where
710

811
import Language.Drasil.Code.Imperative.GOOL.ClassInterface (AuxiliarySym(..),
912
package)
10-
import Language.Drasil.Code.FileData (FileAndContents(..), PackageData(..))
13+
import Language.Drasil.Code.FileData (FileAndContents(..),
14+
hasPathAndDocToFileAndContents, PackageData(..), pattern PackageData)
1115

1216
import Language.Drasil.Code.Imperative.GOOL.LanguageRenderer.PythonRenderer (unPP)
1317
import Language.Drasil.Code.Imperative.GOOL.LanguageRenderer.JavaRenderer (unJP)

code/drasil-code/test/Main.hs

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
1+
{-# LANGUAGE PatternSynonyms, TupleSections #-}
2+
13
-- | Main module to gather all the GOOL tests and generate them.
24
module Main (main) where
35

4-
import Drasil.GOOL (Label, OOProg, unJC, unPC, unCSC,
5-
unCPPC, unSC, initialState, FileData(..), ProgData(..), ModData(..),
6-
headers, sources, mainMod)
6+
import Drasil.GOOL (Label, OOProg, unJC, unPC, unCSC, unCPPC, unSC,
7+
initialState, ProgData(..), headers, sources, mainMod)
78
import qualified Drasil.GOOL as OO (unCI, ProgramSym(..))
89
import Drasil.GProc (ProcProg, unJLC)
910
import qualified Drasil.GProc as Proc (unCI, ProgramSym(..))
1011

1112
import Language.Drasil.Code (ImplementationType(..), makeSds)
1213
import Language.Drasil.GOOL (AuxiliarySym(..), package,
13-
FileAndContents(fileDoc), PackageData(..), unPP, unJP, unCSP, unCPPP, unSP,
14-
unJLP)
15-
import qualified Language.Drasil.GOOL as D (filePath)
14+
hasPathAndDocToFileAndContents, PackageData(..), pattern PackageData,
15+
unPP, unJP, unCSP, unCPPP, unSP, unJLP)
16+
import qualified Language.Drasil.GOOL as D (filePath, FileAndContents(..))
1617

17-
import Utils.Drasil (createDirIfMissing)
18+
import Utils.Drasil (createDirIfMissing, createFile)
1819

19-
import Text.PrettyPrint.HughesPJ (Doc, render)
20+
import Text.PrettyPrint.HughesPJ (render)
2021
import Control.Monad.State (evalState, runState)
2122
import Control.Lens ((^.))
23+
import Data.Functor ((<&>))
24+
import Data.Foldable (traverse_)
2225
import System.Directory (setCurrentDirectory, getCurrentDirectory)
23-
import System.FilePath.Posix (takeDirectory)
24-
import System.IO (hClose, hPutStrLn, openFile, IOMode(WriteMode))
26+
import System.FilePath ((</>))
2527
import Prelude hiding (return,print,log,exp,sin,cos,tan)
2628

2729
import HelloWorld (helloWorldOO, helloWorldProc)
@@ -62,14 +64,14 @@ main = do
6264

6365
-- | Gathers all information needed to generate code, sorts it, and calls the renderers.
6466
genCode :: [PackageData ProgData] -> IO()
65-
genCode files = createCodeFiles (concatMap (\p -> replicate (length (progMods
66-
(packageProg p)) + length (packageAux p)) (progName $ packageProg p)) files) $
67-
makeCode (map (progMods . packageProg) files) (map packageAux files)
67+
genCode files =
68+
createCodeFiles $ files >>= \(PackageData prog aux) ->
69+
let label = progName prog
70+
modCode = progMods prog <&> \modFileData ->
71+
(label, hasPathAndDocToFileAndContents modFileData)
72+
auxCode = aux <&> (label,)
73+
in modCode ++ auxCode
6874

69-
-- Cannot assign the list of tests in a where clause and re-use it because the
70-
-- "r" type variable needs to be instantiated to two different types
71-
-- (CodeInfo and a renderer) each time this function is called
72-
-- | Gathers the GOOL file tests and prepares them for rendering
7375
classes :: (OOProg r, AuxiliarySym r', Monad r') => (r (OO.Program r) -> ProgData) ->
7476
(r' (PackageData ProgData) -> PackageData ProgData) -> [PackageData ProgData]
7577
classes unRepr unRepr' = zipWith
@@ -95,31 +97,12 @@ jlClasses unRepr unRepr' = zipWith
9597
(map (Proc.unCI . (`evalState` initialState)) [helloWorldProc,
9698
fileTestsProc, vectorTestProc, nameGenTestProc])
9799

98-
-- | Formats code to be rendered.
99-
makeCode :: [[FileData]] -> [[FileAndContents]] -> [(FilePath, Doc)]
100-
makeCode files auxs = concat $ zipWith (++)
101-
(map (map (\fd -> (filePath fd, modDoc $ fileMod fd))) files)
102-
(map (map (\fileAndContents ->
103-
(D.filePath fileAndContents, fileDoc fileAndContents))) auxs)
104-
105-
-- zip (map filePath files) (map (modDoc . fileMod) files)
106-
-- ++ zip (map D.filePath auxs) (map fileDoc auxs)
107-
108100
------------------
109101
-- IO Functions --
110102
------------------
111103

112104
-- | Creates the requested 'Code' by producing files.
113-
createCodeFiles :: [Label] -> [(FilePath, Doc)] -> IO () -- [(FilePath, Doc)] -> IO ()
114-
createCodeFiles ns cs = mapM_ createCodeFile (zip ns cs)
115-
116-
-- | Helper that creates the file and renders code.
117-
createCodeFile :: (Label, (FilePath, Doc)) -> IO ()
118-
createCodeFile (n, (path, code)) = do
119-
createDirIfMissing False n
120-
setCurrentDirectory n
121-
createDirIfMissing True (takeDirectory path)
122-
h <- openFile path WriteMode
123-
hPutStrLn h (render code)
124-
hClose h
125-
setCurrentDirectory ".."
105+
createCodeFiles :: [(Label, D.FileAndContents)] -> IO ()
106+
createCodeFiles = traverse_ $ \(name, file) -> do
107+
let path = name </> D.filePath file -- FIXME [Brandon Bosman, Feb. 10, 2026]: make GOOL allow us to add name to path internally
108+
createFile path (render $ D.fileDoc file)

0 commit comments

Comments
 (0)