summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCsaba Hruska <csaba.hruska@gmail.com>2015-09-17 15:16:25 +0200
committerCsaba Hruska <csaba.hruska@gmail.com>2015-09-17 15:16:25 +0200
commite81d462c37e97c7511da285d04ec716602f5bddb (patch)
tree59b792636effde4acb3a501d17700942b1462032
parentcc6e335b445cbbeaca3c83bf694404ed4949fb3f (diff)
ddl: work on C++ backend
-rw-r--r--Language.hs26
-rw-r--r--lib/RT.hpp86
-rw-r--r--templates/data.cpp.ede4
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
179csType :: AliasMap -> Type -> String 179csType :: AliasMap -> Type -> String -- TODO
180csType aliasMap a = case normalize aliasMap a of 180csType 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
194cppType :: AliasMap -> Type -> String 194cppType :: AliasMap -> Type -> String
195cppType aliasMap = \case 195cppType 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
250hasFieldNames :: [Field] -> Bool 236hasFieldNames :: [Field] -> Bool
251hasFieldNames [] = False 237hasFieldNames [] = False
diff --git a/lib/RT.hpp b/lib/RT.hpp
index 8ecd47a..569ebf2 100644
--- a/lib/RT.hpp
+++ b/lib/RT.hpp
@@ -17,10 +17,72 @@ typedef float Float;
17typedef bool Bool; 17typedef bool Bool;
18typedef std::string String; 18typedef std::string String;
19 19
20template<typename T1>
21struct Maybe
22{
23 T1 data;
24 bool valid;
25};
26
27template<typename T>
28struct V2 { T x,y; };
29
30template<typename T>
31struct V3 { T x,y,z; };
32
33template<typename T>
34struct V4 { T x,y,z,w; };
35
36typedef struct V2<Int> V2I;
37typedef struct V2<Word> V2U;
38typedef struct V2<Float> V2F;
39typedef struct V2<Bool> V2B;
40
41typedef struct V3<Int> V3I;
42typedef struct V3<Word> V3U;
43typedef struct V3<Float> V3F;
44typedef struct V3<Bool> V3B;
45
46typedef struct V4<Int> V4I;
47typedef struct V4<Word> V4U;
48typedef struct V4<Float> V4F;
49typedef struct V4<Bool> V4B;
50
51typedef struct V2<V2F> M22F;
52typedef struct V2<V3F> M32F;
53typedef struct V2<V4F> M42F;
54typedef struct V3<V2F> M23F;
55typedef struct V3<V3F> M33F;
56typedef struct V3<V4F> M43F;
57typedef struct V4<V2F> M24F;
58typedef struct V4<V3F> M34F;
59typedef struct V4<V4F> M44F;
60
61
20template<typename T> 62template<typename T>
21json toJSON(T &v); 63json toJSON(T &v);
22 64
23template<typename any> 65template<typename any>
66json toJSON(Maybe<any> &value) {
67 return json();
68}
69
70template<typename any>
71json toJSON(V2<any> &value) {
72 return json();
73}
74
75template<typename any>
76json toJSON(V3<any> &value) {
77 return json();
78}
79
80template<typename any>
81json toJSON(V4<any> &value) {
82 return json();
83}
84
85template<typename any>
24json toJSON(std::vector<any> &v) { 86json 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>
38T fromJSON(T &v, json &obj); 100T fromJSON(T &v, json &obj);
39 101
40template<typename any> 102template<typename any>
103Maybe<any> fromJSON(Maybe<any> &v, json &obj) {
104 Maybe<any> a;
105 return a;
106}
107
108template<typename any>
109V2<any> fromJSON(V2<any> &v, json &obj) {
110 V2<any> a;
111 return a;
112}
113
114template<typename any>
115V3<any> fromJSON(V3<any> &v, json &obj) {
116 V3<any> a;
117 return a;
118}
119
120template<typename any>
121V4<any> fromJSON(V4<any> &v, json &obj) {
122 V4<any> a;
123 return a;
124}
125
126template<typename any>
41std::vector<any> fromJSON(std::vector<any> &v, json &obj) { 127std::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
22template<> {{ t.value.dataName }} fromJSON<{{ t.value.dataName }}>({{ t.value.dataName }} &v, json &obj) { 25template<> {{ 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 %}