git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Various additions/fixes
commitbd6f345705d02d0687d68a0c4285b7974d1b8bb1 authorAndy Arvanitis <andyarvanitis@gmail.com> dateSat 11 Aug 2018, 21:24 (8 years ago) parentf233ff1
3 files changed+70−30
runtime/purescript.h+45−21 src/CodeGen/Cpp/Printer.hs+24−8 support/Makefile+1−1
runtime/purescript.h+45−21
@@ -29,8 +29,11 @@ namespace purescript {
2929
3030 class boxed : public std::shared_ptr<void> {
3131
32− using fn = std::function<boxed(const boxed&)>;
33− using eff_fn = std::function<boxed(void)>;
32+ public:
33+ using fn_t = std::function<boxed(const boxed&)>;
34+ using eff_fn_t = std::function<boxed(void)>;
35+ using dict_t = std::map<const string, boxed>;
36+ using array_t = std::deque<boxed>;
3437
3538 public:
3639 using std::shared_ptr<void>::shared_ptr;
@@ -52,54 +55,71 @@ namespace purescript {
5255
5356 boxed(const std::nullptr_t) : std::shared_ptr<void>() {
5457 }
55−
56− boxed(std::initializer_list<boxed> l) : std::shared_ptr<void>(std::make_shared<std::deque<boxed>>(l)) {
58+
59+ boxed(array_t&& l) : std::shared_ptr<void>(std::make_shared<array_t>(std::move(l))) {
5760 }
5861
59− boxed(std::initializer_list<std::pair<const string, boxed>> m) : std::shared_ptr<void>(std::make_shared<std::map<string, boxed>>(m)) {
62+ boxed(dict_t&& m) : std::shared_ptr<void>(std::make_shared<dict_t>(std::move(m))) {
6063 }
6164
6265 template <typename T,
6366 typename = typename std::enable_if<!std::is_same<boxed,T>::value>::type>
6467 boxed(const T& f,
65− typename std::enable_if<std::is_assignable<std::function<boxed(boxed)>,T>::value>::type* = 0) : std::shared_ptr<void>(std::make_shared<fn>(f)) {
68+ typename std::enable_if<std::is_assignable<std::function<boxed(boxed)>,T>::value>::type* = 0) : std::shared_ptr<void>(std::make_shared<fn_t>(f)) {
6669 }
6770
6871 template <typename T,
6972 typename = typename std::enable_if<!std::is_same<boxed,T>::value>::type>
7073 boxed(const T& f,
71− typename std::enable_if<std::is_assignable<std::function<boxed(void)>,T>::value>::type* = 0) : std::shared_ptr<void>(std::make_shared<eff_fn>(f)) {
74+ typename std::enable_if<std::is_assignable<std::function<boxed(void)>,T>::value>::type* = 0) : std::shared_ptr<void>(std::make_shared<eff_fn_t>(f)) {
7275 }
7376
7477 template <typename T,
7578 typename = typename std::enable_if<!std::is_same<boxed,T>::value &&
76− std::is_convertible<T,fn>::value>::type>
79+ std::is_convertible<T,fn_t>::value>::type>
7780 auto operator=(const T& right) -> boxed& {
7881 if (std::shared_ptr<void>::operator bool()) {
79− auto& f = *static_cast<fn*>(get());
82+ auto& f = *static_cast<fn_t*>(get());
8083 f = right;
8184 } else {
82− reset(new fn(right));
85+ reset(new fn_t(right));
8386 }
8487 return *this;
8588 }
8689
8790 auto operator()(const boxed& arg) const -> boxed {
88− auto& f = *static_cast<fn*>(get());
91+ auto& f = *static_cast<fn_t*>(get());
8992 return f(arg);
9093 }
9194
9295 auto operator()() const -> boxed {
93− auto& f = *static_cast<eff_fn*>(get());
96+ auto& f = *static_cast<eff_fn_t*>(get());
9497 return f();
9598 }
9699
97− auto operator[](const char key[]) const -> boxed {
98− const auto& dict = *static_cast<const std::map<const string, boxed>*>(get());
100+ auto operator[](const char key[]) const -> const boxed& {
101+ const auto& dict = *static_cast<const dict_t*>(get());
99102 return dict.at(key);
100103 }
101104
102− };
105+ auto operator[](const char key[]) -> boxed& {
106+ auto& dict = *static_cast<dict_t*>(get());
107+ return dict[key];
108+ }
109+
110+#ifdef NDEBUG
111+ auto operator[](const int index) const -> const boxed& {
112+ const auto& array = *static_cast<const array_t*>(get());
113+ return array[index];
114+ }
115+#else
116+ auto operator[](const int index) const -> const boxed& {
117+ const auto& array = *static_cast<const array_t*>(get());
118+ return array.at(index);
119+ }
120+#endif // NDEBUG
121+
122+ }; // class boxed
103123
104124 template <typename T, typename... Args>
105125 inline auto box(Args&&... args) -> boxed {
@@ -112,6 +132,12 @@ namespace purescript {
112132 return unboxed;
113133 }
114134
135+ template <typename T,
136+ typename = typename std::enable_if<!std::is_same<T, const boxed&>::value>::type>
137+ constexpr auto unbox(const T value) -> T {
138+ return value;
139+ }
140+
115141 template <typename T>
116142 inline auto peek(boxed& b) -> T& {
117143 auto& unboxed = *static_cast<T*>(b.get());
@@ -123,12 +149,10 @@ namespace purescript {
123149 return box<T>(unbox<T>(b));
124150 }
125151
126− using fn_t = std::function<boxed(const boxed&)>;
127− using dict_t = std::map<const string, boxed>;
128− using array_t = std::deque<boxed>;
129−
130− using as_array = std::initializer_list<boxed>;
131− using as_dict = std::initializer_list<std::pair<const string, boxed>>;
152+ using fn_t = boxed::fn_t;
153+ using eff_fn_t = boxed::eff_fn_t;
154+ using dict_t = boxed::dict_t;
155+ using array_t = boxed::array_t;
132156
133157 constexpr auto undefined = nullptr;
134158
src/CodeGen/Cpp/Printer.hs+24−8
@@ -52,13 +52,13 @@ literals = mkPattern' match'
5252 match (BooleanLiteral _ False) = return $ emit "false"
5353 -- match (ArrayLiteral _ []) = return $ emit "std::initializer_list<boxed>{}"
5454 match (ArrayLiteral _ xs) = mconcat <$> sequence
55− [ return $ emit "as_array { "
55+ [ return $ emit "array_t{ {"
5656 , intercalate (emit ", ") <$> forM xs prettyPrintCpp'
57− , return $ emit " }"
57+ , return $ emit "} }"
5858 ]
5959 -- match (ObjectLiteral _ []) = return $ emit "std::initializer_list<std::pair<const string, boxed>>{}"
6060 match (ObjectLiteral _ ps) = mconcat <$> sequence
61− [ return $ emit "as_dict {\n"
61+ [ return $ emit "dict_t{ {\n"
6262 , withIndent $ do
6363 cpps <- forM ps $ \(key, value) -> do
6464 value' <- prettyPrintCpp' value
@@ -67,7 +67,7 @@ literals = mkPattern' match'
6767 return $ intercalate (emit ",\n") $ map (indentString <>) cpps
6868 , return $ emit "\n"
6969 , currentIndent
70− , return $ emit "}"
70+ , return $ emit "} }"
7171 ]
7272 where
7373 objectPropertyToString :: (Emit gen) => PSString -> gen
@@ -106,7 +106,7 @@ literals = mkPattern' match'
106106 ]
107107 match (App ss (App _ (App _ (Indexer _ (Var _ fn) (Var _ fnMod)) [Indexer _ (Var _ dict) (Var _ dictMod)]) [x]) [y])
108108 | fnMod == dictMod
109− , dictMod == C.dataSemiring || dictMod == C.dataRing || dictMod == C.dataEuclideanRing
109+ , dictMod == C.dataEq || dictMod == C.dataSemiring || dictMod == C.dataRing || dictMod == C.dataEuclideanRing
110110 , Just t <- unboxType dict
111111 = mconcat <$> sequence
112112 [ return $ emit $ unbox t x
@@ -231,7 +231,7 @@ literals = mkPattern' match'
231231 , prettyPrintCpp' thens
232232 , maybe (return mempty) (fmap (emit " else " <>) . prettyPrintCpp') elses
233233 ]
234− match (IfElse _ (Binary _ EqualTo x@Var{} y@(NumericLiteral _ n)) thens elses) = mconcat <$> sequence
234+ match (IfElse _ (Binary _ EqualTo x y@(NumericLiteral _ n)) thens elses) = mconcat <$> sequence
235235 [ return $ emit "if ("
236236 , return $ emit $ unbox t x
237237 , return $ emit $ " == "
@@ -243,7 +243,15 @@ literals = mkPattern' match'
243243 where
244244 t | Left _ <- n = int
245245 | Right _ <- n = float
246−
246+ match (IfElse _ (Binary _ EqualTo a b@StringLiteral{}) thens elses) = mconcat <$> sequence
247+ [ return $ emit "if ("
248+ , return $ emit $ unbox string a
249+ , return $ emit $ " == "
250+ , prettyPrintCpp' b
251+ , return $ emit ") "
252+ , prettyPrintCpp' thens
253+ , maybe (return mempty) (fmap (emit " else " <>) . prettyPrintCpp') elses
254+ ]
247255 match (IfElse _ cond thens elses) = mconcat <$> sequence
248256 [ return $ emit "if ("
249257 , prettyPrintCpp' cond
@@ -401,7 +409,8 @@ unbox t v = "unbox<" <> t <> ">(" <> prettyPrintCpp1 v <> ")"
401409
402410unboxType :: Text -> Maybe Text
403411unboxType t
404− | t == C.semiringInt ||
412+ | t == C.eqInt ||
413+ t == C.semiringInt ||
405414 t == C.ringInt
406415 = Just int
407416 | t == C.semiringNumber ||
@@ -412,6 +421,7 @@ unboxType t
412421
413422renderOp :: Text -> Text
414423renderOp op
424+ | op == C.eq = " == "
415425 | op == C.add = " + "
416426 | op == C.sub = " - "
417427 | op == C.mul = " * "
@@ -476,6 +486,12 @@ int = "int"
476486float :: Text
477487float = "double"
478488
489+bool :: Text
490+bool = "bool"
491+
492+string :: Text
493+string = "string"
494+
479495foreignDict :: Text
480496foreignDict = "_foreign_"
481497
support/Makefile+1−1
@@ -27,7 +27,7 @@ override PURSFLAGS += compile --codegen corefn
2727override CFLAGS += --std=c++11
2828
2929DEBUG := "-DDEBUG -g"
30−RELEASE := "-O3"
30+RELEASE := "-DNDEBUG -O3"
3131
3232INCLUDES := -I $(CC_SRC)
3333BIN_DIR := $(OUTPUT)/bin