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

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., \\)
]