diff --git a/src/Mima/Format/Common.hs b/src/Mima/Format/Common.hs new file mode 100644 index 0000000..4e39a18 --- /dev/null +++ b/src/Mima/Format/Common.hs @@ -0,0 +1,19 @@ +module Mima.Format.Common + ( toHex + , fixedWidthHex + , fixedWidthHexAddress + ) where + +import qualified Data.Text as T +import Numeric + +import Mima.Word + +toHex :: (Integral a, Show a) => a -> T.Text +toHex a = T.pack $ showHex a "" + +fixedWidthHex :: (Integral a, Show a) => Int -> a -> T.Text +fixedWidthHex n = T.justifyRight n '0' . toHex + +fixedWidthHexAddress :: MimaAddress -> T.Text +fixedWidthHexAddress = fixedWidthHex 5 diff --git a/src/Mima/Format/FlagFile.hs b/src/Mima/Format/FlagFile.hs new file mode 100644 index 0000000..c5556ab --- /dev/null +++ b/src/Mima/Format/FlagFile.hs @@ -0,0 +1,29 @@ +{-# LANGUAGE OverloadedStrings #-} + +module Mima.Format.FlagFile + ( formatFlagFile + ) where + +import qualified Data.Map as Map +import qualified Data.Set as Set +import qualified Data.Text as T + +import Mima.Flag +import Mima.Format.Common + +fFlagSet :: Set.Set Char -> T.Text +fFlagSet = T.pack . Set.toAscList + +fRange :: AddressRange -> T.Text +fRange r + | lower == upper = fixedWidthHexAddress lower + | otherwise = fixedWidthHexAddress lower <> "-" <> fixedWidthHexAddress upper + where + lower = lowerAddress r + upper = upperAddress r + +fLine :: (AddressRange, Set.Set Char) -> T.Text +fLine (r, s) = fRange r <> ": " <> fFlagSet s <> "\n" + +formatFlagFile :: AllFlags -> T.Text +formatFlagFile = mconcat . map fLine . Map.assocs