Skip to content
Open

Wip #10

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
14 changes: 4 additions & 10 deletions src/Lib2.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,8 @@ module Lib2
-- It should match the grammar from Laboratory work #1.
-- Currently it has no constructors but you can introduce
-- as many as needed.
data Query

-- | The instances are needed basically for tests
instance Eq Query where
(==) _ _= False

instance Show Query where
show _ = ""
data Query = Print | Add Int
deriving (Show, Eq)

-- | Parses user's input.
-- The function must have tests.
Expand All @@ -28,12 +22,12 @@ parseQuery _ = Left "Not implemented 2"
-- | An entity which represents your program's state.
-- Currently it has no constructors but you can introduce
-- as many as needed.
data State
data State = Sum Int

-- | Creates an initial program's state.
-- It is called once when the program starts.
emptyState :: State
emptyState = error "Not implemented 1"
emptyState = Sum 0

-- | Updates a state according to a query.
-- This allows your program to share the state
Expand Down
43 changes: 41 additions & 2 deletions src/Lib3.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE EmptyCase #-}
module Lib3
( stateTransition,
StorageOp (..),
Expand All @@ -9,9 +10,10 @@ module Lib3
renderStatements
) where


import Control.Concurrent ( Chan )
import Control.Concurrent.STM(STM, TVar)
import qualified Lib2
import Control.Concurrent.STM (STM, TVar, atomically, readTVar, writeTVar, readTVarIO, modifyTVar)

data StorageOp = Save String (Chan ()) | Load (Chan String)
-- | This function is started from main
Expand Down Expand Up @@ -70,4 +72,41 @@ renderStatements _ = error "Not implemented 5"
-- is stored in transactinal variable
stateTransition :: TVar Lib2.State -> Command -> Chan StorageOp ->
IO (Either String (Maybe String))
stateTransition _ _ ioChan = return $ Left "Not implemented 6"
stateTransition state command ioChan =
case command of
StatementCommand sc ->
updateState state sc
SaveCommand -> do
jakie <- newChan
st <- readTVarIO state
writeChan ioChan (Save (renderStatements (marshallState st)) jakie)
_ <- readChan jakie
return $ Right Nothing
LoadCommand -> do
jakie <- newChan
writeChan ioChan (Load jakie)
file <- readChan jakie
case parseStatements file of
Left e -> return $ Left e
Right (st, "") -> updateState state st
Right _ -> return $ Left "Statements are not fully parsed"

updateState :: TVar Lib2.State -> Statements ->
IO (Either String (Maybe String))
updateState state (Single q) = atomically $ updateState' state [q]
updateState state (Batch b) = atomically $ updateState' state b

updateState' :: TVar Lib2.State -> [Lib2.Query] -> STM (Either String (Maybe String))
updateState' _ [] = return $ Right Nothing
updateState' state (h:t) = do
case h of
Lib2.Print -> do
Lib2.Sum s <- readTVar state
us <- updateState' state t
case us of
Left e -> return $ Left e
Right Nothing -> return $ Right $ Just $ show s
Right (Just next) -> return $ Right $ Just $ concat [show s, "\n", next]
Lib2.Add v -> do
modifyTVar state (\(Lib2.Sum s) -> Lib2.Sum (s + v))
return $ Right Nothing