From 203259b0ae275054da47075be18cfe25d7d7ed7a Mon Sep 17 00:00:00 2001 From: Bernhard Urban Date: Sun, 31 Oct 2010 00:30:08 +0200 Subject: [PATCH] 3a_asm: parse a comment \o/ --- 3a_asm/DTFormat.hs | 4 ++- 3a_asm/Main.hs | 64 +++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 60 insertions(+), 8 deletions(-) diff --git a/3a_asm/DTFormat.hs b/3a_asm/DTFormat.hs index 819e083..aad67e0 100644 --- a/3a_asm/DTFormat.hs +++ b/3a_asm/DTFormat.hs @@ -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 diff --git a/3a_asm/Main.hs b/3a_asm/Main.hs index d83e022..17ffc86 100644 --- a/3a_asm/Main.hs +++ b/3a_asm/Main.hs @@ -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" -- 2.25.1