3a_asm: using Expr in both stages
[calu.git] / 3a_asm / Main.hs
index fa5a12c386a5315ddc8d0a6704747fa2aa8d495b..2e99807e06f0cf10a41347045dcf3d427becbeb3 100644 (file)
@@ -1,9 +1,8 @@
 -- as for deep thoughts ISA
 -----------------------------------------------------------------------------
-
-
 import DT
 import DTFormat
+import Expr_eval
 
 import Control.Applicative hiding ((<|>),many)
 
@@ -25,9 +24,9 @@ main = do
        args <- getArgs
        content <- getContents
        let src = (filter (((/=) "") . snd) $ (zip [1..] (lines content)))
-       let (dict,formatedsrc) = convertDTF src NoState 0x00 0x00 []
+       let (dict,formatedsrc) = convertDTF src NoState 0x00 0x00 [("start_",0x00)]
        printf "\nlabels:\n"
-       sequence_ [printf "%10s @ 0x%08x\n" l a | (l,a) <- (reverse dict)]
+       sequence_ [printf "%20s @ 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
@@ -87,7 +86,7 @@ convertDTF ((lno,str):xs) state datacnt instrcnt dict = (newdict, (actlist newdt
        ndict (DTF_Define l v _) = dict `add_elem` (l,v)
        ndict _ = dict
 
-       newdtf = case (parse parseDTFLine "" (str++"\n")) of
+       newdtf = case (parse (parseDTFLine dict) "" (str++"\n")) of
                Left err -> error ("couldn't parse line " ++ (show lno) ++ ": " ++ (show err))
                Right (DTF_SectionToDet _ v c l s) ->
                        case state of
@@ -111,12 +110,14 @@ convertDTF ((lno,str):xs) state datacnt instrcnt dict = (newdict, (actlist newdt
 
 testDTF :: String -> IO ()
 testDTF input =
-       case (parse parseDTFLine "" (input++"\n")) of
+       case (parse (parseDTFLine dict) "" (input++"\n")) of
                Left err -> do { putStr "failz ;(\n"; print err}
                Right x -> do { print x }
+       where
+       dict = [("lolz", 0x1337), ("rofl", 0xaaaa)]
 
-parseDTFLine :: Parser DTF
-parseDTFLine = foldl1 (<|>) (fmap try lineFormats) <* char '\n'
+parseDTFLine :: [DictElem] -> Parser DTF
+parseDTFLine dict = foldl1 (<|>) (fmap (\x -> try (x dict)) lineFormats) <* char '\n'
 
 lineFormats = [lf_define, lf_sdata, lf_stext, lf_org, lf_data, lf_comment, lf_toparse, lf_label]
 
@@ -140,68 +141,63 @@ parseComment = do
        comment <- many $ noneOf "\n"
        return $ comment
 
-parseConst :: Parser Word32
-parseConst = do
-       skipMany space
-       -- TODO: only decimal and hex (since this is supported by read)
-       -- TODO: howto check too big values? atm they get truncated
-       str <- try(do pref <- string "0x"; z <- many1 hexDigit; return $ (pref ++ z)) <|> (many1 digit)
-       let val = read str
-       return $ val
+parseConst :: [DictElem] -> Parser Word32
+parseConst d = expr d
 
 -- teh pars0rs
-lf_data = do
+lf_data = do
        l <- try (parseLabel) <|> string ""
        skipMany space
        fill <- string ".fill "
        -- TODO: atm 32bit imm only
        code <- many1 $ noneOf "\n;"
        -- TODO: this is quite ugly here :/
-       let (Right val) = parse parseConst "" code
+       let (Right val) = parse (parseConst d) "" code
        comment <- try(parseComment) <|> parseMySpaces
        return $ DTF_SectionToDet 0 val (fill ++ code) l comment
 
-lf_comment = do
+lf_comment = do
        comment <- parseComment
        return $ DTF_Comment comment
 
-lf_label = do
+lf_label = do
        l <- parseLabel
        comment <- try(parseComment) <|> parseMySpaces
        return $ DTF_Label l comment 0
 
-lf_toparse = do
+lf_toparse = do
        l <- try(parseLabel) <|> string ""
        skipMany space
        code <- many1 $ noneOf "\n;"
        comment <- try(parseComment) <|> parseMySpaces
        return $ DTF_InstrToParse 0 code code l comment
 
-lf_org = do
+lf_org = do
        skipMany space
        string ".org"
-       val <- parseConst
+       val <- parseConst d
        parseMySpaces
        return $ DTF_Org val
 
-lf_sdata = do
+lf_sdata = do
        skipMany space
        string ".data"
        parseMySpaces
        return $ DTF_State InData
 
-lf_stext = do
+lf_stext = do
        skipMany space
        string ".text"
        parseMySpaces
        return $ DTF_State InText
 
-lf_define = do
+lf_define = do
        skipMany space
        string ".define"
        parseMySpaces
        id <- parseIdent
-       parseMySpaces; char ','; parseMySpaces
-       ret <- parseConst
+       char ','
+       -- TODO: expressions with (expr) do not work ;(
+       ret <- parseConst d
        comment <- try(parseComment) <|> parseMySpaces
        return $ DTF_Define id ret comment