3a_asm: 'sum.s' is parseable now :)
[calu.git] / 3a_asm / Main.hs
index df442a41d523869416f7db400302bf85c058ee19..2515674cf1444255fe6604d116ff55cc0ea7aa09 100644 (file)
@@ -1,7 +1,6 @@
 -- as for deep thoughts ISA
 -----------------------------------------------------------------------------
 
-module Main where
 
 import DT
 import DTFormat
@@ -31,6 +30,20 @@ main = do
        sequence_ [printf "%10s @ 0x%08x\n" l a | (l,a) <- (reverse dict)]
        printf "\nparsed asm:\n"
        sequence_ [printf "%s" (show x) | x <- formatedsrc]
+       let parsed = parseInstr dict formatedsrc
+       printf "\nafter parsing the instructions:\n"
+       sequence_ [printf "%s" (show x) | x <- parsed]
+
+
+parseInstr :: [DictLabel] -> [DTF] -> [DTF]
+parseInstr _ [] = []
+parseInstr dict ((DTF_InstrToParse a instr c l s):xs) =
+       (DTF_Instr a bytecode c l s):(parseInstr dict xs)
+       where
+       bytecode = case (parse (instruction dict) "" (instr++"\n")) of
+               Left err -> error ("couldn't parse Instruction: " ++ instr ++ "\n" ++ show err)
+               Right x -> x
+parseInstr dict (x:xs) = x:(parseInstr dict xs)
 
 
 type Counter = Word32
@@ -39,22 +52,6 @@ inc :: Counter -> Counter
 inc = ((+) 4)
 
 
-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
-
-
 convertDTF :: [(Int,String)] -> DT_State -> Counter -> Counter -> [DictLabel] -> ([DictLabel], [DTF])
 convertDTF [] _ _ _ d = (d,[])
 convertDTF ((lno,str):xs) state datacnt instrcnt dict = (newdict, (actlist newdtf))
@@ -145,11 +142,6 @@ parseConst = do
        let val = read str
        return $ val
 
-parseMySpaces :: Parser String
-parseMySpaces = do
-       ret <- many $ oneOf "\t "
-       return $ ret
-
 -- teh pars0rs
 lf_data = do
        l <- try (parseLabel) <|> string ""
@@ -196,3 +188,4 @@ lf_stext = do
        string ".text"
        parseMySpaces
        return $ DTF_State InText
+