1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
{-# LANGUAGE RankNTypes, OverloadedStrings, DeriveGeneric, LambdaCase #-}
module Language where
import GHC.Generics
import Data.Aeson (ToJSON(..),FromJSON(..))
import Control.Monad.Writer
import Data.String
import Data.List
import Data.Map (Map)
import qualified Data.Map as Map
instance IsString Type where
fromString a = Data a
data ModuleDef
= ModuleDef
{ moduleName :: String
, imports :: [String]
, definitions :: [DataDef]
}
deriving (Show,Generic)
data DataDef
= DataDef
{ dataName :: String
, constructors :: [ConstructorDef]
, instances :: [Instance]
}
| TypeAlias
{ aliasName :: String
, aliasType :: Type
}
deriving (Show,Generic)
data ConstructorDef
= ConstructorDef
{ name :: String
, fields :: [Field]
}
deriving (Show,Generic)
data Field
= Field
{ fieldName :: String
, fieldType :: Type
}
deriving (Show,Generic)
data Instance
= Show
| Eq
| Ord
deriving (Show,Generic)
data Target
= Haskell
| PureScript
| Cpp
| CSharp
deriving (Show,Generic)
data Type
= Int
| Int32
| Word
| Word32
| Float
| Bool
| String
| V2 Type
| V3 Type
| V4 Type
-- higher order types
| Array Type
| List Type
| Maybe Type
| Map Type Type
-- user defined
| Data String
deriving (Show,Generic)
parens :: String -> String
parens a
| 1 == length (words a) = a
| otherwise = "(" ++ a ++ ")"
type AliasMap = Map String Type
normalize :: AliasMap -> Type -> Type
normalize aliasMap t@(Data n) = Map.findWithDefault t n aliasMap
normalize _ t = t
psType :: AliasMap -> Type -> String
psType aliasMap = \case
Int -> "Int"
Int32 -> "Int32"
Word -> "Word"
Word32 -> "Word32"
Float -> "Float"
Bool -> "Bool"
String -> "String"
V2 Int -> "V2I"
V2 Word -> "V2U"
V2 Float -> "V2F"
V2 Bool -> "V2B"
V2 (V2 Float) -> "M22F"
V2 (V3 Float) -> "M32F"
V2 (V4 Float) -> "M42F"
V3 Int -> "V3I"
V3 Word -> "V3U"
V3 Float -> "V3F"
V3 Bool -> "V3B"
V3 (V2 Float) -> "M23F"
V3 (V3 Float) -> "M33F"
V3 (V4 Float) -> "M43F"
V4 Int -> "V4I"
V4 Word -> "V4U"
V4 Float -> "V4F"
V4 Bool -> "V4B"
V4 (V2 Float) -> "M24F"
V4 (V3 Float) -> "M34F"
V4 (V4 Float) -> "M44F"
Array t -> "Array " ++ parens (psType aliasMap t)
List t -> "List " ++ parens (psType aliasMap t)
Maybe t -> "Maybe " ++ parens (psType aliasMap t)
Map k v
| String <- normalize aliasMap k -> "StrMap " ++ parens (psType aliasMap v)
| otherwise -> "Map " ++ parens (psType aliasMap k) ++ " " ++ parens (psType aliasMap v)
-- user defined
Data t -> t
x -> error $ "unknown type: " ++ show x
hsType :: AliasMap -> Type -> String
hsType aliasMap = \case
Int -> "Int"
Int32 -> "Int32"
Word -> "Word"
Word32 -> "Word32"
Float -> "Float"
Bool -> "Bool"
String -> "String"
V2 Int -> "V2I"
V2 Word -> "V2U"
V2 Float -> "V2F"
V2 Bool -> "V2B"
V2 (V2 Float) -> "M22F"
V2 (V3 Float) -> "M32F"
V2 (V4 Float) -> "M42F"
V3 Int -> "V3I"
V3 Word -> "V3U"
V3 Float -> "V3F"
V3 Bool -> "V3B"
V3 (V2 Float) -> "M23F"
V3 (V3 Float) -> "M33F"
V3 (V4 Float) -> "M43F"
V4 Int -> "V4I"
V4 Word -> "V4U"
V4 Float -> "V4F"
V4 Bool -> "V4B"
V4 (V2 Float) -> "M24F"
V4 (V3 Float) -> "M34F"
V4 (V4 Float) -> "M44F"
Array t -> "Vector " ++ parens (hsType aliasMap t)
List t -> "[" ++ hsType aliasMap t ++ "]"
Maybe t -> "Maybe " ++ parens (hsType aliasMap t)
Map k v -> "Map " ++ parens (hsType aliasMap k) ++ " " ++ parens (hsType aliasMap v)
-- user defined
Data t -> t
x -> error $ "unknown type: " ++ show x
csType :: AliasMap -> Type -> String -- TODO
csType aliasMap a = case normalize aliasMap a of
Data t -> t
Int -> "int"
Int32 -> "int"
Word -> "uint"
Word32 -> "uint"
Float -> "float"
Bool -> "bool"
String -> "string"
Array t -> "List<" ++ csType aliasMap t ++ ">"
List t -> "List<" ++ csType aliasMap t ++ ">"
Map k v -> "Dictionary<" ++ csType aliasMap k ++ ", " ++ csType aliasMap v ++ ">"
_ -> "int"
cppType :: AliasMap -> Type -> String
cppType aliasMap = \case
Int -> "Int"
Int32 -> "Int32"
Word -> "Word"
Word32 -> "Word32"
Float -> "Float"
Bool -> "Bool"
String -> "String"
V2 Int -> "V2I"
V2 Word -> "V2U"
V2 Float -> "V2F"
V2 Bool -> "V2B"
V2 (V2 Float) -> "M22F"
V2 (V3 Float) -> "M32F"
V2 (V4 Float) -> "M42F"
V3 Int -> "V3I"
V3 Word -> "V3U"
V3 Float -> "V3F"
V3 Bool -> "V3B"
V3 (V2 Float) -> "M23F"
V3 (V3 Float) -> "M33F"
V3 (V4 Float) -> "M43F"
V4 Int -> "V4I"
V4 Word -> "V4U"
V4 Float -> "V4F"
V4 Bool -> "V4B"
V4 (V2 Float) -> "M24F"
V4 (V3 Float) -> "M34F"
V4 (V4 Float) -> "M44F"
Array t -> "std::vector<" ++ cppType aliasMap t ++ ">"
List t -> "std::vector<" ++ cppType aliasMap t ++ ">"
Maybe t -> "Maybe<" ++ cppType aliasMap t ++ ">"
Map k v -> "std::map<" ++ cppType aliasMap k ++ ", " ++ cppType aliasMap v ++ ">"
-- user defined
Data t -> case normalize aliasMap (Data t) of
Data n -> "std::shared_ptr<::" ++ n ++ ">"
_ -> "::" ++ t
x -> error $ "unknown type: " ++ show x
hasFieldNames :: [Field] -> Bool
hasFieldNames [] = False
hasFieldNames l = all (not . null . fieldName) l
constType :: DataDef -> String
constType = head . words . show
instance ToJSON ConstructorDef
instance ToJSON DataDef
instance ToJSON Instance
instance ToJSON Field
instance ToJSON Type
instance FromJSON ConstructorDef
instance FromJSON DataDef
instance FromJSON Instance
instance FromJSON Field
instance FromJSON Type
type MDef = Writer [ModuleDef]
type DDef = Writer ([DataDef],[String])
type CDef = Writer ([ConstructorDef],[Instance])
module_ :: String -> DDef () -> MDef ()
module_ n m = tell [let (d,i) = execWriter m in ModuleDef n i d]
import_ :: [String] -> DDef ()
import_ l = tell (mempty,l)
data_ :: String -> CDef () -> DDef ()
data_ n l = tell ([let (c,i) = execWriter l in DataDef n c i],mempty)
alias_ :: String -> Type -> DDef ()
alias_ n t = tell ([TypeAlias n t],mempty)
a #= b = alias_ a b
class IsField a where
toField :: a -> Field
instance IsField Field where
toField a = a
instance IsField Type where
toField a = Field "" a
deriving_ :: [Target] -> [Instance] -> CDef ()
deriving_ t l = tell (mempty,l)
const_ :: String -> [Type] -> CDef ()
const_ n t = tell ([ConstructorDef n [Field a b | Field a b <- map toField t]],mempty)
constR_ :: String -> [Field] -> CDef ()
constR_ n t = tell ([ConstructorDef n [Field a b | Field a b <- map toField t]],mempty)
enum_ :: String -> CDef ()
enum_ n = tell ([ConstructorDef n []],mempty)
v2b = V2 Bool
v3b = V3 Bool
v4b = V4 Bool
v2u = V2 Word
v3u = V3 Word
v4u = V4 Word
v2i = V2 Int
v3i = V3 Int
v4i = V4 Int
v2f = V2 Float
v3f = V3 Float
v4f = V4 Float
m22 = V2 v2f
m23 = V3 v2f
m24 = V4 v2f
m32 = V2 v3f
m33 = V3 v3f
m34 = V4 v3f
m42 = V2 v4f
m43 = V3 v4f
m44 = V4 v4f
(#::) :: String -> Type -> Field
a #:: b = Field a b
{-
definitions:
ADT
Record
Vector
instances:
Show
Eq
Ord
serialization:
json: encode/decode
other?
-}
|