{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
module PlutusTx.TH (
compile,
compileUntyped,
loadFromFile) where
import Data.Proxy
import Language.Haskell.TH qualified as TH
import Language.Haskell.TH.Syntax qualified as TH
import Language.Haskell.TH.Syntax.Compat qualified as TH
import PlutusTx.Code
import PlutusTx.Plugin.Utils
import Control.Monad.IO.Class
import Data.ByteString qualified as BS
import Prelude
compile :: TH.SpliceQ a -> TH.SpliceQ (CompiledCode a)
compile :: forall a. SpliceQ a -> SpliceQ (CompiledCode a)
compile SpliceQ a
e = forall a (m :: * -> *). Quote m => m Exp -> Splice m a
TH.unsafeSpliceCoerce forall a b. (a -> b) -> a -> b
$ Q Exp -> Q Exp
compileUntyped forall a b. (a -> b) -> a -> b
$ forall a. TExp a -> Exp
TH.unType forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a. Splice m a -> m (TExp a)
TH.examineSplice SpliceQ a
e
loadFromFile :: FilePath -> TH.SpliceQ (CompiledCode a)
loadFromFile :: forall a. FilePath -> SpliceQ (CompiledCode a)
loadFromFile FilePath
fp = forall a (m :: * -> *). m (TExp a) -> Splice m a
TH.liftSplice forall a b. (a -> b) -> a -> b
$ do
ByteString
bs <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ FilePath -> IO ByteString
BS.readFile FilePath
fp
forall (m :: * -> *) a. Splice m a -> m (TExp a)
TH.examineSplice [|| SerializedCode bs Nothing mempty ||]
compileUntyped :: TH.Q TH.Exp -> TH.Q TH.Exp
compileUntyped :: Q Exp -> Q Exp
compileUntyped Q Exp
e = do
FilePath -> Q ()
TH.addCorePlugin FilePath
"PlutusTx.Plugin"
Loc
loc <- Q Loc
TH.location
let locStr :: FilePath
locStr = forall a. Ppr a => a -> FilePath
TH.pprint Loc
loc
[| plc (Proxy :: Proxy $(TH.litT $ TH.strTyLit locStr)) $(e) |]