aboutsummaryrefslogtreecommitdiff
path: root/hsm-core/Hsm/Core/Serial.hs
diff options
context:
space:
mode:
Diffstat (limited to 'hsm-core/Hsm/Core/Serial.hs')
-rw-r--r--hsm-core/Hsm/Core/Serial.hs43
1 files changed, 24 insertions, 19 deletions
diff --git a/hsm-core/Hsm/Core/Serial.hs b/hsm-core/Hsm/Core/Serial.hs
index a0efca3..7c607ff 100644
--- a/hsm-core/Hsm/Core/Serial.hs
+++ b/hsm-core/Hsm/Core/Serial.hs
@@ -2,21 +2,22 @@
module Hsm.Core.Serial
( makeSerial
- ) where
+ )
+where
import GHC.Num (integerFromInt)
import Language.Haskell.TH
- ( Body(NormalB)
- , Clause(Clause)
- , Con(NormalC)
- , Dec(DataD, FunD, SigD)
- , DerivClause(DerivClause)
- , Exp(LitE)
- , Lit(IntegerL)
+ ( Body (NormalB)
+ , Clause (Clause)
+ , Con (NormalC)
+ , Dec (DataD, FunD, SigD)
+ , DerivClause (DerivClause)
+ , Exp (LitE)
+ , Lit (IntegerL)
, Name
- , Pat(ConP)
+ , Pat (ConP)
, Q
- , Type(AppT, ArrowT, ConT)
+ , Type (AppT, ArrowT, ConT)
, mkName
)
@@ -36,13 +37,17 @@ import Language.Haskell.TH
-- Generates a data type `GPIOPin` with constructors `GPIO2`, `GPIO3` `GPIO4`,
-- and a function `pinLine :: GPIOPin -> Int`.
makeSerial :: String -> String -> String -> Name -> [Int] -> Q [Dec]
-makeSerial name suffix mapFun mapType idxs =
- return [DataD [] dtName [] Nothing (idxCons <$> idxs) [derivClause], SigD mapFunName . AppT (AppT ArrowT $ ConT dtName) $ ConT mapType, FunD mapFunName $ mapFunClause <$> idxs]
+makeSerial name suffix mapFunction mapType indices =
+ return
+ [ DataD [] dataName [] Nothing (indexCons <$> indices) [deriveClause]
+ , SigD mapFunctionName $ ArrowT `AppT` ConT dataName `AppT` ConT mapType
+ , FunD mapFunctionName $ mapFunctionClause <$> indices
+ ]
where
- dtName = mkName $ name <> suffix
- idxName idx = mkName $ name <> show idx
- idxCons idx = NormalC (idxName idx) []
- derivClause = DerivClause Nothing [ConT ''Bounded, ConT ''Enum, ConT ''Show]
- mapFunName = mkName mapFun
- mapFunBody = NormalB . LitE . IntegerL . integerFromInt
- mapFunClause idx = Clause [ConP (idxName idx) [] []] (mapFunBody idx) []
+ dataName = mkName $ name <> suffix
+ indexName index = mkName $ name <> show index
+ indexCons index = NormalC (indexName index) []
+ deriveClause = DerivClause Nothing [ConT ''Bounded, ConT ''Enum, ConT ''Show]
+ mapFunctionName = mkName mapFunction
+ mapFunctionBody = NormalB . LitE . IntegerL . integerFromInt
+ mapFunctionClause index = Clause [ConP (indexName index) [] []] (mapFunctionBody index) []