aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorYann Herklotz <git@ymhg.org>2019-05-10 17:41:44 +0100
committerYann Herklotz <git@ymhg.org>2019-05-10 17:41:44 +0100
commit1bf5b56da8df267fd33e738b53e29e832854856b (patch)
tree83b451ca816e9fffc3faadef3ed37054549820f6 /src
parent8241ebad9374187b20ee0fdd43029a2a5ddfbb4e (diff)
downloadverismith-1bf5b56da8df267fd33e738b53e29e832854856b.tar.gz
verismith-1bf5b56da8df267fd33e738b53e29e832854856b.zip
Add constant expression to expression conversion and vice versa
Diffstat (limited to 'src')
-rw-r--r--src/VeriFuzz/Verilog.hs2
-rw-r--r--src/VeriFuzz/Verilog/AST.hs21
2 files changed, 23 insertions, 0 deletions
diff --git a/src/VeriFuzz/Verilog.hs b/src/VeriFuzz/Verilog.hs
index 4f9fd52..701a7d6 100644
--- a/src/VeriFuzz/Verilog.hs
+++ b/src/VeriFuzz/Verilog.hs
@@ -53,6 +53,8 @@ module VeriFuzz.Verilog
-- * Expression
, Expr(..)
, ConstExpr(..)
+ , constToExpr
+ , exprToConst
, constNum
-- * Assignment
, Assign(..)
diff --git a/src/VeriFuzz/Verilog/AST.hs b/src/VeriFuzz/Verilog/AST.hs
index c4d889b..30634c0 100644
--- a/src/VeriFuzz/Verilog/AST.hs
+++ b/src/VeriFuzz/Verilog/AST.hs
@@ -59,6 +59,8 @@ module VeriFuzz.Verilog.AST
, Expr(..)
, ConstExpr(..)
, ConstExprF(..)
+ , constToExpr
+ , exprToConst
, Range(..)
, constNum
, constParamId
@@ -265,6 +267,25 @@ data ConstExpr = ConstNum { _constNum :: {-# UNPACK #-} !BitVec }
| ConstStr { _constStr :: {-# UNPACK #-} !Text }
deriving (Eq, Show, Ord, Data)
+constToExpr :: ConstExpr -> Expr
+constToExpr (ConstNum a) = Number a
+constToExpr (ParamId a) = Id a
+constToExpr (ConstConcat a) = Concat $ fmap constToExpr a
+constToExpr (ConstUnOp a b) = UnOp a $ constToExpr b
+constToExpr (ConstBinOp a b c) = BinOp (constToExpr a) b $ constToExpr c
+constToExpr (ConstCond a b c) = Cond (constToExpr a) (constToExpr b) $ constToExpr c
+constToExpr (ConstStr a) = Str a
+
+exprToConst :: Expr -> ConstExpr
+exprToConst (Number a) = ConstNum a
+exprToConst (Id a) = ParamId a
+exprToConst (Concat a) = ConstConcat $ fmap exprToConst a
+exprToConst (UnOp a b) = ConstUnOp a $ exprToConst b
+exprToConst (BinOp a b c) = ConstBinOp (exprToConst a) b $ exprToConst c
+exprToConst (Cond a b c) = ConstCond (exprToConst a) (exprToConst b) $ exprToConst c
+exprToConst (Str a) = ConstStr a
+exprToConst _ = error "Not a constant expression"
+
instance Num ConstExpr where
a + b = ConstBinOp a BinPlus b
a - b = ConstBinOp a BinMinus b