Skip to content

Commit

Permalink
Make evaluation stateful over variables
Browse files Browse the repository at this point in the history
  • Loading branch information
jaspervdj committed Oct 22, 2024
1 parent ddd447d commit 2faa89c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
26 changes: 17 additions & 9 deletions lib/Patat/Eval.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module Patat.Eval
--------------------------------------------------------------------------------
import qualified Control.Concurrent.Async as Async
import Control.Exception (finally)
import Control.Monad.State (StateT, runStateT)
import Control.Monad.Trans (liftIO)
import qualified Data.HashMap.Strict as HMS
import Data.Maybe (maybeToList)
import qualified Data.Text as T
Expand All @@ -25,11 +27,15 @@ import qualified Text.Pandoc.Definition as Pandoc

--------------------------------------------------------------------------------
eval :: Presentation -> IO Presentation
eval presentation = case psEval (pSettings presentation) of
Nothing -> pure presentation
Just settings -> do
slides <- traverse (evalSlide settings) (pSlides presentation)
pure presentation {pSlides = slides}
eval presentation = do
(pres, varGen) <- runStateT work (pVarGen presentation)
pure pres {pVarGen = varGen}
where
work = case psEval (pSettings presentation) of
Nothing -> pure presentation
Just settings -> do
slides <- traverse (evalSlide settings) (pSlides presentation)
pure presentation {pSlides = slides}


--------------------------------------------------------------------------------
Expand All @@ -40,7 +46,7 @@ lookupSettings classes settings = do


--------------------------------------------------------------------------------
evalSlide :: EvalSettingsMap -> Slide -> IO Slide
evalSlide :: EvalSettingsMap -> Slide -> StateT VarGen IO Slide
evalSlide settings slide = case slideContent slide of
TitleSlide _ _ -> pure slide
ContentSlide instrs0 -> do
Expand All @@ -51,7 +57,7 @@ evalSlide settings slide = case slideContent slide of
--------------------------------------------------------------------------------
evalInstruction
:: EvalSettingsMap -> Instruction Pandoc.Block
-> IO [Instruction Pandoc.Block]
-> StateT VarGen IO [Instruction Pandoc.Block]
evalInstruction settings instr = case instr of
Pause -> pure [Pause]
ModifyLast i -> map ModifyLast <$> evalInstruction settings i
Expand All @@ -64,10 +70,12 @@ evalInstruction settings instr = case instr of


--------------------------------------------------------------------------------
evalBlock :: EvalSettingsMap -> Pandoc.Block -> IO [Instruction Pandoc.Block]
evalBlock
:: EvalSettingsMap -> Pandoc.Block
-> StateT VarGen IO [Instruction Pandoc.Block]
evalBlock settings orig@(Pandoc.CodeBlock attr@(_, classes, _) txt)
| [s@EvalSettings {..}] <- lookupSettings classes settings = do
out <- unsafeInterleaveIO $ do
out <- liftIO $ unsafeInterleaveIO $ do
EvalResult {..} <- evalCode s txt
pure $ case erExitCode of
ExitSuccess -> erStdout
Expand Down
2 changes: 2 additions & 0 deletions lib/Patat/Presentation/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ data Presentation = Presentation
, pTransitionGens :: !(Seq (Maybe TransitionGen)) -- One for each slide.
, pActiveFragment :: !Index
, pSyntaxMap :: !Skylighting.SyntaxMap
-- | Used to generate new variables inside the presentation.
, pVarGen :: Instruction.VarGen
}


Expand Down
1 change: 1 addition & 0 deletions lib/Patat/Presentation/Read.hs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ pandocToPresentation pFilePath pEncodingFallback pSettings pSyntaxMap
!pBreadcrumbs = collectBreadcrumbs pSlides
!pActiveFragment = (0, 0)
!pAuthor = concat (Pandoc.docAuthors meta)
!pVarGen = Instruction.zeroVarGen
pSlideSettings <- Seq.traverseWithIndex
(\i ->
first (\err -> "on slide " ++ show (i + 1) ++ ": " ++ err) .
Expand Down

0 comments on commit 2faa89c

Please sign in to comment.