Skip to content

Instantly share code, notes, and snippets.

@christiaanb
Created August 28, 2014 15:24
Show Gist options
  • Save christiaanb/199d0e77d1372def989c to your computer and use it in GitHub Desktop.
Save christiaanb/199d0e77d1372def989c to your computer and use it in GitHub Desktop.
module CLaSH.Signal.Enabled.SCase where
import Data.Default
import Language.Haskell.TH
scase e = do
e' <- e
case e' of
CaseE e2 ms -> do
nm1 <- newName "eb"
ms' <- mapM (smatch (VarE nm1)) ms
return (CaseE e2 ms')
_ -> error "not a case statement"
smatch :: Exp -> Match -> MatchQ
smatch e (Match (TupP [ConP t ps,VarP n]) b ds) = do
i <- reify t
ms <- mkMatcher (return e) i
error ("correct: " ++ pprint ms)
smatch _ _ = error "not a tuple with a variable"
mkMatcher :: ExpQ -> Info -> ExpQ
mkMatcher e (DataConI nm ty parenName fixity) = do
appsE [[| fmap |],simpleMatcher,e]
where
simpleMatcher = lamCaseE [ match (recP nm []) (normalB [| True |]) []
, match wildP (normalB [| False |]) []
]
mkPattern :: Pat -> ExpQ
mkPattern (ConP t ps) = lamCaseE [ ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment