diff options
Diffstat (limited to 'Language.hs')
-rw-r--r-- | Language.hs | 105 |
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 | |||
12 | instance IsString Type where | 12 | instance IsString Type where |
13 | fromString a = Data a | 13 | fromString a = Data a |
14 | 14 | ||
15 | data ModuleDef | ||
16 | = ModuleDef | ||
17 | { moduleName :: String | ||
18 | , imports :: [String] | ||
19 | , definitions :: [DataDef] | ||
20 | } | ||
21 | deriving (Show,Generic) | ||
22 | |||
15 | data DataDef | 23 | data DataDef |
16 | = DataDef | 24 | = DataDef |
17 | { dataName :: String | 25 | { dataName :: String |
@@ -41,6 +49,13 @@ data Field | |||
41 | data Instance | 49 | data Instance |
42 | = Show | 50 | = Show |
43 | | Eq | 51 | | Eq |
52 | | Ord | ||
53 | deriving (Show,Generic) | ||
54 | |||
55 | data Target | ||
56 | = Haskell | ||
57 | | PureScript | ||
58 | | Cpp | ||
44 | deriving (Show,Generic) | 59 | deriving (Show,Generic) |
45 | 60 | ||
46 | data Type | 61 | data 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 | ||
178 | cppType :: AliasMap -> Type -> String | ||
179 | cppType 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 | |||
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 | |||
163 | hasFieldNames :: [Field] -> Bool | 249 | hasFieldNames :: [Field] -> Bool |
164 | hasFieldNames [] = False | 250 | hasFieldNames [] = False |
165 | hasFieldNames l = all (not . null . fieldName) l | 251 | hasFieldNames l = all (not . null . fieldName) l |
@@ -179,14 +265,21 @@ instance FromJSON Instance | |||
179 | instance FromJSON Field | 265 | instance FromJSON Field |
180 | instance FromJSON Type | 266 | instance FromJSON Type |
181 | 267 | ||
182 | type DDef = Writer [DataDef] | 268 | type MDef = Writer [ModuleDef] |
269 | type DDef = Writer ([DataDef],[String]) | ||
183 | type CDef = Writer ([ConstructorDef],[Instance]) | 270 | type CDef = Writer ([ConstructorDef],[Instance]) |
184 | 271 | ||
185 | data_ :: forall a . String -> CDef () -> DDef () | 272 | module_ :: String -> DDef () -> MDef () |
186 | data_ n l = tell [let (c,i) = execWriter l in DataDef n c i] | 273 | module_ n m = tell [let (d,i) = execWriter m in ModuleDef n i d] |
274 | |||
275 | import_ :: [String] -> DDef () | ||
276 | import_ l = tell (mempty,l) | ||
277 | |||
278 | data_ :: String -> CDef () -> DDef () | ||
279 | data_ n l = tell ([let (c,i) = execWriter l in DataDef n c i],mempty) | ||
187 | 280 | ||
188 | alias_ :: String -> Type -> DDef () | 281 | alias_ :: String -> Type -> DDef () |
189 | alias_ n t = tell [TypeAlias n t] | 282 | alias_ n t = tell ([TypeAlias n t],mempty) |
190 | 283 | ||
191 | a #= b = alias_ a b | 284 | a #= b = alias_ a b |
192 | 285 | ||
@@ -199,8 +292,8 @@ instance IsField Field where | |||
199 | instance IsField Type where | 292 | instance IsField Type where |
200 | toField a = Field "" a | 293 | toField a = Field "" a |
201 | 294 | ||
202 | deriving_ :: [Instance] -> CDef () | 295 | deriving_ :: [Target] -> [Instance] -> CDef () |
203 | deriving_ l = tell (mempty,l) | 296 | deriving_ t l = tell (mempty,l) |
204 | 297 | ||
205 | const_ :: String -> [Type] -> CDef () | 298 | const_ :: String -> [Type] -> CDef () |
206 | const_ n t = tell ([ConstructorDef n [Field a b | Field a b <- map toField t]],mempty) | 299 | const_ n t = tell ([ConstructorDef n [Field a b | Field a b <- map toField t]],mempty) |