sv2v/src/Language/SystemVerilog/AST/Expr.hs

88 lines
2.7 KiB
Haskell
Raw Normal View History

{- sv2v
- Author: Zachary Snow <zach@zachjs.com>
- Initial Verilog AST Author: Tom Hawkins <tomahawkins@gmail.com>
-
- SystemVerilog expressions
-}
module Language.SystemVerilog.AST.Expr
( Expr (..)
, Range
, showAssignment
, showRanges
, Args (..)
) where
import Data.List (intercalate)
import Text.Printf (printf)
import Language.SystemVerilog.AST.Op
import Language.SystemVerilog.AST.ShowHelp
import {-# SOURCE #-} Language.SystemVerilog.AST.Type
type Range = (Expr, Expr)
data Expr
= String String
| Number String
| Ident Identifier
| Range Expr Range
| Bit Expr Expr
| Repeat Expr [Expr]
| Concat [Expr]
| Call Identifier Args
| UniOp UniOp Expr
| BinOp BinOp Expr Expr
| Mux Expr Expr Expr
2019-03-27 05:34:17 +01:00
| Cast (Either Type Expr) Expr
| Dot Expr Identifier
| Pattern [(Maybe Identifier, Expr)]
deriving (Eq, Ord)
instance Show Expr where
show (Number str ) = str
show (Ident str ) = str
show (String str ) = printf "\"%s\"" str
show (Bit e b ) = printf "%s[%s]" (show e) (show b)
show (Range e r ) = printf "%s%s" (show e) (showRange r)
show (Repeat e l ) = printf "{%s {%s}}" (show e) (commas $ map show l)
show (Concat l ) = printf "{%s}" (commas $ map show l)
show (UniOp a b ) = printf "(%s %s)" (show a) (show b)
2019-03-25 19:40:57 +01:00
show (BinOp o a b) = printf "(%s %s %s)" (show a) (show o) (show b)
show (Dot e n ) = printf "%s.%s" (show e) n
show (Mux c a b) = printf "(%s ? %s : %s)" (show c) (show a) (show b)
show (Call f l ) = printf "%s(%s)" f (show l)
2019-03-27 05:34:17 +01:00
show (Cast tore e ) = printf "%s'(%s)" tStr (show e)
where
tStr = case tore of
Left a -> show a
Right a -> show a
show (Pattern l ) =
printf "'{\n%s\n}" (indent $ intercalate ",\n" $ map showPatternItem l)
where
showPatternItem :: (Maybe Identifier, Expr) -> String
showPatternItem (Nothing, e) = show e
showPatternItem (Just n , e) = printf "%s: %s" n (show e)
data Args
= Args [Maybe Expr] [(Identifier, Maybe Expr)]
deriving (Eq, Ord)
instance Show Args where
show (Args pnArgs kwArgs) = commas strs
where
strs = (map showPnArg pnArgs) ++ (map showKwArg kwArgs)
showPnArg = maybe "" show
showKwArg (x, me) = printf ".%s(%s)" (show x) (showPnArg me)
showAssignment :: Maybe Expr -> String
showAssignment Nothing = ""
showAssignment (Just val) = " = " ++ show val
showRanges :: [Range] -> String
showRanges [] = ""
showRanges l = " " ++ (concatMap showRange l)
showRange :: Range -> String
showRange (h, l) = printf "[%s:%s]" (show h) (show l)