diff options
Diffstat (limited to 'ddl/Language.hs')
-rw-r--r-- | ddl/Language.hs | 83 |
1 files changed, 60 insertions, 23 deletions
diff --git a/ddl/Language.hs b/ddl/Language.hs index 79c3056..989314a 100644 --- a/ddl/Language.hs +++ b/ddl/Language.hs | |||
@@ -1,4 +1,4 @@ | |||
1 | {-# LANGUAGE RankNTypes, OverloadedStrings, DeriveGeneric, LambdaCase #-} | 1 | {-# LANGUAGE RankNTypes, OverloadedStrings, DeriveGeneric, LambdaCase, RecordWildCards #-} |
2 | module Language where | 2 | module Language where |
3 | 3 | ||
4 | import GHC.Generics | 4 | import GHC.Generics |
@@ -8,6 +8,8 @@ import Data.String | |||
8 | import Data.List | 8 | import Data.List |
9 | import Data.Map (Map) | 9 | import Data.Map (Map) |
10 | import qualified Data.Map as Map | 10 | import qualified Data.Map as Map |
11 | import Data.Set (Set) | ||
12 | import qualified Data.Set as Set | ||
11 | 13 | ||
12 | instance IsString Type where | 14 | instance IsString Type where |
13 | fromString a = Data a | 15 | fromString a = Data a |
@@ -67,17 +69,21 @@ data Type | |||
67 | | Float | 69 | | Float |
68 | | Bool | 70 | | Bool |
69 | | String | 71 | | String |
70 | | V2 Type | ||
71 | | V3 Type | ||
72 | | V4 Type | ||
73 | -- higher order types | 72 | -- higher order types |
74 | | Array Type | 73 | | V2 { type_ :: Type } |
75 | | List Type | 74 | | V3 { type_ :: Type } |
76 | | Maybe Type | 75 | | V4 { type_ :: Type } |
77 | | Map Type Type | 76 | | Array { type_ :: Type } |
77 | | List { type_ :: Type } | ||
78 | | Maybe { type_ :: Type } | ||
79 | | Map { key_ :: Type, value_ :: Type } | ||
78 | -- user defined | 80 | -- user defined |
79 | | Data String | 81 | | Data { name_ :: String } |
80 | deriving (Show,Generic) | 82 | deriving (Show,Generic,Eq,Ord) |
83 | |||
84 | collectTypes :: AliasMap -> ModuleDef -> Set Type | ||
85 | collectTypes aliasMap ModuleDef{..} = Set.fromList $ map (normalize aliasMap) $ concat | ||
86 | [Data dataName : [fieldType | ConstructorDef{..} <- constructors, Field{..} <- fields] | DataDef{..} <- definitions] | ||
81 | 87 | ||
82 | parens :: String -> String | 88 | parens :: String -> String |
83 | parens a | 89 | parens a |
@@ -88,6 +94,13 @@ type AliasMap = Map String Type | |||
88 | 94 | ||
89 | normalize :: AliasMap -> Type -> Type | 95 | normalize :: AliasMap -> Type -> Type |
90 | normalize aliasMap t@(Data n) = Map.findWithDefault t n aliasMap | 96 | normalize aliasMap t@(Data n) = Map.findWithDefault t n aliasMap |
97 | normalize aliasMap (V2 t) = V2 $ normalize aliasMap t | ||
98 | normalize aliasMap (V3 t) = V3 $ normalize aliasMap t | ||
99 | normalize aliasMap (V4 t) = V4 $ normalize aliasMap t | ||
100 | normalize aliasMap (Array t) = Array $ normalize aliasMap t | ||
101 | normalize aliasMap (List t) = List $ normalize aliasMap t | ||
102 | normalize aliasMap (Maybe t) = Maybe $ normalize aliasMap t | ||
103 | normalize aliasMap (Map k v) = Map (normalize aliasMap k) (normalize aliasMap v) | ||
91 | normalize _ t = t | 104 | normalize _ t = t |
92 | 105 | ||
93 | psType :: AliasMap -> Type -> String | 106 | psType :: AliasMap -> Type -> String |
@@ -222,21 +235,41 @@ swiftType aliasMap = \case | |||
222 | javaType :: AliasMap -> Type -> String -- TODO | 235 | javaType :: AliasMap -> Type -> String -- TODO |
223 | javaType aliasMap a = case normalize aliasMap a of | 236 | javaType aliasMap a = case normalize aliasMap a of |
224 | Data t -> t | 237 | Data t -> t |
225 | Int -> "int" | 238 | Int -> "Integer" |
226 | Int32 -> "int" | 239 | Int32 -> "Integer" |
227 | Word -> "int" | 240 | Word -> "Integer" |
228 | Word32 -> "int" | 241 | Word32 -> "Integer" |
229 | Float -> "float" | 242 | Float -> "Float" |
230 | Bool -> "boolean" | 243 | Bool -> "Boolean" |
231 | String -> "String" | 244 | String -> "String" |
232 | Array t -> "ArrayList<" ++ javaType aliasMap t ++ ">" | 245 | Array t -> "ArrayList<" ++ javaType aliasMap t ++ ">" |
233 | List t -> "ArrayList<" ++ javaType aliasMap t ++ ">" | 246 | List t -> "ArrayList<" ++ javaType aliasMap t ++ ">" |
234 | Map k v -> "HashMap<" ++ javaType aliasMap k ++ ", " ++ javaType aliasMap v ++ ">" | 247 | Map k v -> "HashMap<" ++ javaType aliasMap k ++ ", " ++ javaType aliasMap v ++ ">" |
235 | _ -> "int" | 248 | _ -> "Integer" |
249 | --x -> error $ "javaType: " ++ show x | ||
236 | 250 | ||
237 | csType :: AliasMap -> Type -> String -- TODO | 251 | csTypeEnum :: AliasMap -> Type -> String |
238 | csType aliasMap a = case normalize aliasMap a of | 252 | csTypeEnum aliasMap a = case normalize aliasMap a of |
239 | Data t -> t | 253 | Data t -> t |
254 | Int -> "Int" | ||
255 | Int32 -> "Int32" | ||
256 | Word -> "Word" | ||
257 | Word32 -> "Word32" | ||
258 | Float -> "Float" | ||
259 | Bool -> "Bool" | ||
260 | String -> "String" | ||
261 | Array t -> "Array_" ++ csTypeEnum aliasMap t | ||
262 | List t -> "List_" ++ csTypeEnum aliasMap t | ||
263 | Map k v -> "Map_" ++ csTypeEnum aliasMap k ++ "_" ++ csTypeEnum aliasMap v | ||
264 | V2 t -> "V2_" ++ csTypeEnum aliasMap t | ||
265 | V3 t -> "V3_" ++ csTypeEnum aliasMap t | ||
266 | V4 t -> "V4_" ++ csTypeEnum aliasMap t | ||
267 | Maybe t -> "Maybe_" ++ csTypeEnum aliasMap t | ||
268 | x -> error $ "unknown type: " ++ show x | ||
269 | |||
270 | csType :: String -> AliasMap -> Type -> String -- TODO | ||
271 | csType moduleName aliasMap a = case normalize aliasMap a of | ||
272 | Data t -> "global::" ++ moduleName ++ "." ++ t | ||
240 | Int -> "int" | 273 | Int -> "int" |
241 | Int32 -> "int" | 274 | Int32 -> "int" |
242 | Word -> "uint" | 275 | Word -> "uint" |
@@ -244,10 +277,14 @@ csType aliasMap a = case normalize aliasMap a of | |||
244 | Float -> "float" | 277 | Float -> "float" |
245 | Bool -> "bool" | 278 | Bool -> "bool" |
246 | String -> "string" | 279 | String -> "string" |
247 | Array t -> "List<" ++ csType aliasMap t ++ ">" | 280 | Array t -> "List<" ++ csType moduleName aliasMap t ++ ">" |
248 | List t -> "List<" ++ csType aliasMap t ++ ">" | 281 | List t -> "List<" ++ csType moduleName aliasMap t ++ ">" |
249 | Map k v -> "Dictionary<" ++ csType aliasMap k ++ ", " ++ csType aliasMap v ++ ">" | 282 | Map k v -> "Dictionary<" ++ csType moduleName aliasMap k ++ ", " ++ csType moduleName aliasMap v ++ ">" |
250 | _ -> "int" | 283 | V2 t -> "V2<" ++ csType moduleName aliasMap t ++ ">" |
284 | V3 t -> "V3<" ++ csType moduleName aliasMap t ++ ">" | ||
285 | V4 t -> "V4<" ++ csType moduleName aliasMap t ++ ">" | ||
286 | Maybe t -> "Maybe<" ++ parens (csType moduleName aliasMap t) ++ ">" | ||
287 | x -> error $ "unknown type: " ++ show x | ||
251 | 288 | ||
252 | cppType :: AliasMap -> Type -> String | 289 | cppType :: AliasMap -> Type -> String |
253 | cppType aliasMap = \case | 290 | cppType aliasMap = \case |