module Synthesizer.LLVM.Debug.Counter where

import qualified Control.Monad.Trans.Reader as R
import qualified Data.IORef as IORef
import qualified Data.List.HT as ListHT


newtype T ident = Cons Int
   deriving (Eq, Ord)

instance Enum (T ident) where
   fromEnum (Cons n) = n
   toEnum n = (Cons n)

format :: Int -> T ident -> String
format pad (Cons n) =
   ListHT.padLeft '0' pad (show n)

new :: IO (IORef.IORef (T ident))
new =
   IORef.newIORef (Cons 0)

{-
wrap :: (Int -> IO ()) -> R.ReaderT (T ident) IO ()
wrap act =
   R.ReaderT $ \(Cons n) -> act n
-}

with :: IORef.IORef (T ident) -> R.ReaderT (T ident) IO () -> IO ()
with cnt act = do
   R.runReaderT act =<< IORef.readIORef cnt
   IORef.modifyIORef cnt succ