diff options
-rw-r--r-- | Definitions.hs | 85 | ||||
-rw-r--r-- | Generate.hs | 34 | ||||
-rw-r--r-- | Language.hs | 105 | ||||
-rw-r--r-- | templates/data.cpp.ede | 63 | ||||
-rw-r--r-- | templates/data.hpp.ede | 51 | ||||
-rw-r--r-- | templates/data.hs.ede | 11 | ||||
-rw-r--r-- | templates/data.purs.ede | 4 |
7 files changed, 278 insertions, 75 deletions
diff --git a/Definitions.hs b/Definitions.hs index d4b69be..9618407 100644 --- a/Definitions.hs +++ b/Definitions.hs | |||
@@ -1,10 +1,10 @@ | |||
1 | {-# LANGUAGE OverloadedStrings #-} | 1 | {-# LANGUAGE OverloadedStrings #-} |
2 | module Definitions where | 2 | module Definitions (modules) where |
3 | 3 | ||
4 | import Control.Monad.Writer | 4 | import Control.Monad.Writer |
5 | import Language | 5 | import Language |
6 | 6 | ||
7 | ir = execWriter $ do | 7 | ir = do |
8 | -- type aliases | 8 | -- type aliases |
9 | "StreamName" #= Int | 9 | "StreamName" #= Int |
10 | "ProgramName" #= Int | 10 | "ProgramName" #= Int |
@@ -118,6 +118,7 @@ ir = execWriter $ do | |||
118 | enum_ "UTexture2DMSArray" | 118 | enum_ "UTexture2DMSArray" |
119 | enum_ "UTextureBuffer" | 119 | enum_ "UTextureBuffer" |
120 | enum_ "UTexture2DRect" | 120 | enum_ "UTexture2DRect" |
121 | deriving_ [Haskell] [Eq,Ord] | ||
121 | 122 | ||
122 | data_ "PointSpriteCoordOrigin" $ do | 123 | data_ "PointSpriteCoordOrigin" $ do |
123 | enum_ "LowerLeft" | 124 | enum_ "LowerLeft" |
@@ -219,9 +220,6 @@ ir = execWriter $ do | |||
219 | , "backStencilOp" #:: "StencilOperation" -- Used for back faced triangles. | 220 | , "backStencilOp" #:: "StencilOperation" -- Used for back faced triangles. |
220 | ] | 221 | ] |
221 | 222 | ||
222 | data_ "StencilTests" $ do | ||
223 | const_ "StencilTests" ["StencilTest", "StencilTest"] | ||
224 | |||
225 | data_ "StencilTest" $ do | 223 | data_ "StencilTest" $ do |
226 | constR_ "StencilTest" | 224 | constR_ "StencilTest" |
227 | [ "stencilComparision" #:: "ComparisonFunction" -- The function used to compare the @stencilReference@ and the stencil buffers value with. | 225 | [ "stencilComparision" #:: "ComparisonFunction" -- The function used to compare the @stencilReference@ and the stencil buffers value with. |
@@ -229,6 +227,9 @@ ir = execWriter $ do | |||
229 | , "stencilMask" #:: Word32 -- A bit mask with ones in each position that should be compared and written to the stencil buffer. | 227 | , "stencilMask" #:: Word32 -- A bit mask with ones in each position that should be compared and written to the stencil buffer. |
230 | ] | 228 | ] |
231 | 229 | ||
230 | data_ "StencilTests" $ do | ||
231 | const_ "StencilTests" ["StencilTest", "StencilTest"] | ||
232 | |||
232 | -- primitive types | 233 | -- primitive types |
233 | data_ "FetchPrimitive" $ do | 234 | data_ "FetchPrimitive" $ do |
234 | enum_ "Points" | 235 | enum_ "Points" |
@@ -236,7 +237,7 @@ ir = execWriter $ do | |||
236 | enum_ "Triangles" | 237 | enum_ "Triangles" |
237 | enum_ "LinesAdjacency" | 238 | enum_ "LinesAdjacency" |
238 | enum_ "TrianglesAdjacency" | 239 | enum_ "TrianglesAdjacency" |
239 | deriving_ [Show,Eq] | 240 | deriving_ [PureScript] [Show,Eq] |
240 | 241 | ||
241 | data_ "OutputPrimitive" $ do | 242 | data_ "OutputPrimitive" $ do |
242 | enum_ "TrianglesOutput" | 243 | enum_ "TrianglesOutput" |
@@ -248,7 +249,7 @@ ir = execWriter $ do | |||
248 | enum_ "RG" | 249 | enum_ "RG" |
249 | enum_ "RGB" | 250 | enum_ "RGB" |
250 | enum_ "RGBA" | 251 | enum_ "RGBA" |
251 | deriving_ [Show] | 252 | deriving_ [PureScript] [Show] |
252 | 253 | ||
253 | data_ "Blending" $ do | 254 | data_ "Blending" $ do |
254 | enum_ "NoBlending" | 255 | enum_ "NoBlending" |
@@ -284,7 +285,7 @@ ir = execWriter $ do | |||
284 | const_ "IntT" ["ColorArity"] | 285 | const_ "IntT" ["ColorArity"] |
285 | const_ "WordT" ["ColorArity"] | 286 | const_ "WordT" ["ColorArity"] |
286 | enum_ "ShadowT" | 287 | enum_ "ShadowT" |
287 | deriving_ [Show] | 288 | deriving_ [PureScript] [Show] |
288 | 289 | ||
289 | data_ "TextureType" $ do | 290 | data_ "TextureType" $ do |
290 | const_ "Texture1D" ["TextureDataType", Int] | 291 | const_ "Texture1D" ["TextureDataType", Int] |
@@ -314,14 +315,15 @@ ir = execWriter $ do | |||
314 | enum_ "ClampToEdge" | 315 | enum_ "ClampToEdge" |
315 | enum_ "ClampToBorder" | 316 | enum_ "ClampToBorder" |
316 | 317 | ||
317 | data_ "ImageRef" $ do | ||
318 | const_ "TextureImage" ["TextureName", Int, Maybe Int] -- Texture name, mip index, array index | ||
319 | const_ "Framebuffer" ["ImageSemantic"] | ||
320 | |||
321 | data_ "ImageSemantic" $ do | 318 | data_ "ImageSemantic" $ do |
322 | enum_ "Depth" | 319 | enum_ "Depth" |
323 | enum_ "Stencil" | 320 | enum_ "Stencil" |
324 | enum_ "Color" | 321 | enum_ "Color" |
322 | deriving_ [Haskell] [Eq] | ||
323 | |||
324 | data_ "ImageRef" $ do | ||
325 | const_ "TextureImage" ["TextureName", Int, Maybe Int] -- Texture name, mip index, array index | ||
326 | const_ "Framebuffer" ["ImageSemantic"] | ||
325 | 327 | ||
326 | data_ "ClearImage" $ do | 328 | data_ "ClearImage" $ do |
327 | constR_ "ClearImage" | 329 | constR_ "ClearImage" |
@@ -344,16 +346,6 @@ ir = execWriter $ do | |||
344 | const_ "SaveImage" ["FrameBufferComponent", "ImageRef"] -- from framebuffer component to texture (image) | 346 | const_ "SaveImage" ["FrameBufferComponent", "ImageRef"] -- from framebuffer component to texture (image) |
345 | const_ "LoadImage" ["ImageRef", "FrameBufferComponent"] -- from texture (image) to framebuffer component | 347 | const_ "LoadImage" ["ImageRef", "FrameBufferComponent"] -- from texture (image) to framebuffer component |
346 | 348 | ||
347 | data_ "TextureDescriptor" $ do -- texture size, type, array, mipmap | ||
348 | constR_ "TextureDescriptor" | ||
349 | [ "textureType" #:: "TextureType" | ||
350 | , "textureSize" #:: "Value" | ||
351 | , "textureSemantic" #:: "ImageSemantic" | ||
352 | , "textureSampler" #:: "SamplerDescriptor" | ||
353 | , "textureBaseLevel" #:: Int | ||
354 | , "textureMaxLevel" #:: Int | ||
355 | ] | ||
356 | |||
357 | data_ "SamplerDescriptor" $ do | 349 | data_ "SamplerDescriptor" $ do |
358 | constR_ "SamplerDescriptor" | 350 | constR_ "SamplerDescriptor" |
359 | [ "samplerWrapS" #:: "EdgeMode" | 351 | [ "samplerWrapS" #:: "EdgeMode" |
@@ -368,6 +360,16 @@ ir = execWriter $ do | |||
368 | , "samplerCompareFunc" #:: Maybe "ComparisonFunction" | 360 | , "samplerCompareFunc" #:: Maybe "ComparisonFunction" |
369 | ] | 361 | ] |
370 | 362 | ||
363 | data_ "TextureDescriptor" $ do -- texture size, type, array, mipmap | ||
364 | constR_ "TextureDescriptor" | ||
365 | [ "textureType" #:: "TextureType" | ||
366 | , "textureSize" #:: "Value" | ||
367 | , "textureSemantic" #:: "ImageSemantic" | ||
368 | , "textureSampler" #:: "SamplerDescriptor" | ||
369 | , "textureBaseLevel" #:: Int | ||
370 | , "textureMaxLevel" #:: Int | ||
371 | ] | ||
372 | |||
371 | data_ "Parameter" $ do | 373 | data_ "Parameter" $ do |
372 | constR_ "Parameter" | 374 | constR_ "Parameter" |
373 | [ "name" #:: String | 375 | [ "name" #:: String |
@@ -428,8 +430,9 @@ ir = execWriter $ do | |||
428 | , "streams" #:: Array "StreamData" | 430 | , "streams" #:: Array "StreamData" |
429 | , "commands" #:: Array "Command" | 431 | , "commands" #:: Array "Command" |
430 | ] | 432 | ] |
433 | deriving_ [Haskell] [Show] | ||
431 | 434 | ||
432 | mesh = execWriter $ do | 435 | mesh = do |
433 | data_ "MeshAttribute" $ do | 436 | data_ "MeshAttribute" $ do |
434 | const_ "A_Float" [Array Float] | 437 | const_ "A_Float" [Array Float] |
435 | const_ "A_V2F" [Array v2f] | 438 | const_ "A_V2F" [Array v2f] |
@@ -454,7 +457,7 @@ mesh = execWriter $ do | |||
454 | , "mPrimitive" #:: "MeshPrimitive" | 457 | , "mPrimitive" #:: "MeshPrimitive" |
455 | ] | 458 | ] |
456 | 459 | ||
457 | typeInfo = execWriter $ do | 460 | typeInfo = do |
458 | data_ "TypeInfo" $ do | 461 | data_ "TypeInfo" $ do |
459 | constR_ "TypeInfo" | 462 | constR_ "TypeInfo" |
460 | [ "startLine" #:: Int | 463 | [ "startLine" #:: Int |
@@ -466,29 +469,11 @@ typeInfo = execWriter $ do | |||
466 | 469 | ||
467 | data_ "MyEither" $ do | 470 | data_ "MyEither" $ do |
468 | const_ "MyLeft" ["TypeInfo", Array "TypeInfo"] | 471 | const_ "MyLeft" ["TypeInfo", Array "TypeInfo"] |
469 | const_ "MyRight" ["TypeInfo"{- "Pipeline" -}, Array "TypeInfo"] | 472 | const_ "MyRight" ["Pipeline", Array "TypeInfo"] |
470 | 473 | ||
471 | {- | 474 | modules = do |
472 | type TypeInfoRecord = | 475 | module_ "IR" ir |
473 | { startLine :: Int | 476 | module_ "Mesh" mesh |
474 | , startColumn :: Int | 477 | module_ "TypeInfo" $ do |
475 | , endLine :: Int | 478 | import_ ["IR"] |
476 | , endColumn :: Int | 479 | typeInfo |
477 | , text :: String | ||
478 | } | ||
479 | data TypeInfo = TypeInfo TypeInfoRecord | ||
480 | |||
481 | instance decodeJsonTypeInfo :: DecodeJson TypeInfo where | ||
482 | decodeJson json = do | ||
483 | obj <- decodeJson json | ||
484 | startL <- obj .? "startL" | ||
485 | startC <- obj .? "startC" | ||
486 | endL <- obj .? "endL" | ||
487 | endC <- obj .? "endC" | ||
488 | text <- obj .? "text" | ||
489 | return $ TypeInfo {startLine:startL, startColumn:startC, endLine:endL, endColumn:endC, text:text} | ||
490 | |||
491 | data MyEither | ||
492 | = MyLeft TypeInfo (Array TypeInfo) | ||
493 | | MyRight Pipeline (Array TypeInfo) | ||
494 | -} \ No newline at end of file | ||
diff --git a/Generate.hs b/Generate.hs index 8bc725f..3b5bb0e 100644 --- a/Generate.hs +++ b/Generate.hs | |||
@@ -9,6 +9,7 @@ import Data.Text (Text) | |||
9 | import qualified Data.Map as Map | 9 | import qualified Data.Map as Map |
10 | 10 | ||
11 | import Data.Time.Clock | 11 | import Data.Time.Clock |
12 | import Control.Monad.Writer | ||
12 | 13 | ||
13 | import Definitions | 14 | import Definitions |
14 | import Language | 15 | import Language |
@@ -21,31 +22,36 @@ instance Unquote Type | |||
21 | 22 | ||
22 | main :: IO () | 23 | main :: IO () |
23 | main = do | 24 | main = do |
24 | irHs <- eitherParseFile "templates/data.hs.ede" | 25 | dataHpp <- eitherParseFile "templates/data.hpp.ede" |
25 | irPs <- eitherParseFile "templates/data.purs.ede" | 26 | dataCpp <- eitherParseFile "templates/data.cpp.ede" |
26 | let generate name def = do | 27 | dataHs <- eitherParseFile "templates/data.hs.ede" |
28 | dataPs <- eitherParseFile "templates/data.purs.ede" | ||
29 | let generate (ModuleDef name imports def) = do | ||
27 | dt <- getCurrentTime | 30 | dt <- getCurrentTime |
28 | let env = fromPairs | 31 | let env = fromPairs |
29 | [ "dataAndType" .= def | 32 | [ "dataAndType" .= def |
30 | , "definitions" .= [a | a@DataDef{} <- def ] | 33 | , "definitions" .= [a | a@DataDef{} <- def ] |
31 | , "moduleName" .= name | 34 | , "moduleName" .= name |
32 | , "dateTime" .= dt | 35 | , "dateTime" .= dt |
36 | , "imports" .= imports | ||
33 | ] | 37 | ] |
34 | aliasMap = Map.fromList [(n,t) | TypeAlias n t <- def] | 38 | aliasMap = Map.fromList [(n,t) | TypeAlias n t <- def] |
35 | mylib :: HashMap Text Term | 39 | mylib :: HashMap Text Term |
36 | mylib = HashMap.fromList | 40 | mylib = HashMap.fromList |
37 | -- boolean | 41 | [ "hasFieldNames" @: hasFieldNames |
38 | [ "hasFieldNames" @: hasFieldNames | 42 | , "parens" @: parens |
39 | , "parens" @: parens | 43 | , "constType" @: constType |
40 | , "constType" @: constType | 44 | , "hsType" @: hsType aliasMap |
41 | , "hsType" @: hsType aliasMap | 45 | , "psType" @: psType aliasMap |
42 | , "psType" @: psType aliasMap | 46 | , "cppType" @: cppType aliasMap |
47 | , "mangleTypeName" @: mangleTypeName aliasMap | ||
43 | ] | 48 | ] |
44 | 49 | ||
45 | -- Haskell | 50 | -- Haskell |
46 | either error (\x -> writeFile ("out/" ++ name ++ ".hs") $ LText.unpack x) $ irHs >>= (\t -> eitherRenderWith mylib t env) | 51 | either error (\x -> writeFile ("out/" ++ name ++ ".hs") $ LText.unpack x) $ dataHs >>= (\t -> eitherRenderWith mylib t env) |
47 | -- Purescript | 52 | -- Purescript |
48 | either error (\x -> writeFile ("out/" ++ name ++ ".purs") $ LText.unpack x) $ irPs >>= (\t -> eitherRenderWith mylib t env) | 53 | either error (\x -> writeFile ("out/" ++ name ++ ".purs") $ LText.unpack x) $ dataPs >>= (\t -> eitherRenderWith mylib t env) |
49 | generate "IR" ir | 54 | -- C++ |
50 | generate "Mesh" mesh | 55 | either error (\x -> writeFile ("out/" ++ name ++ ".hpp") $ LText.unpack x) $ dataHpp >>= (\t -> eitherRenderWith mylib t env) |
51 | generate "TypeInfo" typeInfo | 56 | either error (\x -> writeFile ("out/" ++ name ++ ".cpp") $ LText.unpack x) $ dataCpp >>= (\t -> eitherRenderWith mylib t env) |
57 | mapM_ generate $ execWriter modules | ||
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) |
diff --git a/templates/data.cpp.ede b/templates/data.cpp.ede new file mode 100644 index 0000000..8e106a6 --- /dev/null +++ b/templates/data.cpp.ede | |||
@@ -0,0 +1,63 @@ | |||
1 | // generated file, do not modify! | ||
2 | // {{ dateTime }} | ||
3 | |||
4 | #include "{{ moduleName }}.hpp" | ||
5 | |||
6 | template<> json toJSON<String>(String &v) { | ||
7 | return json(v); | ||
8 | } | ||
9 | |||
10 | template<> json toJSON<Float>(Float &v) { | ||
11 | return json(v); | ||
12 | } | ||
13 | |||
14 | template<> json toJSON<bool>(bool &v) { | ||
15 | return json(v); | ||
16 | } | ||
17 | |||
18 | template<> json toJSON<int>(int &v) { | ||
19 | return json(v); | ||
20 | } | ||
21 | |||
22 | template<> json toJSON<unsigned int>(unsigned int &v) { | ||
23 | return json(v); | ||
24 | } | ||
25 | |||
26 | template<typename any> | ||
27 | json toJSON(std::vector<any> &v) { | ||
28 | json obj = json::array(); | ||
29 | for (any i : v) { | ||
30 | obj.push_back(toJSON(i)); | ||
31 | } | ||
32 | return obj; | ||
33 | } | ||
34 | |||
35 | template<typename k, typename v> | ||
36 | json toJSON(std::map<k,v> &value) { | ||
37 | return json(); | ||
38 | } | ||
39 | |||
40 | {% for t in definitions %} | ||
41 | template<> json toJSON<{{ t.value.dataName }}>({{ t.value.dataName }} &v) { | ||
42 | json obj; | ||
43 | switch (v.tag) { {% for c in t.value.constructors %} | ||
44 | case ::{{ t.value.dataName }}::tag::{{ c.value.name }}: | ||
45 | obj["tag"] = "{{ c.value.name }}";{% if !(c.value.fields | empty) %} | ||
46 | { | ||
47 | auto tv = static_cast<::data::{{ c.value.name }}&>(v);{% for f in c.value.fields %}{% if c.value.fields | hasFieldNames %} | ||
48 | obj["{{ f.value.fieldName }}"] = toJSON(tv.{{ f.value.fieldName }});{% else %} | ||
49 | obj["arg{{ f.index0 }}"] = toJSON(tv._{{ f.index0 }});{% endif %}{% endfor %} | ||
50 | }{% endif %} | ||
51 | break;{% endfor %} | ||
52 | } | ||
53 | return obj; | ||
54 | } | ||
55 | {% endfor %} | ||
56 | |||
57 | {# | ||
58 | {% for c in t.value.constructors %} | ||
59 | json data::{{ c.value.name }}::toJSON() { | ||
60 | return obj; | ||
61 | } | ||
62 | {% endif %}{% endfor %}{% endfor %} | ||
63 | #} \ No newline at end of file | ||
diff --git a/templates/data.hpp.ede b/templates/data.hpp.ede new file mode 100644 index 0000000..10e1ff4 --- /dev/null +++ b/templates/data.hpp.ede | |||
@@ -0,0 +1,51 @@ | |||
1 | // generated file, do not modify! | ||
2 | // {{ dateTime }} | ||
3 | |||
4 | #ifndef HEADER_{{ moduleName }}_H | ||
5 | #define HEADER_{{ moduleName }}_H | ||
6 | |||
7 | #include <vector> | ||
8 | #include <map> | ||
9 | #include <string> | ||
10 | |||
11 | #include "json.hpp" | ||
12 | |||
13 | typedef int Int; | ||
14 | typedef int Int32; | ||
15 | typedef unsigned int Word; | ||
16 | typedef unsigned int Word32; | ||
17 | typedef float Float; | ||
18 | typedef bool Bool; | ||
19 | typedef std::string String; | ||
20 | |||
21 | using json = nlohmann::json; | ||
22 | |||
23 | template<typename T> | ||
24 | json toJSON(T &v); | ||
25 | |||
26 | {% for m in imports %} | ||
27 | #include "{{ m.value }}.hpp" | ||
28 | {% endfor %} | ||
29 | |||
30 | {% for t in dataAndType %} | ||
31 | {% case t.value | constType %} | ||
32 | {% when "DataDef" %} | ||
33 | class {{ t.value.dataName }} { | ||
34 | public: | ||
35 | enum class tag { {% for c in t.value.constructors %} | ||
36 | {{ c.value.name }}{% if !c.last %},{% endif %}{% endfor %} | ||
37 | } tag; | ||
38 | }; | ||
39 | namespace data { {% for c in t.value.constructors %}{% if !(c.value.fields | empty) %} | ||
40 | class {{ c.value.name }} : public ::{{ t.value.dataName }} { | ||
41 | public:{% for f in c.value.fields %}{% if c.value.fields | hasFieldNames %} | ||
42 | {{ f.value.fieldType | cppType }} {{ f.value.fieldName }};{% else %} | ||
43 | {{ f.value.fieldType | cppType | parens }} _{{ f.index0 }};{% endif %}{% endfor %} | ||
44 | };{% endif %}{% endfor %} | ||
45 | } | ||
46 | {% when "TypeAlias" %} | ||
47 | typedef {{ t.value.aliasType | cppType }} {{ t.value.aliasName }}; | ||
48 | |||
49 | {% endcase %} | ||
50 | {% endfor %} | ||
51 | #endif | ||
diff --git a/templates/data.hs.ede b/templates/data.hs.ede index fccd7dc..54a148b 100644 --- a/templates/data.hs.ede +++ b/templates/data.hs.ede | |||
@@ -15,6 +15,10 @@ import Data.Aeson hiding (Value,Bool) | |||
15 | import Data.Aeson.Types hiding (Value,Bool) | 15 | import Data.Aeson.Types hiding (Value,Bool) |
16 | import Control.Monad | 16 | import Control.Monad |
17 | 17 | ||
18 | {% for m in imports %} | ||
19 | import {{ m.value }} | ||
20 | {% endfor %} | ||
21 | |||
18 | {% for t in dataAndType %} | 22 | {% for t in dataAndType %} |
19 | {% case t.value | constType %} | 23 | {% case t.value | constType %} |
20 | {% when "DataDef" %} | 24 | {% when "DataDef" %} |
@@ -33,17 +37,14 @@ type {{ t.value.aliasName }} = {{ t.value.aliasType | hsType }} | |||
33 | 37 | ||
34 | {% endfor %} | 38 | {% endfor %} |
35 | 39 | ||
36 | (.-) :: Text -> Text -> Pair | ||
37 | a .- b = a .= b | ||
38 | |||
39 | {% for t in definitions %} | 40 | {% for t in definitions %} |
40 | instance ToJSON {{ t.value.dataName }} where | 41 | instance ToJSON {{ t.value.dataName }} where |
41 | toJSON v = case v of{% for c in t.value.constructors %}{% if c.value.fields | hasFieldNames %} | 42 | toJSON v = case v of{% for c in t.value.constructors %}{% if c.value.fields | hasFieldNames %} |
42 | {{ c.value.name }}{..} -> object | 43 | {{ c.value.name }}{..} -> object |
43 | [ "tag" .- "{{ c.value.name }}"{% for f in c.value.fields %} | 44 | [ "tag" .= ("{{ c.value.name }}" :: Text){% for f in c.value.fields %} |
44 | , "{{ f.value.fieldName }}" .= {{ f.value.fieldName }}{% endfor %} | 45 | , "{{ f.value.fieldName }}" .= {{ f.value.fieldName }}{% endfor %} |
45 | ]{% else %} | 46 | ]{% else %} |
46 | {{ c.value.name }}{% for f in c.value.fields %} arg{{ f.index0 }}{% endfor %} -> object [ "tag" .- "{{ c.value.name }}"{% for f in c.value.fields %}, "arg{{ f.index0 }}" .= arg{{ f.index0 }}{% endfor %}]{% endif %}{% endfor %} | 47 | {{ c.value.name }}{% for f in c.value.fields %} arg{{ f.index0 }}{% endfor %} -> object [ "tag" .= ("{{ c.value.name }}" :: Text){% for f in c.value.fields %}, "arg{{ f.index0 }}" .= arg{{ f.index0 }}{% endfor %}]{% endif %}{% endfor %} |
47 | 48 | ||
48 | instance FromJSON {{ t.value.dataName }} where | 49 | instance FromJSON {{ t.value.dataName }} where |
49 | parseJSON (Object obj) = do | 50 | parseJSON (Object obj) = do |
diff --git a/templates/data.purs.ede b/templates/data.purs.ede index 957fff4..0474931 100644 --- a/templates/data.purs.ede +++ b/templates/data.purs.ede | |||
@@ -16,6 +16,10 @@ import Data.Argonaut.Printer (printJson) | |||
16 | import Data.Argonaut.Encode (EncodeJson, encodeJson) | 16 | import Data.Argonaut.Encode (EncodeJson, encodeJson) |
17 | import Data.Argonaut.Decode (DecodeJson, decodeJson) | 17 | import Data.Argonaut.Decode (DecodeJson, decodeJson) |
18 | 18 | ||
19 | {% for m in imports %} | ||
20 | import {{ m.value }} | ||
21 | {% endfor %} | ||
22 | |||
19 | {% for t in dataAndType %} | 23 | {% for t in dataAndType %} |
20 | {% case t.value | constType %} | 24 | {% case t.value | constType %} |
21 | {% when "DataDef" %} | 25 | {% when "DataDef" %} |