module DTFormat where import Data.Word import Text.Printf import Text.Parsec import Text.Parsec.String import Text.Parsec.Combinator import Control.Monad data DT_State = NoState | InData | InText deriving (Show,Eq) type Address = Word32 type Value = Word32 type ValueToParse = String type Code = String type Label = String type Comment = String data DTF = DTF_Data Address Value Code Label Comment | -- 0;... DTF_Instr Address Value Code Label Comment | -- 1;... DTF_Comment Comment | -- 2;... DTF_Label Label Comment Address | -- 3;... -- types for intern processing DTF_InstrToParse Address ValueToParse Code Label Comment | DTF_SectionToDet Address Value Code Label Comment | DTF_Org Address | DTF_State DT_State instance Show (DTF) where showsPrec n = showsDTF showsDTF :: DTF -> ShowS showsDTF (DTF_Data a v c l s) = (++) (datins "0" a v c l s) showsDTF (DTF_Instr a v c l s) = (++) (datins "1" a v c l s) showsDTF (DTF_Comment c) = (++) (printf "2;%s\n" c) showsDTF (DTF_Label l c _) = (++) (printf "3;%s;%s\n" l c) showsDTF (DTF_InstrToParse a v c l s) = (++) (printf "itp;%08x;%s;%s;%s;%s\n" a v c l s) showsDTF (DTF_SectionToDet a v c l s) = (++) (datins "std" a v c l s) showsDTF (DTF_Org a) = (++) (printf "org;%08x\n" a) showsDTF (DTF_State s) = (++) (printf "sta;%s\n" (show s)) datins :: String -> Address -> Value -> Code -> Label -> Comment -> String datins = printf "%s;%08x;%08x;%s;%s;%s\n" type DictLabel = (String,Word32) get_label :: String -> [DictLabel] -> Maybe Word32 get_label = lookup add_label :: [DictLabel] -> (String,Word32) -> [DictLabel] add_label dic (s,w) | s == "" = dic -- ignore empty string | already_in = error ("Label " ++ s ++ " already exists") | otherwise = (s,w):dic where already_in = case (get_label s dic) of Just _ -> True Nothing -> False -- some common functions parseMySpaces :: Parser String parseMySpaces = do ret <- many $ oneOf "\t " return $ ret