diff options
author | Csaba Hruska <csaba.hruska@gmail.com> | 2015-09-26 10:53:48 +0200 |
---|---|---|
committer | Csaba Hruska <csaba.hruska@gmail.com> | 2015-09-26 10:53:48 +0200 |
commit | b010b1e7006d6ffcf226590dd9bac04a694f8cbd (patch) | |
tree | de8a6afaed9b82cda73b7cee37a3b9b5477677e5 | |
parent | 307895ce6e4fb875afba0999c257b573eff6aa4c (diff) |
ddl: initial swift backend
-rw-r--r-- | Generate.hs | 10 | ||||
-rw-r--r-- | Language.hs | 58 | ||||
-rw-r--r-- | templates/data.java.ede | 26 | ||||
-rw-r--r-- | templates/data.swift.ede | 25 |
4 files changed, 119 insertions, 0 deletions
diff --git a/Generate.hs b/Generate.hs index a35cb66..4eea1dc 100644 --- a/Generate.hs +++ b/Generate.hs | |||
@@ -22,6 +22,8 @@ instance Unquote Type | |||
22 | 22 | ||
23 | main :: IO () | 23 | main :: IO () |
24 | main = do | 24 | main = do |
25 | dataSwift <- eitherParseFile "templates/data.swift.ede" | ||
26 | dataJava <- eitherParseFile "templates/data.java.ede" | ||
25 | dataHpp <- eitherParseFile "templates/data.hpp.ede" | 27 | dataHpp <- eitherParseFile "templates/data.hpp.ede" |
26 | dataCpp <- eitherParseFile "templates/data.cpp.ede" | 28 | dataCpp <- eitherParseFile "templates/data.cpp.ede" |
27 | dataCs <- eitherParseFile "templates/data.cs.ede" | 29 | dataCs <- eitherParseFile "templates/data.cs.ede" |
@@ -46,6 +48,8 @@ main = do | |||
46 | , "psType" @: psType aliasMap | 48 | , "psType" @: psType aliasMap |
47 | , "cppType" @: cppType aliasMap | 49 | , "cppType" @: cppType aliasMap |
48 | , "csType" @: csType aliasMap | 50 | , "csType" @: csType aliasMap |
51 | , "javaType" @: javaType aliasMap | ||
52 | , "swiftType" @: swiftType aliasMap | ||
49 | ] | 53 | ] |
50 | 54 | ||
51 | -- Haskell | 55 | -- Haskell |
@@ -55,6 +59,12 @@ main = do | |||
55 | -- C++ | 59 | -- C++ |
56 | either error (\x -> writeFile ("out/" ++ name ++ ".hpp") $ LText.unpack x) $ dataHpp >>= (\t -> eitherRenderWith mylib t env) | 60 | either error (\x -> writeFile ("out/" ++ name ++ ".hpp") $ LText.unpack x) $ dataHpp >>= (\t -> eitherRenderWith mylib t env) |
57 | either error (\x -> writeFile ("out/" ++ name ++ ".cpp") $ LText.unpack x) $ dataCpp >>= (\t -> eitherRenderWith mylib t env) | 61 | either error (\x -> writeFile ("out/" ++ name ++ ".cpp") $ LText.unpack x) $ dataCpp >>= (\t -> eitherRenderWith mylib t env) |
62 | {- | ||
63 | -- Java | ||
64 | either error (\x -> writeFile ("out/" ++ name ++ ".java") $ LText.unpack x) $ dataJava >>= (\t -> eitherRenderWith mylib t env) | ||
58 | -- C# | 65 | -- C# |
59 | either error (\x -> writeFile ("out/" ++ name ++ ".cs") $ LText.unpack x) $ dataCs >>= (\t -> eitherRenderWith mylib t env) | 66 | either error (\x -> writeFile ("out/" ++ name ++ ".cs") $ LText.unpack x) $ dataCs >>= (\t -> eitherRenderWith mylib t env) |
67 | -} | ||
68 | -- Swift | ||
69 | either error (\x -> writeFile ("out/" ++ name ++ ".swift") $ LText.unpack x) $ dataSwift >>= (\t -> eitherRenderWith mylib t env) | ||
60 | mapM_ generate $ execWriter modules | 70 | mapM_ generate $ execWriter modules |
diff --git a/Language.hs b/Language.hs index ab96349..11bce87 100644 --- a/Language.hs +++ b/Language.hs | |||
@@ -176,6 +176,64 @@ hsType aliasMap = \case | |||
176 | Data t -> t | 176 | Data t -> t |
177 | x -> error $ "unknown type: " ++ show x | 177 | x -> error $ "unknown type: " ++ show x |
178 | 178 | ||
179 | swiftType :: AliasMap -> Type -> String | ||
180 | swiftType aliasMap = \case | ||
181 | Int -> "Int" | ||
182 | Int32 -> "Int32" | ||
183 | Word -> "UInt" | ||
184 | Word32 -> "UInt32" | ||
185 | Float -> "Float" | ||
186 | Bool -> "Bool" | ||
187 | String -> "String" | ||
188 | {- | ||
189 | V2 Int -> "V2I" | ||
190 | V2 Word -> "V2U" | ||
191 | V2 Float -> "V2F" | ||
192 | V2 Bool -> "V2B" | ||
193 | V2 (V2 Float) -> "M22F" | ||
194 | V2 (V3 Float) -> "M32F" | ||
195 | V2 (V4 Float) -> "M42F" | ||
196 | |||
197 | V3 Int -> "V3I" | ||
198 | V3 Word -> "V3U" | ||
199 | V3 Float -> "V3F" | ||
200 | V3 Bool -> "V3B" | ||
201 | V3 (V2 Float) -> "M23F" | ||
202 | V3 (V3 Float) -> "M33F" | ||
203 | V3 (V4 Float) -> "M43F" | ||
204 | |||
205 | V4 Int -> "V4I" | ||
206 | V4 Word -> "V4U" | ||
207 | V4 Float -> "V4F" | ||
208 | V4 Bool -> "V4B" | ||
209 | V4 (V2 Float) -> "M24F" | ||
210 | V4 (V3 Float) -> "M34F" | ||
211 | V4 (V4 Float) -> "M44F" | ||
212 | -} | ||
213 | Array t -> "Array<" ++ parens (swiftType aliasMap t) ++ ">" | ||
214 | List t -> "Array<" ++ swiftType aliasMap t ++ ">" | ||
215 | Maybe t -> parens (swiftType aliasMap t) ++ "?" | ||
216 | Map k v -> "Dictionary<" ++ parens (swiftType aliasMap k) ++ ", " ++ parens (swiftType aliasMap v) ++ ">" | ||
217 | -- user defined | ||
218 | Data t -> t | ||
219 | _ -> "Int" | ||
220 | x -> error $ "unknown type: " ++ show x | ||
221 | |||
222 | javaType :: AliasMap -> Type -> String -- TODO | ||
223 | javaType aliasMap a = case normalize aliasMap a of | ||
224 | Data t -> t | ||
225 | Int -> "int" | ||
226 | Int32 -> "int" | ||
227 | Word -> "int" | ||
228 | Word32 -> "int" | ||
229 | Float -> "float" | ||
230 | Bool -> "boolean" | ||
231 | String -> "String" | ||
232 | Array t -> "ArrayList<" ++ javaType aliasMap t ++ ">" | ||
233 | List t -> "ArrayList<" ++ javaType aliasMap t ++ ">" | ||
234 | Map k v -> "HashMap<" ++ javaType aliasMap k ++ ", " ++ javaType aliasMap v ++ ">" | ||
235 | _ -> "int" | ||
236 | |||
179 | csType :: AliasMap -> Type -> String -- TODO | 237 | csType :: AliasMap -> Type -> String -- TODO |
180 | csType aliasMap a = case normalize aliasMap a of | 238 | csType aliasMap a = case normalize aliasMap a of |
181 | Data t -> t | 239 | Data t -> t |
diff --git a/templates/data.java.ede b/templates/data.java.ede new file mode 100644 index 0000000..c85a521 --- /dev/null +++ b/templates/data.java.ede | |||
@@ -0,0 +1,26 @@ | |||
1 | // generated file, do not modify! | ||
2 | // {{ dateTime }} | ||
3 | |||
4 | {% for m in imports %} | ||
5 | import {{ m.value }}; | ||
6 | {% endfor %} | ||
7 | |||
8 | public class {{ moduleName }} { | ||
9 | {% for t in dataAndType %} | ||
10 | {% case t.value | constType %} | ||
11 | {% when "DataDef" %} | ||
12 | public class {{ t.value.dataName }} { | ||
13 | public enum Tag { {% for c in t.value.constructors %} | ||
14 | {{ c.value.name }}{% if !c.last %},{% endif %}{% endfor %} | ||
15 | } | ||
16 | public Tag tag; | ||
17 | {% for c in t.value.constructors %}{% if !(c.value.fields | empty) %} | ||
18 | public class {{ c.value.name }}_ extends {{ t.value.dataName }} { {% for f in c.value.fields %}{% if c.value.fields | hasFieldNames %} | ||
19 | public {{ f.value.fieldType | javaType }} {{ f.value.fieldName }};{% else %} | ||
20 | public {{ f.value.fieldType | javaType }} _{{ f.index0 }};{% endif %}{% endfor %} | ||
21 | }{% endif %}{% endfor %} | ||
22 | } | ||
23 | |||
24 | {% endcase %} | ||
25 | {% endfor %} | ||
26 | } \ No newline at end of file | ||
diff --git a/templates/data.swift.ede b/templates/data.swift.ede new file mode 100644 index 0000000..09b90da --- /dev/null +++ b/templates/data.swift.ede | |||
@@ -0,0 +1,25 @@ | |||
1 | // generated file, do not modify! | ||
2 | // {{ dateTime }} | ||
3 | |||
4 | {% for t in dataAndType %} | ||
5 | {% case t.value | constType %} | ||
6 | {% when "DataDef" %} | ||
7 | enum {{ t.value.dataName }} { | ||
8 | {% for c in t.value.constructors %} | ||
9 | {% if c.value.fields | hasFieldNames %} | ||
10 | case {{ c.value.name }}({{ c.value.name }}_Data) | ||
11 | struct {{ c.value.name }}_Data { | ||
12 | {% for f in c.value.fields %} | ||
13 | var {{ f.value.fieldName }} : {{ f.value.fieldType | swiftType }} | ||
14 | {% endfor %} | ||
15 | } | ||
16 | {% else %} | ||
17 | case {{ c.value.name }}{% if !(c.value.fields | empty) %}({% for f in c.value.fields %}{{ f.value.fieldType | swiftType }}{% if !f.last %},{% endif %}{% endfor %}){% endif %} | ||
18 | {% endif %} | ||
19 | {% endfor %} | ||
20 | } | ||
21 | {% when "TypeAlias" %} | ||
22 | typealias {{ t.value.aliasName }} = {{ t.value.aliasType | swiftType }} | ||
23 | {% endcase %} | ||
24 | |||
25 | {% endfor %} | ||