summaryrefslogtreecommitdiffstats
path: root/lib/Phi/X11/Atoms.hs
blob: 38f8f3c1cfea92415b6a4884406a3247aaf0457c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
{-# LANGUAGE TemplateHaskell #-}

module Phi.X11.Atoms ( Atoms(..)
                     , initAtoms
                     ) where

import Control.Monad
import Language.Haskell.TH
import Graphics.X11

import Phi.X11.AtomList


$(let atomsName = mkName "Atoms"
      atomNames = map (\atom -> (atom, mkName ("atom" ++ atom))) atoms
      fields = map (\(_, name) -> (name, IsStrict, ConT ''Atom)) atomNames
  in return [DataD [] atomsName [] [RecC atomsName fields] []]
 )

initAtoms :: Display -> IO Atoms
initAtoms display =
  $(do
       atomNames <- mapM (\atom -> do
                             varName <- newName ('_':atom)
                             return (atom, mkName ("atom" ++ atom), varName)
                         ) atoms
       atomInitializers <- forM atomNames $
                           \(atom, _, varName) -> liftM (BindS (VarP varName)) [| internAtom display atom False |]
           
       let atomFieldExps = map (\(_, atomName, varName) -> (atomName, VarE varName)) atomNames
           atomsName = mkName "Atoms"
           atomsContruction = NoBindS $ AppE (VarE 'return) $ RecConE atomsName atomFieldExps

       return $ DoE $ atomInitializers ++ [atomsContruction]
   )