From 4e33d595cc7bd78f191d574582d8643f518c9e33 Mon Sep 17 00:00:00 2001 From: Yehowshua Immanuel Date: Mon, 25 Nov 2024 17:33:18 -0500 Subject: [PATCH] save changes before re-factor --- atoms.txt | 49 ++++++++++++++++++++++++++++++++++++++++++++ src/Haskellator.hs | 42 +++++++++++++++++++++++++------------ src/RtlilAstTypes.hs | 5 +++++ src/Util.hs | 37 +++++++++++++++++++++++++++++++++ 4 files changed, 120 insertions(+), 13 deletions(-) create mode 100644 atoms.txt create mode 100644 src/Util.hs diff --git a/atoms.txt b/atoms.txt new file mode 100644 index 0000000..b8574ba --- /dev/null +++ b/atoms.txt @@ -0,0 +1,49 @@ + - [x] ::= | + - [x] ::= “" + + - [x] ::= “” + + - [x] ::= + ’ * + - [x] ::= “0” | “1” | “2” | “3” | “4” | “5” | “6” | “7” | “8” | “9” + - [x] ::= “0” | “1” | “x” | “z” | “m” | “-“ + - [x] ::= “-“? + + - [ ] ::= ? * + - [x] ::= “autoidx” + - [ ] ::= * + - [x] ::= “module” + - [ ] ::= ( )* + - [ ] ::= “parameter” ? + - [ ] ::= | | + - [x] ::= “end” + - [ ] ::= “attribute” + - [ ] ::= “[” (“:” )? “]” “{” * “}” + - [ ] ::= “connect” + - [ ] ::= * + - [ ] ::= “wire” * + - [ ] ::= + - [ ] ::= “width” “offset” “input” “output” “inout” “upto” “signed” + - [ ] ::= * + - [ ] ::= “memory” * + - [ ] ::= “width” “size” “offset” + - [ ] ::= * * + - [ ] ::= “cell” + - [ ] ::= + - [ ] ::= + - [ ] ::= “parameter” (“signed” | “real”)? “connect” + - [ ] ::= “end” + - [ ] ::= * + - [ ] ::= “process” + - [ ] ::= * ? * * + - [ ] ::= “assign” + - [ ] ::= + - [ ] ::= + - [ ] ::= “end” + - [ ] ::= * + - [ ] := * “switch” + - [ ] ::= * + - [ ] ::= “case” ? + - [ ] ::= (“,” )* + - [ ] ::= ( | )* + - [ ] ::= “end” + - [ ] ::= * + - [ ] ::= “sync” “sync” “global” “sync” “init” “sync” “always” + - [ ] ::= “low” | “high” | “posedge” | “negedge” | “edge” + - [ ] ::= “update” diff --git a/src/Haskellator.hs b/src/Haskellator.hs index e280220..487de0c 100644 --- a/src/Haskellator.hs +++ b/src/Haskellator.hs @@ -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 \ No newline at end of file diff --git a/src/RtlilAstTypes.hs b/src/RtlilAstTypes.hs index d8e39c1..889d414 100644 --- a/src/RtlilAstTypes.hs +++ b/src/RtlilAstTypes.hs @@ -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 diff --git a/src/Util.hs b/src/Util.hs new file mode 100644 index 0000000..43bb020 --- /dev/null +++ b/src/Util.hs @@ -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., \\) + ]