{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE PostfixOperators #-}

-- | The logic to render C++ auxiliary files is contained in this module
module Language.Drasil.Code.Imperative.GOOL.LanguageRenderer.CppRenderer (
  CppProject(..)
) where

import Language.Drasil.Choices (ImplementationType(..))
import Language.Drasil.Code.Imperative.GOOL.ClassInterface (ReadMeInfo(..),
  PackageSym(..), AuxiliarySym(..))
import qualified 
  Language.Drasil.Code.Imperative.GOOL.LanguageRenderer.LanguagePolymorphic as 
  G (doxConfig, readMe, sampleInput, makefile, noRunIfLib, doxDocConfig, 
  docIfEnabled)
import Language.Drasil.Code.Imperative.GOOL.Data (AuxData(..), ad, 
  PackData(..), packD)
import Language.Drasil.Code.Imperative.Build.AST (BuildConfig, Runnable, 
  asFragment, buildAll, cppCompiler, nativeBinary, executable, sharedLibrary)
import Language.Drasil.Code.Imperative.Doxygen.Import (no)

import GOOL.Drasil (onCodeList, cppName, cppVersion)

import Prelude hiding (break,print,(<>),sin,cos,tan,floor,const,log,exp)
import Text.PrettyPrint.HughesPJ (Doc)

-- | Holds a C++ project.
newtype CppProject a = CPPP {CppProject a -> a
unCPPP :: a}

instance Functor CppProject where
  fmap :: (a -> b) -> CppProject a -> CppProject b
fmap f :: a -> b
f (CPPP x :: a
x) = b -> CppProject b
forall a. a -> CppProject a
CPPP (a -> b
f a
x)

instance Applicative CppProject where
  pure :: a -> CppProject a
pure = a -> CppProject a
forall a. a -> CppProject a
CPPP
  (CPPP f :: a -> b
f) <*> :: CppProject (a -> b) -> CppProject a -> CppProject b
<*> (CPPP x :: a
x) = b -> CppProject b
forall a. a -> CppProject a
CPPP (a -> b
f a
x)

instance Monad CppProject where
  return :: a -> CppProject a
return = a -> CppProject a
forall a. a -> CppProject a
CPPP
  CPPP x :: a
x >>= :: CppProject a -> (a -> CppProject b) -> CppProject b
>>= f :: a -> CppProject b
f = a -> CppProject b
f a
x

instance PackageSym CppProject where
  type Package CppProject = PackData
  package :: ProgData
-> [CppProject (Auxiliary CppProject)]
-> CppProject (Package CppProject)
package p :: ProgData
p = ([AuxData] -> PackData)
-> [CppProject AuxData] -> CppProject PackData
forall (m :: * -> *) a b. Monad m => ([a] -> b) -> [m a] -> m b
onCodeList (ProgData -> [AuxData] -> PackData
packD ProgData
p)

instance AuxiliarySym CppProject where
  type Auxiliary CppProject = AuxData
  type AuxHelper CppProject = Doc
  doxConfig :: String
-> GOOLState -> Verbosity -> CppProject (Auxiliary CppProject)
doxConfig = CppProject (AuxHelper CppProject)
-> String
-> GOOLState
-> Verbosity
-> CppProject (Auxiliary CppProject)
forall (r :: * -> *).
AuxiliarySym r =>
r (AuxHelper r)
-> String -> GOOLState -> Verbosity -> r (Auxiliary r)
G.doxConfig CppProject (AuxHelper CppProject)
forall (r :: * -> *). AuxiliarySym r => r (AuxHelper r)
optimizeDox
  readMe :: ReadMeInfo -> CppProject (Auxiliary CppProject)
readMe rmi :: ReadMeInfo
rmi =
    ReadMeInfo -> CppProject (Auxiliary CppProject)
forall (r :: * -> *).
AuxiliarySym r =>
ReadMeInfo -> r (Auxiliary r)
G.readMe ReadMeInfo
rmi {
        langName :: String
langName = String
cppName,
        langVersion :: String
langVersion = String
cppVersion}
  sampleInput :: ChunkDB -> DataDesc -> [Expr] -> CppProject (Auxiliary CppProject)
sampleInput = ChunkDB -> DataDesc -> [Expr] -> CppProject (Auxiliary CppProject)
forall (r :: * -> *).
AuxiliarySym r =>
ChunkDB -> DataDesc -> [Expr] -> r (Auxiliary r)
G.sampleInput

  optimizeDox :: CppProject (AuxHelper CppProject)
optimizeDox = Doc -> CppProject Doc
forall (m :: * -> *) a. Monad m => a -> m a
return Doc
no
  
  makefile :: [String]
-> ImplementationType
-> [Comments]
-> GOOLState
-> ProgData
-> CppProject (Auxiliary CppProject)
makefile fs :: [String]
fs it :: ImplementationType
it cms :: [Comments]
cms = Maybe BuildConfig
-> Maybe Runnable
-> Maybe DocConfig
-> GOOLState
-> ProgData
-> CppProject (Auxiliary CppProject)
forall (r :: * -> *).
AuxiliarySym r =>
Maybe BuildConfig
-> Maybe Runnable
-> Maybe DocConfig
-> GOOLState
-> ProgData
-> r (Auxiliary r)
G.makefile ([String] -> ImplementationType -> Maybe BuildConfig
cppBuildConfig [String]
fs ImplementationType
it) (ImplementationType -> Maybe Runnable -> Maybe Runnable
G.noRunIfLib ImplementationType
it Maybe Runnable
cppRunnable) ([Comments] -> DocConfig -> Maybe DocConfig
G.docIfEnabled [Comments]
cms DocConfig
G.doxDocConfig)
  
  auxHelperDoc :: CppProject (AuxHelper CppProject) -> Doc
auxHelperDoc = CppProject (AuxHelper CppProject) -> Doc
forall a. CppProject a -> a
unCPPP
  auxFromData :: String -> Doc -> CppProject (Auxiliary CppProject)
auxFromData fp :: String
fp d :: Doc
d = AuxData -> CppProject (Auxiliary CppProject)
forall (m :: * -> *) a. Monad m => a -> m a
return (AuxData -> CppProject (Auxiliary CppProject))
-> AuxData -> CppProject (Auxiliary CppProject)
forall a b. (a -> b) -> a -> b
$ String -> Doc -> AuxData
ad String
fp Doc
d

-- helpers
-- | Create a build configuration for C++ files. Takes in 'FilePath's and the type of implementation.
cppBuildConfig :: [FilePath] -> ImplementationType -> Maybe BuildConfig
cppBuildConfig :: [String] -> ImplementationType -> Maybe BuildConfig
cppBuildConfig fs :: [String]
fs it :: ImplementationType
it = ([CommandFragment] -> CommandFragment -> [[CommandFragment]])
-> BuildName -> Maybe BuildConfig
buildAll (\i :: [CommandFragment]
i o :: CommandFragment
o -> [CommandFragment
cppCompiler CommandFragment -> [CommandFragment] -> [CommandFragment]
forall a. a -> [a] -> [a]
: [CommandFragment]
i [CommandFragment] -> [CommandFragment] -> [CommandFragment]
forall a. [a] -> [a] -> [a]
++ (String -> CommandFragment) -> [String] -> [CommandFragment]
forall a b. (a -> b) -> [a] -> [b]
map String -> CommandFragment
asFragment
  ("--std=c++11" String -> [String] -> [String]
forall a. a -> [a] -> [a]
: ImplementationType -> [String]
target ImplementationType
it [String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
++ ["-o"]) [CommandFragment] -> [CommandFragment] -> [CommandFragment]
forall a. [a] -> [a] -> [a]
++ [CommandFragment
o] [CommandFragment] -> [CommandFragment] -> [CommandFragment]
forall a. [a] -> [a] -> [a]
++ (String -> [CommandFragment]) -> [String] -> [CommandFragment]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\f :: String
f -> (String -> CommandFragment) -> [String] -> [CommandFragment]
forall a b. (a -> b) -> [a] -> [b]
map 
  String -> CommandFragment
asFragment ["-I", String
f]) [String]
fs]) (ImplementationType -> BuildName
outName ImplementationType
it)
  where target :: ImplementationType -> [String]
target Library = ["-shared", "-fPIC"]
        target Program = []
        outName :: ImplementationType -> BuildName
outName Library = BuildName
sharedLibrary
        outName Program = BuildName
executable

-- | Default runnable information for C++ files.
cppRunnable :: Maybe Runnable
cppRunnable :: Maybe Runnable
cppRunnable = Maybe Runnable
nativeBinary