git.iko.soy/rowtype-yoga/purescript-native
on ca5aef4 help sign in
379 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>
35#include <iso646.h> // mostly for MS Visual Studio compiler
37#define DECLARE_OPERATOR(op, ty, rty) \
38 friend auto operator op (const any&, ty) -> rty; \
39 friend auto operator op (ty, const any&) -> rty;
41#define DECLARE_COMPARISON_OPERATOR(op) \
42 friend auto operator op (const any&, const any&) -> bool; \
43 DECLARE_OPERATOR(op, long, bool) \
44 DECLARE_OPERATOR(op, double, bool) \
45 DECLARE_OPERATOR(op, char, bool) \
46 DECLARE_OPERATOR(op, const char *, bool)
49namespace PureScript {
51using string = const char *;
52using nullptr_t = std::nullptr_t;
54// Workaround for missing C++11 version in gcc
55class runtime_error : public std::runtime_error {
56public:
57 runtime_error(const char message[]) : std::runtime_error(std::string(message)) {}
58};
60const bool undefined = false;
61const size_t constructor = 0;
63// A variant data class designed to provide some features of dynamic typing.
64//
65class any {
67 public:
68 enum Type {
69 Thunk = 0x00,
70 Integer = 0x01,
71 Double = 0x02,
72 Character = 0x03,
73 Boolean = 0x04,
74 StringLiteral = 0x05,
75 Function = 0x06,
76 Shared = 0x08,
77 String = 0x08,
78 Map = 0x09,
79 Data = 0x0A,
80 Array = 0x0B,
81 Closure = 0x0C,
82 EffFunction = 0x0D,
83 Pointer = 0x0E
84 };
86 private:
87 mutable Type type;
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 thunk t;
144 mutable long i;
145 mutable double d;
146 mutable char c;
147 mutable bool b;
148 mutable string r;
149 mutable fn f;
150 mutable shared<std::string> s;
151 mutable shared<map> m;
152 mutable shared<data> v;
153 mutable shared<array> a;
154 mutable shared<closure> l;
155 mutable shared<eff_fn> e;
156 mutable shared<void> p;
157 };
159 public:
161 any(const long val) : type(Type::Integer), i(val) {}
162 any(const int val) : type(Type::Integer), i(val) {}
163 any(const unsigned int val) : type(Type::Integer), i(val) {}
164 any(const double val) : type(Type::Double), d(val) {}
165 any(const char val) : type(Type::Character), 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), b(val) {}
170 template <size_t N>
171 any(const char (&val)[N]) : type(Type::StringLiteral), r(val) {}
172 any(char * val) : type(Type::String), s(make_shared<std::string>(val)) {}
174 any(const std::string& val) : type(Type::String), s(make_shared<std::string>(val)) {}
175 any(std::string&& val) : type(Type::String), s(make_shared<std::string>(std::move(val))) {}
177 any(const shared<std::string>& val) : type(Type::String), s(val) {}
178 any(shared<std::string>&& val) noexcept : type(Type::String), s(std::move(val)) {}
180 any(const map& val) : type(Type::Map), m(make_shared<map>(val)) {}
181 any(map&& val) noexcept : type(Type::Map), m(make_shared<map>(std::move(val))) {}
183 any(const data& val) : type(Type::Data), v(make_shared<data>(val)) {}
184 any(data&& val) noexcept : type(Type::Data), v(make_shared<data>(std::move(val))) {}
186 any(const array& val) : type(Type::Array), a(make_shared<array>(val)) {}
187 any(array&& val) noexcept : type(Type::Array), 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), 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), 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), 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), 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), 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), p(std::move(val)) {}
214 any(std::nullptr_t) : type(Type::Pointer), p(nullptr) {}
216 private:
218 auto copy(const any& other) const -> void {
219 if (type & Type::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 (type & Type::Shared) {
228 new (&p) shared<void>(std::move(other.p));
229 } else {
230 f = other.f;
231 }
232 }
234 auto destruct() noexcept -> void {
235 if (type & Type::Shared) {
236 p.~shared<void>();
237 }
238 }
240 public:
242 any(const any& other) : type(other.type) {
243 copy(other);
244 }
246 any(any&& other) noexcept : type(other.type) {
247 move(other);
248 }
250 auto operator=(const any& rhs) -> any& {
251 destruct();
252 type = rhs.type;
253 copy(rhs);
254 return *this;
255 }
257 auto operator=(any&& rhs) noexcept -> any& {
258 destruct();
259 type = rhs.type;
260 move(rhs);
261 return *this;
262 }
264 any() = delete;
266 ~any() {
267 destruct();
268 }
270 template <typename T>
271 auto cast() const -> typename std::enable_if<std::is_same<T, long>::value, T>::type;
273 template <typename T>
274 auto cast() const -> typename std::enable_if<std::is_same<T, double>::value, T>::type;
276 template <typename T>
277 auto cast() const -> typename std::enable_if<std::is_same<T, char>::value, T>::type;
279 template <typename T>
280 auto cast() const -> typename std::enable_if<std::is_same<T, bool>::value, T>::type;
282 template <typename T>
283 auto cast() const -> typename std::enable_if<std::is_same<T, string>::value, T>::type;
285 template <typename T>
286 auto cast() const -> typename std::enable_if<std::is_same<T, map>::value, const T&>::type;
288 template <typename T>
289 auto cast() const -> typename std::enable_if<std::is_same<T, data>::value, const T&>::type;
291 template <typename T>
292 auto cast() const -> typename std::enable_if<std::is_same<T, array>::value, const T&>::type;
294 template <typename T>
295 auto cast() const -> typename std::enable_if<std::is_assignable<shared<void>,T>::value, typename T::element_type*>::type {
296 return static_cast<typename T::element_type*>(extractPointer());
297 }
299 auto operator()(const any&) const -> any;
300 auto operator()(const as_thunk) const -> const any&;
301 auto operator()() const -> any;
303 operator long() const;
304 operator double() const;
305 operator bool() const;
307 operator string() const;
308 operator const map&() const;
309 operator const data&() const;
310 operator const array&() const;
312 auto operator[](const char[]) const -> const any&;
313 auto operator[](const size_t) const -> const any&;
314 auto operator[](const any&) const -> const any&;
316 auto contains(const char[]) const -> bool;
318 auto extractPointer() const -> void*;
320 static auto unthunkVariant(const any&) -> const any&;
322 DECLARE_COMPARISON_OPERATOR(==)
323 DECLARE_COMPARISON_OPERATOR(!=)
324 DECLARE_COMPARISON_OPERATOR(<)
325 DECLARE_COMPARISON_OPERATOR(<=)
326 DECLARE_COMPARISON_OPERATOR(>)
327 DECLARE_COMPARISON_OPERATOR(>=)
329 // For MS Visual Studio compiler: broken typed-enum workaround
330 DECLARE_OPERATOR(==, int, bool)
331 DECLARE_OPERATOR(!=, int, bool)
333 friend auto operator+(const any&, const any&) -> any;
335 DECLARE_OPERATOR(+, long, long)
336 DECLARE_OPERATOR(+, double, double)
337 DECLARE_OPERATOR(+, char, char)
338 DECLARE_OPERATOR(+, const char *, std::string)
340 friend auto operator-(const any&, const any&) -> any;
342 DECLARE_OPERATOR(-, long, long)
343 DECLARE_OPERATOR(-, double, double)
344 DECLARE_OPERATOR(-, char, char)
346 friend auto operator*(const any&, const any&) -> any;
348 DECLARE_OPERATOR(*, long, long)
349 DECLARE_OPERATOR(*, double, double)
350 DECLARE_OPERATOR(*, char, char)
352 friend auto operator/(const any&, const any&) -> any;
354 DECLARE_OPERATOR(/, long, long)
355 DECLARE_OPERATOR(/, double, double)
356 DECLARE_OPERATOR(/, char, char)
358 friend auto operator%(const any&, const any&) -> any;
360 DECLARE_OPERATOR(%, long, long)
361 DECLARE_OPERATOR(%, char, char)
363 friend auto operator-(const any&) -> any; // unary negate
364};
366// For MS Visual Studio compiler: broken typed-enum workaround
367inline auto operator==(const any& lhs, int rhs) -> bool {
368 return lhs == (long)rhs;
370inline auto operator!=(const any& lhs, int rhs) -> bool {
371 return lhs != (long)rhs;
374} // namespace PureScript
376#undef DECLARE_OPERATOR
377#undef DECLARE_COMPARISON_OPERATOR
379#endif // PureScript_HH