summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCsaba Hruska <csaba.hruska@gmail.com>2015-09-10 13:14:26 +0200
committerCsaba Hruska <csaba.hruska@gmail.com>2015-09-10 13:14:26 +0200
commitf3b37b12d00bb98dd08cd13acd304a5dd6046ec5 (patch)
tree5e0d254593765cc219bec269371b563524835085
parentee3343a56fc02b6bd2eec93b4c839f08f7ef9a25 (diff)
fix purescript data codegen
-rw-r--r--Definitions.hs3
-rw-r--r--Generate.hs25
-rw-r--r--Language.hs56
-rw-r--r--templates/data.purs.ede20
4 files changed, 73 insertions, 31 deletions
diff --git a/Definitions.hs b/Definitions.hs
index 0423123..743c233 100644
--- a/Definitions.hs
+++ b/Definitions.hs
@@ -236,6 +236,7 @@ ir = execWriter $ do
236 enum_ "Triangles" 236 enum_ "Triangles"
237 enum_ "LinesAdjacency" 237 enum_ "LinesAdjacency"
238 enum_ "TrianglesAdjacency" 238 enum_ "TrianglesAdjacency"
239 deriving_ [Show,Eq]
239 240
240 data_ "OutputPrimitive" $ do 241 data_ "OutputPrimitive" $ do
241 enum_ "TrianglesOutput" 242 enum_ "TrianglesOutput"
@@ -247,6 +248,7 @@ ir = execWriter $ do
247 enum_ "RG" 248 enum_ "RG"
248 enum_ "RGB" 249 enum_ "RGB"
249 enum_ "RGBA" 250 enum_ "RGBA"
251 deriving_ [Show]
250 252
251 data_ "Blending" $ do 253 data_ "Blending" $ do
252 enum_ "NoBlending" 254 enum_ "NoBlending"
@@ -282,6 +284,7 @@ ir = execWriter $ do
282 const_ "IntT" ["ColorArity"] 284 const_ "IntT" ["ColorArity"]
283 const_ "WordT" ["ColorArity"] 285 const_ "WordT" ["ColorArity"]
284 enum_ "ShadowT" 286 enum_ "ShadowT"
287 deriving_ [Show]
285 288
286 data_ "TextureType" $ do 289 data_ "TextureType" $ do
287 const_ "Texture1D" ["TextureDataType", Int] 290 const_ "Texture1D" ["TextureDataType", Int]
diff --git a/Generate.hs b/Generate.hs
index d4946fb..a701b22 100644
--- a/Generate.hs
+++ b/Generate.hs
@@ -4,8 +4,9 @@ import Text.EDE
4import Text.EDE.Filters 4import Text.EDE.Filters
5 5
6import Data.HashMap.Strict (HashMap) 6import Data.HashMap.Strict (HashMap)
7import qualified Data.HashMap.Strict as Map 7import qualified Data.HashMap.Strict as HashMap
8import Data.Text (Text) 8import Data.Text (Text)
9import qualified Data.Map as Map
9 10
10import Data.Time.Clock 11import Data.Time.Clock
11 12
@@ -18,17 +19,6 @@ instance Quote [Char]
18instance Unquote DataDef 19instance Unquote DataDef
19instance Unquote Type 20instance Unquote Type
20 21
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 () 22main :: IO ()
33main = do 23main = do
34 irHs <- eitherParseFile "templates/data.hs.ede" 24 irHs <- eitherParseFile "templates/data.hs.ede"
@@ -45,6 +35,17 @@ main = do
45 , "moduleName" .= name 35 , "moduleName" .= name
46 , "dateTime" .= dt 36 , "dateTime" .= dt
47 ] 37 ]
38 aliasMap = Map.fromList [(n,t) | TypeAlias n t <- def]
39 mylib :: HashMap Text Term
40 mylib = HashMap.fromList
41 -- boolean
42 [ "hasFieldNames" @: hasFieldNames
43 , "parens" @: parens
44 , "constType" @: constType
45 , "hsType" @: hsType aliasMap
46 , "psType" @: psType aliasMap
47 ]
48
48 -- Haskell 49 -- 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 ++ ".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 ++ "Encode.hs") $ LText.unpack x) $ irEncodeHs >>= (\t -> eitherRenderWith mylib t env)
diff --git a/Language.hs b/Language.hs
index 1950c6e..d4cd1b6 100644
--- a/Language.hs
+++ b/Language.hs
@@ -6,6 +6,8 @@ import Data.Aeson (ToJSON(..),FromJSON(..))
6import Control.Monad.Writer 6import Control.Monad.Writer
7import Data.String 7import Data.String
8import Data.List 8import Data.List
9import Data.Map (Map)
10import qualified Data.Map as Map
9 11
10instance IsString Type where 12instance IsString Type where
11 fromString a = Data a 13 fromString a = Data a
@@ -14,6 +16,7 @@ data DataDef
14 = DataDef 16 = DataDef
15 { dataName :: String 17 { dataName :: String
16 , constructors :: [ConstructorDef] 18 , constructors :: [ConstructorDef]
19 , instances :: [Instance]
17 } 20 }
18 | TypeAlias 21 | TypeAlias
19 { aliasName :: String 22 { aliasName :: String
@@ -35,6 +38,11 @@ data Field
35 } 38 }
36 deriving (Show,Generic) 39 deriving (Show,Generic)
37 40
41data Instance
42 = Show
43 | Eq
44 deriving (Show,Generic)
45
38data Type 46data Type
39 = Int 47 = Int
40 | Int32 48 | Int32
@@ -60,8 +68,14 @@ parens a
60 | 1 == length (words a) = a 68 | 1 == length (words a) = a
61 | otherwise = "(" ++ a ++ ")" 69 | otherwise = "(" ++ a ++ ")"
62 70
63psType :: Type -> String 71type AliasMap = Map String Type
64psType = \case 72
73normalize :: AliasMap -> Type -> Type
74normalize aliasMap t@(Data n) = Map.findWithDefault t n aliasMap
75normalize _ t = t
76
77psType :: AliasMap -> Type -> String
78psType aliasMap = \case
65 Int -> "Int" 79 Int -> "Int"
66 Int32 -> "Int32" 80 Int32 -> "Int32"
67 Word -> "Word" 81 Word -> "Word"
@@ -94,17 +108,18 @@ psType = \case
94 V4 (V3 Float) -> "M34F" 108 V4 (V3 Float) -> "M34F"
95 V4 (V4 Float) -> "M44F" 109 V4 (V4 Float) -> "M44F"
96 110
97 Array t -> "Array " ++ parens (hsType t) 111 Array t -> "Array " ++ parens (psType aliasMap t)
98 List t -> "List " ++ parens (hsType t) 112 List t -> "List " ++ parens (psType aliasMap t)
99 Maybe t -> "Maybe " ++ parens (hsType t) 113 Maybe t -> "Maybe " ++ parens (psType aliasMap t)
100 Map String v -> "StrMap " ++ parens (hsType v) 114 Map k v
101 Map k v -> "Map " ++ parens (hsType k) ++ " " ++ parens (hsType v) 115 | String <- normalize aliasMap k -> "StrMap " ++ parens (psType aliasMap v)
116 | otherwise -> "Map " ++ parens (psType aliasMap k) ++ " " ++ parens (psType aliasMap v)
102 -- user defined 117 -- user defined
103 Data t -> t 118 Data t -> t
104 x -> error $ "unknown type: " ++ show x 119 x -> error $ "unknown type: " ++ show x
105 120
106hsType :: Type -> String 121hsType :: AliasMap -> Type -> String
107hsType = \case 122hsType aliasMap = \case
108 Int -> "Int" 123 Int -> "Int"
109 Int32 -> "Int32" 124 Int32 -> "Int32"
110 Word -> "Word" 125 Word -> "Word"
@@ -137,10 +152,10 @@ hsType = \case
137 V4 (V3 Float) -> "M34F" 152 V4 (V3 Float) -> "M34F"
138 V4 (V4 Float) -> "M44F" 153 V4 (V4 Float) -> "M44F"
139 154
140 Array t -> "[" ++ hsType t ++ "]" 155 Array t -> "[" ++ hsType aliasMap t ++ "]"
141 List t -> "[" ++ hsType t ++ "]" 156 List t -> "[" ++ hsType aliasMap t ++ "]"
142 Maybe t -> "Maybe " ++ parens (hsType t) 157 Maybe t -> "Maybe " ++ parens (hsType aliasMap t)
143 Map k v -> "Map " ++ parens (hsType k) ++ " " ++ parens (hsType v) 158 Map k v -> "Map " ++ parens (hsType aliasMap k) ++ " " ++ parens (hsType aliasMap v)
144 -- user defined 159 -- user defined
145 Data t -> t 160 Data t -> t
146 x -> error $ "unknown type: " ++ show x 161 x -> error $ "unknown type: " ++ show x
@@ -154,19 +169,21 @@ constType = head . words . show
154 169
155instance ToJSON ConstructorDef 170instance ToJSON ConstructorDef
156instance ToJSON DataDef 171instance ToJSON DataDef
172instance ToJSON Instance
157instance ToJSON Field 173instance ToJSON Field
158instance ToJSON Type 174instance ToJSON Type
159 175
160instance FromJSON ConstructorDef 176instance FromJSON ConstructorDef
161instance FromJSON DataDef 177instance FromJSON DataDef
178instance FromJSON Instance
162instance FromJSON Field 179instance FromJSON Field
163instance FromJSON Type 180instance FromJSON Type
164 181
165type DDef = Writer [DataDef] 182type DDef = Writer [DataDef]
166type CDef = Writer [ConstructorDef] 183type CDef = Writer ([ConstructorDef],[Instance])
167 184
168data_ :: forall a . String -> CDef () -> DDef () 185data_ :: forall a . String -> CDef () -> DDef ()
169data_ n l = tell [DataDef n $ execWriter l] 186data_ n l = tell [let (c,i) = execWriter l in DataDef n c i]
170 187
171alias_ :: String -> Type -> DDef () 188alias_ :: String -> Type -> DDef ()
172alias_ n t = tell [TypeAlias n t] 189alias_ n t = tell [TypeAlias n t]
@@ -182,14 +199,17 @@ instance IsField Field where
182instance IsField Type where 199instance IsField Type where
183 toField a = Field "" a 200 toField a = Field "" a
184 201
202deriving_ :: [Instance] -> CDef ()
203deriving_ l = tell (mempty,l)
204
185const_ :: String -> [Type] -> CDef () 205const_ :: String -> [Type] -> CDef ()
186const_ n t = tell [ConstructorDef n [Field a b | Field a b <- map toField t]] 206const_ n t = tell ([ConstructorDef n [Field a b | Field a b <- map toField t]],mempty)
187 207
188constR_ :: String -> [Field] -> CDef () 208constR_ :: String -> [Field] -> CDef ()
189constR_ n t = tell [ConstructorDef n [Field a b | Field a b <- map toField t]] 209constR_ n t = tell ([ConstructorDef n [Field a b | Field a b <- map toField t]],mempty)
190 210
191enum_ :: String -> CDef () 211enum_ :: String -> CDef ()
192enum_ n = tell [ConstructorDef n []] 212enum_ n = tell ([ConstructorDef n []],mempty)
193 213
194v2b = V2 Bool 214v2b = V2 Bool
195v3b = V3 Bool 215v3b = V3 Bool
diff --git a/templates/data.purs.ede b/templates/data.purs.ede
index ccaa7de..a26f292 100644
--- a/templates/data.purs.ede
+++ b/templates/data.purs.ede
@@ -2,8 +2,17 @@
2-- {{ dateTime }} 2-- {{ dateTime }}
3 3
4module {{ moduleName }} where 4module {{ moduleName }} where
5import Prelude
6import Data.Generic
7import Data.Maybe (Maybe(..))
8import Data.StrMap (StrMap(..))
9import Data.Map (Map(..))
10import Data.List (List(..))
11import Linear
5 12
6{% for t in definitions %} 13{% for t in dataAndType %}
14{% case t.value | constType %}
15{% when "DataDef" %}
7data {{ t.value.dataName }}{% for c in t.value.constructors %} 16data {{ t.value.dataName }}{% for c in t.value.constructors %}
8{% if c.value.fields | hasFieldNames %} 17{% if c.value.fields | hasFieldNames %}
9 {% if c.first %}={% else %}|{% endif %} {{ c.value.name }} 18 {% if c.first %}={% else %}|{% endif %} {{ c.value.name }}
@@ -13,4 +22,13 @@ data {{ t.value.dataName }}{% for c in t.value.constructors %}
13{% else %} 22{% else %}
14 {% if c.first %}={% else %}|{% endif %} {{ c.value.name }}{% for f in c.value.fields %} {{ f.value.fieldType | psType | parens }}{% endfor %}{% endif %}{% endfor %} 23 {% if c.first %}={% else %}|{% endif %} {{ c.value.name }}{% for f in c.value.fields %} {{ f.value.fieldType | psType | parens }}{% endfor %}{% endif %}{% endfor %}
15 24
25{% when "TypeAlias" %}
26type {{ t.value.aliasName }} = {{ t.value.aliasType | psType }}
27
28{% endcase %}
16{% endfor %} 29{% endfor %}
30{% for t in definitions %}{% let l = t.value.instances | length %}{% if l > 0 %}{# FIXME!!! #}
31derive instance generic{{ t.value.dataName }} :: Generic {{ t.value.dataName }}
32instance show{{ t.value.dataName }} :: Show {{ t.value.dataName }} where show = gShow
33instance eq{{ t.value.dataName }} :: Eq {{ t.value.dataName }} where eq = gEq
34{% endif %}{% endlet %}{% endfor %}