save changes before re-factor

This commit is contained in:
Yehowshua Immanuel 2024-11-25 17:33:18 -05:00
parent bcd5d32290
commit 4e33d595cc
4 changed files with 120 additions and 13 deletions

49
atoms.txt Normal file
View file

@ -0,0 +1,49 @@
- [x] <id> ::= <public-id> | <autogen-id>
- [x] <public-id> ::= “" <nonws>+
- [x] <autogen-id> ::= “” <nonws>+
- [x] <value> ::= <decimal-digit>+ <binary-digit>*
- [x] <decimal-digit> ::= “0” | “1” | “2” | “3” | “4” | “5” | “6” | “7” | “8” | “9”
- [x] <binary-digit> ::= “0” | “1” | “x” | “z” | “m” | “-“
- [x] <integer> ::= “-“? <decimal-digit>+
- [ ] <file> ::= <autoidx-stmt>? <module>*
- [x] <autoidx-stmt> ::= “autoidx” <integer> <eol>
- [ ] <module> ::= <attr-stmt>* <module-stmt> <module-body> <module-end-stmt>
- [x] <module-stmt> ::= “module” <id> <eol>
- [ ] <module-body> ::= (<param-stmt> <wire> <memory> <cell> <process> )*
- [ ] <param-stmt> ::= “parameter” <id> <constant>? <eol>
- [ ] <constant> ::= <value> | <integer> | <string>
- [x] <module-end-stmt> ::= “end” <eol>
- [ ] <attr-stmt> ::= “attribute” <id> <constant> <eol>
- [ ] <sigspec> ::= <constant> <sigspec> “[” <integer> (“:” <integer>)? “]” “{” <sigspec>* “}”
- [ ] <conn-stmt> ::= “connect” <sigspec> <sigspec> <eol>
- [ ] <wire> ::= <attr-stmt>* <wire-stmt>
- [ ] <wire-stmt> ::= “wire” <wire-option>* <wire-id> <eol>
- [ ] <wire-id> ::= <id>
- [ ] <wire-option> ::= “width” <integer> “offset” <integer> “input” <integer> “output” <integer> “inout” <integer> “upto” “signed”
- [ ] <memory> ::= <attr-stmt>* <memory-stmt>
- [ ] <memory-stmt> ::= “memory” <memory-option>* <id> <eol>
- [ ] <memory-option> ::= “width” <integer> “size” <integer> “offset” <integer>
- [ ] <cell> ::= <attr-stmt>* <cell-stmt> <cell-body-stmt>* <cell-end-stmt>
- [ ] <cell-stmt> ::= “cell” <cell-id> <cell-type> <eol>
- [ ] <cell-id> ::= <id>
- [ ] <cell-type> ::= <id>
- [ ] <cell-body-stmt> ::= “parameter” (“signed” | “real”)? <id> <constant> <eol> “connect” <id> <sigspec> <eol>
- [ ] <cell-end-stmt> ::= “end” <eol>
- [ ] <process> ::= <attr-stmt>* <proc-stmt> <process-body> <proc-end-stmt>
- [ ] <proc-stmt> ::= “process” <id> <eol>
- [ ] <process-body> ::= <assign-stmt>* <switch>? <assign-stmt>* <sync>*
- [ ] <assign-stmt> ::= “assign” <dest-sigspec> <src-sigspec> <eol>
- [ ] <dest-sigspec> ::= <sigspec>
- [ ] <src-sigspec> ::= <sigspec>
- [ ] <proc-end-stmt> ::= “end” <eol>
- [ ] <switch> ::= <switch-stmt> <case>* <switch-end-stmt>
- [ ] <switch-stmt> := <attr-stmt>* “switch” <sigspec> <eol>
- [ ] <case> ::= <attr-stmt>* <case-stmt> <case-body>
- [ ] <case-stmt> ::= “case” <compare>? <eol>
- [ ] <compare> ::= <sigspec> (“,” <sigspec>)*
- [ ] <case-body> ::= (<switch> | <assign-stmt>)*
- [ ] <switch-end-stmt> ::= “end” <eol>
- [ ] <sync> ::= <sync-stmt> <update-stmt>*
- [ ] <sync-stmt> ::= “sync” <sync-type> <sigspec> <eol> “sync” “global” <eol> “sync” “init” <eol> “sync” “always” <eol>
- [ ] <sync-type> ::= “low” | “high” | “posedge” | “negedge” | “edge”
- [ ] <update-stmt> ::= “update” <dest-sigspec> <src-sigspec> <eol>

View file

@ -2,21 +2,20 @@
-- https://github.com/YosysHQ/yosys/blob/111b747d2797238eadf541879848492a9d34909a/docs/source/yosys_internals/formats/rtlil_text.rst
module Haskellator(a, val) where
import Data.Char (digitToInt)
import Data.List (foldl')
import Control.Monad (void)
import Text.Parsec
import Text.Parsec.String (Parser)
import RtlilAstTypes(
PublicId(..),
Id(..),
AutogenId(..),
AutoIdxStmt(..),
Value(..)
)
binaryStringToInt :: String -> Int
binaryStringToInt = foldl' (\acc x -> acc * 2 + digitToInt x) 0
import Util(
binaryStringToInt,
pEscapedChar)
-- https://github.com/YosysHQ/yosys/blob/111b747d2797238eadf541879848492a9d34909a/frontends/rtlil/rtlil_lexer.l#L88C1-L88C17
nonws :: Parser Char
@ -34,17 +33,29 @@ pPublicId = PublicId <$> (char '\\' *> many1 nonws)
pAutogenId :: Parser AutogenId
pAutogenId = AutogenId <$> (char '$' *> many1 nonws)
pId :: Parser Id
pId = Public <$> pPublicId
<|> Autogen <$> pAutogenId
decimalDigit :: Parser Char
decimalDigit = oneOf "0123456789"
-- update in the future to support 4 state logic
-- by converting x and z to 0 and warning about it.
binaryDigit :: Parser Char
binaryDigit = oneOf "01"
pBinaryDigit :: Parser Char
pBinaryDigit = oneOf "01"
pString :: Parser String
pString =
between delimiter delimiter parseString
where
delimiter = char '"'
parseString = many (pEscapedChar <|> noneOf "\\\"")
pValue :: Parser Value
pValue = Value <$> pInteger
<*> (binaryStringToInt <$> many1 binaryDigit)
<*> (binaryStringToInt <$> many1 pBinaryDigit)
pInteger :: Parser Int
pInteger = do
@ -55,16 +66,21 @@ pInteger = do
Just _ -> -value
Nothing -> value
pAutogenIdx :: Parser AutoIdxStmt
pAutogenIdx = AutoIdxStmt <$> (string "autoidx" *> pWs *> pInteger <* pEol)
pAutoIdxStmt :: Parser AutoIdxStmt
pAutoIdxStmt = AutoIdxStmt <$> (string "autoidx" *> pWs *> pInteger <* pEol)
pModuleStmt :: Parser Id
pModuleStmt = string "module" *> pWs *> pId <* pEol
pModuleEndStmt :: Parser ()
pModuleEndStmt = void (string "end")
-- pModuleEndStmt :: Parser String
-- pModuleEndStmt = string "end" <* pEol
-- pModuleStmt :: Parser ()
-- pModuleStmt =
val = parse pInteger "pInteger" "721"
-- val = parse pInteger "pInteger" "721"
val = parse pModuleStmt "pModuleStmt" "module \\top\n"
a :: Int
a = 3

View file

@ -2,11 +2,16 @@ module RtlilAstTypes(
PublicId(..),
AutogenId(..),
AutoIdxStmt(..),
Id(..),
Value(..)
) where
import Text.Read (Lexeme(Ident))
data PublicId = PublicId String deriving (Show)
data AutogenId = AutogenId String deriving (Show)
data Id = Public PublicId
| Autogen AutogenId
deriving (Show)
data AutoIdxStmt = AutoIdxStmt Int deriving (Show)
data Value = Value
{ width :: Int

37
src/Util.hs Normal file
View file

@ -0,0 +1,37 @@
module Util(
binaryStringToInt,
pEscapedChar,
pEscapedChar
) where
import Control.Monad (void)
import Text.Parsec
import Text.Parsec.String (Parser)
import Data.Char (digitToInt)
import Data.List (foldl')
binaryStringToInt :: String -> Int
binaryStringToInt = foldl' (\acc x -> acc * 2 + digitToInt x) 0
octalStringToInt :: String -> Maybe Int
octalStringToInt str
| all (`elem` ['0'..'7']) str = Just $ foldl' (\acc x -> acc * 8 + digitToInt x) 0 str
| otherwise = Nothing
pOctal :: Parser Char
pOctal = do
digits <- count 1 digit -- At least 1 digit
moreDigits <- option "" (count 2 digit) -- Up to 3 digits total
case octalStringToInt (digits ++ moreDigits) of
Just value -> return $ toEnum value -- Convert integer to a Char
Nothing -> fail "Invalid octal escape sequence"
pEscapedChar :: Parser Char
pEscapedChar = do
char '\\' -- Match the backslash
choice
[ char 'n' >> return '\n' -- \n → newline
, char 't' >> return '\t' -- \t → tab
, try pOctal -- \123 → octal escape
, anyChar -- Any other escaped char (e.g., \\)
]