diff options
author | Csaba Hruska <csaba.hruska@gmail.com> | 2016-11-15 21:46:41 +0100 |
---|---|---|
committer | Csaba Hruska <csaba.hruska@gmail.com> | 2016-11-15 21:46:41 +0100 |
commit | c7df33b55d5faa03e7dda4fe5515eb4562006877 (patch) | |
tree | bccc389078e3981f30554f51db1df3d802286d79 | |
parent | 0762c7388287eee8e4aacee616b3fbaa3709ab57 (diff) |
implement selective instance codegen for purescript backend
-rw-r--r-- | ddl/Generate.hs | 4 | ||||
-rw-r--r-- | ddl/Language.hs | 13 | ||||
-rw-r--r-- | ddl/templates/data.purs.ede | 17 |
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 | |||
21 | instance Unquote [Field] | 21 | instance Unquote [Field] |
22 | instance Unquote [Char] | 22 | instance Unquote [Char] |
23 | instance Quote [Char] | 23 | instance Quote [Char] |
24 | instance Quote [Instance] | ||
24 | instance Unquote DataDef | 25 | instance Unquote DataDef |
25 | instance Unquote Type | 26 | instance Unquote Type |
27 | instance Unquote [(Target,Instance)] | ||
26 | 28 | ||
27 | main :: IO () | 29 | main :: IO () |
28 | main = do | 30 | main = 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 | ||
64 | data Type | 64 | data 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 | ||
84 | filterInstances :: Target -> [(Target,Instance)] -> [Instance] | ||
85 | filterInstances target l = [inst | (t,inst) <- l, t == target] | ||
86 | |||
84 | hasEnumConstructor :: DataDef -> Bool | 87 | hasEnumConstructor :: DataDef -> Bool |
85 | hasEnumConstructor DataDef{..} = or [null fields | ConstructorDef{..} <- constructors] | 88 | hasEnumConstructor DataDef{..} = or [null fields | ConstructorDef{..} <- constructors] |
86 | hasEnumConstructor _ = False | 89 | hasEnumConstructor _ = False |
@@ -359,16 +362,18 @@ instance ToJSON DataDef | |||
359 | instance ToJSON Instance | 362 | instance ToJSON Instance |
360 | instance ToJSON Field | 363 | instance ToJSON Field |
361 | instance ToJSON Type | 364 | instance ToJSON Type |
365 | instance ToJSON Target | ||
362 | 366 | ||
363 | instance FromJSON ConstructorDef | 367 | instance FromJSON ConstructorDef |
364 | instance FromJSON DataDef | 368 | instance FromJSON DataDef |
365 | instance FromJSON Instance | 369 | instance FromJSON Instance |
366 | instance FromJSON Field | 370 | instance FromJSON Field |
367 | instance FromJSON Type | 371 | instance FromJSON Type |
372 | instance FromJSON Target | ||
368 | 373 | ||
369 | type MDef = Writer [ModuleDef] | 374 | type MDef = Writer [ModuleDef] |
370 | type DDef = Writer ([DataDef],[String]) | 375 | type DDef = Writer ([DataDef],[String]) |
371 | type CDef = Writer ([ConstructorDef],[Instance]) | 376 | type CDef = Writer ([ConstructorDef],[(Target,Instance)]) |
372 | 377 | ||
373 | module_ :: String -> DDef () -> MDef () | 378 | module_ :: String -> DDef () -> MDef () |
374 | module_ n m = tell [let (d,i) = execWriter m in ModuleDef n i d] | 379 | module_ 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 | ||
396 | deriving_ :: [Target] -> [Instance] -> CDef () | 401 | deriving_ :: [Target] -> [Instance] -> CDef () |
397 | deriving_ t l = tell (mempty,l) | 402 | deriving_ targets instances = tell (mempty,[(t,i) | i <- instances, t <- targets]) |
398 | 403 | ||
399 | const_ :: String -> [Type] -> CDef () | 404 | const_ :: String -> [Type] -> CDef () |
400 | const_ n t = tell ([ConstructorDef n [Field a b | Field a b <- map toField t]],mempty) | 405 | const_ 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 | |||
43 | derive instance generic{{ t.value.dataName }} :: Generic {{ t.value.dataName }} | 47 | derive instance generic{{ t.value.dataName }} :: Generic {{ t.value.dataName }} |
48 | {% endif %} | ||
49 | {% case i.value %} | ||
50 | {% when "Show" %} | ||
44 | instance show{{ t.value.dataName }} :: Show {{ t.value.dataName }} where show = gShow | 51 | instance show{{ t.value.dataName }} :: Show {{ t.value.dataName }} where show = gShow |
52 | {% when "Eq" %} | ||
45 | instance eq{{ t.value.dataName }} :: Eq {{ t.value.dataName }} where eq = gEq | 53 | instance 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 %} |