module Language.Drasil.Code.Imperative.GenerateGOOL (ClassType(..),
  genModuleWithImports, genModuleWithImportsProc, genModule, genModuleProc,
  genDoxConfig, genReadMe, primaryClass, auxClass, fApp, fAppProc, ctorCall,
  fAppInOut, fAppInOutProc
) where

import Data.Bifunctor (second)
import qualified Data.Map as Map (lookup)
import Data.Maybe (catMaybes)
import Control.Monad.State (get, modify)
import Control.Lens ((^.))

import Drasil.FileHandling (FileLayout)
import Drasil.GProc (ProcProg)
import qualified Drasil.GProc as Proc (FileSym(..), ModuleSym(..))
import Language.Drasil hiding (List)
import Language.Drasil.Code.Imperative.DrasilState (GenState, DrasilState(..),
  getDoxOutput, getSoftwareDossierFiles, HasChoices(..))
import Language.Drasil.SoftwareDossier.SoftwareDossierSym (SoftwareDossierSym(..),
  SoftwareDossierState)
import Language.Drasil.Code.Imperative.README.Core (ReadMeInfo(..))
import Language.Drasil.Choices (Comments(..), SoftwareDossierFile(..))
import Language.Drasil.Mod (Name, Description, Import)
import Drasil.Metadata (watermark)
import Drasil.System (HasSystemMeta(..))
import Drasil.SRS (HasSmithEtAlSRS(..))

import Drasil.GOOL (SVariable, SValue, Class, CSStateVar, NamedArgs, OOProg, CS,
  FS, MS, VS, TypeData, ValueSym(..), Argument(..), ValueExpression(..),
  InternalValueExp, OOValueExpression(..), SelfSym(..), VariableValue(..),
  FuncAppStatement(..), OOFuncAppStatement(..), ClassSym(..), CodeType(..),
  TypeElim(..), objMethodCallMixedArgs)
import qualified Drasil.GOOL as OO (FileSym(..), ModuleSym(..))

-- | Defines a GOOL module. If the user chose 'CommentMod', the module will have
-- Doxygen comments. If the user did not choose 'CommentMod' but did choose
-- 'CommentFunc', a module-level Doxygen comment is still created, though it only
-- documents the file name, because without this Doxygen will not find the
-- function-level comments in the file.
genModuleWithImports
  :: (OOProg r vis stmt mthd stvr attch prg file mod bod block)
  => Name
  -> Description
  -> [Import]
  -> [GenState (Maybe (MS (r mthd)))]
  -> [GenState (Maybe (CS (r Class)))]
  -> GenState (FS (r file))
genModuleWithImports :: forall (r :: * -> *) vis stmt mthd stvr attch prg file mod bod
       block.
OOProg r vis stmt mthd stvr attch prg file mod bod block =>
Name
-> Name
-> [Name]
-> [GenState (Maybe (MS (r mthd)))]
-> [GenState (Maybe (CS (r Class)))]
-> GenState (FS (r file))
genModuleWithImports Name
n Name
desc [Name]
is [GenState (Maybe (MS (r mthd)))]
maybeMs [GenState (Maybe (CS (r Class)))]
maybeCs = do
  g <- StateT DrasilState Identity DrasilState
forall s (m :: * -> *). MonadState s m => m s
get
  modify (\DrasilState
s -> DrasilState
s { currentModule = n })
  let as = (Person -> Name) -> [Person] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map Person -> Name
forall n. HasName n => n -> Name
fullName (DrasilState
g DrasilState -> Getting [Person] DrasilState [Person] -> [Person]
forall s a. s -> Getting a s a -> a
^. Getting [Person] DrasilState [Person]
forall c. HasSystemMeta c => Lens' c [Person]
Lens' DrasilState [Person]
authors)
  cs <- sequence maybeCs
  ms <- sequence maybeMs
  let commMod | Comments
CommentMod Comments -> [Comments] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` DrasilState
g DrasilState
-> Getting [Comments] DrasilState [Comments] -> [Comments]
forall s a. s -> Getting a s a -> a
^. Getting [Comments] DrasilState [Comments]
forall a. HasChoices a => Lens' a [Comments]
Lens' DrasilState [Comments]
commented                   = Name -> Name -> [Name] -> Name -> FS (r file) -> FS (r file)
forall {k} (r :: k -> *) (file :: k) (mod :: k).
FileSym r file mod =>
Name -> Name -> [Name] -> Name -> FS (r file) -> FS (r file)
OO.docMod Name
desc Name
watermark [Name]
as (DrasilState
g DrasilState -> Getting Name DrasilState Name -> Name
forall s a. s -> Getting a s a -> a
^. Getting Name DrasilState Name
forall a. HasChoices a => Lens' a Name
Lens' DrasilState Name
date)
              | Comments
CommentFunc Comments -> [Comments] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` DrasilState
g DrasilState
-> Getting [Comments] DrasilState [Comments] -> [Comments]
forall s a. s -> Getting a s a -> a
^. Getting [Comments] DrasilState [Comments]
forall a. HasChoices a => Lens' a [Comments]
Lens' DrasilState [Comments]
commented Bool -> Bool -> Bool
&& Bool -> Bool
not ([Maybe (MS (r mthd))] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Maybe (MS (r mthd))]
ms) = Name -> Name -> [Name] -> Name -> FS (r file) -> FS (r file)
forall {k} (r :: k -> *) (file :: k) (mod :: k).
FileSym r file mod =>
Name -> Name -> [Name] -> Name -> FS (r file) -> FS (r file)
OO.docMod Name
"" Name
watermark [] Name
""
              | Bool
otherwise                                          = FS (r file) -> FS (r file)
forall a. a -> a
id
  return $ commMod $ OO.fileDoc $ OO.buildModule n is (catMaybes ms) (catMaybes cs)

-- | Generates a module for when imports do not need to be explicitly stated.
genModule
  :: (OOProg r vis stmt mthd stvr attch prg file mod bod block)
  => Name
  -> Description
  -> [GenState (Maybe (MS (r mthd)))]
  -> [GenState (Maybe (CS (r Class)))]
  -> GenState (FS (r file))
genModule :: forall (r :: * -> *) vis stmt mthd stvr attch prg file mod bod
       block.
OOProg r vis stmt mthd stvr attch prg file mod bod block =>
Name
-> Name
-> [GenState (Maybe (MS (r mthd)))]
-> [GenState (Maybe (CS (r Class)))]
-> GenState (FS (r file))
genModule Name
n Name
desc = Name
-> Name
-> [Name]
-> [GenState (Maybe (MS (r mthd)))]
-> [GenState (Maybe (CS (r Class)))]
-> GenState (FS (r file))
forall (r :: * -> *) vis stmt mthd stvr attch prg file mod bod
       block.
OOProg r vis stmt mthd stvr attch prg file mod bod block =>
Name
-> Name
-> [Name]
-> [GenState (Maybe (MS (r mthd)))]
-> [GenState (Maybe (CS (r Class)))]
-> GenState (FS (r file))
genModuleWithImports Name
n Name
desc []

-- | Generates a Doxygen configuration file if the user has comments enabled.
genDoxConfig :: (SoftwareDossierSym r) => SoftwareDossierState ->
  GenState (Maybe (r FileLayout))
genDoxConfig :: forall (r :: * -> *).
SoftwareDossierSym r =>
SoftwareDossierState -> GenState (Maybe (r FileLayout))
genDoxConfig SoftwareDossierState
s = do
  g <- StateT DrasilState Identity DrasilState
forall s (m :: * -> *). MonadState s m => m s
get
  let n = DrasilState
g DrasilState -> Getting Name DrasilState Name -> Name
forall s a. s -> Getting a s a -> a
^. Getting Name DrasilState Name
forall c. HasSmithEtAlSRS c => Lens' c Name
Lens' DrasilState Name
programName
      cms = DrasilState
g DrasilState
-> Getting [Comments] DrasilState [Comments] -> [Comments]
forall s a. s -> Getting a s a -> a
^. Getting [Comments] DrasilState [Comments]
forall a. HasChoices a => Lens' a [Comments]
Lens' DrasilState [Comments]
commented
      v = DrasilState -> Verbosity
getDoxOutput DrasilState
g
  return $ if not (null cms) then doxConfig n s v else Nothing

-- | Generates a README file.
genReadMe :: (SoftwareDossierSym r) => ReadMeInfo -> GenState (Maybe (r FileLayout))
genReadMe :: forall (r :: * -> *).
SoftwareDossierSym r =>
ReadMeInfo -> GenState (Maybe (r FileLayout))
genReadMe ReadMeInfo
rmi = do
  g <- StateT DrasilState Identity DrasilState
forall s (m :: * -> *). MonadState s m => m s
get
  let n = DrasilState
g DrasilState -> Getting Name DrasilState Name -> Name
forall s a. s -> Getting a s a -> a
^. Getting Name DrasilState Name
forall c. HasSmithEtAlSRS c => Lens' c Name
Lens' DrasilState Name
programName
  return $ getReadMe (getSoftwareDossierFiles g) rmi {caseName = n}

-- | Helper for generating a README file.
getReadMe :: (SoftwareDossierSym r) => [SoftwareDossierFile] -> ReadMeInfo -> Maybe (r FileLayout)
getReadMe :: forall (r :: * -> *).
SoftwareDossierSym r =>
[SoftwareDossierFile] -> ReadMeInfo -> Maybe (r FileLayout)
getReadMe [SoftwareDossierFile]
auxl ReadMeInfo
rmi = if SoftwareDossierFile
ReadME SoftwareDossierFile -> [SoftwareDossierFile] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [SoftwareDossierFile]
auxl then r FileLayout -> Maybe (r FileLayout)
forall a. a -> Maybe a
Just (ReadMeInfo -> r FileLayout
forall (r :: * -> *).
SoftwareDossierSym r =>
ReadMeInfo -> r FileLayout
readMe ReadMeInfo
rmi) else Maybe (r FileLayout)
forall a. Maybe a
Nothing

data ClassType = Primary | Auxiliary

-- | Generates a primary or auxiliary class with the given name, description,
-- state variables, and methods. The 'Maybe' 'Name' parameter is the name of the
-- interface the class implements, if applicable.
mkClass :: (ClassSym r vis mthd stvr attch) => ClassType -> Name -> Maybe Name ->
  Description -> [CSStateVar r stvr] -> GenState [MS (r mthd)] ->
    GenState [MS (r mthd)] -> GenState (CS (r Class))
mkClass :: forall (r :: * -> *) vis mthd stvr attch.
ClassSym r vis mthd stvr attch =>
ClassType
-> Name
-> Maybe Name
-> Name
-> [CSStateVar r stvr]
-> GenState [MS (r mthd)]
-> GenState [MS (r mthd)]
-> GenState (CS (r Class))
mkClass ClassType
s Name
n Maybe Name
l Name
desc [CSStateVar r stvr]
vs GenState [MS (r mthd)]
cstrs GenState [MS (r mthd)]
mths = do
  g <- StateT DrasilState Identity DrasilState
forall s (m :: * -> *). MonadState s m => m s
get
  modify (\DrasilState
ds -> DrasilState
ds {currentClass = n})
  cs <- cstrs
  ms <- mths
  modify (\DrasilState
ds -> DrasilState
ds {currentClass = ""})
  let getFunc ClassType
Primary = Maybe Name
-> [CSStateVar r stvr]
-> [MS (r mthd)]
-> [MS (r mthd)]
-> CS (r Class)
getFunc' Maybe Name
l
      getFunc ClassType
Auxiliary = Name
-> Maybe Name
-> [CSStateVar r stvr]
-> [MS (r mthd)]
-> [MS (r mthd)]
-> CS (r Class)
forall (r :: * -> *) vis mthd stvr attch.
ClassSym r vis mthd stvr attch =>
Name
-> Maybe Name
-> [CSStateVar r stvr]
-> [MS (r mthd)]
-> [MS (r mthd)]
-> CS (r Class)
extraClass Name
n Maybe Name
forall a. Maybe a
Nothing
      getFunc' Maybe Name
Nothing = Maybe Name
-> [CSStateVar r stvr]
-> [MS (r mthd)]
-> [MS (r mthd)]
-> CS (r Class)
forall (r :: * -> *) vis mthd stvr attch.
ClassSym r vis mthd stvr attch =>
Maybe Name
-> [CSStateVar r stvr]
-> [MS (r mthd)]
-> [MS (r mthd)]
-> CS (r Class)
buildClass Maybe Name
forall a. Maybe a
Nothing
      getFunc' (Just Name
intfc) = Name
-> [Name]
-> [CSStateVar r stvr]
-> [MS (r mthd)]
-> [MS (r mthd)]
-> CS (r Class)
forall (r :: * -> *) vis mthd stvr attch.
ClassSym r vis mthd stvr attch =>
Name
-> [Name]
-> [CSStateVar r stvr]
-> [MS (r mthd)]
-> [MS (r mthd)]
-> CS (r Class)
implementingClass Name
n [Name
intfc]
      c = ClassType
-> [CSStateVar r stvr]
-> [MS (r mthd)]
-> [MS (r mthd)]
-> CS (r Class)
getFunc ClassType
s [CSStateVar r stvr]
vs [MS (r mthd)]
cs [MS (r mthd)]
ms
  return $ if CommentClass `elem` g ^. commented
    then docClass desc c
    else c

-- | Generates a primary class.
primaryClass :: (ClassSym r vis mthd stvr attch) => Name -> Maybe Name -> Description ->
  [CSStateVar r stvr] -> GenState [MS (r mthd)] -> GenState [MS (r mthd)] ->
  GenState (CS (r Class))
primaryClass :: forall (r :: * -> *) vis mthd stvr attch.
ClassSym r vis mthd stvr attch =>
Name
-> Maybe Name
-> Name
-> [CSStateVar r stvr]
-> GenState [MS (r mthd)]
-> GenState [MS (r mthd)]
-> GenState (CS (r Class))
primaryClass = ClassType
-> Name
-> Maybe Name
-> Name
-> [CSStateVar r stvr]
-> GenState [MS (r mthd)]
-> GenState [MS (r mthd)]
-> GenState (CS (r Class))
forall (r :: * -> *) vis mthd stvr attch.
ClassSym r vis mthd stvr attch =>
ClassType
-> Name
-> Maybe Name
-> Name
-> [CSStateVar r stvr]
-> GenState [MS (r mthd)]
-> GenState [MS (r mthd)]
-> GenState (CS (r Class))
mkClass ClassType
Primary

-- | Generates an auxiliary class (for when a module contains multiple classes).
auxClass :: (ClassSym r vis mthd stvr attch) => Name -> Maybe Name -> Description ->
  [CSStateVar r stvr] -> GenState [MS (r mthd)] -> GenState [MS (r mthd)] ->
  GenState (CS (r Class))
auxClass :: forall (r :: * -> *) vis mthd stvr attch.
ClassSym r vis mthd stvr attch =>
Name
-> Maybe Name
-> Name
-> [CSStateVar r stvr]
-> GenState [MS (r mthd)]
-> GenState [MS (r mthd)]
-> GenState (CS (r Class))
auxClass = ClassType
-> Name
-> Maybe Name
-> Name
-> [CSStateVar r stvr]
-> GenState [MS (r mthd)]
-> GenState [MS (r mthd)]
-> GenState (CS (r Class))
forall (r :: * -> *) vis mthd stvr attch.
ClassSym r vis mthd stvr attch =>
ClassType
-> Name
-> Maybe Name
-> Name
-> [CSStateVar r stvr]
-> GenState [MS (r mthd)]
-> GenState [MS (r mthd)]
-> GenState (CS (r Class))
mkClass ClassType
Auxiliary

-- | Converts lists or objects to pointer arguments, since we use pointerParam
-- for list or object-type parameters.
mkArg :: (Argument r, TypeElim r) => SValue r -> SValue r
mkArg :: forall (r :: * -> *).
(Argument r, TypeElim r) =>
SValue r -> SValue r
mkArg SValue r
v = do
  vl <- SValue r
v
  let mkArg' (List CodeType
_) = SValue r -> SValue r
forall (r :: * -> *). Argument r => SValue r -> SValue r
pointerArg
      mkArg' (Object Name
_) = SValue r -> SValue r
forall (r :: * -> *). Argument r => SValue r -> SValue r
pointerArg
      mkArg' CodeType
_ = SValue r -> SValue r
forall a. a -> a
id
  mkArg' (getCodeType $ valueType vl) (return vl)

-- | Gets the current module and calls mkArg on the arguments.
-- Called by more specific function call generators ('fApp' and 'ctorCall').
fCall :: (Argument r, TypeElim r) => (Name -> [SValue r] -> NamedArgs r ->
  SValue r) -> [SValue r] -> NamedArgs r -> GenState (SValue r)
fCall :: forall (r :: * -> *).
(Argument r, TypeElim r) =>
(Name -> [SValue r] -> NamedArgs r -> SValue r)
-> [SValue r] -> NamedArgs r -> GenState (SValue r)
fCall Name -> [SValue r] -> NamedArgs r -> SValue r
f [SValue r]
vl NamedArgs r
ns = do
  g <- StateT DrasilState Identity DrasilState
forall s (m :: * -> *). MonadState s m => m s
get
  let cm = DrasilState -> Name
currentModule DrasilState
g
      args = (SValue r -> SValue r) -> [SValue r] -> [SValue r]
forall a b. (a -> b) -> [a] -> [b]
map SValue r -> SValue r
forall (r :: * -> *).
(Argument r, TypeElim r) =>
SValue r -> SValue r
mkArg [SValue r]
vl
      nargs = ((SVariable r, SValue r) -> (SVariable r, SValue r))
-> NamedArgs r -> NamedArgs r
forall a b. (a -> b) -> [a] -> [b]
map ((SValue r -> SValue r)
-> (SVariable r, SValue r) -> (SVariable r, SValue r)
forall b c a. (b -> c) -> (a, b) -> (a, c)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second SValue r -> SValue r
forall (r :: * -> *).
(Argument r, TypeElim r) =>
SValue r -> SValue r
mkArg) NamedArgs r
ns
  return $ f cm args nargs

-- | Function call generator.
-- The first parameter (@m@) is the module where the function is defined.
-- If @m@ is not the current module, use GOOL's function for calling functions from
--   external modules.
-- If @m@ is the current module and the function is in export map, use GOOL's basic
--   function for function applications.
-- If @m@ is the current module and function is not exported, use GOOL's function for
--   calling a method on self. This assumes all private methods are dynamic,
--   which is true for this generator.
fApp
  ::
    ( Argument r
    , VariableValue r
    , SelfSym r
    , InternalValueExp r
    , ValueExpression r
    , TypeElim r
    )
  => Name -> Name -> VS (r TypeData) -> [SValue r] -> NamedArgs r -> GenState (SValue r)
fApp :: forall (r :: * -> *).
(Argument r, VariableValue r, SelfSym r, InternalValueExp r,
 ValueExpression r, TypeElim r) =>
Name
-> Name
-> VS (r TypeData)
-> [SValue r]
-> NamedArgs r
-> GenState (SValue r)
fApp Name
m Name
s VS (r TypeData)
t [SValue r]
vl NamedArgs r
ns = do
  g <- StateT DrasilState Identity DrasilState
forall s (m :: * -> *). MonadState s m => m s
get
  fCall (\Name
cm [SValue r]
args NamedArgs r
nargs ->
    if Name
m Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
/= Name
cm then Name -> MixedCall r
forall (r :: * -> *). ValueExpression r => Name -> MixedCall r
extFuncAppMixedArgs Name
m Name
s VS (r TypeData)
t [SValue r]
args NamedArgs r
nargs else
      if Name -> Map Name Name -> Maybe Name
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Name
s (DrasilState -> Map Name Name
eMap DrasilState
g) Maybe Name -> Maybe Name -> Bool
forall a. Eq a => a -> a -> Bool
== Name -> Maybe Name
forall a. a -> Maybe a
Just Name
cm then MixedCall r
forall (r :: * -> *). ValueExpression r => MixedCall r
funcAppMixedArgs Name
s VS (r TypeData)
t [SValue r]
args NamedArgs r
nargs
      else VS (r TypeData)
-> SValue r -> Name -> [SValue r] -> NamedArgs r -> SValue r
forall (r :: * -> *).
InternalValueExp r =>
VS (r TypeData)
-> SValue r -> Name -> [SValue r] -> NamedArgs r -> SValue r
objMethodCallMixedArgs VS (r TypeData)
t (SVariable r -> SValue r
forall (r :: * -> *). VariableValue r => SVariable r -> SValue r
valueOf SVariable r
forall (r :: * -> *). SelfSym r => SVariable r
self) Name
s [SValue r]
args NamedArgs r
nargs) vl ns

-- | Logic similar to 'fApp', but the self case is not required here
-- (because constructor will never be private). Calls 'newObjMixedArgs'.
ctorCall
  :: (Argument r, OOValueExpression r, TypeElim r)
  => Name -> VS (r TypeData) -> [SValue r] -> NamedArgs r -> GenState (SValue r)
ctorCall :: forall (r :: * -> *).
(Argument r, OOValueExpression r, TypeElim r) =>
Name
-> VS (r TypeData)
-> [SValue r]
-> NamedArgs r
-> GenState (SValue r)
ctorCall Name
m VS (r TypeData)
t = (Name -> [SValue r] -> NamedArgs r -> SValue r)
-> [SValue r] -> NamedArgs r -> GenState (SValue r)
forall (r :: * -> *).
(Argument r, TypeElim r) =>
(Name -> [SValue r] -> NamedArgs r -> SValue r)
-> [SValue r] -> NamedArgs r -> GenState (SValue r)
fCall (\Name
cm [SValue r]
args NamedArgs r
nargs -> if Name
m Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
/= Name
cm then
  Name -> MixedCtorCall r
forall (r :: * -> *).
OOValueExpression r =>
Name -> MixedCtorCall r
extNewObjMixedArgs Name
m VS (r TypeData)
t [SValue r]
args NamedArgs r
nargs else MixedCtorCall r
forall (r :: * -> *). OOValueExpression r => MixedCtorCall r
newObjMixedArgs VS (r TypeData)
t [SValue r]
args NamedArgs r
nargs)

-- | Logic similar to 'fApp', but for In/Out calls.
fAppInOut :: (OOFuncAppStatement r stmt) => Name -> Name -> [SValue r] ->
  [SVariable r] -> [SVariable r] -> GenState (MS (r stmt))
fAppInOut :: forall (r :: * -> *) stmt.
OOFuncAppStatement r stmt =>
Name
-> Name
-> [SValue r]
-> [SVariable r]
-> [SVariable r]
-> GenState (MS (r stmt))
fAppInOut Name
m Name
n [SValue r]
ins [SVariable r]
outs [SVariable r]
both = do
  g <- StateT DrasilState Identity DrasilState
forall s (m :: * -> *). MonadState s m => m s
get
  let cm = DrasilState -> Name
currentModule DrasilState
g
  return $ if m /= cm then extInOutCall m n ins outs both else if Map.lookup n
    (eMap g) == Just cm then inOutCall n ins outs both else
    selfInOutCall n ins outs both

-- Procedural Versions --

-- | Defines a GOOL module. If the user chose 'CommentMod', the module will have
-- Doxygen comments. If the user did not choose 'CommentMod' but did choose
-- 'CommentFunc', a module-level Doxygen comment is still created, though it only
-- documents the file name, because without this Doxygen will not find the
-- function-level comments in the file.
genModuleWithImportsProc
  :: (ProcProg r vis stmt mthd prg file mod bod block)
  => Name
  -> Description
  -> [Import]
  -> [GenState (Maybe (MS (r mthd)))]
  -> GenState (FS (r file))
genModuleWithImportsProc :: forall (r :: * -> *) vis stmt mthd prg file mod bod block.
ProcProg r vis stmt mthd prg file mod bod block =>
Name
-> Name
-> [Name]
-> [GenState (Maybe (MS (r mthd)))]
-> GenState (FS (r file))
genModuleWithImportsProc Name
n Name
desc [Name]
is [GenState (Maybe (MS (r mthd)))]
maybeMs = do
  g <- StateT DrasilState Identity DrasilState
forall s (m :: * -> *). MonadState s m => m s
get
  modify (\DrasilState
s -> DrasilState
s { currentModule = n })
  let as = (Person -> Name) -> [Person] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map Person -> Name
forall n. HasName n => n -> Name
fullName (DrasilState
g DrasilState -> Getting [Person] DrasilState [Person] -> [Person]
forall s a. s -> Getting a s a -> a
^. Getting [Person] DrasilState [Person]
forall c. HasSystemMeta c => Lens' c [Person]
Lens' DrasilState [Person]
authors)
  ms <- sequence maybeMs
  let commMod | Comments
CommentMod Comments -> [Comments] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` DrasilState
g DrasilState
-> Getting [Comments] DrasilState [Comments] -> [Comments]
forall s a. s -> Getting a s a -> a
^. Getting [Comments] DrasilState [Comments]
forall a. HasChoices a => Lens' a [Comments]
Lens' DrasilState [Comments]
commented                   = Name -> Name -> [Name] -> Name -> FS (r file) -> FS (r file)
forall {k} (r :: k -> *) (file :: k) (mod :: k).
FileSym r file mod =>
Name -> Name -> [Name] -> Name -> FS (r file) -> FS (r file)
Proc.docMod Name
desc Name
watermark [Name]
as (DrasilState
g DrasilState -> Getting Name DrasilState Name -> Name
forall s a. s -> Getting a s a -> a
^. Getting Name DrasilState Name
forall a. HasChoices a => Lens' a Name
Lens' DrasilState Name
date)
              | Comments
CommentFunc Comments -> [Comments] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` DrasilState
g DrasilState
-> Getting [Comments] DrasilState [Comments] -> [Comments]
forall s a. s -> Getting a s a -> a
^. Getting [Comments] DrasilState [Comments]
forall a. HasChoices a => Lens' a [Comments]
Lens' DrasilState [Comments]
commented Bool -> Bool -> Bool
&& Bool -> Bool
not ([Maybe (MS (r mthd))] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Maybe (MS (r mthd))]
ms) = Name -> Name -> [Name] -> Name -> FS (r file) -> FS (r file)
forall {k} (r :: k -> *) (file :: k) (mod :: k).
FileSym r file mod =>
Name -> Name -> [Name] -> Name -> FS (r file) -> FS (r file)
Proc.docMod Name
"" Name
watermark [] Name
""
              | Bool
otherwise                                          = FS (r file) -> FS (r file)
forall a. a -> a
id
  return $ commMod $ Proc.fileDoc $ Proc.buildModule n is (catMaybes ms)

-- | Generates a module for when imports do not need to be explicitly stated.
genModuleProc
  :: (ProcProg r vis stmt mthd prg file mod bod block)
  => Name
  -> Description
  -> [GenState (Maybe (MS (r mthd)))]
  -> GenState (FS (r file))
genModuleProc :: forall (r :: * -> *) vis stmt mthd prg file mod bod block.
ProcProg r vis stmt mthd prg file mod bod block =>
Name
-> Name
-> [GenState (Maybe (MS (r mthd)))]
-> GenState (FS (r file))
genModuleProc Name
n Name
desc = Name
-> Name
-> [Name]
-> [GenState (Maybe (MS (r mthd)))]
-> GenState (FS (r file))
forall (r :: * -> *) vis stmt mthd prg file mod bod block.
ProcProg r vis stmt mthd prg file mod bod block =>
Name
-> Name
-> [Name]
-> [GenState (Maybe (MS (r mthd)))]
-> GenState (FS (r file))
genModuleWithImportsProc Name
n Name
desc []

-- | Function call generator.
-- The first parameter (@m@) is the module where the function is defined.
-- If @m@ is not the current module, use GOOL's function for calling functions from
--   external modules.
-- If @m@ is the current module and the function is in export map, use GOOL's basic
--   function for function applications.
-- If @m@ is the current module and function is not exported, use GOOL's function for
--   calling a method on self. This assumes all private methods are dynamic,
--   which is true for this generator.
fAppProc
  :: (Argument r, TypeElim r, ValueExpression r) => Name
  -> Name
  -> VS (r TypeData)
  -> [SValue r]
  -> NamedArgs r
  -> GenState (SValue r)
fAppProc :: forall (r :: * -> *).
(Argument r, TypeElim r, ValueExpression r) =>
Name
-> Name
-> VS (r TypeData)
-> [SValue r]
-> NamedArgs r
-> GenState (SValue r)
fAppProc Name
m Name
s VS (r TypeData)
t [SValue r]
vl NamedArgs r
ns = do
  g <- StateT DrasilState Identity DrasilState
forall s (m :: * -> *). MonadState s m => m s
get
  fCall (\Name
cm [SValue r]
args NamedArgs r
nargs ->
    if Name
m Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
/= Name
cm then Name -> MixedCall r
forall (r :: * -> *). ValueExpression r => Name -> MixedCall r
extFuncAppMixedArgs Name
m Name
s VS (r TypeData)
t [SValue r]
args NamedArgs r
nargs else
      if Name -> Map Name Name -> Maybe Name
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Name
s (DrasilState -> Map Name Name
eMap DrasilState
g) Maybe Name -> Maybe Name -> Bool
forall a. Eq a => a -> a -> Bool
== Name -> Maybe Name
forall a. a -> Maybe a
Just Name
cm then MixedCall r
forall (r :: * -> *). ValueExpression r => MixedCall r
funcAppMixedArgs Name
s VS (r TypeData)
t [SValue r]
args NamedArgs r
nargs
      else Name -> SValue r
forall a. HasCallStack => Name -> a
error Name
"fAppProc: Procedural languages do not support method calls.") vl ns

-- | Logic similar to 'fApp', but for In/Out calls.
fAppInOutProc :: (FuncAppStatement r stmt) => Name -> Name -> [SValue r] ->
  [SVariable r] -> [SVariable r] -> GenState (MS (r stmt))
fAppInOutProc :: forall (r :: * -> *) stmt.
FuncAppStatement r stmt =>
Name
-> Name
-> [SValue r]
-> [SVariable r]
-> [SVariable r]
-> GenState (MS (r stmt))
fAppInOutProc Name
m Name
n [SValue r]
ins [SVariable r]
outs [SVariable r]
both = do
  g <- StateT DrasilState Identity DrasilState
forall s (m :: * -> *). MonadState s m => m s
get
  let cm = DrasilState -> Name
currentModule DrasilState
g
  return $ if m /= cm then extInOutCall m n ins outs both else if Map.lookup n
    (eMap g) == Just cm then inOutCall n ins outs both
    else error "fAppInOutProc: Procedural languages do not support method calls."