git.iko.soy/rowtype-yoga/purescript-native
on b64dad8 help sign in
369 lines 11 kB history raw
1///////////////////////////////////////////////////////////////////////////////
2//
3// Module : purescript.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// Basic types and functions to support purescript-to-C++1x rendering
12//
13///////////////////////////////////////////////////////////////////////////////
14//
15#ifndef PureScript_HH
16#define PureScript_HH
18// Standard includes
20#if defined(DEBUG)
21 #include <cassert>
22#else
23 #define NDEBUG
24 #undef assert
25 #define assert(x)
26#endif
28#include <memory>
29#include <functional>
30#include <string>
31#include <vector>
32#include <deque>
33#include <utility>
34#include <stdexcept>
36#define DECLARE_OPERATOR(op, ty, rty) \
37 friend auto operator op (const any&, ty) -> rty; \
38 friend auto operator op (ty, const any&) -> rty;
40#define DECLARE_COMPARISON_OPERATOR(op) \
41 friend auto operator op (const any&, const any&) -> bool; \
42 DECLARE_OPERATOR(op, long, bool) \
43 DECLARE_OPERATOR(op, double, bool) \
44 DECLARE_OPERATOR(op, char, bool) \
45 DECLARE_OPERATOR(op, const char *, bool)
48namespace PureScript {
50using string = const char *;
51using nullptr_t = std::nullptr_t;
53// Workaround for missing C++11 version in gcc
54class runtime_error : public std::runtime_error {
55public:
56 runtime_error(const char message[]) : std::runtime_error(std::string(message)) {}
57};
59const bool undefined = false;
60const size_t constructor = 0;
62// A variant data class designed to provide some features of dynamic typing.
63//
64class any {
66 public:
67 enum class Type : int8_t {
68 Unknown,
69 Integer,
70 Double,
71 Character,
72 Boolean,
73 StringLiteral,
74 String,
75 Map,
76 Data,
77 Array,
78 Function,
79 Closure,
80 EffFunction,
81 Thunk,
82 Pointer
83 };
85 private:
86 mutable Type type;
87 mutable bool is_shared;
89 public:
90 struct as_thunk {
91 };
92 static constexpr as_thunk unthunk = as_thunk{};
94 using map = std::vector<std::pair<const char * const, const any>>;
95 using data = std::vector<any>;
96 using array = std::deque<any>;
97 using fn = auto (*)(const any&) -> any;
98 using thunk = auto (*)(const as_thunk) -> const any&;
100 private:
101 class closure {
102 public:
103 virtual auto operator()(const any&) const -> any = 0;
104 virtual ~closure() {}
105 };
107 template <typename T>
108 class _closure : public closure {
109 const T lambda;
110 public:
111 _closure(const T& l) noexcept : lambda(l) {}
112 auto operator()(const any& arg) const -> any {
113 return lambda(arg);
114 }
115 };
117 class eff_fn {
118 public:
119 virtual auto operator()() const -> any = 0;
120 };
122 template <typename T>
123 class _eff_fn : public eff_fn {
124 const T lambda;
125 public:
126 _eff_fn(const T& l) noexcept : lambda(l) {}
127 auto operator()() const -> any {
128 return lambda();
129 }
130 };
132 public:
133 template <typename T>
134 using shared = std::shared_ptr<T>;
136 template <typename T, typename... Args>
137 inline static auto make_shared(Args&&... args) -> decltype(std::make_shared<T>(std::forward<Args>(args)...)) {
138 return std::make_shared<T>(std::forward<Args>(args)...);
139 }
141 private:
142 union {
143 mutable long i;
144 mutable double d;
145 mutable char c;
146 mutable bool b;
147 mutable string r;
148 mutable shared<std::string> s;
149 mutable shared<map> m;
150 mutable shared<data> v;
151 mutable shared<array> a;
152 mutable fn f;
153 mutable shared<closure> l;
154 mutable shared<eff_fn> e;
155 mutable thunk t;
156 mutable shared<void> p;
157 };
159 public:
161 any(const long val) : type(Type::Integer), is_shared(false), i(val) {}
162 any(const int val) : type(Type::Integer), is_shared(false), i(val) {}
163 any(const unsigned int val) : type(Type::Integer), is_shared(false), i(val) {}
164 any(const double val) : type(Type::Double), is_shared(false), d(val) {}
165 any(const char val) : type(Type::Character), is_shared(false), c(val) {}
167 template <typename T, typename = typename std::enable_if<std::is_same<bool,T>::value>::type>
168 any(const T val) : type(Type::Boolean), is_shared(false), b(val) {}
170 template <size_t N>
171 any(const char (&val)[N]) : type(Type::StringLiteral), is_shared(false), r(val) {}
172 any(char * val) : type(Type::String), is_shared(true), s(make_shared<std::string>(val)) {}
174 any(const std::string& val) : type(Type::String), is_shared(true), s(make_shared<std::string>(val)) {}
175 any(std::string&& val) : type(Type::String), is_shared(true), s(make_shared<std::string>(std::move(val))) {}
177 any(const shared<std::string>& val) : type(Type::String), is_shared(true), s(val) {}
178 any(shared<std::string>&& val) noexcept : type(Type::String), is_shared(true), s(std::move(val)) {}
180 any(const map& val) : type(Type::Map), is_shared(true), m(make_shared<map>(val)) {}
181 any(map&& val) noexcept : type(Type::Map), is_shared(true), m(make_shared<map>(std::move(val))) {}
183 any(const data& val) : type(Type::Data), is_shared(true), v(make_shared<data>(val)) {}
184 any(data&& val) noexcept : type(Type::Data), is_shared(true), v(make_shared<data>(std::move(val))) {}
186 any(const array& val) : type(Type::Array), is_shared(true), a(make_shared<array>(val)) {}
187 any(array&& val) noexcept : type(Type::Array), is_shared(true), a(make_shared<array>(std::move(val))) {}
189 template <typename T>
190 any(const T& val, typename std::enable_if<std::is_convertible<T,fn>::value>::type* = 0)
191 : type(Type::Function), is_shared(false), f(val) {}
193 template <typename T, typename = typename std::enable_if<!std::is_same<any,T>::value &&
194 !std::is_convertible<T,fn>::value>::type>
195 any(const T& val, typename std::enable_if<std::is_assignable<std::function<any(const any&)>,T>::value>::type* = 0)
196 : type(Type::Closure), is_shared(true), l(make_shared<_closure<T>>(val)) {}
198 template <typename T>
199 any(const T& val, typename std::enable_if<std::is_assignable<std::function<any()>,T>::value>::type* = 0)
200 : type(Type::EffFunction), is_shared(true), e(make_shared<_eff_fn<T>>(val)) {}
202 template <typename T>
203 any(const T& val, typename std::enable_if<std::is_convertible<T,thunk>::value>::type* = 0)
204 : type(Type::Thunk), is_shared(false), t(val) {}
206 template <typename T>
207 any(const T& val, typename std::enable_if<std::is_assignable<shared<void>,T>::value>::type* = 0)
208 : type(Type::Pointer), is_shared(true), p(val) {}
210 template <typename T>
211 any(T&& val, typename std::enable_if<std::is_assignable<shared<void>,T>::value>::type* = 0) noexcept
212 : type(Type::Pointer), is_shared(true), p(std::move(val)) {}
214 any(std::nullptr_t) : type(Type::Pointer), is_shared(true), p(nullptr) {}
216 private:
218 auto copy(const any& other) const -> void {
219 if (is_shared) {
220 new (&p) shared<void>(other.p);
221 } else {
222 f = other.f;
223 }
224 }
226 auto move(any& other) const noexcept -> void {
227 if (is_shared) {
228 new (&p) shared<void>(std::move(other.p));
229 } else {
230 f = other.f;
231 }
232 }
234 auto del() noexcept -> void {
235 if (is_shared) {
236 p.~shared<void>();
237 }
238 }
240 public:
242 any(const any& other) : type(other.type), is_shared(other.is_shared) {
243 copy(other);
244 }
246 any(any&& other) noexcept : type(other.type), is_shared(other.is_shared) {
247 move(other);
248 }
250 auto operator=(const any& rhs) -> any& {
251 del();
252 type = rhs.type;
253 is_shared = rhs.is_shared;
254 copy(rhs);
255 return *this;
256 }
258 auto operator=(any&& rhs) noexcept -> any& {
259 del();
260 type = rhs.type;
261 is_shared = rhs.is_shared;
262 move(rhs);
263 return *this;
264 }
266 any() = delete;
268 ~any() {
269 del();
270 }
272 template <typename T>
273 auto cast() const -> typename std::enable_if<std::is_same<T, long>::value, T>::type;
275 template <typename T>
276 auto cast() const -> typename std::enable_if<std::is_same<T, double>::value, T>::type;
278 template <typename T>
279 auto cast() const -> typename std::enable_if<std::is_same<T, char>::value, T>::type;
281 template <typename T>
282 auto cast() const -> typename std::enable_if<std::is_same<T, bool>::value, T>::type;
284 template <typename T>
285 auto cast() const -> typename std::enable_if<std::is_same<T, string>::value, T>::type;
287 template <typename T>
288 auto cast() const -> typename std::enable_if<std::is_same<T, map>::value, const T&>::type;
290 template <typename T>
291 auto cast() const -> typename std::enable_if<std::is_same<T, data>::value, const T&>::type;
293 template <typename T>
294 auto cast() const -> typename std::enable_if<std::is_same<T, array>::value, const T&>::type;
296 template <typename T>
297 auto cast() const -> typename std::enable_if<std::is_assignable<shared<void>,T>::value, typename T::element_type*>::type {
298 return static_cast<typename T::element_type*>(extractPointer());
299 }
301 auto operator()(const any&) const -> any;
302 auto operator()(const as_thunk) const -> const any&;
303 auto operator()() const -> any;
305 operator long() const;
306 operator double() const;
307 operator bool() const;
309 operator string() const;
310 operator const map&() const;
311 operator const data&() const;
312 operator const array&() const;
314 auto operator[](const char[]) const -> const any&;
315 auto operator[](const size_t) const -> const any&;
316 auto operator[](const any&) const -> const any&;
318 auto contains(const char[]) const -> bool;
320 auto extractPointer() const -> void*;
322 static auto unthunkVariant(const any&) -> const any&;
324 DECLARE_COMPARISON_OPERATOR(==)
325 DECLARE_COMPARISON_OPERATOR(!=)
326 DECLARE_COMPARISON_OPERATOR(<)
327 DECLARE_COMPARISON_OPERATOR(<=)
328 DECLARE_COMPARISON_OPERATOR(>)
329 DECLARE_COMPARISON_OPERATOR(>=)
331 friend auto operator+(const any&, const any&) -> any;
333 DECLARE_OPERATOR(+, long, long)
334 DECLARE_OPERATOR(+, double, double)
335 DECLARE_OPERATOR(+, char, char)
336 DECLARE_OPERATOR(+, const char *, std::string)
338 friend auto operator-(const any&, const any&) -> any;
340 DECLARE_OPERATOR(-, long, long)
341 DECLARE_OPERATOR(-, double, double)
342 DECLARE_OPERATOR(-, char, char)
344 friend auto operator*(const any&, const any&) -> any;
346 DECLARE_OPERATOR(*, long, long)
347 DECLARE_OPERATOR(*, double, double)
348 DECLARE_OPERATOR(*, char, char)
350 friend auto operator/(const any&, const any&) -> any;
352 DECLARE_OPERATOR(/, long, long)
353 DECLARE_OPERATOR(/, double, double)
354 DECLARE_OPERATOR(/, char, char)
356 friend auto operator%(const any&, const any&) -> any;
358 DECLARE_OPERATOR(%, long, long)
359 DECLARE_OPERATOR(%, char, char)
361 friend auto operator-(const any&) -> any; // unary negate
362};
364} // namespace PureScript
366#undef DECLARE_OPERATOR
367#undef DECLARE_COMPARISON_OPERATOR
369#endif // PureScript_HH