{-# 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 Prelude hiding (break,print,(<>),sin,cos,tan,floor,const,log,exp)

import Drasil.GOOL (cppName, cppVersion)

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

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

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

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

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

instance AuxiliarySym CppProject where
  doxConfig :: String
-> SoftwareDossierState -> Verbosity -> CppProject FileAndContents
doxConfig = CppProject Doc
-> String
-> SoftwareDossierState
-> Verbosity
-> CppProject FileAndContents
forall (r :: * -> *).
(AuxiliarySym r, Applicative r) =>
r Doc
-> String -> SoftwareDossierState -> Verbosity -> r FileAndContents
G.doxConfig CppProject Doc
forall (r :: * -> *). AuxiliarySym r => r Doc
optimizeDox
  readMe :: ReadMeInfo -> CppProject FileAndContents
readMe ReadMeInfo
rmi =
    ReadMeInfo -> CppProject FileAndContents
forall (r :: * -> *).
Applicative r =>
ReadMeInfo -> r FileAndContents
G.readMe ReadMeInfo
rmi {
        langName = cppName,
        langVersion = cppVersion}

  optimizeDox :: CppProject Doc
optimizeDox = Doc -> CppProject Doc
forall a. a -> CppProject a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Doc
no

  makefile :: [String]
-> ImplementationType
-> [Comments]
-> SoftwareDossierState
-> ProgData
-> CppProject FileAndContents
makefile [String]
fs ImplementationType
it [Comments]
cms = Maybe BuildConfig
-> Maybe Runnable
-> Maybe DocConfig
-> SoftwareDossierState
-> ProgData
-> CppProject FileAndContents
forall (r :: * -> *).
Applicative r =>
Maybe BuildConfig
-> Maybe Runnable
-> Maybe DocConfig
-> SoftwareDossierState
-> ProgData
-> r FileAndContents
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 Doc -> Doc
auxHelperDoc = CppProject Doc -> Doc
forall a. CppProject a -> a
unCPPP

-- 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 [String]
fs ImplementationType
it = ([CommandFragment] -> CommandFragment -> [[CommandFragment]])
-> BuildName -> Maybe BuildConfig
buildAll (\[CommandFragment]
i 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
  (String
"--std=c++11" String -> [String] -> [String]
forall a. a -> [a] -> [a]
: ImplementationType -> [String]
target ImplementationType
it [String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
++ [String
"-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 (\String
f -> (String -> CommandFragment) -> [String] -> [CommandFragment]
forall a b. (a -> b) -> [a] -> [b]
map
  String -> CommandFragment
asFragment [String
"-I", String
f]) [String]
fs]) (ImplementationType -> BuildName
outName ImplementationType
it)
  where target :: ImplementationType -> [String]
target ImplementationType
Library = [String
"-shared", String
"-fPIC"]
        target ImplementationType
Program = []
        outName :: ImplementationType -> BuildName
outName ImplementationType
Library = BuildName
sharedLibrary
        outName ImplementationType
Program = BuildName
executable

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