Skip to content

Commit

Permalink
create PortDecl, Range data type. modify inputDeclaration. (AST under…
Browse files Browse the repository at this point in the history
… construction...)
  • Loading branch information
kei-os committed Dec 19, 2008
1 parent b1427ba commit d09f3ca
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 51 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2008-12-19 kei-os <[email protected]>

* PortDecl, Range を用意して inputDeclarationが ModuleItemを返すようにした。AST化の途中。

2008-12-18 kei-os <[email protected]>

* ModuleItem型を用意してみた。(ASTテストのため)
Expand Down
116 changes: 65 additions & 51 deletions Vparsec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ commaSep1 = P.commaSep1 lexer
data Module = Module
{ mName :: String
, mPorts :: [String]
-- , mItems :: [String] -- XXX TODO
, mItems :: [ModuleItem] -- XXX test for AST
} deriving (Eq, Show)

-- XXX test for AST
data ModuleItem = MIDECL String
| PARAM_DECL String
| CONT_ASSIGN String
| INPUT_DECL String
| INPUT_DECL PortDecl -- XXX on impl
-- | INPUT_DECL String -- XXX on impl
| OUTPUT_DECL String
| INOUT_DECL String
| REG_DECL String
Expand All @@ -70,25 +70,14 @@ data ModuleItem = MIDECL String
| INITIAL String
| ALWAYS String deriving (Eq, Show)

data Port = Port
{ pName :: String
, pDirection :: Direction
, pRange :: Range
} deriving (Eq, Show)

data Direction = Input | Output deriving (Eq, Show)

data Range = Range
{ rMin :: Integer
, rMax :: Integer
, rWidth :: Integer
} deriving (Eq, Ord, Show)

data DeclType = Param | Direction | Net | Reg deriving (Show)


type Max = Int
type Min = Int
type Width = Int
type Range = (Max, Min, Width)

data PortDecl = PortDecl { pName :: [String], pRange :: Range } deriving (Show, Eq, Ord)

data Direction = Input | Output | Inout deriving (Eq, Show)

{- for tiny parser test -}
test :: Show a => Parser a -> String -> IO ()
Expand Down Expand Up @@ -277,68 +266,86 @@ commaParamAssignment = do { a <- comma

--inputDeclaration :: Parser String
inputDeclaration :: Parser ModuleItem
inputDeclaration = do { a <- symbol "input"
; b <- range <|> string ""
inputDeclaration = do { symbol "input"
; b <- rangeOrEmpty
; c <- listOfPortIdentifiers
; d <- semi
; semi
-- ; return $ a ++ b ++ c ++ d}
; return $ INPUT_DECL $ a ++ b ++ c ++ d}
-- ; return $ INPUT_DECL $ a ++ b ++ c ++ d}
; return $ INPUT_DECL $ PortDecl { pName = c, pRange = b } }
<?> "inputDeclaration"

listOfPortIdentifiers :: Parser String
listOfPortIdentifiers :: Parser [String]
listOfPortIdentifiers = do { a <- portIdentifier
; b <- many commaPortIdentifier
; return $ a ++ (concat b) }
-- ; return $ a ++ (concat b) }
; return (a:b) }
<?> "listOfPortIdentifiers"
where
commaPortIdentifier :: Parser String
commaPortIdentifier = do { a <- comma
; b <- lexeme identifier
; return $ a ++ b }
commaPortIdentifier = do { comma ; lexeme identifier >>= return }

portIdentifier :: Parser String
portIdentifier = identifier

range :: Parser String
range = brackets range_
rangeOrEmpty :: Parser Range
rangeOrEmpty = try(lexeme range)
<|> do { string ""; return (0, 0, 1) }
<?> "rangeOrEmpty"

--range :: Parser String
range :: Parser Range
--range = brackets range_
range = do { symbol "["
; a <- range_
; symbol "]"
; return a }
<?> "range"
where
range_ :: Parser String
range_ = do { a <- lexeme constantExpression
; b <- colon
; c <- lexeme constantExpression
; return $ a ++ b ++ c }
-- range_ :: Parser String
range_ :: Parser Range
range_ = do { max <- lexeme constantExpression
; colon
; min <- lexeme constantExpression
-- ; return $ a ++ b ++ c }
; return (read max, read min, (read max) - (read min) + 1 ) } -- XXX TODO improve
<?> "range_"

--outputDeclaration :: Parser String
outputDeclaration :: Parser ModuleItem
outputDeclaration = do { a <- symbol "output"
; b <- range <|> string ""
; c <- listOfPortIdentifiers
-- ; b <- range <|> string ""
; rangeOrEmpty -- XXX TODO : impl range
-- ; c <- listOfPortIdentifiers
; listOfPortIdentifiers
; d <- semi
-- ; return $ a ++ b ++ c ++ d }
; return $ OUTPUT_DECL $ a ++ b ++ c ++ d }
; return $ OUTPUT_DECL $ a {-++ b ++ c-} ++ d }
<?> "outputDeclaration"

--inoutDeclaration :: Parser String
inoutDeclaration :: Parser ModuleItem
inoutDeclaration = do { a <- symbol "inout"
; b <- range <|> string ""
; c <- listOfPortIdentifiers
-- ; b <- range <|> string ""
; rangeOrEmpty -- XXX TODO : impl range
-- ; c <- listOfPortIdentifiers
; listOfPortIdentifiers
; d <- semi
-- ; return $ a ++ b ++ c ++ d }
; return $ INOUT_DECL $ a ++ b ++ c ++ d }
; return $ INOUT_DECL $ a {-++ b ++ c-} ++ d }
<?> "inoutDeclaration"

--netDeclaration :: Parser String
netDeclaration :: Parser ModuleItem
netDeclaration = do { a <- nettype
; b <- try(vecorscal) <|> string ""
; c <- try(range) <|> string ""
-- ; c <- try(range) <|> string ""
; rangeOrEmpty -- XXX TODO : impl range
; d <- try(delay3) <|> string ""
; e <- listOfNetIdentifiers
; f <- semi
-- ; return $ a ++ b ++ c ++ d ++ e ++ f }
; return $ NET_DECL $ a ++ b ++ c ++ d ++ e ++ f }
; return $ NET_DECL $ a ++ b {-++ c-} ++ d ++ e ++ f }
<?> "netDeclaration"
where
vecorscal :: Parser String
Expand Down Expand Up @@ -366,11 +373,12 @@ delay3 = string "" -- XXX TODO : impl
--regDeclaration :: Parser String
regDeclaration :: Parser ModuleItem
regDeclaration = do { a <- symbol "reg"
; b <- range <|> string ""
-- ; b <- range <|> string ""
; rangeOrEmpty -- XXX TODO : impl range
; c <- listOfRegisterVariables
; d <- semi
-- ; return $ a ++ b ++ c ++ d }
; return $ REG_DECL $ a ++ b ++ c ++ d }
; return $ REG_DECL $ a {-++ b -} ++ c ++ d }
<?> "regDeclaration"

listOfRegisterVariables :: Parser String
Expand All @@ -386,8 +394,10 @@ listOfRegisterVariables = do { a <- lexeme registerVariable

registerVariable :: Parser String
registerVariable = do { a <- lexeme identifier
; b <- lexeme range <|> string ""
; return $ a ++ b }
-- ; b <- lexeme range <|> string ""
-- ; return $ a ++ b }
; rangeOrEmpty -- XXX TODO : impl range
; return a }
<?> "registerVariable"

--timeDeclaration :: Parser String
Expand Down Expand Up @@ -660,8 +670,10 @@ lvalue = identifier
; b <- brackets expression
; return $ a ++ b }
<|> do { a <- lexeme identifier
; b <- range
; return $ a ++ b }
-- ; b <- range
-- ; return $ a ++ b }
; range
; return a }
<|> concatenation
<?> "lvalue"

Expand Down Expand Up @@ -722,8 +734,10 @@ binaryOperator = try(symbol "+")
primary :: Parser String
primary = try(number)
<|> try(do { a <- identifier
; b <- range
; return $ a ++ b })
-- ; b <- range
-- ; return $ a ++ b })
; range
; return a })
<|> try(brackets expression)
<|> try(lexeme identifier)
<|> try(concatenation)
Expand Down

0 comments on commit d09f3ca

Please sign in to comment.