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

import Language.Drasil hiding (List)
import Language.Drasil.Code.Imperative.DrasilState (GenState, DrasilState(..))
import Language.Drasil.Code.Imperative.GOOL.ClassInterface (AuxiliarySym(..))
import Language.Drasil.Code.Imperative.ReadMe.Import (ReadMeInfo(..))
import Language.Drasil.Choices (Comments(..), AuxFile(..))
import Language.Drasil.CodeSpec (HasOldCodeSpec(..))
import Language.Drasil.Mod (Name, Description, Import)

import Drasil.GOOL (VSType, SVariable, SValue, MSStatement, SMethod,
  CSStateVar, SClass, NamedArgs, SharedProg, OOProg, TypeElim(..),
  ValueSym(..), Argument(..), ValueExpression(..), OOValueExpression(..),
  FuncAppStatement(..), OOFuncAppStatement(..), ClassSym(..), CodeType(..),
  GOOLState)
import qualified Drasil.GOOL as OO (SFile, FileSym(..), ModuleSym(..))

import Drasil.GProc (ProcProg)
import qualified Drasil.GProc as Proc (SFile, FileSym(..), ModuleSym(..))

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

-- | 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) => Name -> Description -> [Import] ->
  [GenState (Maybe (SMethod r))] -> [GenState (Maybe (SClass r))] -> 
  GenState (OO.SFile r)
genModuleWithImports :: forall (r :: * -> *).
OOProg r =>
Name
-> Name
-> [Name]
-> [GenState (Maybe (SMethod r))]
-> [GenState (Maybe (SClass r))]
-> GenState (SFile r)
genModuleWithImports Name
n Name
desc [Name]
is [GenState (Maybe (SMethod r))]
maybeMs [GenState (Maybe (SClass r))]
maybeCs = do
  DrasilState
g <- StateT DrasilState Identity DrasilState
forall s (m :: * -> *). MonadState s m => m s
get
  (DrasilState -> DrasilState) -> StateT DrasilState Identity ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify (\DrasilState
s -> DrasilState
s { currentModule = n })
  let as :: [Name]
as = (Person -> Name) -> [Person] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map Person -> Name
forall n. HasName n => n -> Name
name (DrasilState -> CodeSpec
codeSpec DrasilState
g CodeSpec -> Getting [Person] CodeSpec [Person] -> [Person]
forall s a. s -> Getting a s a -> a
^. Getting [Person] CodeSpec [Person]
forall c. HasOldCodeSpec c => Lens' c [Person]
Lens' CodeSpec [Person]
authorsO )
  [Maybe (SClass r)]
cs <- [GenState (Maybe (SClass r))]
-> StateT DrasilState Identity [Maybe (SClass r)]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
forall (m :: * -> *) a. Monad m => [m a] -> m [a]
sequence [GenState (Maybe (SClass r))]
maybeCs
  [Maybe (SMethod r)]
ms <- [GenState (Maybe (SMethod r))]
-> StateT DrasilState Identity [Maybe (SMethod r)]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
forall (m :: * -> *) a. Monad m => [m a] -> m [a]
sequence [GenState (Maybe (SMethod r))]
maybeMs
  let commMod :: SFile r -> SFile r
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 -> [Comments]
commented DrasilState
g                   = Name -> [Name] -> Name -> SFile r -> SFile r
forall (r :: * -> *).
FileSym r =>
Name -> [Name] -> Name -> SFile r -> SFile r
OO.docMod Name
desc
                  [Name]
as (DrasilState -> Name
date DrasilState
g)
              | 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 -> [Comments]
commented DrasilState
g Bool -> Bool -> Bool
&& Bool -> Bool
not ([Maybe (SMethod r)] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Maybe (SMethod r)]
ms) = Name -> [Name] -> Name -> SFile r -> SFile r
forall (r :: * -> *).
FileSym r =>
Name -> [Name] -> Name -> SFile r -> SFile r
OO.docMod Name
"" []
                  Name
""
              | Bool
otherwise                                       = SFile r -> SFile r
forall a. a -> a
id
  SFile r -> GenState (SFile r)
forall a. a -> StateT DrasilState Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return (SFile r -> GenState (SFile r)) -> SFile r -> GenState (SFile r)
forall a b. (a -> b) -> a -> b
$ SFile r -> SFile r
commMod (SFile r -> SFile r) -> SFile r -> SFile r
forall a b. (a -> b) -> a -> b
$ FSModule r -> SFile r
forall (r :: * -> *). FileSym r => FSModule r -> SFile r
OO.fileDoc (FSModule r -> SFile r) -> FSModule r -> SFile r
forall a b. (a -> b) -> a -> b
$ Name -> [Name] -> [SMethod r] -> [SClass r] -> FSModule r
forall (r :: * -> *).
ModuleSym r =>
Name -> [Name] -> [SMethod r] -> [SClass r] -> FSModule r
OO.buildModule Name
n [Name]
is ([Maybe (SMethod r)] -> [SMethod r]
forall a. [Maybe a] -> [a]
catMaybes [Maybe (SMethod r)]
ms) ([Maybe (SClass r)] -> [SClass r]
forall a. [Maybe a] -> [a]
catMaybes [Maybe (SClass r)]
cs)

-- | Generates a module for when imports do not need to be explicitly stated.
genModule :: (OOProg r) => Name -> Description ->
  [GenState (Maybe (SMethod r))] -> [GenState (Maybe (SClass r))] ->
  GenState (OO.SFile r)
genModule :: forall (r :: * -> *).
OOProg r =>
Name
-> Name
-> [GenState (Maybe (SMethod r))]
-> [GenState (Maybe (SClass r))]
-> GenState (SFile r)
genModule Name
n Name
desc = Name
-> Name
-> [Name]
-> [StateT
      DrasilState
      Identity
      (Maybe (StateT MethodState Identity (r (Method r))))]
-> [StateT
      DrasilState
      Identity
      (Maybe (StateT ClassState Identity (r (Class r))))]
-> StateT
     DrasilState Identity (StateT FileState Identity (r (File r)))
forall (r :: * -> *).
OOProg r =>
Name
-> Name
-> [Name]
-> [GenState (Maybe (SMethod r))]
-> [GenState (Maybe (SClass r))]
-> GenState (SFile r)
genModuleWithImports Name
n Name
desc []

-- | Generates a Doxygen configuration file if the user has comments enabled.
genDoxConfig :: (AuxiliarySym r) => GOOLState -> GenState (Maybe (r (Auxiliary r)))
genDoxConfig :: forall (r :: * -> *).
AuxiliarySym r =>
GOOLState -> GenState (Maybe (r (Auxiliary r)))
genDoxConfig GOOLState
s = do
  DrasilState
g <- StateT DrasilState Identity DrasilState
forall s (m :: * -> *). MonadState s m => m s
get
  let n :: Name
n = DrasilState -> CodeSpec
codeSpec DrasilState
g CodeSpec -> Getting Name CodeSpec Name -> Name
forall s a. s -> Getting a s a -> a
^. Getting Name CodeSpec Name
forall c. HasOldCodeSpec c => Lens' c Name
Lens' CodeSpec Name
pNameO
      cms :: [Comments]
cms = DrasilState -> [Comments]
commented DrasilState
g
      v :: Verbosity
v = DrasilState -> Verbosity
doxOutput DrasilState
g
  Maybe (r (Auxiliary r)) -> GenState (Maybe (r (Auxiliary r)))
forall a. a -> StateT DrasilState Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (r (Auxiliary r)) -> GenState (Maybe (r (Auxiliary r))))
-> Maybe (r (Auxiliary r)) -> GenState (Maybe (r (Auxiliary r)))
forall a b. (a -> b) -> a -> b
$ if Bool -> Bool
not ([Comments] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Comments]
cms) then r (Auxiliary r) -> Maybe (r (Auxiliary r))
forall a. a -> Maybe a
Just (Name -> GOOLState -> Verbosity -> r (Auxiliary r)
forall (r :: * -> *).
AuxiliarySym r =>
Name -> GOOLState -> Verbosity -> r (Auxiliary r)
doxConfig Name
n GOOLState
s Verbosity
v) else Maybe (r (Auxiliary r))
forall a. Maybe a
Nothing

-- | Generates a README file.
genReadMe :: (AuxiliarySym r) => ReadMeInfo -> GenState (Maybe (r (Auxiliary r)))
genReadMe :: forall (r :: * -> *).
AuxiliarySym r =>
ReadMeInfo -> GenState (Maybe (r (Auxiliary r)))
genReadMe ReadMeInfo
rmi = do
  DrasilState
g <- StateT DrasilState Identity DrasilState
forall s (m :: * -> *). MonadState s m => m s
get
  let n :: Name
n = DrasilState -> CodeSpec
codeSpec DrasilState
g CodeSpec -> Getting Name CodeSpec Name -> Name
forall s a. s -> Getting a s a -> a
^. Getting Name CodeSpec Name
forall c. HasOldCodeSpec c => Lens' c Name
Lens' CodeSpec Name
pNameO
  Maybe (r (Auxiliary r)) -> GenState (Maybe (r (Auxiliary r)))
forall a. a -> StateT DrasilState Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (r (Auxiliary r)) -> GenState (Maybe (r (Auxiliary r))))
-> Maybe (r (Auxiliary r)) -> GenState (Maybe (r (Auxiliary r)))
forall a b. (a -> b) -> a -> b
$ [AuxFile] -> ReadMeInfo -> Maybe (r (Auxiliary r))
forall (r :: * -> *).
AuxiliarySym r =>
[AuxFile] -> ReadMeInfo -> Maybe (r (Auxiliary r))
getReadMe (DrasilState -> [AuxFile]
auxiliaries DrasilState
g) ReadMeInfo
rmi {caseName = n}

-- | Helper for generating a README file.
getReadMe :: (AuxiliarySym r) => [AuxFile] -> ReadMeInfo -> Maybe (r (Auxiliary r))
getReadMe :: forall (r :: * -> *).
AuxiliarySym r =>
[AuxFile] -> ReadMeInfo -> Maybe (r (Auxiliary r))
getReadMe [AuxFile]
auxl ReadMeInfo
rmi = if AuxFile
ReadME AuxFile -> [AuxFile] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [AuxFile]
auxl then r (Auxiliary r) -> Maybe (r (Auxiliary r))
forall a. a -> Maybe a
Just (ReadMeInfo -> r (Auxiliary r)
forall (r :: * -> *).
AuxiliarySym r =>
ReadMeInfo -> r (Auxiliary r)
readMe ReadMeInfo
rmi) else Maybe (r (Auxiliary r))
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 :: (OOProg r) => ClassType -> Name -> Maybe Name -> Description ->
  [CSStateVar r] -> GenState [SMethod r] -> GenState [SMethod r] ->
  GenState (SClass r)
mkClass :: forall (r :: * -> *).
OOProg r =>
ClassType
-> Name
-> Maybe Name
-> Name
-> [CSStateVar r]
-> GenState [SMethod r]
-> GenState [SMethod r]
-> GenState (SClass r)
mkClass ClassType
s Name
n Maybe Name
l Name
desc [CSStateVar r]
vs GenState [SMethod r]
cstrs GenState [SMethod r]
mths = do
  DrasilState
g <- StateT DrasilState Identity DrasilState
forall s (m :: * -> *). MonadState s m => m s
get
  (DrasilState -> DrasilState) -> StateT DrasilState Identity ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify (\DrasilState
ds -> DrasilState
ds {currentClass = n})
  [SMethod r]
cs <- GenState [SMethod r]
cstrs
  [SMethod r]
ms <- GenState [SMethod r]
mths
  (DrasilState -> DrasilState) -> StateT DrasilState Identity ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify (\DrasilState
ds -> DrasilState
ds {currentClass = ""})
  let getFunc :: ClassType
-> [CS (r (StateVar r))]
-> [MS (r (Method r))]
-> [MS (r (Method r))]
-> CS (r (Class r))
getFunc ClassType
Primary = Maybe Name
-> [CS (r (StateVar r))]
-> [MS (r (Method r))]
-> [MS (r (Method r))]
-> CS (r (Class r))
forall {r :: * -> *}.
ClassSym r =>
Maybe Name
-> [CS (r (StateVar r))]
-> [MS (r (Method r))]
-> [MS (r (Method r))]
-> CS (r (Class r))
getFunc' Maybe Name
l
      getFunc ClassType
Auxiliary = Name
-> Maybe Name
-> [CS (r (StateVar r))]
-> [MS (r (Method r))]
-> [MS (r (Method r))]
-> CS (r (Class r))
forall (r :: * -> *).
ClassSym r =>
Name
-> Maybe Name
-> [CSStateVar r]
-> [SMethod r]
-> [SMethod r]
-> SClass r
extraClass Name
n Maybe Name
forall a. Maybe a
Nothing
      getFunc' :: Maybe Name
-> [CS (r (StateVar r))]
-> [MS (r (Method r))]
-> [MS (r (Method r))]
-> CS (r (Class r))
getFunc' Maybe Name
Nothing = Maybe Name
-> [CS (r (StateVar r))]
-> [MS (r (Method r))]
-> [MS (r (Method r))]
-> CS (r (Class r))
forall {r :: * -> *}.
ClassSym r =>
Maybe Name
-> [CS (r (StateVar r))]
-> [MS (r (Method r))]
-> [MS (r (Method r))]
-> CS (r (Class r))
buildClass Maybe Name
forall a. Maybe a
Nothing
      getFunc' (Just Name
intfc) = Name
-> [Name]
-> [CS (r (StateVar r))]
-> [MS (r (Method r))]
-> [MS (r (Method r))]
-> CS (r (Class r))
forall (r :: * -> *).
ClassSym r =>
Name
-> [Name]
-> [CSStateVar r]
-> [SMethod r]
-> [SMethod r]
-> SClass r
implementingClass Name
n [Name
intfc]
      c :: SClass r
c = ClassType
-> [CSStateVar r] -> [SMethod r] -> [SMethod r] -> SClass r
forall {r :: * -> *}.
ClassSym r =>
ClassType
-> [CS (r (StateVar r))]
-> [MS (r (Method r))]
-> [MS (r (Method r))]
-> CS (r (Class r))
getFunc ClassType
s [CSStateVar r]
vs [SMethod r]
cs [SMethod r]
ms
  SClass r -> GenState (SClass r)
forall a. a -> StateT DrasilState Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return (SClass r -> GenState (SClass r))
-> SClass r -> GenState (SClass r)
forall a b. (a -> b) -> a -> b
$ if Comments
CommentClass Comments -> [Comments] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` DrasilState -> [Comments]
commented DrasilState
g
    then Name -> SClass r -> SClass r
forall (r :: * -> *). ClassSym r => Name -> SClass r -> SClass r
docClass Name
desc SClass r
c
    else SClass r
c

-- | Generates a primary class.
primaryClass :: (OOProg r) => Name -> Maybe Name -> Description ->
  [CSStateVar r] -> GenState [SMethod r] -> GenState [SMethod r] ->
  GenState (SClass r)
primaryClass :: forall (r :: * -> *).
OOProg r =>
Name
-> Maybe Name
-> Name
-> [CSStateVar r]
-> GenState [SMethod r]
-> GenState [SMethod r]
-> GenState (SClass r)
primaryClass = ClassType
-> Name
-> Maybe Name
-> Name
-> [StateT ClassState Identity (r (StateVar r))]
-> StateT
     DrasilState Identity [StateT MethodState Identity (r (Method r))]
-> StateT
     DrasilState Identity [StateT MethodState Identity (r (Method r))]
-> StateT
     DrasilState Identity (StateT ClassState Identity (r (Class r)))
forall (r :: * -> *).
OOProg r =>
ClassType
-> Name
-> Maybe Name
-> Name
-> [CSStateVar r]
-> GenState [SMethod r]
-> GenState [SMethod r]
-> GenState (SClass r)
mkClass ClassType
Primary

-- | Generates an auxiliary class (for when a module contains multiple classes).
auxClass :: (OOProg r) => Name -> Maybe Name -> Description ->
  [CSStateVar r] -> GenState [SMethod r] -> GenState [SMethod r] ->
  GenState (SClass r)
auxClass :: forall (r :: * -> *).
OOProg r =>
Name
-> Maybe Name
-> Name
-> [CSStateVar r]
-> GenState [SMethod r]
-> GenState [SMethod r]
-> GenState (SClass r)
auxClass = ClassType
-> Name
-> Maybe Name
-> Name
-> [StateT ClassState Identity (r (StateVar r))]
-> StateT
     DrasilState Identity [StateT MethodState Identity (r (Method r))]
-> StateT
     DrasilState Identity [StateT MethodState Identity (r (Method r))]
-> StateT
     DrasilState Identity (StateT ClassState Identity (r (Class r)))
forall (r :: * -> *).
OOProg r =>
ClassType
-> Name
-> Maybe Name
-> Name
-> [CSStateVar r]
-> GenState [SMethod r]
-> GenState [SMethod r]
-> GenState (SClass r)
mkClass ClassType
Auxiliary

-- | Converts lists or objects to pointer arguments, since we use pointerParam
-- for list or object-type parameters.
mkArg :: (SharedProg r) => SValue r -> SValue r
mkArg :: forall (r :: * -> *). SharedProg r => SValue r -> SValue r
mkArg SValue r
v = do
  r (Value r)
vl <- SValue r
v
  let mkArg' :: CodeType -> SValue r -> SValue r
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
  CodeType -> SValue r -> SValue r
forall {r :: * -> *}.
Argument r =>
CodeType -> SValue r -> SValue r
mkArg' (r (Type r) -> CodeType
forall (r :: * -> *). TypeElim r => r (Type r) -> CodeType
getType (r (Type r) -> CodeType) -> r (Type r) -> CodeType
forall a b. (a -> b) -> a -> b
$ r (Value r) -> r (Type r)
forall (r :: * -> *). ValueSym r => r (Value r) -> r (Type r)
valueType r (Value r)
vl) (r (Value r) -> SValue r
forall a. a -> StateT ValueState Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return r (Value r)
vl)


-- | Gets the current module and calls mkArg on the arguments.
-- Called by more specific function call generators ('fApp' and 'ctorCall').
fCall :: (SharedProg r) => (Name -> [SValue r] -> NamedArgs r -> SValue r) ->
  [SValue r] -> NamedArgs r -> GenState (SValue r)
fCall :: forall (r :: * -> *).
SharedProg 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
  DrasilState
g <- StateT DrasilState Identity DrasilState
forall s (m :: * -> *). MonadState s m => m s
get
  let cm :: Name
cm = DrasilState -> Name
currentModule DrasilState
g
      args :: [SValue r]
args = (SValue r -> SValue r) -> [SValue r] -> [SValue r]
forall a b. (a -> b) -> [a] -> [b]
map SValue r -> SValue r
forall (r :: * -> *). SharedProg r => SValue r -> SValue r
mkArg [SValue r]
vl
      nargs :: NamedArgs r
nargs = ((VS (r (Variable r)), SValue r)
 -> (VS (r (Variable r)), SValue r))
-> NamedArgs r -> NamedArgs r
forall a b. (a -> b) -> [a] -> [b]
map ((SValue r -> SValue r)
-> (VS (r (Variable r)), SValue r)
-> (VS (r (Variable 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 :: * -> *). SharedProg r => SValue r -> SValue r
mkArg) NamedArgs r
ns
  SValue r -> GenState (SValue r)
forall a. a -> StateT DrasilState Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return (SValue r -> GenState (SValue r))
-> SValue r -> GenState (SValue r)
forall a b. (a -> b) -> a -> b
$ Name -> [SValue r] -> NamedArgs r -> SValue r
f Name
cm [SValue r]
args NamedArgs r
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 :: (OOProg r) => Name -> Name -> VSType r -> [SValue r] ->
  NamedArgs r -> GenState (SValue r)
fApp :: forall (r :: * -> *).
OOProg r =>
Name
-> Name
-> VSType r
-> [SValue r]
-> NamedArgs r
-> GenState (SValue r)
fApp Name
m Name
s VSType r
t [SValue r]
vl NamedArgs r
ns = do
  DrasilState
g <- StateT DrasilState Identity DrasilState
forall s (m :: * -> *). MonadState s m => m s
get
  (Name -> [SValue r] -> NamedArgs r -> SValue r)
-> [SValue r] -> NamedArgs r -> GenState (SValue r)
forall (r :: * -> *).
SharedProg 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 -> MixedCall r
forall (r :: * -> *). ValueExpression r => Name -> MixedCall r
extFuncAppMixedArgs Name
m Name
s VSType r
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 VSType r
t [SValue r]
args NamedArgs r
nargs
      else MixedCall r
forall (r :: * -> *). OOValueExpression r => MixedCall r
selfFuncAppMixedArgs Name
s VSType r
t [SValue r]
args NamedArgs r
nargs) [SValue r]
vl NamedArgs r
ns

-- | Logic similar to 'fApp', but the self case is not required here
-- (because constructor will never be private). Calls 'newObjMixedArgs'.
ctorCall :: (OOProg r) => Name -> VSType r -> [SValue r] -> NamedArgs r
  -> GenState (SValue r)
ctorCall :: forall (r :: * -> *).
OOProg r =>
Name
-> VSType r -> [SValue r] -> NamedArgs r -> GenState (SValue r)
ctorCall Name
m VSType r
t = (Name
 -> [StateT ValueState Identity (r (Value r))]
 -> [(StateT ValueState Identity (r (Variable r)),
      StateT ValueState Identity (r (Value r)))]
 -> StateT ValueState Identity (r (Value r)))
-> [StateT ValueState Identity (r (Value r))]
-> [(StateT ValueState Identity (r (Variable r)),
     StateT ValueState Identity (r (Value r)))]
-> StateT
     DrasilState Identity (StateT ValueState Identity (r (Value r)))
forall (r :: * -> *).
SharedProg r =>
(Name -> [SValue r] -> NamedArgs r -> SValue r)
-> [SValue r] -> NamedArgs r -> GenState (SValue r)
fCall (\Name
cm [StateT ValueState Identity (r (Value r))]
args [(StateT ValueState Identity (r (Variable r)),
  StateT ValueState Identity (r (Value 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 => MixedCall r
extNewObjMixedArgs Name
m VSType r
t [StateT ValueState Identity (r (Value r))]
args [(StateT ValueState Identity (r (Variable r)),
  StateT ValueState Identity (r (Value r)))]
nargs else MixedCtorCall r
forall (r :: * -> *). OOValueExpression r => MixedCtorCall r
newObjMixedArgs VSType r
t [StateT ValueState Identity (r (Value r))]
args [(StateT ValueState Identity (r (Variable r)),
  StateT ValueState Identity (r (Value r)))]
nargs)

-- | Logic similar to 'fApp', but for In/Out calls.
fAppInOut :: (OOProg r) => Name -> Name -> [SValue r] ->
  [SVariable r] -> [SVariable r] -> GenState (MSStatement r)
fAppInOut :: forall (r :: * -> *).
OOProg r =>
Name
-> Name
-> [SValue r]
-> [SVariable r]
-> [SVariable r]
-> GenState (MSStatement r)
fAppInOut Name
m Name
n [SValue r]
ins [SVariable r]
outs [SVariable r]
both = do
  DrasilState
g <- StateT DrasilState Identity DrasilState
forall s (m :: * -> *). MonadState s m => m s
get
  let cm :: Name
cm = DrasilState -> Name
currentModule DrasilState
g
  MSStatement r -> GenState (MSStatement r)
forall a. a -> StateT DrasilState Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return (MSStatement r -> GenState (MSStatement r))
-> MSStatement r -> GenState (MSStatement r)
forall a b. (a -> b) -> a -> b
$ if Name
m Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
/= Name
cm then Name -> InOutCall r
forall (r :: * -> *). FuncAppStatement r => Name -> InOutCall r
extInOutCall Name
m Name
n [SValue r]
ins [SVariable r]
outs [SVariable r]
both else if Name -> Map Name Name -> Maybe Name
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Name
n
    (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 InOutCall r
forall (r :: * -> *). FuncAppStatement r => InOutCall r
inOutCall Name
n [SValue r]
ins [SVariable r]
outs [SVariable r]
both else
    InOutCall r
forall (r :: * -> *). OOFuncAppStatement r => InOutCall r
selfInOutCall Name
n [SValue r]
ins [SVariable r]
outs [SVariable r]
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) => Name -> Description -> [Import] ->
  [GenState (Maybe (SMethod r))] -> GenState (Proc.SFile r)
genModuleWithImportsProc :: forall (r :: * -> *).
ProcProg r =>
Name
-> Name
-> [Name]
-> [GenState (Maybe (SMethod r))]
-> GenState (SFile r)
genModuleWithImportsProc Name
n Name
desc [Name]
is [GenState (Maybe (SMethod r))]
maybeMs = do
  DrasilState
g <- StateT DrasilState Identity DrasilState
forall s (m :: * -> *). MonadState s m => m s
get
  (DrasilState -> DrasilState) -> StateT DrasilState Identity ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify (\DrasilState
s -> DrasilState
s { currentModule = n })
  let as :: [Name]
as = (Person -> Name) -> [Person] -> [Name]
forall a b. (a -> b) -> [a] -> [b]
map Person -> Name
forall n. HasName n => n -> Name
name (DrasilState -> CodeSpec
codeSpec DrasilState
g CodeSpec -> Getting [Person] CodeSpec [Person] -> [Person]
forall s a. s -> Getting a s a -> a
^. Getting [Person] CodeSpec [Person]
forall c. HasOldCodeSpec c => Lens' c [Person]
Lens' CodeSpec [Person]
authorsO )
  [Maybe (SMethod r)]
ms <- [GenState (Maybe (SMethod r))]
-> StateT DrasilState Identity [Maybe (SMethod r)]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
forall (m :: * -> *) a. Monad m => [m a] -> m [a]
sequence [GenState (Maybe (SMethod r))]
maybeMs
  let commMod :: SFile r -> SFile r
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 -> [Comments]
commented DrasilState
g                   = Name -> [Name] -> Name -> SFile r -> SFile r
forall (r :: * -> *).
FileSym r =>
Name -> [Name] -> Name -> SFile r -> SFile r
Proc.docMod Name
desc
                  [Name]
as (DrasilState -> Name
date DrasilState
g)
              | 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 -> [Comments]
commented DrasilState
g Bool -> Bool -> Bool
&& Bool -> Bool
not ([Maybe (SMethod r)] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Maybe (SMethod r)]
ms) = Name -> [Name] -> Name -> SFile r -> SFile r
forall (r :: * -> *).
FileSym r =>
Name -> [Name] -> Name -> SFile r -> SFile r
Proc.docMod Name
"" []
                  Name
""
              | Bool
otherwise                                       = SFile r -> SFile r
forall a. a -> a
id
  SFile r -> GenState (SFile r)
forall a. a -> StateT DrasilState Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return (SFile r -> GenState (SFile r)) -> SFile r -> GenState (SFile r)
forall a b. (a -> b) -> a -> b
$ SFile r -> SFile r
commMod (SFile r -> SFile r) -> SFile r -> SFile r
forall a b. (a -> b) -> a -> b
$ FSModule r -> SFile r
forall (r :: * -> *). FileSym r => FSModule r -> SFile r
Proc.fileDoc (FSModule r -> SFile r) -> FSModule r -> SFile r
forall a b. (a -> b) -> a -> b
$ Name -> [Name] -> [SMethod r] -> FSModule r
forall (r :: * -> *).
ModuleSym r =>
Name -> [Name] -> [SMethod r] -> FSModule r
Proc.buildModule Name
n [Name]
is ([Maybe (SMethod r)] -> [SMethod r]
forall a. [Maybe a] -> [a]
catMaybes [Maybe (SMethod r)]
ms)

-- | Generates a module for when imports do not need to be explicitly stated.
genModuleProc :: (ProcProg r) => Name -> Description ->
  [GenState (Maybe (SMethod r))] -> GenState (Proc.SFile r)
genModuleProc :: forall (r :: * -> *).
ProcProg r =>
Name
-> Name -> [GenState (Maybe (SMethod r))] -> GenState (SFile r)
genModuleProc Name
n Name
desc = Name
-> Name
-> [Name]
-> [StateT
      DrasilState
      Identity
      (Maybe (StateT MethodState Identity (r (Method r))))]
-> StateT
     DrasilState Identity (StateT FileState Identity (r (File r)))
forall (r :: * -> *).
ProcProg r =>
Name
-> Name
-> [Name]
-> [GenState (Maybe (SMethod r))]
-> GenState (SFile r)
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 :: (SharedProg r) => Name -> Name -> VSType r -> [SValue r] ->
  NamedArgs r -> GenState (SValue r)
fAppProc :: forall (r :: * -> *).
SharedProg r =>
Name
-> Name
-> VSType r
-> [SValue r]
-> NamedArgs r
-> GenState (SValue r)
fAppProc Name
m Name
s VSType r
t [SValue r]
vl NamedArgs r
ns = do
  DrasilState
g <- StateT DrasilState Identity DrasilState
forall s (m :: * -> *). MonadState s m => m s
get
  (Name -> [SValue r] -> NamedArgs r -> SValue r)
-> [SValue r] -> NamedArgs r -> GenState (SValue r)
forall (r :: * -> *).
SharedProg 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 -> MixedCall r
forall (r :: * -> *). ValueExpression r => Name -> MixedCall r
extFuncAppMixedArgs Name
m Name
s VSType r
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 VSType r
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.") [SValue r]
vl NamedArgs r
ns

-- | Logic similar to 'fApp', but for In/Out calls.
fAppInOutProc :: (SharedProg r) => Name -> Name -> [SValue r] ->
  [SVariable r] -> [SVariable r] -> GenState (MSStatement r)
fAppInOutProc :: forall (r :: * -> *).
SharedProg r =>
Name
-> Name
-> [SValue r]
-> [SVariable r]
-> [SVariable r]
-> GenState (MSStatement r)
fAppInOutProc Name
m Name
n [SValue r]
ins [SVariable r]
outs [SVariable r]
both = do
  DrasilState
g <- StateT DrasilState Identity DrasilState
forall s (m :: * -> *). MonadState s m => m s
get
  let cm :: Name
cm = DrasilState -> Name
currentModule DrasilState
g
  MSStatement r -> GenState (MSStatement r)
forall a. a -> StateT DrasilState Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return (MSStatement r -> GenState (MSStatement r))
-> MSStatement r -> GenState (MSStatement r)
forall a b. (a -> b) -> a -> b
$ if Name
m Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
/= Name
cm then Name -> InOutCall r
forall (r :: * -> *). FuncAppStatement r => Name -> InOutCall r
extInOutCall Name
m Name
n [SValue r]
ins [SVariable r]
outs [SVariable r]
both else if Name -> Map Name Name -> Maybe Name
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Name
n
    (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 InOutCall r
forall (r :: * -> *). FuncAppStatement r => InOutCall r
inOutCall Name
n [SValue r]
ins [SVariable r]
outs [SVariable r]
both
    else Name -> MSStatement r
forall a. HasCallStack => Name -> a
error Name
"fAppInOutProc: Procedural languages do not support method calls."