From e81d462c37e97c7511da285d04ec716602f5bddb Mon Sep 17 00:00:00 2001 From: Csaba Hruska Date: Thu, 17 Sep 2015 15:16:25 +0200 Subject: ddl: work on C++ backend --- Language.hs | 26 ++++----------- lib/RT.hpp | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++ 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 Data t -> t x -> error $ "unknown type: " ++ show x -csType :: AliasMap -> Type -> String +csType :: AliasMap -> Type -> String -- TODO csType aliasMap a = case normalize aliasMap a of Data t -> t Int -> "int" @@ -193,7 +193,6 @@ csType aliasMap a = case normalize aliasMap a of cppType :: AliasMap -> Type -> String cppType aliasMap = \case - Data t -> "::" ++ t Int -> "Int" Int32 -> "Int32" Word -> "Word" @@ -201,18 +200,6 @@ cppType aliasMap = \case Float -> "Float" Bool -> "Bool" String -> "String" - Array t -> "std::vector<" ++ cppType aliasMap t ++ ">" - List t -> "std::vector<" ++ cppType aliasMap t ++ ">" - Map k v -> "std::map<" ++ cppType aliasMap k ++ ", " ++ cppType aliasMap v ++ ">" - _ -> "int" -{- - Int -> "Int" - Int32 -> "Int32" - Word -> "Word" - Word32 -> "Word32" - Float -> "Float" - Bool -> "Bool" - String -> "String" V2 Int -> "V2I" V2 Word -> "V2U" @@ -238,14 +225,13 @@ cppType aliasMap = \case V4 (V3 Float) -> "M34F" V4 (V4 Float) -> "M44F" - Array t -> "Vector " ++ parens (cppType aliasMap t) - List t -> "[" ++ cppType aliasMap t ++ "]" - Maybe t -> "Maybe " ++ parens (cppType aliasMap t) - Map k v -> "Map " ++ parens (cppType aliasMap k) ++ " " ++ parens (cppType aliasMap v) + Array t -> "std::vector<" ++ cppType aliasMap t ++ ">" + List t -> "std::vector<" ++ cppType aliasMap t ++ ">" + Maybe t -> "Maybe<" ++ cppType aliasMap t ++ ">" + Map k v -> "std::map<" ++ cppType aliasMap k ++ ", " ++ cppType aliasMap v ++ ">" -- user defined - Data t -> t + Data t -> "::" ++ t x -> error $ "unknown type: " ++ show x --} hasFieldNames :: [Field] -> Bool hasFieldNames [] = False diff --git a/lib/RT.hpp b/lib/RT.hpp index 8ecd47a..569ebf2 100644 --- a/lib/RT.hpp +++ b/lib/RT.hpp @@ -17,9 +17,71 @@ typedef float Float; typedef bool Bool; typedef std::string String; +template +struct Maybe +{ + T1 data; + bool valid; +}; + +template +struct V2 { T x,y; }; + +template +struct V3 { T x,y,z; }; + +template +struct V4 { T x,y,z,w; }; + +typedef struct V2 V2I; +typedef struct V2 V2U; +typedef struct V2 V2F; +typedef struct V2 V2B; + +typedef struct V3 V3I; +typedef struct V3 V3U; +typedef struct V3 V3F; +typedef struct V3 V3B; + +typedef struct V4 V4I; +typedef struct V4 V4U; +typedef struct V4 V4F; +typedef struct V4 V4B; + +typedef struct V2 M22F; +typedef struct V2 M32F; +typedef struct V2 M42F; +typedef struct V3 M23F; +typedef struct V3 M33F; +typedef struct V3 M43F; +typedef struct V4 M24F; +typedef struct V4 M34F; +typedef struct V4 M44F; + + template json toJSON(T &v); +template +json toJSON(Maybe &value) { + return json(); +} + +template +json toJSON(V2 &value) { + return json(); +} + +template +json toJSON(V3 &value) { + return json(); +} + +template +json toJSON(V4 &value) { + return json(); +} + template json toJSON(std::vector &v) { json obj = json::array(); @@ -37,6 +99,30 @@ json toJSON(std::map &value) { template T fromJSON(T &v, json &obj); +template +Maybe fromJSON(Maybe &v, json &obj) { + Maybe a; + return a; +} + +template +V2 fromJSON(V2 &v, json &obj) { + V2 a; + return a; +} + +template +V3 fromJSON(V3 &v, json &obj) { + V3 a; + return a; +} + +template +V4 fromJSON(V4 &v, json &obj) { + V4 a; + return a; +} + template std::vector fromJSON(std::vector &v, json &obj) { std::vector 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 @@ // generated file, do not modify! // {{ dateTime }} +#include +#include + #include "{{ moduleName }}.hpp" {% for t in definitions %} @@ -20,6 +23,7 @@ template<> json toJSON<{{ t.value.dataName }}>({{ t.value.dataName }} &v) { } template<> {{ t.value.dataName }} fromJSON<{{ t.value.dataName }}>({{ t.value.dataName }} &v, json &obj) { + std::cout << "fromJSON: {{ t.value.dataName }}\n"; enum ::{{ t.value.dataName }}::tag tagType; std::string tag = obj["tag"]; {% for c in t.value.constructors %} -- cgit v1.2.3