{-# 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 :: SpliceQ a -> SpliceQ (CompiledCode a)
compile SpliceQ a
e = Q Exp -> SpliceQ (CompiledCode a)
forall a (m :: * -> *). Quote m => m Exp -> Splice m a
TH.unsafeSpliceCoerce (Q Exp -> SpliceQ (CompiledCode a))
-> Q Exp -> SpliceQ (CompiledCode a)
forall a b. (a -> b) -> a -> b
$ Q Exp -> Q Exp
compileUntyped (Q Exp -> Q Exp) -> Q Exp -> Q Exp
forall a b. (a -> b) -> a -> b
$ TExp a -> Exp
forall a. TExp a -> Exp
TH.unType (TExp a -> Exp) -> SpliceQ a -> Q Exp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SpliceQ a -> SpliceQ a
forall (m :: * -> *) a. Splice m a -> Splice m a
TH.examineSplice SpliceQ a
e
loadFromFile :: FilePath -> TH.SpliceQ (CompiledCode a)
loadFromFile :: FilePath -> SpliceQ (CompiledCode a)
loadFromFile FilePath
fp = SpliceQ (CompiledCode a) -> SpliceQ (CompiledCode a)
forall a (m :: * -> *). m (TExp a) -> m (TExp a)
TH.liftSplice (SpliceQ (CompiledCode a) -> SpliceQ (CompiledCode a))
-> SpliceQ (CompiledCode a) -> SpliceQ (CompiledCode a)
forall a b. (a -> b) -> a -> b
$ do
ByteString
bs <- IO ByteString -> Q ByteString
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO ByteString -> Q ByteString) -> IO ByteString -> Q ByteString
forall a b. (a -> b) -> a -> b
$ FilePath -> IO ByteString
BS.readFile FilePath
fp
SpliceQ (CompiledCode a) -> SpliceQ (CompiledCode a)
forall (m :: * -> *) a. Splice m a -> Splice m 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 = Loc -> FilePath
forall a. Ppr a => a -> FilePath
TH.pprint Loc
loc
[| plc (Proxy :: Proxy $(TH.litT $ TH.strTyLit locStr)) $(e) |]