{-# LANGUAGE TemplateHaskell #-}
module Data.GCode.TH where
import Language.Haskell.TH
import qualified Data.Char
genShortcuts :: Name -> Q [Dec]
genShortcuts names = do
info <- reify names
case info of
TyConI (DataD _cxt _name _tyvarbndr _kind constructors _deriv)
-> do
a <- mapM genTests constructors
b <- mapM genConstructors constructors
return $ a ++ b
_ -> error "Unexpected reify input for genShortcuts"
where
genTests (NormalC name _bangs) = do
varName <- newName "x"
let
funName = mkName $ "is" ++ (nameBase name)
return $ FunD funName
[ Clause
[VarP varName]
(NormalB (InfixE (Just (VarE varName)) (VarE (mkName "codeIsRS274")) (Just (ConE name))))
[]
]
genTests _ = error "Unexpteced input for genTests"
genConstructors (NormalC name _bangs) = do
let
funName = mkName $ (\(x:rest) -> (Data.Char.toLower x : rest)) (nameBase name)
return $ FunD funName
[ Clause
[]
(NormalB ( (VarE (mkName "codeFromName")) `AppE` (ConE name)) )
[]
]
genConstructors _ = error "Unexpteced input for genConstructors"
genWriterEndos :: Name -> Q [Dec]
genWriterEndos names = do
info <- reify names
case info of
TyConI (DataD _cxt _name _tyvarbndr _kind constructors _deriv)
-> do
a <- mapM genConstructors constructors
b <- mapM genConstructorsArgs constructors
return $ a ++ b
_ -> error "Unexpected reify input for genWriterEndos"
where
genConstructors (NormalC name _bangs) = do
let
funName = mkName $ (\(x:rest) -> (Data.Char.toLower x : rest ++ "'")) (nameBase name)
return $ FunD funName
[ Clause
[]
(NormalB ( (VarE (mkName "generateName")) `AppE` (ConE name)) )
[]
]
genConstructors _ = error "Unexpteced input for genConstructors"
genConstructorsArgs (NormalC name _bangs) = do
endoName <- newName "x"
let
funName = mkName $ (\(x:rest) -> (Data.Char.toLower x : rest)) (nameBase name)
return $ FunD funName
[ Clause
[VarP endoName]
(NormalB (((VarE (mkName "generateNameArgs")) `AppE` (ConE name)) `AppE` (VarE endoName)) )
[]
]
genConstructorsArgs _ = error "Unexpteced input for genConstructorArgs"