git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
C++: handle non-closure eff funcs
commit8d452c179eed8800c6c5f61b68914dac152d38bf authorAndy Arvanitis <andyarvanitis@gmail.com> dateTue 26 Apr 2016, 10:35 (11 years ago) parentfa9c565
2 files changed+18−6
pcc/include/purescript.cc+3 pcc/include/purescript.hh+15−6
pcc/include/purescript.cc+3
@@ -38,6 +38,9 @@ auto any::operator()(const as_thunk) const -> const any& {
3838
3939auto any::operator()() const -> any {
4040 const any& variant = unthunkVariant(*this);
41+ if (variant.type == Type::EffClosure) {
42+ return (*variant.el)();
43+ }
4144 assert(variant.type == Type::EffFunction);
4245 return (*variant.e)();
4346}
pcc/include/purescript.hh+15−6
@@ -61,13 +61,14 @@ class any {
6161 Boolean = 0x04,
6262 StringLiteral = 0x05,
6363 Function = 0x06,
64+ EffFunction = 0x07,
6465 Shared = 0x08,
6566 String = 0x08,
6667 Map = 0x09,
6768 Data = 0x0A,
6869 Array = 0x0B,
6970 Closure = 0x0C,
70− EffFunction = 0x0D,
71+ EffClosure = 0x0D,
7172 Pointer = 0x0E
7273 };
7374
@@ -83,6 +84,7 @@ class any {
8384 using data = std::vector<any>;
8485 using array = std::deque<any>;
8586 using fn = auto (*)(const any&) -> any;
87+ using eff_fn = auto (*)() -> any;
8688 using thunk = auto (*)(const as_thunk) -> const any&;
8789
8890 private:
@@ -102,16 +104,16 @@ class any {
102104 }
103105 };
104106
105− class eff_fn {
107+ class eff_closure {
106108 public:
107109 virtual auto operator()() const -> any = 0;
108110 };
109111
110112 template <typename T>
111− class _eff_fn : public eff_fn {
113+ class _eff_closure : public eff_closure {
112114 const T lambda;
113115 public:
114− _eff_fn(const T& l) noexcept : lambda(l) {}
116+ _eff_closure(const T& l) noexcept : lambda(l) {}
115117 auto operator()() const -> any {
116118 return lambda();
117119 }
@@ -135,12 +137,13 @@ class any {
135137 mutable bool b;
136138 mutable cstring r;
137139 mutable fn f;
140+ mutable eff_fn e;
138141 mutable shared<std::string> s;
139142 mutable shared<map> m;
140143 mutable shared<data> v;
141144 mutable shared<array> a;
142145 mutable shared<closure> l;
143− mutable shared<eff_fn> e;
146+ mutable shared<eff_closure> el;
144147 mutable shared<void> p;
145148 };
146149
@@ -184,8 +187,14 @@ class any {
184187 : type(Type::Closure), l(make_shared<_closure<T>>(val)) {}
185188
186189 template <typename T>
190+ any(const T& val, typename std::enable_if<std::is_convertible<T,eff_fn>::value>::type* = 0) noexcept
191+ : type(Type::EffFunction), e(val) {}
192+
193+ template <typename T,
194+ typename = typename std::enable_if<!std::is_same<any,T>::value &&
195+ !std::is_convertible<T,eff_fn>::value>::type>
187196 any(const T& val, typename std::enable_if<std::is_assignable<std::function<any()>,T>::value>::type* = 0)
188− : type(Type::EffFunction), e(make_shared<_eff_fn<T>>(val)) {}
197+ : type(Type::EffClosure), el(make_shared<_eff_closure<T>>(val)) {}
189198
190199 template <typename T>
191200 any(const T& val, typename std::enable_if<std::is_convertible<T,thunk>::value>::type* = 0) noexcept