diff options
author | Csaba Hruska <csaba.hruska@gmail.com> | 2015-09-16 22:51:00 +0200 |
---|---|---|
committer | Csaba Hruska <csaba.hruska@gmail.com> | 2015-09-16 22:51:00 +0200 |
commit | 44e12314657ec0655251a82ca81336f8eab9cd70 (patch) | |
tree | 09abd2e95926269a790c5b4fe05e49e74fae1d39 | |
parent | 76f4fd3deff8fd63e15d2198a2f633d933ccbae9 (diff) |
initial C# support in DDL
-rw-r--r-- | Generate.hs | 5 | ||||
-rw-r--r-- | Language.hs | 31 | ||||
-rw-r--r-- | templates/data.cs.ede | 27 |
3 files changed, 47 insertions, 16 deletions
diff --git a/Generate.hs b/Generate.hs index 3b5bb0e..a35cb66 100644 --- a/Generate.hs +++ b/Generate.hs | |||
@@ -24,6 +24,7 @@ main :: IO () | |||
24 | main = do | 24 | main = do |
25 | dataHpp <- eitherParseFile "templates/data.hpp.ede" | 25 | dataHpp <- eitherParseFile "templates/data.hpp.ede" |
26 | dataCpp <- eitherParseFile "templates/data.cpp.ede" | 26 | dataCpp <- eitherParseFile "templates/data.cpp.ede" |
27 | dataCs <- eitherParseFile "templates/data.cs.ede" | ||
27 | dataHs <- eitherParseFile "templates/data.hs.ede" | 28 | dataHs <- eitherParseFile "templates/data.hs.ede" |
28 | dataPs <- eitherParseFile "templates/data.purs.ede" | 29 | dataPs <- eitherParseFile "templates/data.purs.ede" |
29 | let generate (ModuleDef name imports def) = do | 30 | let generate (ModuleDef name imports def) = do |
@@ -44,7 +45,7 @@ main = do | |||
44 | , "hsType" @: hsType aliasMap | 45 | , "hsType" @: hsType aliasMap |
45 | , "psType" @: psType aliasMap | 46 | , "psType" @: psType aliasMap |
46 | , "cppType" @: cppType aliasMap | 47 | , "cppType" @: cppType aliasMap |
47 | , "mangleTypeName" @: mangleTypeName aliasMap | 48 | , "csType" @: csType aliasMap |
48 | ] | 49 | ] |
49 | 50 | ||
50 | -- Haskell | 51 | -- Haskell |
@@ -54,4 +55,6 @@ main = do | |||
54 | -- C++ | 55 | -- C++ |
55 | either error (\x -> writeFile ("out/" ++ name ++ ".hpp") $ LText.unpack x) $ dataHpp >>= (\t -> eitherRenderWith mylib t env) | 56 | either error (\x -> writeFile ("out/" ++ name ++ ".hpp") $ LText.unpack x) $ dataHpp >>= (\t -> eitherRenderWith mylib t env) |
56 | either error (\x -> writeFile ("out/" ++ name ++ ".cpp") $ LText.unpack x) $ dataCpp >>= (\t -> eitherRenderWith mylib t env) | 57 | either error (\x -> writeFile ("out/" ++ name ++ ".cpp") $ LText.unpack x) $ dataCpp >>= (\t -> eitherRenderWith mylib t env) |
58 | -- C# | ||
59 | either error (\x -> writeFile ("out/" ++ name ++ ".cs") $ LText.unpack x) $ dataCs >>= (\t -> eitherRenderWith mylib t env) | ||
57 | mapM_ generate $ execWriter modules | 60 | mapM_ generate $ execWriter modules |
diff --git a/Language.hs b/Language.hs index 1a2385d..e0e53cf 100644 --- a/Language.hs +++ b/Language.hs | |||
@@ -56,6 +56,7 @@ data Target | |||
56 | = Haskell | 56 | = Haskell |
57 | | PureScript | 57 | | PureScript |
58 | | Cpp | 58 | | Cpp |
59 | | CSharp | ||
59 | deriving (Show,Generic) | 60 | deriving (Show,Generic) |
60 | 61 | ||
61 | data Type | 62 | data Type |
@@ -175,6 +176,21 @@ hsType aliasMap = \case | |||
175 | Data t -> t | 176 | Data t -> t |
176 | x -> error $ "unknown type: " ++ show x | 177 | x -> error $ "unknown type: " ++ show x |
177 | 178 | ||
179 | csType :: AliasMap -> Type -> String | ||
180 | csType aliasMap a = case normalize aliasMap a of | ||
181 | Data t -> t | ||
182 | Int -> "int" | ||
183 | Int32 -> "int" | ||
184 | Word -> "uint" | ||
185 | Word32 -> "uint" | ||
186 | Float -> "float" | ||
187 | Bool -> "bool" | ||
188 | String -> "string" | ||
189 | Array t -> "List<" ++ csType aliasMap t ++ ">" | ||
190 | List t -> "List<" ++ csType aliasMap t ++ ">" | ||
191 | Map k v -> "Dictionary<" ++ csType aliasMap k ++ ", " ++ csType aliasMap v ++ ">" | ||
192 | _ -> "int" | ||
193 | |||
178 | cppType :: AliasMap -> Type -> String | 194 | cppType :: AliasMap -> Type -> String |
179 | cppType aliasMap = \case | 195 | cppType aliasMap = \case |
180 | Data t -> "::" ++ t | 196 | Data t -> "::" ++ t |
@@ -231,21 +247,6 @@ cppType aliasMap = \case | |||
231 | x -> error $ "unknown type: " ++ show x | 247 | x -> error $ "unknown type: " ++ show x |
232 | -} | 248 | -} |
233 | 249 | ||
234 | mangleTypeName :: AliasMap -> Type -> String | ||
235 | mangleTypeName aliasMap t = case normalize aliasMap t of | ||
236 | {- | ||
237 | Int -> "Int" | ||
238 | Int32 -> "Int32" | ||
239 | Word -> "Word" | ||
240 | Word32 -> "Word32" | ||
241 | Float -> "Float" | ||
242 | Bool -> "Bool" | ||
243 | String -> "String" | ||
244 | -} | ||
245 | -- user defined | ||
246 | Data t -> "ToJSON" | ||
247 | t -> cppType aliasMap t | ||
248 | |||
249 | hasFieldNames :: [Field] -> Bool | 250 | hasFieldNames :: [Field] -> Bool |
250 | hasFieldNames [] = False | 251 | hasFieldNames [] = False |
251 | hasFieldNames l = all (not . null . fieldName) l | 252 | hasFieldNames l = all (not . null . fieldName) l |
diff --git a/templates/data.cs.ede b/templates/data.cs.ede new file mode 100644 index 0000000..c88d41c --- /dev/null +++ b/templates/data.cs.ede | |||
@@ -0,0 +1,27 @@ | |||
1 | // generated file, do not modify! | ||
2 | // {{ dateTime }} | ||
3 | |||
4 | using System.Collections.Generic; | ||
5 | |||
6 | {% for m in imports %} | ||
7 | using {{ m.value }}; | ||
8 | {% endfor %} | ||
9 | |||
10 | {% for t in dataAndType %} | ||
11 | {% case t.value | constType %} | ||
12 | {% when "DataDef" %} | ||
13 | class {{ t.value.dataName }} { | ||
14 | public enum Tag { {% for c in t.value.constructors %} | ||
15 | {{ c.value.name }}{% if !c.last %},{% endif %}{% endfor %} | ||
16 | }; | ||
17 | public Tag tag; | ||
18 | }; | ||
19 | |||
20 | namespace data { {% for c in t.value.constructors %}{% if !(c.value.fields | empty) %} | ||
21 | class {{ c.value.name }} : global::{{ t.value.dataName }} { {% for f in c.value.fields %}{% if c.value.fields | hasFieldNames %} | ||
22 | public {{ f.value.fieldType | csType }} {{ f.value.fieldName }};{% else %} | ||
23 | public {{ f.value.fieldType | csType | parens }} _{{ f.index0 }};{% endif %}{% endfor %} | ||
24 | };{% endif %}{% endfor %} | ||
25 | } | ||
26 | {% endcase %} | ||
27 | {% endfor %} | ||