summaryrefslogtreecommitdiff
path: root/Language.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Language.hs')
-rw-r--r--Language.hs105
1 files changed, 99 insertions, 6 deletions
diff --git a/Language.hs b/Language.hs
index 15780f6..1a2385d 100644
--- a/Language.hs
+++ b/Language.hs
@@ -12,6 +12,14 @@ import qualified Data.Map as Map
12instance IsString Type where 12instance IsString Type where
13 fromString a = Data a 13 fromString a = Data a
14 14
15data ModuleDef
16 = ModuleDef
17 { moduleName :: String
18 , imports :: [String]
19 , definitions :: [DataDef]
20 }
21 deriving (Show,Generic)
22
15data DataDef 23data DataDef
16 = DataDef 24 = DataDef
17 { dataName :: String 25 { dataName :: String
@@ -41,6 +49,13 @@ data Field
41data Instance 49data Instance
42 = Show 50 = Show
43 | Eq 51 | Eq
52 | Ord
53 deriving (Show,Generic)
54
55data Target
56 = Haskell
57 | PureScript
58 | Cpp
44 deriving (Show,Generic) 59 deriving (Show,Generic)
45 60
46data Type 61data Type
@@ -160,6 +175,77 @@ hsType aliasMap = \case
160 Data t -> t 175 Data t -> t
161 x -> error $ "unknown type: " ++ show x 176 x -> error $ "unknown type: " ++ show x
162 177
178cppType :: AliasMap -> Type -> String
179cppType aliasMap = \case
180 Data t -> "::" ++ t
181 Int -> "Int"
182 Int32 -> "Int32"
183 Word -> "Word"
184 Word32 -> "Word32"
185 Float -> "Float"
186 Bool -> "Bool"
187 String -> "String"
188 Array t -> "std::vector<" ++ cppType aliasMap t ++ ">"
189 List t -> "std::vector<" ++ cppType aliasMap t ++ ">"
190 Map k v -> "std::map<" ++ cppType aliasMap k ++ ", " ++ cppType aliasMap v ++ ">"
191 _ -> "int"
192{-
193 Int -> "Int"
194 Int32 -> "Int32"
195 Word -> "Word"
196 Word32 -> "Word32"
197 Float -> "Float"
198 Bool -> "Bool"
199 String -> "String"
200
201 V2 Int -> "V2I"
202 V2 Word -> "V2U"
203 V2 Float -> "V2F"
204 V2 Bool -> "V2B"
205 V2 (V2 Float) -> "M22F"
206 V2 (V3 Float) -> "M32F"
207 V2 (V4 Float) -> "M42F"
208
209 V3 Int -> "V3I"
210 V3 Word -> "V3U"
211 V3 Float -> "V3F"
212 V3 Bool -> "V3B"
213 V3 (V2 Float) -> "M23F"
214 V3 (V3 Float) -> "M33F"
215 V3 (V4 Float) -> "M43F"
216
217 V4 Int -> "V4I"
218 V4 Word -> "V4U"
219 V4 Float -> "V4F"
220 V4 Bool -> "V4B"
221 V4 (V2 Float) -> "M24F"
222 V4 (V3 Float) -> "M34F"
223 V4 (V4 Float) -> "M44F"
224
225 Array t -> "Vector " ++ parens (cppType aliasMap t)
226 List t -> "[" ++ cppType aliasMap t ++ "]"
227 Maybe t -> "Maybe " ++ parens (cppType aliasMap t)
228 Map k v -> "Map " ++ parens (cppType aliasMap k) ++ " " ++ parens (cppType aliasMap v)
229 -- user defined
230 Data t -> t
231 x -> error $ "unknown type: " ++ show x
232-}
233
234mangleTypeName :: AliasMap -> Type -> String
235mangleTypeName 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
163hasFieldNames :: [Field] -> Bool 249hasFieldNames :: [Field] -> Bool
164hasFieldNames [] = False 250hasFieldNames [] = False
165hasFieldNames l = all (not . null . fieldName) l 251hasFieldNames l = all (not . null . fieldName) l
@@ -179,14 +265,21 @@ instance FromJSON Instance
179instance FromJSON Field 265instance FromJSON Field
180instance FromJSON Type 266instance FromJSON Type
181 267
182type DDef = Writer [DataDef] 268type MDef = Writer [ModuleDef]
269type DDef = Writer ([DataDef],[String])
183type CDef = Writer ([ConstructorDef],[Instance]) 270type CDef = Writer ([ConstructorDef],[Instance])
184 271
185data_ :: forall a . String -> CDef () -> DDef () 272module_ :: String -> DDef () -> MDef ()
186data_ n l = tell [let (c,i) = execWriter l in DataDef n c i] 273module_ n m = tell [let (d,i) = execWriter m in ModuleDef n i d]
274
275import_ :: [String] -> DDef ()
276import_ l = tell (mempty,l)
277
278data_ :: String -> CDef () -> DDef ()
279data_ n l = tell ([let (c,i) = execWriter l in DataDef n c i],mempty)
187 280
188alias_ :: String -> Type -> DDef () 281alias_ :: String -> Type -> DDef ()
189alias_ n t = tell [TypeAlias n t] 282alias_ n t = tell ([TypeAlias n t],mempty)
190 283
191a #= b = alias_ a b 284a #= b = alias_ a b
192 285
@@ -199,8 +292,8 @@ instance IsField Field where
199instance IsField Type where 292instance IsField Type where
200 toField a = Field "" a 293 toField a = Field "" a
201 294
202deriving_ :: [Instance] -> CDef () 295deriving_ :: [Target] -> [Instance] -> CDef ()
203deriving_ l = tell (mempty,l) 296deriving_ t l = tell (mempty,l)
204 297
205const_ :: String -> [Type] -> CDef () 298const_ :: String -> [Type] -> CDef ()
206const_ n t = tell ([ConstructorDef n [Field a b | Field a b <- map toField t]],mempty) 299const_ n t = tell ([ConstructorDef n [Field a b | Field a b <- map toField t]],mempty)