RTLIL parser presumably finished except for comment parser

This commit is contained in:
Yehowshua Immanuel 2024-12-09 11:34:26 -05:00
parent 851a18f82d
commit 8e0073a1e6
4 changed files with 70 additions and 18 deletions

View file

@ -71,3 +71,8 @@ $ rtlil-parse
multi-clock domain circuits and their associated primitives such multi-clock domain circuits and their associated primitives such
as asynchronous FIFOs, but I plan to make sure simulation of such as asynchronous FIFOs, but I plan to make sure simulation of such
circuits is possible and correct. circuits is possible and correct.
# Lessons Learned
- Should have written parser to be token based where after consuming
and capturing token, we consume and discard all following whitespaces
as well as comments...

View file

@ -42,6 +42,7 @@
- [x] Remove all instances of `_ <-` - [x] Remove all instances of `_ <-`
- [ ] Module - [ ] Module
- [ ] Remove weird GHC imports - [ ] Remove weird GHC imports
- [x] Are the `try` statements in `pWireOption` correctly constructed?
- [ ] Consider the very weird case where the process body has nothing, - [ ] Consider the very weird case where the process body has nothing,
thus, `pEolAndAdvanceToNextNonWs` may never get invoked in any of thus, `pEolAndAdvanceToNextNonWs` may never get invoked in any of
the sub-parsers encapsulated in `pProcessBody`. Do we need to the sub-parsers encapsulated in `pProcessBody`. Do we need to

View file

@ -5,11 +5,15 @@ module RTLILParser.AST (
-- Values -- Values
Value(..), Value(..),
-- File
File(..),
-- Autoindex statements -- Autoindex statements
AutoIdxStmt(..), AutoIdxStmt(..),
-- Module -- Module
ParamStmt(..), Constant(..), Module(..), ModuleStmt(..), ModuleBody(..),
ModuleBodyVariant(..), ParamStmt(..), Constant(..),
-- Attribute statements -- Attribute statements
AttrStmt(..), AttrStmt(..),
@ -68,16 +72,22 @@ data Value = Value
-- strings -- strings
-- comments -- comments
-- file -- file
data File = File AutoIdxStmt [Module] deriving (Show)
-- Autoindex statements -- Autoindex statements
data AutoIdxStmt = AutoIdxStmt Int deriving (Show) data AutoIdxStmt = AutoIdxStmt Int deriving (Show)
-- Module -- Module
data ParamStmt = ParamStmt data Module = Module ModuleStmt [AttrStmt] ModuleBody deriving (Show)
{ paramId :: Id data ModuleStmt = ModuleStmt Id deriving (Show)
, paramConstant :: Maybe Constant data ModuleBody = ModuleBody [ModuleBodyVariant] deriving (Show)
} data ModuleBodyVariant = ModuleBodyParamStmt ParamStmt
| ModuleBodyWire Wire
| ModuleBodyMemory Memory
| ModuleBodyCell Cell
| ModuleBodyProcess Process
deriving (Show) deriving (Show)
data ParamStmt = ParamStmt Id (Maybe Constant) deriving (Show)
data Constant = ConstantValue Value data Constant = ConstantValue Value
| ConstantInteger Int | ConstantInteger Int
| ConstantString String | ConstantString String

View file

@ -11,11 +11,15 @@ import RTLILParser.AST (
-- Values -- Values
Value(..), Value(..),
-- File
File(..),
-- Autoindex statements -- Autoindex statements
AutoIdxStmt(..), AutoIdxStmt(..),
-- Module -- Module
ParamStmt(..), Constant(..), Module(..), ModuleStmt(..), ModuleBody(..),
ModuleBodyVariant(..), ParamStmt(..), Constant(..),
-- Attribute statements -- Attribute statements
AttrStmt(..), AttrStmt(..),
@ -106,6 +110,10 @@ pString =
-- comments -- comments
-- file -- file
pFile :: Parser File
pFile = File
<$> pAutoIdxStmt
<*> many pModule
-- Autoindex statements -- Autoindex statements
pAutoIdxStmt :: Parser AutoIdxStmt pAutoIdxStmt :: Parser AutoIdxStmt
@ -114,9 +122,37 @@ pAutoIdxStmt = AutoIdxStmt
pInteger <* pEolAndAdvanceToNextNonWs) pInteger <* pEolAndAdvanceToNextNonWs)
-- Module -- Module
pModuleStmt :: Parser Id pModule :: Parser Module
pModuleStmt = string "module" *> pWs *> pId <* pModule = do
pEolAndAdvanceToNextNonWs attrs <- many pAttrStmt
moduleStmt <- pModuleStmt
moduleBody <- pModuleBody
pModuleEndStmt
return $ Module moduleStmt attrs moduleBody
pModuleStmt :: Parser ModuleStmt
pModuleStmt = ModuleStmt
<$> (string "module" *> pWs *>
pId <* pEolAndAdvanceToNextNonWs)
pModuleBody :: Parser ModuleBody
pModuleBody = ModuleBody
<$> many pModuleBodyVariant
pModuleBodyVariant :: Parser ModuleBodyVariant
pModuleBodyVariant =
-- `pWire`, `pMemory`, `pCell`, `pProcess` all
-- start by parsing attribute statements, so we
-- need backtracking since we can't determin which
-- parser will succeed based on the first character
-- we encounter alone. `pParamStmt` technically doesn't
-- need to be prefixed by `try`, so that is a stylistic
-- choice.
try (ModuleBodyParamStmt <$> pParamStmt) <|>
try (ModuleBodyWire <$> pWire ) <|>
try (ModuleBodyMemory <$> pMemory ) <|>
try (ModuleBodyCell <$> pCell ) <|>
(ModuleBodyProcess <$> pProcess )
pParamStmt :: Parser ParamStmt pParamStmt :: Parser ParamStmt
pParamStmt = ParamStmt pParamStmt = ParamStmt