From 3a7a826bc7d0ab3dce955349d5bff252433048f6 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Sun, 12 May 2019 00:15:03 +0100 Subject: Add Quote.hs --- src/VeriFuzz/Verilog/Quote.hs | 49 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/VeriFuzz/Verilog/Quote.hs (limited to 'src') 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 -- cgit