git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
C++: simplify maps
55df68cAndy Arvanitis
pcc/Make.hs−2
@@ -122,8 +122,6 @@ buildMakeActions outputDir filePathMap usePrefix =
122 writeTextFile (outputDir </> "CMakeLists.txt") (fromString cmakeListsTxt)
122 writeTextFile (outputDir </> "CMakeLists.txt") (fromString cmakeListsTxt)
123 writeTextFile (supportDir </> "PureScript.hh") $ B.unpack $(embedFile "pcc/include/purescript.hh")
123 writeTextFile (supportDir </> "PureScript.hh") $ B.unpack $(embedFile "pcc/include/purescript.hh")
124 writeTextFile (supportDir </> "PureScript.cc") $ B.unpack $(embedFile "pcc/include/purescript.cc")
124 writeTextFile (supportDir </> "PureScript.cc") $ B.unpack $(embedFile "pcc/include/purescript.cc")
125− writeTextFile (supportDir </> "map_key.hh") $ B.unpack $(embedFile "pcc/include/map_key.hh")
126− writeTextFile (supportDir </> "hash.hh") $ B.unpack $(embedFile "pcc/include/hash.hh")
127
125
128 let inputPath = dropExtension $ getInputFile mn
126 let inputPath = dropExtension $ getInputFile mn
129 hfile = addExtension inputPath "hh"
127 hfile = addExtension inputPath "hh"
pcc/include/hash.hhdeleted−42
@@ -1,42 +0,0 @@
1−///////////////////////////////////////////////////////////////////////////////
2−//
3−// Module : hash.hh
4−// Copyright : (c) Andy Arvanitis 2015
5−// License : MIT
6−//
7−// Maintainer : Andy Arvanitis <andy.arvanitis@gmail.com>
8−// Stability : experimental
9−// Portability :
10−//
11−// Provides compile-time hashes for string literals.
12−//
13−///////////////////////////////////////////////////////////////////////////////
14−//
15−#ifndef PS_Hash_HH
16−#define PS_Hash_HH
17−
18−#include <cstdint>
19−
20−namespace PureScript {
21−
22− // Hash function which uses an xor version of the djb2 algorithm
23− //
24− constexpr auto djb2(const char s[], const uint32_t hash = 5381) -> uint32_t {
25− return !s[0] ? hash : djb2(s + 1, 33 * hash ^ s[0]);
26− }
27−
28− // Compile-time string literal hash
29− //
30− template <uint32_t H>
31− struct _compile_time_hash_ {
32− static constexpr long value = H;
33− };
34−
35− template <uint32_t H>
36− constexpr long _compile_time_hash_<H>::value;
37−
38− #define HASH(s) _compile_time_hash_<djb2(s)>::value
39−
40−} // namespace PureScript
41−
42−#endif // PS_Hash_HH
pcc/include/map_key.hhdeleted−46
@@ -1,46 +0,0 @@
1−///////////////////////////////////////////////////////////////////////////////
2−//
3−// Module : map_key.hh
4−// Copyright : (c) Andy Arvanitis 2015
5−// License : MIT
6−//
7−// Maintainer : Andy Arvanitis <andy.arvanitis@gmail.com>
8−// Stability : experimental
9−// Portability :
10−//
11−// Support for maps using keys with compile-time hashes.
12−//
13−///////////////////////////////////////////////////////////////////////////////
14−//
15−#ifndef PS_MapKey_HH
16−#define PS_MapKey_HH
17−
18−#include <utility>
19−#include "hash.hh"
20−
21−namespace PureScript {
22−
23− class map_key_t : public std::pair<uint32_t, std::string> {
24− public:
25− using std::pair<uint32_t, std::string>::pair;
26−
27− class hasher {
28− public:
29− auto operator() (const map_key_t& key) const -> size_t {
30− return key.first;
31− }
32− };
33−
34− class equal {
35− public:
36− auto operator() (const map_key_t& key1, const map_key_t& key2) const -> bool {
37− return key1.second == key2.second;
38− }
39− };
40− };
41−
42− #define KEY(s) map_key_t(HASH(s), s)
43−
44−} // namespace PureScript
45−
46−#endif // PS_MapKey_HH
pcc/include/purescript.cc+1−1
@@ -215,7 +215,7 @@ any::~any() {
215 } while (true);
215 } while (true);
216 }
216 }
217
217
218− auto any::operator[](const map_key_t rhs) const -> const any& {
218+ auto any::operator[](const char rhs[]) const -> const any& {
219 RETURN_VALUE(Type::Map, m->at(rhs),)
219 RETURN_VALUE(Type::Map, m->at(rhs),)
220 }
220 }
221
221
pcc/include/purescript.hh+5−5
@@ -32,9 +32,6 @@
32#include <unordered_map>
32#include <unordered_map>
33#include <stdexcept>
33#include <stdexcept>
34
34
35−#include "map_key.hh"
36−#include "hash.hh"
37−
38namespace PureScript {
35namespace PureScript {
39
36
40using string = std::string;
37using string = std::string;
@@ -43,6 +40,9 @@ using nullptr_t = std::nullptr_t;
43
40
44const bool undefined = false;
41const bool undefined = false;
45
42
43+// TODO: deprecated -- remove
44+#define KEY(k) k
45+
46// A variant data class designed to provide some features of dynamic typing.
46// A variant data class designed to provide some features of dynamic typing.
47//
47//
48class any {
48class any {
@@ -69,7 +69,7 @@ class any {
69 };
69 };
70 static constexpr as_thunk unthunk = as_thunk{};
70 static constexpr as_thunk unthunk = as_thunk{};
71
71
72− using map = std::unordered_map<map_key_t, const any, map_key_t::hasher, map_key_t::equal>;
72+ using map = std::unordered_map<std::string, const any>;
73 using vector = std::vector<any>;
73 using vector = std::vector<any>;
74 using fn = std::function<any(const any&)>;
74 using fn = std::function<any(const any&)>;
75 using eff_fn = std::function<any()>;
75 using eff_fn = std::function<any()>;
@@ -256,7 +256,7 @@ class any {
256 operator const map&() const;
256 operator const map&() const;
257 operator const vector&() const;
257 operator const vector&() const;
258
258
259− auto operator[](const map_key_t) const -> const any&;
259+ auto operator[](const char []) const -> const any&;
260 auto operator[](const vector::size_type) const -> const any&;
260 auto operator[](const vector::size_type) const -> const any&;
261 auto operator[](const any&) const -> const any&;
261 auto operator[](const any&) const -> const any&;
262
262
src/Language/PureScript/Pretty/Cpp.hs+5−4
@@ -76,7 +76,8 @@ literals = mkPattern' match
76 , withIndent $ do
76 , withIndent $ do
77 cpps <- forM ps $ \(key, value) -> do
77 cpps <- forM ps $ \(key, value) -> do
78 val <- prettyPrintCpp' value
78 val <- prettyPrintCpp' value
79− return $ "{ KEY" ++ parens(show key) ++ ", " ++ val ++ " }"
79+ k <- prettyPrintCpp' (CppStringLiteral key)
80+ return $ "{ " ++ k ++ ", " ++ val ++ " }"
80 indentString <- currentIndent
81 indentString <- currentIndent
81 return $ intercalate ", \n" $ map (indentString ++) cpps
82 return $ intercalate ", \n" $ map (indentString ++) cpps
82 , return "\n"
83 , return "\n"
@@ -257,7 +258,7 @@ string s = '"' : concatMap encodeChar (decodeSurrogates s) ++ "\""
257 where
258 where
258 decodeSurrogates :: String -> String
259 decodeSurrogates :: String -> String
259 decodeSurrogates [c] = [c]
260 decodeSurrogates [c] = [c]
260− decodeSurrogates s = catMaybes $ zipWith decodePair s (tail s ++ ['\0'])
261+ decodeSurrogates s' = catMaybes $ zipWith decodePair s' (tail s' ++ ['\0'])
261 where
262 where
262 decodePair :: Char -> Char -> Maybe Char
263 decodePair :: Char -> Char -> Maybe Char
263 decodePair a b
264 decodePair a b
@@ -295,8 +296,8 @@ accessor = mkPattern match
295indexer :: Pattern PrinterState Cpp (String, Cpp)
296indexer :: Pattern PrinterState Cpp (String, Cpp)
296indexer = mkPattern' match
297indexer = mkPattern' match
297 where
298 where
299+ match (CppIndexer (CppNumericLiteral (Left index)) val) = return (show index ++ "UL", val)
298− match (CppIndexer index@(CppNumericLiteral {}) val) = (,) <$> prettyPrintCpp' index <*> pure val
300+ match (CppIndexer index val) = (,) <$> prettyPrintCpp' index <*> pure val
299− match (CppIndexer (CppStringLiteral index) val) = return ("KEY" ++ parens (show index), val)
300 match _ = mzero
301 match _ = mzero
301
302
302lam :: Pattern PrinterState Cpp ((String, [(String, Maybe CppType)], Maybe CppType), Cpp)
303lam :: Pattern PrinterState Cpp ((String, [(String, Maybe CppType)], Maybe CppType), Cpp)