git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge remote-tracking branch 'origin/pure11' into pure11-0.8
commitda2e10c6779c41ffda2ef49de0804892587ca4fc authorAndy Arvanitis <andyarvanitis@gmail.com> dateMon 4 Jan 2016, 20:41 (11 years ago) parente13aa0672f9cc4
5 files changed+63−8
pcc/Make.hs+1 pcc/include/map_key.hh+54 pcc/include/purescript.cc+1−1 pcc/include/purescript.hh+3−5 src/Language/PureScript/Pretty/Cpp.hs+4−2
pcc/Make.hs+1
@@ -122,6 +122,7 @@ buildMakeActions outputDir filePathMap usePrefix =
122122 writeTextFile (outputDir </> "CMakeLists.txt") (fromString cmakeListsTxt)
123123 writeTextFile (supportDir </> "PureScript.hh") $ B.unpack $(embedFile "pcc/include/purescript.hh")
124124 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")
125126
126127 let inputPath = dropExtension $ getInputFile mn
127128 hfile = addExtension inputPath "hh"
pcc/include/map_key.hhnew file+54
@@ -0,0 +1,54 @@
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+namespace PureScript {
19+
20+ struct map_key_t {
21+
22+ const uint64_t hash;
23+ const char * const cstr;
24+
25+ // Compile-time (fnv-1a) hash function
26+ static constexpr auto fnv1a(const char s[], const uint64_t hash = 14695981039346656037UL) -> uint64_t {
27+ return !s[0] ? hash : fnv1a(s + 1, (hash ^ static_cast<unsigned char>(s[0])) * 1099511628211UL);
28+ }
29+
30+ class hasher {
31+ public:
32+ auto operator() (const map_key_t& key) const -> size_t {
33+ return key.hash;
34+ }
35+ };
36+
37+ class equal {
38+ public:
39+ auto operator() (const map_key_t& key1, const map_key_t& key2) const -> bool {
40+ return (key1.cstr == key2.cstr) || strcmp(key1.cstr, key2.cstr) == 0;
41+ }
42+ };
43+ };
44+
45+ template <uint64_t H>
46+ constexpr auto make_map_key(const char s[]) -> const map_key_t {
47+ return map_key_t{H, s};
48+ }
49+
50+ #define KEY(k) make_map_key<map_key_t::fnv1a(k)>(k)
51+
52+} // namespace PureScript
53+
54+#endif // PS_MapKey_HH
pcc/include/purescript.cc+1−1
@@ -215,7 +215,7 @@ any::~any() {
215215 } while (true);
216216 }
217217
218− auto any::operator[](const char rhs[]) const -> const any& {
218+ auto any::operator[](const map_key_t& rhs) const -> const any& {
219219 RETURN_VALUE(Type::Map, m->at(rhs),)
220220 }
221221
pcc/include/purescript.hh+3−5
@@ -31,6 +31,7 @@
3131#include <vector>
3232#include <unordered_map>
3333#include <stdexcept>
34+#include "map_key.hh"
3435
3536namespace PureScript {
3637
@@ -40,9 +41,6 @@ using nullptr_t = std::nullptr_t;
4041
4142const bool undefined = false;
4243
43−// TODO: deprecated -- remove
44−#define KEY(k) k
45−
4644// A variant data class designed to provide some features of dynamic typing.
4745//
4846class any {
@@ -69,7 +67,7 @@ class any {
6967 };
7068 static constexpr as_thunk unthunk = as_thunk{};
7169
72− using map = std::unordered_map<std::string, const any>;
70+ using map = std::unordered_map<map_key_t, const any, map_key_t::hasher, map_key_t::equal>;
7371 using vector = std::vector<any>;
7472 using fn = std::function<any(const any&)>;
7573 using eff_fn = std::function<any()>;
@@ -246,7 +244,7 @@ class any {
246244 operator const map&() const;
247245 operator const vector&() const;
248246
249− auto operator[](const char []) const -> const any&;
247+ auto operator[](const map_key_t&) const -> const any&;
250248 auto operator[](const vector::size_type) const -> const any&;
251249 auto operator[](const any&) const -> const any&;
252250
src/Language/PureScript/Pretty/Cpp.hs+4−2
@@ -77,7 +77,7 @@ literals = mkPattern' match
7777 cpps <- forM ps $ \(key, value) -> do
7878 val <- prettyPrintCpp' value
7979 k <- prettyPrintCpp' (CppStringLiteral key)
80− return $ "{ " ++ k ++ ", " ++ val ++ " }"
80+ return $ "{ " ++ "KEY" ++ parens k ++ ", " ++ val ++ " }"
8181 indentString <- currentIndent
8282 return $ intercalate ", \n" $ map (indentString ++) cpps
8383 , return "\n"
@@ -297,7 +297,9 @@ indexer :: Pattern PrinterState Cpp (String, Cpp)
297297indexer = mkPattern' match
298298 where
299299 match (CppIndexer (CppNumericLiteral (Left index)) val) = return (show index ++ "UL", val)
300− match (CppIndexer index val) = (,) <$> prettyPrintCpp' index <*> pure val
300+ match (CppIndexer index val) = do
301+ index' <- prettyPrintCpp' index
302+ return ("KEY" ++ parens index', val)
301303 match _ = mzero
302304
303305lam :: Pattern PrinterState Cpp ((String, [(String, Maybe CppType)], Maybe CppType), Cpp)