3a_asm: parse a comment \o/
authorBernhard Urban <lewurm@gmail.com>
Sat, 30 Oct 2010 22:30:08 +0000 (00:30 +0200)
committerBernhard Urban <lewurm@gmail.com>
Sat, 30 Oct 2010 22:30:08 +0000 (00:30 +0200)
3a_asm/DTFormat.hs
3a_asm/Main.hs

index 819e083afe3a8e6e493c6c2ffdd9c616cc43dd26..aad67e01bab02379c123a573f9451e2942d1ceba 100644 (file)
@@ -15,7 +15,9 @@ data DTF =
        DTF_Instr Address Value Code Label Comment |
        DTF_Comment Comment |
        DTF_Label Label |
-       DTF_ToParse Address ValueToParse Code Label Comment
+       -- types for intern processing
+       DTF_ToParse Address ValueToParse Code Label Comment |
+       DTF_Org Address
 
 instance Show (DTF) where
        showsPrec n = showsDTF
index d83e022b94bd9c64c20a3cdae7562dace8caa3a6..17ffc864871add022ce59ba8c4df954df600e823 100644 (file)
@@ -16,6 +16,7 @@ import Text.Parsec.String
 import Text.Parsec.Combinator
 import qualified Data.Map as M
 import Data.List
+import Data.Word
 import qualified Data.ByteString.Lazy as BL
 -- import Data.Binary.Put
 
@@ -23,9 +24,10 @@ main :: IO ()
 main = do
        args <- getArgs
        content <- getContents
-       let src = convertDTF (filter ((/=) "") $ lines content)
+       let src = (filter (((/=) "") . snd) $ (zip [1..] (lines content)))
+       let (dict,formatedsrc) = convertDTF src 0x00 0x00 init_labels
        print args
-       print src
+       print formatedsrc
 
 {-
        case runParser DT.parseInstructions () "stdin" src of
@@ -34,9 +36,57 @@ main = do
                        sequence_ [printf "0x%08X\n" x | x <- val]
 -}
 
---                           data    instr
--- convertDTF :: [String] -> Dict -> Dict -> ([DTF],Labels)
+type Counter = Word32
 
-convertDTF :: [String] -> [DTF]
-convertDTF [] = []
-convertDTF (str:xs) = (DTF_Comment str):(convertDTF xs)
+inc :: Counter -> Counter
+inc = ((+) 4)
+
+setabsolute :: Counter -> Counter
+setabsolute x = x
+
+
+type DictLabel = (String -> Word32)
+
+init_labels :: DictLabel
+init_labels _ = 0xffffffff
+
+add_label :: DictLabel -> (String,Word32) -> DictLabel
+add_label dic (s,w)
+       | dic s /= 0xffffffff = error ("Label " ++ s ++ " already exists")
+       | otherwise = newdict
+       where
+       newdict str
+               | str == s = w
+               | otherwise = dic str
+
+
+convertDTF :: [(Int,String)] -> Counter -> Counter -> DictLabel -> (DictLabel, [DTF])
+convertDTF [] _ _ d = (d,[])
+convertDTF ((lno,str):xs) datacnt instrcnt dict =
+       (newdict, (DTF_Comment str):next)
+       where
+       (newdict,next) = convertDTF xs (inc datacnt) (inc instrcnt) dict
+
+testDTF :: String -> IO ()
+testDTF input =
+       case (parse parseDTFLine "" (input++"\n")) of
+               Left err -> do { putStr "failz"; print err}
+               Right x -> do { print x }
+
+parseDTFLine :: Parser DTF
+parseDTFLine = foldl1 (<|>) (fmap try lineFormats) <* char '\n'
+
+lineFormats = [lf_data, lf_comment, lf_label, lf_toparse, lf_org]
+
+lf_data = do string "data"; return $ DTF_Comment "lolz0"
+
+lf_comment = do
+       skipMany space
+       char ';'
+       comment <- many $ noneOf ['\n']
+       newline
+       return $ DTF_Comment comment
+
+lf_label = do string "label"; return $ DTF_Comment "lolz1"
+lf_toparse = do string "toparse"; return $ DTF_Comment "lolz2"
+lf_org = do string "org"; return $ DTF_Comment "lolz3"