733e65938bf84c20470de758e657be345e0fd990
[matthijs/master-project/cλash.git] / TranslatorTypes.hs
1 --
2 -- Simple module providing some types used by Translator. These are in a
3 -- separate module to prevent circular dependencies in Pretty for example.
4 --
5 module TranslatorTypes where
6
7 import qualified Control.Monad.State as State
8 import Flatten
9
10 type FuncMap = [(HsFunction, 
11     (FlatFunction))]
12
13 data VHDLSession = VHDLSession {
14   nameCount :: Int,             -- A counter that can be used to generate unique names
15   funcs     :: FuncMap          -- A map from HsFunction to FlatFunction, HWFunction, VHDL Entity and Architecture
16 } deriving (Show)
17
18 -- Add the function to the session
19 addFunc :: HsFunction -> FlatFunction -> VHDLState ()
20 addFunc hsfunc flatfunc = do
21   fs <- State.gets funcs -- Get the funcs element from the session
22   State.modify (\x -> x {funcs = (hsfunc, flatfunc) : fs }) -- Prepend name and f
23
24 type VHDLState = State.State VHDLSession
25
26 -- Makes the given name unique by appending a unique number.
27 -- This does not do any checking against existing names, so it only guarantees
28 -- uniqueness with other names generated by uniqueName.
29 uniqueName :: String -> VHDLState String
30 uniqueName name = do
31   count <- State.gets nameCount -- Get the funcs element from the session
32   State.modify (\s -> s {nameCount = count + 1})
33   return $ name ++ "_" ++ (show count)
34