summaryrefslogtreecommitdiff
path: root/Generate.hs
diff options
context:
space:
mode:
authorCsaba Hruska <csaba.hruska@gmail.com>2015-09-07 18:37:15 +0200
committerCsaba Hruska <csaba.hruska@gmail.com>2015-09-07 18:37:15 +0200
commitab758fd36fae40f3cc998065b8bf9c4ce5e8169b (patch)
treebaebb3584770c6abb64c821daa62e1769816aaaa /Generate.hs
add data definition edsl
Diffstat (limited to 'Generate.hs')
-rw-r--r--Generate.hs56
1 files changed, 56 insertions, 0 deletions
diff --git a/Generate.hs b/Generate.hs
new file mode 100644
index 0000000..d4946fb
--- /dev/null
+++ b/Generate.hs
@@ -0,0 +1,56 @@
1{-# LANGUAGE OverloadedStrings, FlexibleInstances #-}
2import qualified Data.Text.Lazy as LText
3import Text.EDE
4import Text.EDE.Filters
5
6import Data.HashMap.Strict (HashMap)
7import qualified Data.HashMap.Strict as Map
8import Data.Text (Text)
9
10import Data.Time.Clock
11
12import Definitions
13import Language
14
15instance Unquote [Field]
16instance Unquote [Char]
17instance Quote [Char]
18instance Unquote DataDef
19instance Unquote Type
20
21mylib :: HashMap Text Term
22mylib = Map.fromList
23 -- boolean
24 [ "hasFieldNames" @: hasFieldNames
25 , "parens" @: parens
26 , "constType" @: constType
27 , "hsType" @: hsType
28 , "psType" @: psType
29 ]
30
31
32main :: IO ()
33main = do
34 irHs <- eitherParseFile "templates/data.hs.ede"
35 irEncodeHs <- eitherParseFile "templates/encode.hs.ede"
36 irDecodeHs <- eitherParseFile "templates/decode.hs.ede"
37 irPs <- eitherParseFile "templates/data.purs.ede"
38 irEncodePs <- eitherParseFile "templates/encode.purs.ede"
39 irDecodePs <- eitherParseFile "templates/decode.purs.ede"
40 let generate name def = do
41 dt <- getCurrentTime
42 let env = fromPairs
43 [ "dataAndType" .= def
44 , "definitions" .= [a | a@DataDef{} <- def ]
45 , "moduleName" .= name
46 , "dateTime" .= dt
47 ]
48 -- Haskell
49 either error (\x -> writeFile ("out/" ++ name ++ ".hs") $ LText.unpack x) $ irHs >>= (\t -> eitherRenderWith mylib t env)
50 either error (\x -> writeFile ("out/" ++ name ++ "Encode.hs") $ LText.unpack x) $ irEncodeHs >>= (\t -> eitherRenderWith mylib t env)
51 either error (\x -> writeFile ("out/" ++ name ++ "Decode.hs") $ LText.unpack x) $ irDecodeHs >>= (\t -> eitherRenderWith mylib t env)
52 -- Purescript
53 either error (\x -> writeFile ("out/" ++ name ++ ".purs") $ LText.unpack x) $ irPs >>= (\t -> eitherRenderWith mylib t env)
54 either error (\x -> writeFile ("out/" ++ name ++ "Encode.purs") $ LText.unpack x) $ irEncodePs >>= (\t -> eitherRenderWith mylib t env)
55 either error (\x -> writeFile ("out/" ++ name ++ "Decode.purs") $ LText.unpack x) $ irDecodePs >>= (\t -> eitherRenderWith mylib t env)
56 generate "IR" ir