3a_asm: .define support
[calu.git] / 3a_asm / Main.hs
index 2515674cf1444255fe6604d116ff55cc0ea7aa09..fa5a12c386a5315ddc8d0a6704747fa2aa8d495b 100644 (file)
@@ -35,12 +35,12 @@ main = do
        sequence_ [printf "%s" (show x) | x <- parsed]
 
 
-parseInstr :: [DictLabel] -> [DTF] -> [DTF]
+parseInstr :: [DictElem] -> [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
+       bytecode = case (parse (instruction (a,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)
@@ -52,12 +52,13 @@ inc :: Counter -> Counter
 inc = ((+) 4)
 
 
-convertDTF :: [(Int,String)] -> DT_State -> Counter -> Counter -> [DictLabel] -> ([DictLabel], [DTF])
+convertDTF :: [(Int,String)] -> DT_State -> Counter -> Counter -> [DictElem] -> ([DictElem], [DTF])
 convertDTF [] _ _ _ d = (d,[])
 convertDTF ((lno,str):xs) state datacnt instrcnt dict = (newdict, (actlist newdtf))
        where
        actlist (DTF_Org _) = next
        actlist (DTF_State _) = next
+       actlist (DTF_Define _ _ _) = next
        actlist y = y:next
 
        (newdict,next) = convertDTF xs (nstate newdtf) (ndatacnt newdtf) (ninstrcnt newdtf) (ndict newdtf)
@@ -78,11 +79,12 @@ convertDTF ((lno,str):xs) state datacnt instrcnt dict = (newdict, (actlist newdt
        nstate (DTF_State s) = s
        nstate _ = state
 
-       ndict (DTF_Label l _ a) = dict `add_label` (l,a)
-       ndict (DTF_SectionToDet a _ _ l _) = dict `add_label` (l,a)
-       ndict (DTF_InstrToParse a _ _ l _) = dict `add_label` (l,a)
-       ndict (DTF_Data a _ _ l _) = dict `add_label` (l,a)
-       ndict (DTF_Instr a _ _ l _) = dict `add_label` (l,a)
+       ndict (DTF_Label l _ a) = dict `add_elem` (l,a)
+       ndict (DTF_SectionToDet a _ _ l _) = dict `add_elem` (l,a)
+       ndict (DTF_InstrToParse a _ _ l _) = dict `add_elem` (l,a)
+       ndict (DTF_Data a _ _ l _) = dict `add_elem` (l,a)
+       ndict (DTF_Instr a _ _ l _) = dict `add_elem` (l,a)
+       ndict (DTF_Define l v _) = dict `add_elem` (l,v)
        ndict _ = dict
 
        newdtf = case (parse parseDTFLine "" (str++"\n")) of
@@ -103,7 +105,7 @@ convertDTF ((lno,str):xs) state datacnt instrcnt dict = (newdict, (actlist newdt
                                NoState -> error "missing .data or .text"
                                InData -> (DTF_Label l c datacnt)
                                InText -> (DTF_Label l c instrcnt)
-               Right z -> z -- DTF_Comment, DTF_State
+               Right z -> z -- DTF_Comment, DTF_State, DTF_Define
 
 
 
@@ -116,15 +118,20 @@ testDTF input =
 parseDTFLine :: Parser DTF
 parseDTFLine = foldl1 (<|>) (fmap try lineFormats) <* char '\n'
 
-lineFormats = [lf_sdata, lf_stext, lf_org, lf_data, lf_comment, lf_toparse, lf_label]
+lineFormats = [lf_define, lf_sdata, lf_stext, lf_org, lf_data, lf_comment, lf_toparse, lf_label]
 
 -- helper
+parseIdent :: Parser String
+parseIdent = do
+       ident <- letter
+       idents <- many $ (letter <|> digit <|> char '_')
+       return $ (ident:idents)
+
 parseLabel :: Parser String
 parseLabel = do
-       label <- letter 
-       labels <- many $ (letter <|> digit <|> char '_')
+       l <- parseIdent
        char ':'
-       return $ (label:labels)
+       return $ l
 
 parseComment :: Parser String
 parseComment = do
@@ -189,3 +196,12 @@ lf_stext = do
        parseMySpaces
        return $ DTF_State InText
 
+lf_define = do
+       skipMany space
+       string ".define"
+       parseMySpaces
+       id <- parseIdent
+       parseMySpaces; char ','; parseMySpaces
+       ret <- parseConst
+       comment <- try(parseComment) <|> parseMySpaces
+       return $ DTF_Define id ret comment