2024-09-23 22:11:54 +02:00
|
|
|
module DependencyGenerator
|
|
|
|
( DepGenM
|
|
|
|
, evalDepGenM
|
|
|
|
, inject
|
|
|
|
, runFunction
|
|
|
|
, runFunctionIO
|
|
|
|
, mapDepGenM
|
|
|
|
, mapDepGenM_
|
|
|
|
, filterDepGenM
|
|
|
|
|
|
|
|
, isImageFilename
|
|
|
|
, convertedImageFilename
|
|
|
|
, listDirectory
|
|
|
|
, readTemplate
|
|
|
|
, convertImage
|
|
|
|
, saveFile
|
|
|
|
, runPandoc
|
|
|
|
) where
|
|
|
|
|
|
|
|
import Types.Token (Token(..))
|
|
|
|
import Types.Values
|
|
|
|
import Types.Value (Valuable(..))
|
|
|
|
import Types.Function (Function(..))
|
|
|
|
import Types.FunctionIO (FunctionIO(..))
|
2024-09-24 21:21:49 +02:00
|
|
|
import Types.Dependency (Action(..), Dependency, makeDependency)
|
2024-09-23 22:11:54 +02:00
|
|
|
|
|
|
|
import Control.Monad.State (MonadState, State, runState, put, get)
|
|
|
|
import Control.Monad.Writer (MonadWriter, WriterT, execWriterT, tell)
|
|
|
|
|
|
|
|
newtype DepGenM' a = DepGenM { unDepGenM :: WriterT [Dependency] (State Int) a }
|
|
|
|
deriving (Functor, Applicative, Monad, MonadState Int, MonadWriter [Dependency])
|
|
|
|
|
|
|
|
type DepGenM a = DepGenM' (Token a)
|
|
|
|
|
|
|
|
evalDepGenM' :: Int -> DepGenM () -> ([Dependency], Int)
|
|
|
|
evalDepGenM' top m = runState (execWriterT (unDepGenM m)) top
|
|
|
|
|
|
|
|
evalDepGenM :: DepGenM () -> [Dependency]
|
|
|
|
evalDepGenM m = fst (evalDepGenM' 0 m)
|
|
|
|
|
|
|
|
tellDep :: Dependency -> DepGenM' ()
|
|
|
|
tellDep dep = tell [dep]
|
|
|
|
|
2024-09-24 21:25:58 +02:00
|
|
|
genDependencyM :: (Token a -> DepGenM' Dependency) -> DepGenM a
|
2024-09-23 22:11:54 +02:00
|
|
|
genDependencyM f = do
|
|
|
|
top <- get
|
|
|
|
let top' = top + 1
|
|
|
|
target = Token top'
|
|
|
|
put top'
|
|
|
|
result <- f target
|
|
|
|
tellDep result
|
|
|
|
pure target
|
|
|
|
|
2024-09-24 21:25:58 +02:00
|
|
|
genDependency :: (Token a -> Dependency) -> DepGenM a
|
2024-09-23 22:11:54 +02:00
|
|
|
genDependency f = genDependencyM (pure . f)
|
|
|
|
|
2024-09-24 21:25:58 +02:00
|
|
|
inject :: Valuable a => a -> DepGenM a
|
2024-09-23 22:11:54 +02:00
|
|
|
inject x = genDependency (makeDependency NoToken (Inject (toValue x)))
|
|
|
|
|
2024-09-24 21:25:58 +02:00
|
|
|
getListElem :: Token [a] -> DepGenM a
|
2024-09-23 22:11:54 +02:00
|
|
|
getListElem outer = genDependency (makeDependency outer GetListElem)
|
|
|
|
|
2024-09-24 21:25:58 +02:00
|
|
|
setListElem :: Token a -> Token [a] -> DepGenM ()
|
2024-09-23 22:11:54 +02:00
|
|
|
setListElem a outer = do
|
|
|
|
tellDep (makeDependency a SetListElem outer)
|
|
|
|
pure NoToken
|
|
|
|
|
2024-09-24 21:25:58 +02:00
|
|
|
runFunction :: Function -> Token a -> DepGenM b
|
2024-09-23 22:11:54 +02:00
|
|
|
runFunction f input = genDependency (makeDependency input (Function f))
|
|
|
|
|
2024-09-24 21:25:58 +02:00
|
|
|
runFunctionIO :: FunctionIO -> Token a -> DepGenM b
|
2024-09-23 22:11:54 +02:00
|
|
|
runFunctionIO f input = genDependency (makeDependency input (FunctionIO f))
|
|
|
|
|
2024-09-24 21:25:58 +02:00
|
|
|
mapDepGenM :: (Token a -> DepGenM b) -> Token [a] -> DepGenM [b]
|
2024-09-23 22:11:54 +02:00
|
|
|
mapDepGenM f input = genDependencyM $ \target -> do
|
|
|
|
top <- get
|
|
|
|
let (res, top') = evalDepGenM' top $ do
|
|
|
|
inp <- getListElem input
|
|
|
|
outp <- f inp
|
|
|
|
setListElem outp target
|
|
|
|
put top'
|
|
|
|
pure (makeDependency input (MapComp res) target)
|
|
|
|
|
2024-09-24 22:23:43 +02:00
|
|
|
mapDepGenM_ :: (Token a -> DepGenM ()) -> Token [a] -> DepGenM' ()
|
2024-09-23 22:11:54 +02:00
|
|
|
mapDepGenM_ f input = do
|
|
|
|
_ <- mapDepGenM f input
|
2024-09-24 22:23:43 +02:00
|
|
|
pure ()
|
2024-09-23 22:11:54 +02:00
|
|
|
|
2024-09-24 21:25:58 +02:00
|
|
|
filterDepGenM :: (Token a -> DepGenM Bool) -> Token [a] -> DepGenM [a]
|
2024-09-23 22:11:54 +02:00
|
|
|
filterDepGenM f input = do
|
|
|
|
conds <- mapDepGenM f input
|
|
|
|
genDependency (makeDependency (TupleToken input conds) FilterComp)
|
|
|
|
|
|
|
|
|
|
|
|
isImageFilename :: Token FilePath -> DepGenM Bool
|
|
|
|
isImageFilename = runFunction IsImageFilename
|
|
|
|
|
|
|
|
convertedImageFilename :: Token FilePath -> DepGenM FilePath
|
|
|
|
convertedImageFilename = runFunction ConvertedImageFilename
|
|
|
|
|
|
|
|
listDirectory :: Token FilePath -> DepGenM [FilePath]
|
|
|
|
listDirectory = runFunctionIO ListDirectory
|
|
|
|
|
|
|
|
readTemplate :: Token FilePath -> DepGenM Template
|
|
|
|
readTemplate = runFunctionIO ReadTemplate
|
|
|
|
|
|
|
|
convertImage :: Token ((FilePath, FilePath), ImageConversionSettings) -> DepGenM ()
|
|
|
|
convertImage = runFunctionIO ConvertImage
|
|
|
|
|
|
|
|
saveFile :: Token (String, FilePath) -> DepGenM ()
|
|
|
|
saveFile = runFunctionIO SaveFile
|
|
|
|
|
2024-09-24 22:24:06 +02:00
|
|
|
runPandoc :: Token (FilePath, FilePath) -> DepGenM ()
|
2024-09-23 22:11:54 +02:00
|
|
|
runPandoc = runFunctionIO RunPandoc
|