Format flag file

This commit is contained in:
Joscha 2019-11-18 09:22:35 +00:00
parent f3c7cdf8b3
commit 5b172ad57f
2 changed files with 48 additions and 0 deletions

19
src/Mima/Format/Common.hs Normal file
View file

@ -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

View file

@ -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