summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCsaba Hruska <csaba.hruska@gmail.com>2016-11-15 21:46:41 +0100
committerCsaba Hruska <csaba.hruska@gmail.com>2016-11-15 21:46:41 +0100
commitc7df33b55d5faa03e7dda4fe5515eb4562006877 (patch)
treebccc389078e3981f30554f51db1df3d802286d79
parent0762c7388287eee8e4aacee616b3fbaa3709ab57 (diff)
implement selective instance codegen for purescript backend
-rw-r--r--ddl/Generate.hs4
-rw-r--r--ddl/Language.hs13
-rw-r--r--ddl/templates/data.purs.ede17
3 files changed, 28 insertions, 6 deletions
diff --git a/ddl/Generate.hs b/ddl/Generate.hs
index b276324..855406f 100644
--- a/ddl/Generate.hs
+++ b/ddl/Generate.hs
@@ -21,8 +21,10 @@ import Language
21instance Unquote [Field] 21instance Unquote [Field]
22instance Unquote [Char] 22instance Unquote [Char]
23instance Quote [Char] 23instance Quote [Char]
24instance Quote [Instance]
24instance Unquote DataDef 25instance Unquote DataDef
25instance Unquote Type 26instance Unquote Type
27instance Unquote [(Target,Instance)]
26 28
27main :: IO () 29main :: IO ()
28main = do 30main = do
@@ -60,6 +62,8 @@ main = do
60 , "javaType" @: javaType aliasMap 62 , "javaType" @: javaType aliasMap
61 , "swiftType" @: swiftType aliasMap 63 , "swiftType" @: swiftType aliasMap
62 , "hasEnumConstructor" @: hasEnumConstructor 64 , "hasEnumConstructor" @: hasEnumConstructor
65 , "psInstances" @: filterInstances PureScript
66 , "hsInstances" @: filterInstances Haskell
63 ] 67 ]
64 68
65 toPath a = flip map a $ \case 69 toPath a = flip map a $ \case
diff --git a/ddl/Language.hs b/ddl/Language.hs
index be684db..9701023 100644
--- a/ddl/Language.hs
+++ b/ddl/Language.hs
@@ -26,7 +26,7 @@ data DataDef
26 = DataDef 26 = DataDef
27 { dataName :: String 27 { dataName :: String
28 , constructors :: [ConstructorDef] 28 , constructors :: [ConstructorDef]
29 , instances :: [Instance] 29 , instances :: [(Target,Instance)]
30 } 30 }
31 | TypeAlias 31 | TypeAlias
32 { aliasName :: String 32 { aliasName :: String
@@ -59,7 +59,7 @@ data Target
59 | PureScript 59 | PureScript
60 | Cpp 60 | Cpp
61 | CSharp 61 | CSharp
62 deriving (Show,Generic) 62 deriving (Show,Eq,Generic)
63 63
64data Type 64data Type
65 = Int 65 = Int
@@ -81,6 +81,9 @@ data Type
81 | Data { name_ :: String } 81 | Data { name_ :: String }
82 deriving (Show,Generic,Eq,Ord) 82 deriving (Show,Generic,Eq,Ord)
83 83
84filterInstances :: Target -> [(Target,Instance)] -> [Instance]
85filterInstances target l = [inst | (t,inst) <- l, t == target]
86
84hasEnumConstructor :: DataDef -> Bool 87hasEnumConstructor :: DataDef -> Bool
85hasEnumConstructor DataDef{..} = or [null fields | ConstructorDef{..} <- constructors] 88hasEnumConstructor DataDef{..} = or [null fields | ConstructorDef{..} <- constructors]
86hasEnumConstructor _ = False 89hasEnumConstructor _ = False
@@ -359,16 +362,18 @@ instance ToJSON DataDef
359instance ToJSON Instance 362instance ToJSON Instance
360instance ToJSON Field 363instance ToJSON Field
361instance ToJSON Type 364instance ToJSON Type
365instance ToJSON Target
362 366
363instance FromJSON ConstructorDef 367instance FromJSON ConstructorDef
364instance FromJSON DataDef 368instance FromJSON DataDef
365instance FromJSON Instance 369instance FromJSON Instance
366instance FromJSON Field 370instance FromJSON Field
367instance FromJSON Type 371instance FromJSON Type
372instance FromJSON Target
368 373
369type MDef = Writer [ModuleDef] 374type MDef = Writer [ModuleDef]
370type DDef = Writer ([DataDef],[String]) 375type DDef = Writer ([DataDef],[String])
371type CDef = Writer ([ConstructorDef],[Instance]) 376type CDef = Writer ([ConstructorDef],[(Target,Instance)])
372 377
373module_ :: String -> DDef () -> MDef () 378module_ :: String -> DDef () -> MDef ()
374module_ n m = tell [let (d,i) = execWriter m in ModuleDef n i d] 379module_ n m = tell [let (d,i) = execWriter m in ModuleDef n i d]
@@ -394,7 +399,7 @@ instance IsField Type where
394 toField a = Field "" a 399 toField a = Field "" a
395 400
396deriving_ :: [Target] -> [Instance] -> CDef () 401deriving_ :: [Target] -> [Instance] -> CDef ()
397deriving_ t l = tell (mempty,l) 402deriving_ targets instances = tell (mempty,[(t,i) | i <- instances, t <- targets])
398 403
399const_ :: String -> [Type] -> CDef () 404const_ :: String -> [Type] -> CDef ()
400const_ n t = tell ([ConstructorDef n [Field a b | Field a b <- map toField t]],mempty) 405const_ n t = tell ([ConstructorDef n [Field a b | Field a b <- map toField t]],mempty)
diff --git a/ddl/templates/data.purs.ede b/ddl/templates/data.purs.ede
index f8b3e5d..2374c1d 100644
--- a/ddl/templates/data.purs.ede
+++ b/ddl/templates/data.purs.ede
@@ -39,11 +39,24 @@ type {{ t.value.aliasName }} = {{ t.value.aliasType | psType }}
39 39
40{% endcase %} 40{% endcase %}
41{% endfor %} 41{% endfor %}
42{% for t in definitions %}{% let l = t.value.instances | length %}{% if l > 0 %}{# FIXME!!! #} 42{% for t in definitions %}
43{% let l = t.value.instances | psInstances %}
44{% for i in l %}
45{% if i.first %}
46
43derive instance generic{{ t.value.dataName }} :: Generic {{ t.value.dataName }} 47derive instance generic{{ t.value.dataName }} :: Generic {{ t.value.dataName }}
48{% endif %}
49{% case i.value %}
50{% when "Show" %}
44instance show{{ t.value.dataName }} :: Show {{ t.value.dataName }} where show = gShow 51instance show{{ t.value.dataName }} :: Show {{ t.value.dataName }} where show = gShow
52{% when "Eq" %}
45instance eq{{ t.value.dataName }} :: Eq {{ t.value.dataName }} where eq = gEq 53instance eq{{ t.value.dataName }} :: Eq {{ t.value.dataName }} where eq = gEq
46{% endif %}{% endlet %}{% endfor %} 54{% else %}
55-- FIXME: {{ i.value }} instance is not supported!
56{% endcase %}
57{% endfor %}
58{% endlet %}
59{% endfor %}
47 60
48{# JSON Encode and Decode #} 61{# JSON Encode and Decode #}
49{% for t in definitions %} 62{% for t in definitions %}