diff options
author | Csaba Hruska <csaba.hruska@gmail.com> | 2015-09-17 15:16:25 +0200 |
---|---|---|
committer | Csaba Hruska <csaba.hruska@gmail.com> | 2015-09-17 15:16:25 +0200 |
commit | e81d462c37e97c7511da285d04ec716602f5bddb (patch) | |
tree | 59b792636effde4acb3a501d17700942b1462032 | |
parent | cc6e335b445cbbeaca3c83bf694404ed4949fb3f (diff) |
ddl: work on C++ backend
-rw-r--r-- | Language.hs | 26 | ||||
-rw-r--r-- | lib/RT.hpp | 86 | ||||
-rw-r--r-- | templates/data.cpp.ede | 4 |
3 files changed, 96 insertions, 20 deletions
diff --git a/Language.hs b/Language.hs index e0e53cf..6a5cc33 100644 --- a/Language.hs +++ b/Language.hs | |||
@@ -176,7 +176,7 @@ hsType aliasMap = \case | |||
176 | Data t -> t | 176 | Data t -> t |
177 | x -> error $ "unknown type: " ++ show x | 177 | x -> error $ "unknown type: " ++ show x |
178 | 178 | ||
179 | csType :: AliasMap -> Type -> String | 179 | csType :: AliasMap -> Type -> String -- TODO |
180 | csType aliasMap a = case normalize aliasMap a of | 180 | csType aliasMap a = case normalize aliasMap a of |
181 | Data t -> t | 181 | Data t -> t |
182 | Int -> "int" | 182 | Int -> "int" |
@@ -193,7 +193,6 @@ csType aliasMap a = case normalize aliasMap a of | |||
193 | 193 | ||
194 | cppType :: AliasMap -> Type -> String | 194 | cppType :: AliasMap -> Type -> String |
195 | cppType aliasMap = \case | 195 | cppType aliasMap = \case |
196 | Data t -> "::" ++ t | ||
197 | Int -> "Int" | 196 | Int -> "Int" |
198 | Int32 -> "Int32" | 197 | Int32 -> "Int32" |
199 | Word -> "Word" | 198 | Word -> "Word" |
@@ -201,18 +200,6 @@ cppType aliasMap = \case | |||
201 | Float -> "Float" | 200 | Float -> "Float" |
202 | Bool -> "Bool" | 201 | Bool -> "Bool" |
203 | String -> "String" | 202 | String -> "String" |
204 | Array t -> "std::vector<" ++ cppType aliasMap t ++ ">" | ||
205 | List t -> "std::vector<" ++ cppType aliasMap t ++ ">" | ||
206 | Map k v -> "std::map<" ++ cppType aliasMap k ++ ", " ++ cppType aliasMap v ++ ">" | ||
207 | _ -> "int" | ||
208 | {- | ||
209 | Int -> "Int" | ||
210 | Int32 -> "Int32" | ||
211 | Word -> "Word" | ||
212 | Word32 -> "Word32" | ||
213 | Float -> "Float" | ||
214 | Bool -> "Bool" | ||
215 | String -> "String" | ||
216 | 203 | ||
217 | V2 Int -> "V2I" | 204 | V2 Int -> "V2I" |
218 | V2 Word -> "V2U" | 205 | V2 Word -> "V2U" |
@@ -238,14 +225,13 @@ cppType aliasMap = \case | |||
238 | V4 (V3 Float) -> "M34F" | 225 | V4 (V3 Float) -> "M34F" |
239 | V4 (V4 Float) -> "M44F" | 226 | V4 (V4 Float) -> "M44F" |
240 | 227 | ||
241 | Array t -> "Vector " ++ parens (cppType aliasMap t) | 228 | Array t -> "std::vector<" ++ cppType aliasMap t ++ ">" |
242 | List t -> "[" ++ cppType aliasMap t ++ "]" | 229 | List t -> "std::vector<" ++ cppType aliasMap t ++ ">" |
243 | Maybe t -> "Maybe " ++ parens (cppType aliasMap t) | 230 | Maybe t -> "Maybe<" ++ cppType aliasMap t ++ ">" |
244 | Map k v -> "Map " ++ parens (cppType aliasMap k) ++ " " ++ parens (cppType aliasMap v) | 231 | Map k v -> "std::map<" ++ cppType aliasMap k ++ ", " ++ cppType aliasMap v ++ ">" |
245 | -- user defined | 232 | -- user defined |
246 | Data t -> t | 233 | Data t -> "::" ++ t |
247 | x -> error $ "unknown type: " ++ show x | 234 | x -> error $ "unknown type: " ++ show x |
248 | -} | ||
249 | 235 | ||
250 | hasFieldNames :: [Field] -> Bool | 236 | hasFieldNames :: [Field] -> Bool |
251 | hasFieldNames [] = False | 237 | hasFieldNames [] = False |
@@ -17,10 +17,72 @@ typedef float Float; | |||
17 | typedef bool Bool; | 17 | typedef bool Bool; |
18 | typedef std::string String; | 18 | typedef std::string String; |
19 | 19 | ||
20 | template<typename T1> | ||
21 | struct Maybe | ||
22 | { | ||
23 | T1 data; | ||
24 | bool valid; | ||
25 | }; | ||
26 | |||
27 | template<typename T> | ||
28 | struct V2 { T x,y; }; | ||
29 | |||
30 | template<typename T> | ||
31 | struct V3 { T x,y,z; }; | ||
32 | |||
33 | template<typename T> | ||
34 | struct V4 { T x,y,z,w; }; | ||
35 | |||
36 | typedef struct V2<Int> V2I; | ||
37 | typedef struct V2<Word> V2U; | ||
38 | typedef struct V2<Float> V2F; | ||
39 | typedef struct V2<Bool> V2B; | ||
40 | |||
41 | typedef struct V3<Int> V3I; | ||
42 | typedef struct V3<Word> V3U; | ||
43 | typedef struct V3<Float> V3F; | ||
44 | typedef struct V3<Bool> V3B; | ||
45 | |||
46 | typedef struct V4<Int> V4I; | ||
47 | typedef struct V4<Word> V4U; | ||
48 | typedef struct V4<Float> V4F; | ||
49 | typedef struct V4<Bool> V4B; | ||
50 | |||
51 | typedef struct V2<V2F> M22F; | ||
52 | typedef struct V2<V3F> M32F; | ||
53 | typedef struct V2<V4F> M42F; | ||
54 | typedef struct V3<V2F> M23F; | ||
55 | typedef struct V3<V3F> M33F; | ||
56 | typedef struct V3<V4F> M43F; | ||
57 | typedef struct V4<V2F> M24F; | ||
58 | typedef struct V4<V3F> M34F; | ||
59 | typedef struct V4<V4F> M44F; | ||
60 | |||
61 | |||
20 | template<typename T> | 62 | template<typename T> |
21 | json toJSON(T &v); | 63 | json toJSON(T &v); |
22 | 64 | ||
23 | template<typename any> | 65 | template<typename any> |
66 | json toJSON(Maybe<any> &value) { | ||
67 | return json(); | ||
68 | } | ||
69 | |||
70 | template<typename any> | ||
71 | json toJSON(V2<any> &value) { | ||
72 | return json(); | ||
73 | } | ||
74 | |||
75 | template<typename any> | ||
76 | json toJSON(V3<any> &value) { | ||
77 | return json(); | ||
78 | } | ||
79 | |||
80 | template<typename any> | ||
81 | json toJSON(V4<any> &value) { | ||
82 | return json(); | ||
83 | } | ||
84 | |||
85 | template<typename any> | ||
24 | json toJSON(std::vector<any> &v) { | 86 | json toJSON(std::vector<any> &v) { |
25 | json obj = json::array(); | 87 | json obj = json::array(); |
26 | for (any i : v) { | 88 | for (any i : v) { |
@@ -38,6 +100,30 @@ template<typename T> | |||
38 | T fromJSON(T &v, json &obj); | 100 | T fromJSON(T &v, json &obj); |
39 | 101 | ||
40 | template<typename any> | 102 | template<typename any> |
103 | Maybe<any> fromJSON(Maybe<any> &v, json &obj) { | ||
104 | Maybe<any> a; | ||
105 | return a; | ||
106 | } | ||
107 | |||
108 | template<typename any> | ||
109 | V2<any> fromJSON(V2<any> &v, json &obj) { | ||
110 | V2<any> a; | ||
111 | return a; | ||
112 | } | ||
113 | |||
114 | template<typename any> | ||
115 | V3<any> fromJSON(V3<any> &v, json &obj) { | ||
116 | V3<any> a; | ||
117 | return a; | ||
118 | } | ||
119 | |||
120 | template<typename any> | ||
121 | V4<any> fromJSON(V4<any> &v, json &obj) { | ||
122 | V4<any> a; | ||
123 | return a; | ||
124 | } | ||
125 | |||
126 | template<typename any> | ||
41 | std::vector<any> fromJSON(std::vector<any> &v, json &obj) { | 127 | std::vector<any> fromJSON(std::vector<any> &v, json &obj) { |
42 | std::vector<any> a; | 128 | std::vector<any> a; |
43 | return a; | 129 | return a; |
diff --git a/templates/data.cpp.ede b/templates/data.cpp.ede index c56d9fc..ac6012c 100644 --- a/templates/data.cpp.ede +++ b/templates/data.cpp.ede | |||
@@ -1,6 +1,9 @@ | |||
1 | // generated file, do not modify! | 1 | // generated file, do not modify! |
2 | // {{ dateTime }} | 2 | // {{ dateTime }} |
3 | 3 | ||
4 | #include <string> | ||
5 | #include <iostream> | ||
6 | |||
4 | #include "{{ moduleName }}.hpp" | 7 | #include "{{ moduleName }}.hpp" |
5 | 8 | ||
6 | {% for t in definitions %} | 9 | {% for t in definitions %} |
@@ -20,6 +23,7 @@ template<> json toJSON<{{ t.value.dataName }}>({{ t.value.dataName }} &v) { | |||
20 | } | 23 | } |
21 | 24 | ||
22 | template<> {{ t.value.dataName }} fromJSON<{{ t.value.dataName }}>({{ t.value.dataName }} &v, json &obj) { | 25 | template<> {{ t.value.dataName }} fromJSON<{{ t.value.dataName }}>({{ t.value.dataName }} &v, json &obj) { |
26 | std::cout << "fromJSON: {{ t.value.dataName }}\n"; | ||
23 | enum ::{{ t.value.dataName }}::tag tagType; | 27 | enum ::{{ t.value.dataName }}::tag tagType; |
24 | std::string tag = obj["tag"]; | 28 | std::string tag = obj["tag"]; |
25 | {% for c in t.value.constructors %} | 29 | {% for c in t.value.constructors %} |