6 import Text.Parsec.String
7 import Text.Parsec.Combinator
8 import Text.Parsec.Expr
9 import Text.ParserCombinators.Parsec.Token
10 import Text.ParserCombinators.Parsec.Expr
13 data DT_State = NoState | InData | InText deriving (Show,Eq)
17 type ValueToParse = String
23 DTF_Data Address Value Code Label Comment | -- 0;...
24 DTF_Instr Address Value Code Label Comment | -- 1;...
25 DTF_Comment Comment | -- 2;...
26 DTF_Label Label Comment Address | -- 3;...
27 -- types for intern processing
28 DTF_InstrToParse Address ValueToParse Code Label Comment |
29 DTF_SectionToDet Address Value Code Label Comment |
31 DTF_Define Label Value Comment |
34 instance Show (DTF) where
35 showsPrec n = showsDTF
37 showsDTF :: DTF -> ShowS
38 showsDTF (DTF_Data a v c l s) = (++) (datins "0" a v c l s)
39 showsDTF (DTF_Instr a v c l s) = (++) (datins "1" a v c l s)
40 showsDTF (DTF_Comment c) = (++) (printf "2;%s\n" c)
41 showsDTF (DTF_Label l c _) = (++) (printf "3;%s;%s\n" l c)
42 showsDTF (DTF_InstrToParse a v c l s) = (++) (printf "itp;%08x;%s;%s;%s;%s\n" a v c l s)
43 showsDTF (DTF_SectionToDet a v c l s) = (++) (datins "std" a v c l s)
44 showsDTF (DTF_Org a) = (++) (printf "org;%08x\n" a)
45 showsDTF (DTF_Define l a c) = (++) (printf "def;%s;%08X;%s\n" l a c)
46 showsDTF (DTF_State s) = (++) (printf "sta;%s\n" (show s))
48 datins :: String -> Address -> Value -> Code -> Label -> Comment -> String
49 datins = printf "%s;%08x;%08x;%s;%s;%s\n"
52 -- datastructure for managing labels and defines
53 type Dict = (Address,[DictElem])
54 type DictElem = (String,Word32)
56 get_elem :: String -> [DictElem] -> Word32
57 get_elem s dict = case (lookup s dict) of
58 Nothing -> error ("unknown label or define: \"" ++ s ++ "\"")
61 add_elem :: [DictElem] -> (String,Word32) -> [DictElem]
63 | s == "" = dic -- ignore empty string
64 | already_in = error ("Label or define \"" ++ s ++ "\" already exists")
65 | otherwise = (s,w):dic
67 already_in = case (lookup s dic) of
71 -- some common functions
72 parseMySpaces :: Parser String
74 ret <- many $ oneOf "\t "