Skip to content

Commit

Permalink
Move Var from Instruction to Syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
jaspervdj committed Dec 29, 2024
1 parent 2855c8f commit 87a5f57
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 131 deletions.
11 changes: 4 additions & 7 deletions lib/Patat/Eval.hs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ evalInstruction settings instr = case instr of
ModifyLast i -> map ModifyLast <$> evalInstruction settings i
Append [] -> pure [Append []]
Append blocks -> concat <$> traverse (evalBlock settings) blocks
AppendVar v ->
-- Should not happen since we don't do recursive evaluation.
pure [AppendVar v]
Delete -> pure [Delete]


Expand All @@ -92,14 +89,14 @@ evalBlock settings orig@(CodeBlock attr@(_, classes, _) txt)
var <- state freshVar
tell $ HMS.singleton var $ EvalBlock s attr txt Nothing
pure $ case (evalFragment, evalReplace) of
(False, True) -> [AppendVar var]
(False, False) -> [Append [orig], AppendVar var]
(False, True) -> [Append [VarBlock var]]
(False, False) -> [Append [orig, VarBlock var]]
(True, True) ->
[ Append [orig], Pause
, Delete, AppendVar var
, Delete, Append [VarBlock var]
]
(True, False) ->
[Append [orig], Pause, AppendVar var]
[Append [orig], Pause, Append [VarBlock var]]
| _ : _ : _ <- lookupSettings classes settings =
let msg = "patat eval matched multiple settings for " <>
T.intercalate "," classes in
Expand Down
17 changes: 10 additions & 7 deletions lib/Patat/Presentation/Display.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Control.Monad.Writer (Writer, execWriter, tell)
import qualified Data.Aeson.Extended as A
import Data.Char.WCWidth.Extended (wcstrwidth)
import Data.Foldable (for_)
import qualified Data.HashMap.Strict as HMS
import qualified Data.List as L
import Data.Maybe (fromMaybe, maybeToList)
import qualified Data.Sequence.Extended as Seq
Expand Down Expand Up @@ -68,6 +69,7 @@ displayWithBorders (Size rows columns) pres@Presentation {..} f =
, dsTabStop = maybe 4 A.unFlexibleNum $ psTabStop settings
, dsTheme = fromMaybe Theme.defaultTheme (psTheme settings)
, dsSyntaxMap = pSyntaxMap
, dsResolve = \var -> fromMaybe [] $ HMS.lookup var pVars
}

-- Compute title.
Expand Down Expand Up @@ -323,6 +325,7 @@ prettyBlock ds (Figure _attr blocks) =
-- TODO: the fromPandoc conversion here is weird
prettyBlocks ds blocks

prettyBlock ds (VarBlock var) = prettyBlocks ds $ dsResolve ds var
prettyBlock _ (SpeakerNote _) = mempty
prettyBlock _ (Config _) = mempty

Expand Down Expand Up @@ -404,12 +407,12 @@ type Reference = ([Inline], T.Text, T.Text)
--------------------------------------------------------------------------------
prettyReferences :: DisplaySettings -> [Block] -> [PP.Doc]
prettyReferences ds =
map prettyReference . execWriter . traverse (dftBlock pure tellReference)
map prettyReference . execWriter . dftBlocks (pure . pure) tellReference
where
tellReference :: Inline -> Writer [Reference] Inline
tellReference :: Inline -> Writer [Reference] [Inline]
tellReference inline = do
for_ (toReferenceLink inline) (tell . pure)
pure inline
pure [inline]

prettyReference :: Reference -> PP.Doc
prettyReference (text, target, title) =
Expand All @@ -424,12 +427,12 @@ prettyReferences ds =
<> ")"

newlineToSpace :: [Inline] -> [Inline]
newlineToSpace = runIdentity . traverse (dftInline pure work)
newlineToSpace = runIdentity . dftInlines (pure . pure) work
where
work x = pure $ case x of
SoftBreak -> Space
LineBreak -> Space
_ -> x
SoftBreak -> [Space]
LineBreak -> [Space]
_ -> [x]


--------------------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions lib/Patat/Presentation/Display/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Patat.Presentation.Display.Internal
--------------------------------------------------------------------------------
import Patat.Presentation.Internal (Margins)
import Patat.Presentation.Settings (Wrap)
import Patat.Presentation.Syntax (Block, Var)
import qualified Patat.PrettyPrint as PP
import Patat.Size (Size)
import qualified Patat.Theme as Theme
Expand All @@ -22,6 +23,7 @@ data DisplaySettings = DisplaySettings
, dsMargins :: !Margins
, dsTheme :: !Theme.Theme
, dsSyntaxMap :: !Skylighting.SyntaxMap
, dsResolve :: !(Var -> [Block])
}


Expand Down
2 changes: 1 addition & 1 deletion lib/Patat/Presentation/Fragment.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ fragmentInstructions fs = fromList . concatMap fragmentInstruction . toList
fragmentInstruction Pause = [Pause]
fragmentInstruction (Append []) = [Append []]
fragmentInstruction (Append xs) = fragmentBlocks fs xs
fragmentInstruction (AppendVar v) = [AppendVar v]
fragmentInstruction Delete = [Delete]
fragmentInstruction (ModifyLast f) = map ModifyLast $ fragmentInstruction f

Expand Down Expand Up @@ -69,6 +68,7 @@ fragmentBlock _ block@(Div {}) = [Append [block]]
fragmentBlock _ block@HorizontalRule = [Append [block]]
fragmentBlock _ block@(LineBlock {}) = [Append [block]]
fragmentBlock _ block@(Figure {}) = [Append [block]]
fragmentBlock _ block@(VarBlock {}) = [Append [block]]
fragmentBlock _ block@(SpeakerNote {}) = [Append [block]]
fragmentBlock _ block@(Config {}) = [Append [block]]

Expand Down
33 changes: 1 addition & 32 deletions lib/Patat/Presentation/Instruction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@ module Patat.Presentation.Instruction
, Instruction (..)
, beforePause
, numFragments
, variables

, Fragment (..)
, renderFragment
) where

import Data.Hashable (Hashable)
import qualified Data.HashSet as HS
import Data.List (foldl')
import Patat.Presentation.Syntax

Expand All @@ -48,26 +45,11 @@ fromList = Instructions . go
toList :: Instructions a -> [Instruction a]
toList (Instructions xs) = xs

-- | A variable is like a placeholder in the instructions, something we don't
-- know yet, dynamic content. Currently this is only used for code evaluation.
newtype Var = Var Int deriving (Hashable, Eq, Ord, Show)

-- | Used to generate fresh variables.
newtype VarGen = VarGen Int deriving (Show)

zeroVarGen :: VarGen
zeroVarGen = VarGen 0

freshVar :: VarGen -> (Var, VarGen)
freshVar (VarGen x) = (Var x, VarGen (x + 1))

data Instruction a
-- Pause.
= Pause
-- Append items.
| Append [a]
-- Append the content of a variable.
| AppendVar Var
-- Remove the last item.
| Delete
-- Modify the last block with the provided instruction.
Expand All @@ -77,7 +59,6 @@ data Instruction a
isPause :: Instruction a -> Bool
isPause Pause = True
isPause (Append _) = False
isPause (AppendVar _) = False
isPause Delete = False
isPause (ModifyLast i) = isPause i

Expand All @@ -91,16 +72,10 @@ beforePause n = Instructions . go 0 . unInstructions
go i (Pause : t) = if i >= n then [] else go (i + 1) t
go i (h : t) = h : go i t

variables :: Instructions a -> HS.HashSet Var
variables (Instructions [] ) = mempty
variables (Instructions (AppendVar v : t)) = HS.insert v (variables (Instructions t))
variables (Instructions (ModifyLast i : t)) = variables (Instructions t) <> variables (Instructions [i])
variables (Instructions (_ : t)) = variables (Instructions t)

numFragments :: Instructions a -> Int
numFragments = succ . numPauses

newtype Fragment = Fragment [Block] deriving (Show)
newtype Fragment = Fragment {unFragment :: [Block]} deriving (Show)

renderFragment
:: (Var -> [Block]) -> Instructions Block -> Fragment
Expand All @@ -112,7 +87,6 @@ goBlocks
-> [Block]
goBlocks _ Pause xs = xs
goBlocks _ (Append ys) xs = xs ++ ys
goBlocks resolve (AppendVar v) xs = xs ++ resolve v
goBlocks _ Delete xs = sinit xs
goBlocks resolve (ModifyLast f) xs
| null xs = xs -- Shouldn't happen unless instructions are malformed.
Expand All @@ -125,11 +99,6 @@ goBlock _ (Append ys) block = case block of
BulletList xs -> BulletList $ xs ++ [ys]
OrderedList attr xs -> OrderedList attr $ xs ++ [ys]
_ -> block
goBlock resolve (AppendVar v) block = case block of
-- We can only append to a few specific block types for now.
BulletList xs -> BulletList $ xs ++ [resolve v]
OrderedList attr xs -> OrderedList attr $ xs ++ [resolve v]
_ -> block
goBlock _ Delete block = case block of
-- We can only delete from a few specific block types for now.
BulletList xs -> BulletList $ sinit xs
Expand Down
1 change: 0 additions & 1 deletion lib/Patat/Presentation/Interactive.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ module Patat.Presentation.Interactive

--------------------------------------------------------------------------------
import Data.Char (isDigit)
import Patat.Presentation.Instruction (Var)
import Patat.Presentation.Internal
import Patat.Presentation.Read
import Patat.Presentation.Syntax
Expand Down
6 changes: 5 additions & 1 deletion lib/Patat/Presentation/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,12 @@ activeVars presentation = fromMaybe HS.empty $ do
slide <- getSlide sidx presentation
case slideContent slide of
TitleSlide _ _ -> Nothing
ContentSlide instrs -> pure $ Instruction.variables $
ContentSlide instrs -> pure $
variables $ Instruction.unFragment $
Instruction.renderFragment resolve $
Instruction.beforePause fidx instrs
where
resolve _ = []


--------------------------------------------------------------------------------
Expand Down
1 change: 0 additions & 1 deletion lib/Patat/Presentation/Read.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import qualified Patat.EncodingFallback as EncodingFallback
import qualified Patat.Eval as Eval
import qualified Patat.Presentation.Comments as Comments
import Patat.Presentation.Fragment
import Patat.Presentation.Instruction (VarGen)
import qualified Patat.Presentation.Instruction as Instruction
import Patat.Presentation.Internal
import Patat.Presentation.Syntax
Expand Down
Loading

0 comments on commit 87a5f57

Please sign in to comment.