diff options
author | Csaba Hruska <csaba.hruska@gmail.com> | 2015-09-07 18:37:15 +0200 |
---|---|---|
committer | Csaba Hruska <csaba.hruska@gmail.com> | 2015-09-07 18:37:15 +0200 |
commit | ab758fd36fae40f3cc998065b8bf9c4ce5e8169b (patch) | |
tree | baebb3584770c6abb64c821daa62e1769816aaaa /Generate.hs |
add data definition edsl
Diffstat (limited to 'Generate.hs')
-rw-r--r-- | Generate.hs | 56 |
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 #-} | ||
2 | import qualified Data.Text.Lazy as LText | ||
3 | import Text.EDE | ||
4 | import Text.EDE.Filters | ||
5 | |||
6 | import Data.HashMap.Strict (HashMap) | ||
7 | import qualified Data.HashMap.Strict as Map | ||
8 | import Data.Text (Text) | ||
9 | |||
10 | import Data.Time.Clock | ||
11 | |||
12 | import Definitions | ||
13 | import Language | ||
14 | |||
15 | instance Unquote [Field] | ||
16 | instance Unquote [Char] | ||
17 | instance Quote [Char] | ||
18 | instance Unquote DataDef | ||
19 | instance Unquote Type | ||
20 | |||
21 | mylib :: HashMap Text Term | ||
22 | mylib = Map.fromList | ||
23 | -- boolean | ||
24 | [ "hasFieldNames" @: hasFieldNames | ||
25 | , "parens" @: parens | ||
26 | , "constType" @: constType | ||
27 | , "hsType" @: hsType | ||
28 | , "psType" @: psType | ||
29 | ] | ||
30 | |||
31 | |||
32 | main :: IO () | ||
33 | main = 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 | ||