git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge remote-tracking branch 'origin/pure11' into pure11-0.8
da2e10cAndy Arvanitis
pcc/Make.hs+1
@@ -122,6 +122,7 @@ 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")
125
126
126 let inputPath = dropExtension $ getInputFile mn
127 let inputPath = dropExtension $ getInputFile mn
127 hfile = addExtension inputPath "hh"
128 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() {
215 } while (true);
215 } while (true);
216 }
216 }
217
217
218− auto any::operator[](const char rhs[]) const -> const any& {
218+ auto any::operator[](const map_key_t& 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+3−5
@@ -31,6 +31,7 @@
31#include <vector>
31#include <vector>
32#include <unordered_map>
32#include <unordered_map>
33#include <stdexcept>
33#include <stdexcept>
34+#include "map_key.hh"
34
35
35namespace PureScript {
36namespace PureScript {
36
37
@@ -40,9 +41,6 @@ using nullptr_t = std::nullptr_t;
40
41
41const bool undefined = false;
42const bool undefined = false;
42
43
43−// TODO: deprecated -- remove
44−#define KEY(k) k
45−
46// A variant data class designed to provide some features of dynamic typing.
44// A variant data class designed to provide some features of dynamic typing.
47//
45//
48class any {
46class any {
@@ -69,7 +67,7 @@ class any {
69 };
67 };
70 static constexpr as_thunk unthunk = as_thunk{};
68 static constexpr as_thunk unthunk = as_thunk{};
71
69
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>;
73 using vector = std::vector<any>;
71 using vector = std::vector<any>;
74 using fn = std::function<any(const any&)>;
72 using fn = std::function<any(const any&)>;
75 using eff_fn = std::function<any()>;
73 using eff_fn = std::function<any()>;
@@ -246,7 +244,7 @@ class any {
246 operator const map&() const;
244 operator const map&() const;
247 operator const vector&() const;
245 operator const vector&() const;
248
246
249− auto operator[](const char []) const -> const any&;
247+ auto operator[](const map_key_t&) const -> const any&;
250 auto operator[](const vector::size_type) const -> const any&;
248 auto operator[](const vector::size_type) const -> const any&;
251 auto operator[](const any&) const -> const any&;
249 auto operator[](const any&) const -> const any&;
252
250
src/Language/PureScript/Pretty/Cpp.hs+4−2
@@ -77,7 +77,7 @@ literals = mkPattern' match
77 cpps <- forM ps $ \(key, value) -> do
77 cpps <- forM ps $ \(key, value) -> do
78 val <- prettyPrintCpp' value
78 val <- prettyPrintCpp' value
79 k <- prettyPrintCpp' (CppStringLiteral key)
79 k <- prettyPrintCpp' (CppStringLiteral key)
80− return $ "{ " ++ k ++ ", " ++ val ++ " }"
80+ return $ "{ " ++ "KEY" ++ parens k ++ ", " ++ val ++ " }"
81 indentString <- currentIndent
81 indentString <- currentIndent
82 return $ intercalate ", \n" $ map (indentString ++) cpps
82 return $ intercalate ", \n" $ map (indentString ++) cpps
83 , return "\n"
83 , return "\n"
@@ -297,7 +297,9 @@ indexer :: Pattern PrinterState Cpp (String, Cpp)
297indexer = mkPattern' match
297indexer = mkPattern' match
298 where
298 where
299 match (CppIndexer (CppNumericLiteral (Left index)) val) = return (show index ++ "UL", val)
299 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)
301 match _ = mzero
303 match _ = mzero
302
304
303lam :: Pattern PrinterState Cpp ((String, [(String, Maybe CppType)], Maybe CppType), Cpp)
305lam :: Pattern PrinterState Cpp ((String, [(String, Maybe CppType)], Maybe CppType), Cpp)