Format more numbers
This commit is contained in:
parent
75304e8027
commit
23bd495521
3 changed files with 53 additions and 11 deletions
|
|
@ -1,19 +1,54 @@
|
||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
|
||||||
module Mima.Format.Common
|
module Mima.Format.Common
|
||||||
( toHex
|
( toBin
|
||||||
, fixedWidthHex
|
, toDec
|
||||||
, fixedWidthHexAddress
|
, toHex
|
||||||
|
, chunkedBy
|
||||||
|
, chunkyBin
|
||||||
|
, chunkyDec
|
||||||
|
, chunkyHex
|
||||||
|
, fixWidthBin
|
||||||
|
, fixWidthDec
|
||||||
|
, fixWidthHex
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
import Numeric
|
import Numeric
|
||||||
|
|
||||||
import Mima.Word
|
toBin :: (Integral a, Show a) => a -> T.Text
|
||||||
|
toBin a
|
||||||
|
| a < 0 = "-" <> toBin (- a)
|
||||||
|
toBin 0 = "0"
|
||||||
|
toBin a = T.reverse $ T.pack $ toBin' a
|
||||||
|
where
|
||||||
|
toBin' :: (Integral a, Show a) => a -> String
|
||||||
|
toBin' 0 = []
|
||||||
|
toBin' n = (if n `mod` 2 == 0 then '0' else '1') : toBin' (n `div` 2)
|
||||||
|
|
||||||
|
toDec :: (Integral a, Show a ) => a -> T.Text
|
||||||
|
toDec = T.pack . show
|
||||||
|
|
||||||
toHex :: (Integral a, Show a) => a -> T.Text
|
toHex :: (Integral a, Show a) => a -> T.Text
|
||||||
toHex a = T.pack $ showHex a ""
|
toHex a = T.pack $ showHex a ""
|
||||||
|
|
||||||
fixedWidthHex :: (Integral a, Show a) => Int -> a -> T.Text
|
chunkedBy :: T.Text -> Int -> T.Text -> T.Text
|
||||||
fixedWidthHex n = T.justifyRight n '0' . toHex
|
chunkedBy sep n = T.intercalate sep . T.chunksOf n
|
||||||
|
|
||||||
fixedWidthHexAddress :: MimaAddress -> T.Text
|
chunkyBin :: T.Text -> T.Text
|
||||||
fixedWidthHexAddress = fixedWidthHex 5
|
chunkyBin = chunkedBy " " 4
|
||||||
|
|
||||||
|
chunkyDec :: T.Text -> T.Text
|
||||||
|
chunkyDec = chunkedBy "'" 3
|
||||||
|
|
||||||
|
chunkyHex :: T.Text -> T.Text
|
||||||
|
chunkyHex = chunkedBy " " 2
|
||||||
|
|
||||||
|
fixWidthBin :: Int -> T.Text -> T.Text
|
||||||
|
fixWidthBin n = T.justifyRight n '0'
|
||||||
|
|
||||||
|
fixWidthDec :: Int -> T.Text -> T.Text
|
||||||
|
fixWidthDec n = T.justifyRight n ' '
|
||||||
|
|
||||||
|
fixWidthHex :: Int -> T.Text -> T.Text
|
||||||
|
fixWidthHex n = T.justifyRight n '0'
|
||||||
|
|
|
||||||
|
|
@ -10,14 +10,18 @@ import qualified Data.Text as T
|
||||||
|
|
||||||
import Mima.Flag
|
import Mima.Flag
|
||||||
import Mima.Format.Common
|
import Mima.Format.Common
|
||||||
|
import Mima.Word
|
||||||
|
|
||||||
|
fAddress :: MimaAddress -> T.Text
|
||||||
|
fAddress = fixWidthHex 5 . toHex
|
||||||
|
|
||||||
fFlagSet :: Set.Set Char -> T.Text
|
fFlagSet :: Set.Set Char -> T.Text
|
||||||
fFlagSet = T.pack . Set.toAscList
|
fFlagSet = T.pack . Set.toAscList
|
||||||
|
|
||||||
fRange :: AddressRange -> T.Text
|
fRange :: AddressRange -> T.Text
|
||||||
fRange r
|
fRange r
|
||||||
| lower == upper = fixedWidthHexAddress lower
|
| lower == upper = fAddress lower
|
||||||
| otherwise = fixedWidthHexAddress lower <> "-" <> fixedWidthHexAddress upper
|
| otherwise = fAddress lower <> "-" <> fAddress upper
|
||||||
where
|
where
|
||||||
lower = lowerAddress r
|
lower = lowerAddress r
|
||||||
upper = upperAddress r
|
upper = upperAddress r
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,9 @@ import qualified Data.Text as T
|
||||||
import Mima.Format.Common
|
import Mima.Format.Common
|
||||||
import Mima.Word
|
import Mima.Word
|
||||||
|
|
||||||
|
fAddress :: MimaAddress -> T.Text
|
||||||
|
fAddress = fixWidthHex 5 . toHex
|
||||||
|
|
||||||
type LabelName = T.Text
|
type LabelName = T.Text
|
||||||
|
|
||||||
combineByAddress :: Map.Map LabelName MimaAddress -> Map.Map MimaAddress (Set.Set LabelName)
|
combineByAddress :: Map.Map LabelName MimaAddress -> Map.Map MimaAddress (Set.Set LabelName)
|
||||||
|
|
@ -24,7 +27,7 @@ fLabels :: Set.Set LabelName -> T.Text
|
||||||
fLabels = T.intercalate " " . Set.toAscList
|
fLabels = T.intercalate " " . Set.toAscList
|
||||||
|
|
||||||
fLine :: (MimaAddress, Set.Set LabelName) -> T.Text
|
fLine :: (MimaAddress, Set.Set LabelName) -> T.Text
|
||||||
fLine (a, s) = fixedWidthHexAddress a <> ": " <> fLabels s <> "\n"
|
fLine (a, s) = fAddress a <> ": " <> fLabels s <> "\n"
|
||||||
|
|
||||||
formatSymbolFile :: Map.Map LabelName MimaAddress -> T.Text
|
formatSymbolFile :: Map.Map LabelName MimaAddress -> T.Text
|
||||||
formatSymbolFile = mconcat . map fLine . Map.assocs . combineByAddress
|
formatSymbolFile = mconcat . map fLine . Map.assocs . combineByAddress
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue