diff --git a/app/Main.hs b/app/Main.hs index 372ef30..2535c30 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -1,7 +1,6 @@ module Main where import System.Environment (getArgs) -import System.IO import Control.Exception (catch, IOException) import Text.Show.Pretty (ppShow) @@ -12,15 +11,19 @@ main = do -- Get the command-line arguments args <- getArgs - -- Check if a file name is provided + -- Check if the input and output file names are provided case args of - (filePath:_) -> do - -- Attempt to read the file - contents <- catch (readFile filePath) handleReadError - putStrLn $ ppShow $ Haskellator.runParser contents filePath - [] -> putStrLn "cabal run Haskellator -- " - -- putStrLn $ ppShow Haskellator.val + (inputFilePath:outputFilePath:_) -> do + contents <- catch (readFile inputFilePath) handleReadError + let output = ppShow $ Haskellator.runParser contents inputFilePath + catch (writeFile outputFilePath output) handleWriteError + putStrLn $ "Output written to " ++ outputFilePath + _ -> putStrLn "Usage: cabal run rtlil-parse -- " -- Handle potential file reading errors handleReadError :: IOException -> IO String -handleReadError _ = return "Error: Could not read the file." +handleReadError _ = return "Error: Could not read the input file." + +-- Handle potential file writing errors +handleWriteError :: IOException -> IO () +handleWriteError _ = putStrLn "Error: Could not write to the output file." \ No newline at end of file diff --git a/src/Haskellator.hs b/src/Haskellator.hs index 5232727..24fdf42 100644 --- a/src/Haskellator.hs +++ b/src/Haskellator.hs @@ -1,9 +1,3 @@ -module Haskellator( - val, - runParser - ) where +module Haskellator(runParser) where - import RTLILParser.Parser( - val, - runParser, - ) \ No newline at end of file + import RTLILParser.Parser(runParser) \ No newline at end of file diff --git a/src/RTLILParser/AST.hs b/src/RTLILParser/AST.hs index b6bcdb2..bdece38 100644 --- a/src/RTLILParser/AST.hs +++ b/src/RTLILParser/AST.hs @@ -43,7 +43,7 @@ module RTLILParser.AST ( CaseBodyVariants(..), CaseBody(..), -- Syncs - Sync(..), SyncStmt(..), SyncType(..), UpdateStmt(..) + Sync(..), SyncStmt(..), SyncType(..), UpdateStmtVariants(..) ) where @@ -174,7 +174,7 @@ data CaseBodyVariants = CaseBodySwitchVariant Switch data CaseBody = CaseBody [AssignStmt] [Switch] deriving (Show) -- Syncs -data Sync = Sync SyncStmt [UpdateStmt] deriving (Show) +data Sync = Sync SyncStmt [UpdateStmtVariants] deriving (Show) data SyncStmt = SigSpecPredicated SigSpec SyncType | Global | Init @@ -186,4 +186,6 @@ data SyncType = Low | Negedge | Edge deriving (Show) -data UpdateStmt = UpdateStmt DestSigSpec SrcSigSpec deriving (Show) \ No newline at end of file +data UpdateStmtVariants = UpdateStmt DestSigSpec SrcSigSpec + | MemWrStmt Id SigSpec SigSpec SigSpec Constant [AttrStmt] + deriving (Show) \ No newline at end of file diff --git a/src/RTLILParser/Parser.hs b/src/RTLILParser/Parser.hs index 6d10acf..f546b0c 100644 --- a/src/RTLILParser/Parser.hs +++ b/src/RTLILParser/Parser.hs @@ -1,7 +1,4 @@ -module RTLILParser.Parser( - RTLILParser.Parser.runParser, - a, - val) where +module RTLILParser.Parser(RTLILParser.Parser.runParser) where import Control.Monad (void) import Text.Parsec @@ -52,7 +49,7 @@ import RTLILParser.AST ( CaseBodyVariants(..), CaseBody(..), -- Syncs - Sync(..), SyncStmt(..), SyncType(..), UpdateStmt(..) + Sync(..), SyncStmt(..), SyncType(..), UpdateStmtVariants(..) ) import RTLILParser.Primitives( pWs @@ -68,7 +65,7 @@ import RTLILParser.Primitives( -- taken from: https://yosyshq.readthedocs.io/projects/yosys/en/0.47/appendix/rtlil_text.html -- parsers below are split int sections from the above link -runParser :: String -> SourceName -> File +-- runParser :: String -> SourceName -> File runParser str filename = case parse pFile filename str of Left err -> error $ show err Right val -> val @@ -159,7 +156,6 @@ pModule = p name where moduleStmt <- pModuleStmt moduleBody <- pModuleBody pModuleEndStmt - -- return $ Module moduleStmt attrs $ModuleBody [] return $ Module moduleStmt attrs moduleBody pModuleStmt :: Parser ModuleStmt @@ -216,7 +212,7 @@ pModuleEndStmt :: Parser () pModuleEndStmt = p name where name = "ModuleEndStmt" p = - void (string "end") + void (string "end" <* advanceToNextToken) -- Attribute statements pAttrStmt :: Parser AttrStmt @@ -332,8 +328,8 @@ pMemoryStmt = p name where name = "MemoryStmt" p = do - (string "memory" <* pWs) - options <- (many pMemoryOption <* pMaybeWs) + string "memory" <* pWs + options <- many (try (pMemoryOption <* pMaybeWs)) memoryId <- MemoryID <$> pId advanceToNextToken return $ MemoryStmt memoryId options @@ -534,15 +530,15 @@ pSync = p name where p = Sync <$> pSyncStmt - <*> many pUpdateStmt + <*> many ((try pUpdateStmt) <|> (try pMemWrStmt)) pSyncStmt :: Parser SyncStmt pSyncStmt = p name where name = "SyncStmt" p = pKeywordSync *> - pSigSpecPredicatedSyncStmt <|> - pNonSigSpecPredicatedSyncStmt + (pSigSpecPredicatedSyncStmt <|> + pNonSigSpecPredicatedSyncStmt) where pKeywordSync = string "sync" *> pWs pSigSpecPredicatedSyncStmt :: Parser SyncStmt @@ -574,7 +570,7 @@ pSyncType = p name where (Negedge <$ string "negedge" ) <|> (Edge <$ string "edge" ) -pUpdateStmt :: Parser UpdateStmt +pUpdateStmt :: Parser UpdateStmtVariants pUpdateStmt = p name where name = "UpdateStmt" p = @@ -582,19 +578,16 @@ pUpdateStmt = p name where <$> (string "update" *> pWs *> pDestSigSpec) <*> (pWs *> pSrcSigSpec <* advanceToNextToken) --- would correspond to `123456789[0:9][0:8]` -exampleSigSpecSlice = - SigSpecSlice - ( - SigSpecSlice - (SigSpecConstant (ConstantInteger 123456789)) - (Slice 0 $ Just 9) - ) - (Slice 0 $ Just 8) - --- val = parse pInteger "pInteger" "721" --- val = parse pModuleStmt "pModuleStmt" "module \\top\n" -val = parse pSigSpec "pSigSpecSlice" "123456789[0:9][0:8]" - -a :: Int -a = 3 \ No newline at end of file +pMemWrStmt :: Parser UpdateStmtVariants +pMemWrStmt = p name where + name = "MemWrStmt" + p = + do + attrStmts <- many (try pAttrStmt) + string "memwr" + id <- pWs *> pId + sigSpec <- pWs *> pSigSpec + sigSpec <- pWs *> pSigSpec + sigSpec <- pWs *> pSigSpec + constant <- pWs *> pConstant <* advanceToNextToken + return $ MemWrStmt id sigSpec sigSpec sigSpec constant attrStmts \ No newline at end of file