add corpus and improve invocation ergonomics

This commit is contained in:
Yehowshua Immanuel 2024-12-11 00:20:19 -05:00
parent e295647bbe
commit d096c93434
4 changed files with 42 additions and 50 deletions

View file

@ -1,7 +1,6 @@
module Main where module Main where
import System.Environment (getArgs) import System.Environment (getArgs)
import System.IO
import Control.Exception (catch, IOException) import Control.Exception (catch, IOException)
import Text.Show.Pretty (ppShow) import Text.Show.Pretty (ppShow)
@ -12,15 +11,19 @@ main = do
-- Get the command-line arguments -- Get the command-line arguments
args <- getArgs args <- getArgs
-- Check if a file name is provided -- Check if the input and output file names are provided
case args of case args of
(filePath:_) -> do (inputFilePath:outputFilePath:_) -> do
-- Attempt to read the file contents <- catch (readFile inputFilePath) handleReadError
contents <- catch (readFile filePath) handleReadError let output = ppShow $ Haskellator.runParser contents inputFilePath
putStrLn $ ppShow $ Haskellator.runParser contents filePath catch (writeFile outputFilePath output) handleWriteError
[] -> putStrLn "cabal run Haskellator -- <file-path>" putStrLn $ "Output written to " ++ outputFilePath
-- putStrLn $ ppShow Haskellator.val _ -> putStrLn "Usage: cabal run rtlil-parse -- <input-file-path> <output-file-path>"
-- Handle potential file reading errors -- Handle potential file reading errors
handleReadError :: IOException -> IO String 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."

View file

@ -1,9 +1,3 @@
module Haskellator( module Haskellator(runParser) where
val,
runParser
) where
import RTLILParser.Parser( import RTLILParser.Parser(runParser)
val,
runParser,
)

View file

@ -43,7 +43,7 @@ module RTLILParser.AST (
CaseBodyVariants(..), CaseBody(..), CaseBodyVariants(..), CaseBody(..),
-- Syncs -- Syncs
Sync(..), SyncStmt(..), SyncType(..), UpdateStmt(..) Sync(..), SyncStmt(..), SyncType(..), UpdateStmtVariants(..)
) where ) where
@ -174,7 +174,7 @@ data CaseBodyVariants = CaseBodySwitchVariant Switch
data CaseBody = CaseBody [AssignStmt] [Switch] deriving (Show) data CaseBody = CaseBody [AssignStmt] [Switch] deriving (Show)
-- Syncs -- Syncs
data Sync = Sync SyncStmt [UpdateStmt] deriving (Show) data Sync = Sync SyncStmt [UpdateStmtVariants] deriving (Show)
data SyncStmt = SigSpecPredicated SigSpec SyncType data SyncStmt = SigSpecPredicated SigSpec SyncType
| Global | Global
| Init | Init
@ -186,4 +186,6 @@ data SyncType = Low
| Negedge | Negedge
| Edge | Edge
deriving (Show) deriving (Show)
data UpdateStmt = UpdateStmt DestSigSpec SrcSigSpec deriving (Show) data UpdateStmtVariants = UpdateStmt DestSigSpec SrcSigSpec
| MemWrStmt Id SigSpec SigSpec SigSpec Constant [AttrStmt]
deriving (Show)

View file

@ -1,7 +1,4 @@
module RTLILParser.Parser( module RTLILParser.Parser(RTLILParser.Parser.runParser) where
RTLILParser.Parser.runParser,
a,
val) where
import Control.Monad (void) import Control.Monad (void)
import Text.Parsec import Text.Parsec
@ -52,7 +49,7 @@ import RTLILParser.AST (
CaseBodyVariants(..), CaseBody(..), CaseBodyVariants(..), CaseBody(..),
-- Syncs -- Syncs
Sync(..), SyncStmt(..), SyncType(..), UpdateStmt(..) Sync(..), SyncStmt(..), SyncType(..), UpdateStmtVariants(..)
) )
import RTLILParser.Primitives( import RTLILParser.Primitives(
pWs pWs
@ -68,7 +65,7 @@ import RTLILParser.Primitives(
-- taken from: https://yosyshq.readthedocs.io/projects/yosys/en/0.47/appendix/rtlil_text.html -- 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 -- 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 runParser str filename = case parse pFile filename str of
Left err -> error $ show err Left err -> error $ show err
Right val -> val Right val -> val
@ -159,7 +156,6 @@ pModule = p <?> name where
moduleStmt <- pModuleStmt moduleStmt <- pModuleStmt
moduleBody <- pModuleBody moduleBody <- pModuleBody
pModuleEndStmt pModuleEndStmt
-- return $ Module moduleStmt attrs $ModuleBody []
return $ Module moduleStmt attrs moduleBody return $ Module moduleStmt attrs moduleBody
pModuleStmt :: Parser ModuleStmt pModuleStmt :: Parser ModuleStmt
@ -216,7 +212,7 @@ pModuleEndStmt :: Parser ()
pModuleEndStmt = p <?> name where pModuleEndStmt = p <?> name where
name = "ModuleEndStmt" name = "ModuleEndStmt"
p = p =
void (string "end") void (string "end" <* advanceToNextToken)
-- Attribute statements -- Attribute statements
pAttrStmt :: Parser AttrStmt pAttrStmt :: Parser AttrStmt
@ -332,8 +328,8 @@ pMemoryStmt = p <?> name where
name = "MemoryStmt" name = "MemoryStmt"
p = p =
do do
(string "memory" <* pWs) string "memory" <* pWs
options <- (many pMemoryOption <* pMaybeWs) options <- many (try (pMemoryOption <* pMaybeWs))
memoryId <- MemoryID <$> pId memoryId <- MemoryID <$> pId
advanceToNextToken advanceToNextToken
return $ MemoryStmt memoryId options return $ MemoryStmt memoryId options
@ -534,15 +530,15 @@ pSync = p <?> name where
p = p =
Sync Sync
<$> pSyncStmt <$> pSyncStmt
<*> many pUpdateStmt <*> many ((try pUpdateStmt) <|> (try pMemWrStmt))
pSyncStmt :: Parser SyncStmt pSyncStmt :: Parser SyncStmt
pSyncStmt = p <?> name where pSyncStmt = p <?> name where
name = "SyncStmt" name = "SyncStmt"
p = p =
pKeywordSync *> pKeywordSync *>
pSigSpecPredicatedSyncStmt <|> (pSigSpecPredicatedSyncStmt <|>
pNonSigSpecPredicatedSyncStmt pNonSigSpecPredicatedSyncStmt)
where pKeywordSync = string "sync" *> pWs where pKeywordSync = string "sync" *> pWs
pSigSpecPredicatedSyncStmt :: Parser SyncStmt pSigSpecPredicatedSyncStmt :: Parser SyncStmt
@ -574,7 +570,7 @@ pSyncType = p <?> name where
(Negedge <$ string "negedge" ) <|> (Negedge <$ string "negedge" ) <|>
(Edge <$ string "edge" ) (Edge <$ string "edge" )
pUpdateStmt :: Parser UpdateStmt pUpdateStmt :: Parser UpdateStmtVariants
pUpdateStmt = p <?> name where pUpdateStmt = p <?> name where
name = "UpdateStmt" name = "UpdateStmt"
p = p =
@ -582,19 +578,16 @@ pUpdateStmt = p <?> name where
<$> (string "update" *> pWs *> pDestSigSpec) <$> (string "update" *> pWs *> pDestSigSpec)
<*> (pWs *> pSrcSigSpec <* advanceToNextToken) <*> (pWs *> pSrcSigSpec <* advanceToNextToken)
-- would correspond to `123456789[0:9][0:8]` pMemWrStmt :: Parser UpdateStmtVariants
exampleSigSpecSlice = pMemWrStmt = p <?> name where
SigSpecSlice name = "MemWrStmt"
( p =
SigSpecSlice do
(SigSpecConstant (ConstantInteger 123456789)) attrStmts <- many (try pAttrStmt)
(Slice 0 $ Just 9) string "memwr"
) id <- pWs *> pId
(Slice 0 $ Just 8) sigSpec <- pWs *> pSigSpec
sigSpec <- pWs *> pSigSpec
-- val = parse pInteger "pInteger" "721" sigSpec <- pWs *> pSigSpec
-- val = parse pModuleStmt "pModuleStmt" "module \\top\n" constant <- pWs *> pConstant <* advanceToNextToken
val = parse pSigSpec "pSigSpecSlice" "123456789[0:9][0:8]" return $ MemWrStmt id sigSpec sigSpec sigSpec constant attrStmts
a :: Int
a = 3