Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions code/drasil-code/lib/Language/Drasil/Choices.hs
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,15 @@ makeDocConfig = DocConfig
data Comments = CommentFunc -- ^ Function/method-level comments.
| CommentClass -- ^ Class-level comments.
| CommentMod -- ^ File/Module-level comments.
| CommentVarDecls -- ^ Variable declarations comments.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice addition!

deriving Eq

-- | Renders options for implementation of comments.
instance RenderChoices Comments where
showChs CommentFunc = S "CommentFunc"
showChs CommentClass = S "CommentClass"
showChs CommentMod = S "CommentMod"
showChs CommentVarDecls = S "CommentVarDecls"

-- | Doxygen file verbosity options.
data Verbosity = Verbose | Quiet
Expand Down
41 changes: 25 additions & 16 deletions code/drasil-code/lib/Language/Drasil/Code/Imperative/Generator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import Language.Drasil.Code.Imperative.Modules (genInputMod, genInputModProc,
genConstClass, genConstMod, checkConstClass, genInputClass,
genInputConstraints, genInputConstraintsProc, genInputDerived,
genInputDerivedProc, genInputFormat, genInputFormatProc, genMain, genMainProc,
genMainFunc, genMainFuncProc, genCalcMod, genCalcModProc, genCalcFunc,
genMainFunc, genMainFuncProc, genCalcMod, genCalcModProc,
genCalcFuncProc, genOutputFormat, genOutputFormatProc, genOutputMod,
genOutputModProc, genSampleInput)
import Language.Drasil.Code.Imperative.DrasilState (GenState, DrasilState(..),
Expand All @@ -36,7 +36,7 @@ import Language.Drasil.CodeSpec (CodeSpec(..), HasOldCodeSpec(..), getODE)
import Language.Drasil.Printers (SingleLine(OneLine), sentenceDoc)

import Drasil.GOOL (OOProg, VisibilityTag(..),
ProgData(..), initialState)
ProgData(..), initialState, SMethod)
import qualified Drasil.GOOL as OO (GSProgram, SFile, ProgramSym(..), unCI)
import Drasil.GProc (ProcProg)
import qualified Drasil.GProc as Proc (GSProgram, SFile, ProgramSym(..), unCI)
Expand Down Expand Up @@ -150,8 +150,8 @@ genPackage unRepr = do
db = codeSpec g ^. systemdbO
-- prps = show $ sentenceDoc db Implementation OneLine
-- (foldlSent $ purpose $ codeSpec g)
prps = show $ sentenceDoc db Implementation OneLine
(foldlSent $ codeSpec g ^. purpose)
prps = show $ sentenceDoc db Implementation OneLine
(foldlSent $ codeSpec g ^. purpose)
bckgrnd = show $ sentenceDoc db Implementation OneLine
(foldlSent $ codeSpec g ^. background)
mtvtn = show $ sentenceDoc db Implementation OneLine
Expand Down Expand Up @@ -183,34 +183,43 @@ genProgram :: (OOProg r) => GenState (OO.GSProgram r)
genProgram = do
g <- get
ms <- chooseModules $ modular g
let n = codeSpec g ^. pNameO
let n = codeSpec g ^. pNameO
let p = show $ sentenceDoc (codeSpec g ^. systemdbO) Implementation OneLine $ foldlSent $ codeSpec g ^. purpose
return $ OO.prog n p ms

-- | Generates either a single module or many modules, based on the users choice
-- of modularity.
chooseModules :: (OOProg r) => Modularity -> GenState [OO.SFile r]
chooseModules :: OOProg r => Modularity -> GenState [OO.SFile r]
chooseModules Unmodular = liftS genUnmodular
chooseModules Modular = genModules

-- | Generates an entire SCS program as a single module.
genUnmodular :: (OOProg r) => GenState (OO.SFile r)
genUnmodular :: OOProg r => GenState (OO.SFile r)
genUnmodular = do
g <- get
umDesc <- unmodularDesc
giName <- genICName GetInput
dvName <- genICName DerivedValuesFn
icName <- genICName InputConstraintsFn
let n = codeSpec g ^. pNameO
cls = any (`member` clsMap g) [giName, dvName, icName]
genModuleWithImports n umDesc (concatMap (^. imports) (elems $ extLibMap g))
(genMainFunc
: map (fmap Just) (map genCalcFunc (codeSpec g ^. execOrderO)
++ concatMap genModFuncs (modules g))
++ ((if cls then [] else [genInputFormat Pub, genInputDerived Pub,
genInputConstraints Pub]) ++ [genOutputFormat]))
([genInputClass Auxiliary, genConstClass Auxiliary]
++ map (fmap Just) (concatMap genModClasses $ modules g))
clsyInputs :: OOProg r => [GenState (Maybe (SMethod r))]
clsyInputs
| any (`member` clsMap g) [giName, dvName, icName] = []
| otherwise = [genInputFormat Pub, genInputDerived Pub, genInputConstraints Pub]

modFuncs :: OOProg r => [GenState (Maybe (SMethod r))]
modFuncs = map (fmap Just) $ concatMap genModFuncs (modules g)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an odd piece of code -- it deserves a comment!


components :: OOProg r => [GenState (Maybe (SMethod r))]
components = genMainFunc : modFuncs ++ clsyInputs ++ [genOutputFormat]

classes = [genInputClass Auxiliary, genConstClass Auxiliary] ++ map (fmap Just) (concatMap genModClasses $ modules g)

genModuleWithImports n umDesc
(concatMap (^. imports)
(elems $ extLibMap g))
components
classes

-- | Generates all modules for an SCS program.
genModules :: (OOProg r) => GenState [OO.SFile r]
Expand Down
105 changes: 72 additions & 33 deletions code/drasil-code/lib/Language/Drasil/Code/Imperative/Modules.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import Language.Drasil.Code.DataDesc (DataDesc, junkLine, singleton)
import Language.Drasil.Code.ExtLibImport (defs, imports, steps)
import Language.Drasil.Choices (Comments(..), ConstantStructure(..),
ConstantRepr(..), ConstraintBehaviour(..), ImplementationType(..),
Logging(..), Structure(..), hasSampleInput, InternalConcept(..))
Logging(..), Structure(..), hasSampleInput, InternalConcept(..), Modularity (..))
import Language.Drasil.CodeSpec (HasOldCodeSpec(..))
import Language.Drasil.Expr.Development (Completeness(..))
import Language.Drasil.Printers (SingleLine(OneLine), codeExprDoc, showHasSymbImpl)
Expand All @@ -62,7 +62,7 @@ import Drasil.GOOL (MSBody, MSBlock, SVariable, SValue, MSStatement,
DeclStatement(..), OODeclStatement(..), objDecNewNoParams,
extObjDecNewNoParams, IOStatement(..), ControlStatement(..), ifNoElse,
VisibilitySym(..), MethodSym(..), StateVarSym(..), pubDVar, convType,
convTypeOO, VisibilityTag(..))
convTypeOO, VisibilityTag(..), CommentStatement(..))

import qualified Drasil.GOOL as OO (SFile)
import Drasil.GProc (ProcProg)
Expand Down Expand Up @@ -93,27 +93,67 @@ genMain = genModule "Control" "Controls the flow of the program"
-- functions for reading input values, calculating derived inputs, checking
-- constraints, calculating outputs, and printing outputs.
-- Returns Nothing if the user chose to generate a library.
genMainFunc :: (OOProg r) => GenState (Maybe (SMethod r))
genMainFunc :: OOProg r => GenState (Maybe (SMethod r))
genMainFunc = do
g <- get
let mainFunc Library = return Nothing
mainFunc Program = do
modify (\st -> st {currentScope = MainFn})
v_filename <- mkVar (quantvar inFileName)
logInFile <- maybeLog v_filename
co <- initConsts
ip <- getInputDecl
ics <- genAllInputCalls
varDef <- mapM genCalcCall (codeSpec g ^. execOrderO)
wo <- genOutputCall
return $ Just $ (if CommentFunc `elem` commented g then docMain else
mainFunction) $ bodyStatements $ initLogFileVar (logKind g) mainFn
++ varDecDef v_filename mainFn (arg 0)
: logInFile
-- Constants must be declared before inputs because some derived
-- input definitions or input constraints may use the constants
++ catMaybes [co, ip] ++ ics ++ catMaybes (varDef ++ [wo])
mainFunc $ implType g
g <- get
case (implType g, modular g) of
(Library, _) -> return Nothing
(Program, Modular) -> modularMainFunc
(Program, Unmodular) -> unmodularMainFunc

-- | Structure of a modularized program's "main" function.
modularMainFunc :: OOProg r => GenState (Maybe (SMethod r))
modularMainFunc = do
g <- get
modify (\st -> st {currentScope = MainFn})
v_filename <- mkVar (quantvar inFileName)
logInFile <- maybeLog v_filename
co <- initConsts
ip <- getInputDecl
ics <- genAllInputCalls
varDef <- mapM genCalcCall (codeSpec g ^. execOrderO)
wo <- genOutputCall
let mainW = if CommentFunc `elem` commented g then docMain else mainFunction
return $ Just $ mainW $ bodyStatements $
initLogFileVar (logKind g) mainFn ++ varDecDef v_filename mainFn (arg 0)
: logInFile
-- Constants must be declared before inputs because some derived input
-- definitions or input constraints may use the constants
++ catMaybes [co, ip] ++ ics ++ catMaybes (varDef ++ [wo])

-- | Structure of a unmodular program's "main" function.
unmodularMainFunc :: OOProg r => GenState (Maybe (SMethod r))
unmodularMainFunc = do
g <- get
modify (\st -> st {currentScope = MainFn})
v_filename <- mkVar (quantvar inFileName)
logInFile <- maybeLog v_filename
co <- initConsts
ip <- getInputDecl
ics <- genAllInputCalls -- TODO: How should the the above 3 (inclusive) be changed?
varDef <- mapM declVarCalc (codeSpec g ^. execOrderO)
wo <- genOutputCall
let mainW = if CommentFunc `elem` commented g then docMain else mainFunction
return $ Just $ mainW $ bodyStatements $
initLogFileVar (logKind g) mainFn ++ varDecDef v_filename mainFn (arg 0)
: logInFile
-- Constants must be declared before inputs because some derived input
-- definitions or input constraints may use the constants
++ catMaybes [co, ip] ++ ics ++ catMaybes (varDef ++ [wo])

-- | Declare a variable and immediately assign to it with a given expression.
declVarCalc :: OOProg r => CodeDefinition -> GenState (Maybe (MSStatement r))
declVarCalc c = do
g <- get
let scp = convScope $ currentScope g
let val = c ^. codeExpr
val' <- convExpr val
v <- mkVar (quantvar c)
l <- maybeLog v
cmnt <- getComment c
let cmnt' = [comment cmnt | CommentVarDecls `elem` commented g]
statements = cmnt' ++ varDecDef v scp val' : l
return $ pure $ multi statements

-- | If there are no inputs, the 'inParams' object still needs to be declared
-- if inputs are 'Bundled', constants are stored 'WithInputs', and constant
Expand Down Expand Up @@ -540,17 +580,16 @@ genCalcFunc cdef = do
let nm = codeName cdef
tp <- codeType cdef
v <- mkVar (quantvar cdef)
blcks <- case cdef ^. defType
of Definition -> liftS $ genCalcBlock CalcReturn cdef
(cdef ^. codeExpr)
ODE -> maybe (error $ nm ++ " missing from ExtLibMap")
(\el -> do
defStmts <- mapM convStmt (el ^. defs)
stepStmts <- mapM convStmt (el ^. steps)
return [block (varDec v local : defStmts),
block stepStmts,
block [returnStmt $ valueOf v]])
(Map.lookup nm (extLibMap g))
blcks <- case cdef ^. defType of
Definition -> liftS $ genCalcBlock CalcReturn cdef (cdef ^. codeExpr)
ODE -> maybe (error $ nm ++ " missing from ExtLibMap")
(\el -> do
defStmts <- mapM convStmt (el ^. defs)
stepStmts <- mapM convStmt (el ^. steps)
return [block (varDec v local : defStmts),
block stepStmts,
block [returnStmt $ valueOf v]])
(Map.lookup nm (extLibMap g))
desc <- getComment cdef
publicFunc
nm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ baseChoices = defaultChoices {
dataInfo = makeData Unbundled WithInputs Var,
maps = makeMaps (matchConcepts [(piConst, [Pi])]) spaceToCodeType,
optFeats = makeOptFeats
(makeDocConfig [CommentFunc, CommentClass, CommentMod] Quiet Hide)
(makeDocConfig [CommentFunc, CommentClass, CommentMod, CommentVarDecls] Quiet Hide)
(makeLogConfig [] "log.txt")
[SampleInput "../../../datafiles/projectile/sampleInput.txt", ReadME],
srsConstraints = makeConstraints Warning Warning,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading