aboutsummaryrefslogtreecommitdiffstats
path: root/src/VeriFuzz/Verilog/Quote.hs
diff options
context:
space:
mode:
authorYann Herklotz <git@ymhg.org>2019-05-12 00:15:03 +0100
committerYann Herklotz <git@ymhg.org>2019-05-12 00:15:03 +0100
commit3a7a826bc7d0ab3dce955349d5bff252433048f6 (patch)
tree93223cb3fa3e5a0a61ab0ebcca2ea0919aba0b2d /src/VeriFuzz/Verilog/Quote.hs
parentf3c1942b50bfc294cbede8ae502b5f6cb306da7e (diff)
downloadverismith-3a7a826bc7d0ab3dce955349d5bff252433048f6.tar.gz
verismith-3a7a826bc7d0ab3dce955349d5bff252433048f6.zip
Add Quote.hs
Diffstat (limited to 'src/VeriFuzz/Verilog/Quote.hs')
-rw-r--r--src/VeriFuzz/Verilog/Quote.hs49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/VeriFuzz/Verilog/Quote.hs b/src/VeriFuzz/Verilog/Quote.hs
new file mode 100644
index 0000000..1450f5e
--- /dev/null
+++ b/src/VeriFuzz/Verilog/Quote.hs
@@ -0,0 +1,49 @@
+{-|
+Module : VeriFuzz.Verilog.Quote
+Description : QuasiQuotation for verilog code in Haskell.
+Copyright : (c) 2019, Yann Herklotz Grave
+License : GPL-3
+Maintainer : ymherklotz [at] gmail [dot] com
+Stability : experimental
+Portability : POSIX
+
+QuasiQuotation for verilog code in Haskell.
+-}
+
+{-# LANGUAGE TemplateHaskell #-}
+
+module VeriFuzz.Verilog.Quote
+ ( verilog
+ )
+where
+
+import Data.Data
+import qualified Data.Text as T
+import qualified Language.Haskell.TH as TH
+import Language.Haskell.TH.Quote
+import Language.Haskell.TH.Syntax
+import VeriFuzz.Verilog.Parser
+
+liftDataWithText :: Data a => a -> Q Exp
+liftDataWithText = dataToExpQ $ fmap liftText . cast
+
+liftText :: T.Text -> Q Exp
+liftText txt = AppE (VarE 'T.pack) <$> lift (T.unpack txt)
+
+-- | Quasiquoter for verilog, so that verilog can be written inline and be
+-- parsed to an AST at compile time.
+verilog :: QuasiQuoter
+verilog = QuasiQuoter { quoteExp = quoteVerilog
+ , quotePat = undefined
+ , quoteType = undefined
+ , quoteDec = undefined
+ }
+
+quoteVerilog :: String -> TH.Q TH.Exp
+quoteVerilog s = do
+ loc <- TH.location
+ let pos = TH.loc_filename loc
+ v <- case parseVerilog pos s of
+ Right e -> return e
+ Left e -> fail $ show e
+ liftDataWithText v