git.iko.soy/rowtype-yoga/purescript-native
on bbd67e4 help sign in
403 lines 14 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 <unordered_map>
33#include <stdexcept>
35#include "map_key.hh"
36#include "hash.hh"
38namespace PureScript {
40using string = std::string;
41using runtime_error = std::runtime_error;
42using nullptr_t = std::nullptr_t;
44const bool undefined = false;
46// A variant data class designed to provide some features of dynamic typing.
47//
48class any {
50 public:
51 enum class Type : std::int8_t {
52 Unknown,
53 Integer,
54 Double,
55 Character,
56 Boolean,
57 String,
58 Map,
59 Vector,
60 Function,
61 EffFunction,
62 Thunk,
63 Pointer
64 };
66 Type type = Type::Unknown;
68 struct as_thunk {
69 };
70 static constexpr as_thunk unthunk = as_thunk{};
72 using map = std::unordered_map<map_key_t, const any, map_key_t::hasher, map_key_t::equal>;
73 using vector = std::vector<any>;
74 using fn = std::function<any(const any&)>;
75 using eff_fn = std::function<any()>;
76 using thunk = std::function<const any& (const as_thunk)>;
78 using shared_string = std::shared_ptr<string>;
79 using shared_map = std::shared_ptr<map>;
80 using shared_vector = std::shared_ptr<vector>;
82 using shared_fn = std::shared_ptr<fn>;
83 using shared_eff_fn = std::shared_ptr<eff_fn>;
84 using shared_thunk = std::shared_ptr<thunk>;
86 template <typename T>
87 using shared = std::shared_ptr<T>;
89 using shared_void_ptr = std::shared_ptr<void>;
91 template <typename T>
92 static constexpr auto make_shared(const T& arg) -> shared<T> {
93 return std::make_shared<T>(arg);
94 }
96 template <typename T>
97 static constexpr auto make_shared(T&& arg) -> shared<T> {
98 return std::make_shared<T>(std::move(arg));
99 }
101 template <typename T, typename U>
102 static constexpr auto static_cast_shared(U arg) -> T* {
103 return static_cast<T*>(arg.get());
104 }
106 private:
107 union {
108 long i;
109 double d;
110 char c;
111 bool b;
112 shared_string s;
113 shared_map m;
114 shared_vector v;
115 shared_fn f;
116 shared_eff_fn e;
117 shared_thunk t;
118 shared_void_ptr p;
119 };
121 public:
123 any(const long val) : type(Type::Integer), i(val) {}
124 any(const int val) : type(Type::Integer), i(val) {}
125 any(const unsigned int val) : type(Type::Integer), i(val) {}
127 any(const double val) : type(Type::Double), d(val) {}
129 any(const char val) : type(Type::Character), c(val) {}
131 any(const bool val) : type(Type::Boolean), b(val) {}
133 any(const string& val) : type(Type::String), s(make_shared<string>(val)) {}
134 any(string&& val) : type(Type::String), s(make_shared<string>(std::move(val))) {}
136 any(const char val[]) : type(Type::String), s(make_shared<string>(val)) {}
138 any(const shared_string& val) : type(Type::String), s(val) {}
139 any(shared_string&& val) : type(Type::String), s(std::move(val)) {}
141 any(const map& val) : type(Type::Map), m(make_shared<map>(val)) {}
142 any(map&& val) : type(Type::Map), m(make_shared<map>(std::move(val))) {}
144 any(const shared_map& val) : type(Type::Map), m(val) {}
145 any(shared_map&& val) : type(Type::Map), m(std::move(val)) {}
147 any(const vector& val) : type(Type::Vector), v(make_shared<vector>(val)) {}
148 any(vector&& val) : type(Type::Vector), v(make_shared<vector>(std::move(val))) {}
150 any(const shared_vector& val) : type(Type::Vector), v(val) {}
151 any(shared_vector&& val) : type(Type::Vector), v(std::move(val)) {}
153 any(const shared_fn& val) : type(Type::Function), f(val) {}
154 any(shared_fn&& val) : type(Type::Function), f(std::move(val)) {}
156 template <typename T>
157 any(const T& val, typename std::enable_if<std::is_assignable<fn,T>::value>::type* = 0)
158 : type(Type::Function), f(make_shared<fn>(val)) {}
160 // template <typename T>
161 // any(T&& val, typename std::enable_if<std::is_assignable<fn,T>::value>::type* = 0)
162 // : type(Type::Function), f(std::move(val)) {}
164 any(const shared_eff_fn& val) : type(Type::EffFunction), e(val) {}
165 any(shared_eff_fn&& val) : type(Type::EffFunction), e(std::move(val)) {}
167 template <typename T>
168 any(const T& val, typename std::enable_if<std::is_assignable<eff_fn,T>::value>::type* = 0)
169 : type(Type::EffFunction), e(make_shared<eff_fn>(val)) {}
171 any(const shared_thunk& val) : type(Type::Thunk), t(val) {}
172 any(shared_thunk&& val) : type(Type::Thunk), t(std::move(val)) {}
174 template <typename T>
175 any(const T& val, typename std::enable_if<std::is_assignable<thunk,T>::value>::type* = 0)
176 : type(Type::Thunk), t(make_shared<thunk>(val)) {}
178 template <typename T>
179 any(const T& val, typename std::enable_if<std::is_assignable<shared_void_ptr,T>::value>::type* = 0)
180 : type(Type::Pointer), p(val) {}
182 any(std::nullptr_t) : type(Type::Pointer), p(nullptr) {}
184 template <typename T>
185 any(T&& val, typename std::enable_if<std::is_assignable<shared_void_ptr,T>::value>::type* = 0)
186 : type(Type::Pointer), p(std::move(val)) {}
188 any(const any&);
190 void swap(any&&);
192 any(any&& val) {
193 swap(std::move(val));
194 }
196 auto operator=(any val) -> any& {
197 swap(std::move(val));
198 return *this;
199 }
201 any() = delete;
202 ~any();
204 template <typename T>
205 auto cast() const -> typename std::enable_if<std::is_same<T, long>::value, T>::type;
207 template <typename T>
208 auto cast() const -> typename std::enable_if<std::is_same<T, double>::value, T>::type;
210 template <typename T>
211 auto cast() const -> typename std::enable_if<std::is_same<T, char>::value, T>::type;
213 template <typename T>
214 auto cast() const -> typename std::enable_if<std::is_same<T, bool>::value, T>::type;
216 template <typename T>
217 auto cast() const -> typename std::enable_if<std::is_same<T, string>::value, const T&>::type;
219 template <typename T>
220 auto cast() const -> typename std::enable_if<std::is_same<T, const char*>::value, const T>::type;
222 template <typename T>
223 auto cast() const -> typename std::enable_if<std::is_same<T, map>::value, const T&>::type;
225 template <typename T>
226 auto cast() const -> typename std::enable_if<std::is_same<T, vector>::value, const T&>::type;
228 template <typename T>
229 auto cast() const -> typename std::enable_if<std::is_assignable<shared_void_ptr,T>::value, typename T::element_type*>::type {
230 if (type == Type::Pointer) {
231 return static_cast_shared<typename T::element_type>(p);
232 }
233 const any* valuePtr = this;
234 do {
235 assert(valuePtr->type == Type::Thunk);
236 const any& value = (*valuePtr->t)(unthunk);
237 if (value.type == Type::Pointer) {
238 return static_cast_shared<typename T::element_type>(value.p);
239 }
240 valuePtr = &value;
241 } while (valuePtr->type != Type::Unknown);
242 return nullptr;
243 }
245 auto operator()(const any) const -> any;
247 auto operator()(const as_thunk) const -> const any&;
249 auto operator()() const -> any;
251 operator long() const;
252 operator double() const;
253 operator bool() const;
255 operator const string&() const;
256 operator const map&() const;
257 operator const vector&() const;
259 auto operator[](const map_key_t) const -> const any&;
260 auto operator[](const vector::size_type) const -> const any&;
261 auto operator[](const any&) const -> const any&;
263 auto extractPointer() const -> const void*;
265 static auto extract_value(const any&) -> const any&;
267 auto operator==(const any&) const -> bool;
268 auto operator==(const long) const -> bool;
269 auto operator==(const double) const -> bool;
270 auto operator==(const char) const -> bool;
271 auto operator==(const bool) const -> bool;
272 auto operator==(const string&) const -> bool;
273 auto operator==(const char * const) const -> bool;
275 auto operator!=(const any&) const -> bool;
276 auto operator!=(const long) const -> bool;
277 auto operator!=(const double) const -> bool;
278 auto operator!=(const char) const -> bool;
279 auto operator!=(const bool) const -> bool;
280 auto operator!=(const string&) const -> bool;
281 auto operator!=(const char * const) const -> bool;
283 auto operator<(const any&) const -> bool;
284 auto operator<(const long) const -> bool;
285 auto operator<(const double) const -> bool;
286 auto operator<(const char) const -> bool;
287 auto operator<(const bool) const -> bool;
288 auto operator<(const string&) const -> bool;
289 auto operator<(const char * const) const -> bool;
291 auto operator<=(const any&) const -> bool;
292 auto operator<=(const long) const -> bool;
293 auto operator<=(const double) const -> bool;
294 auto operator<=(const char) const -> bool;
295 auto operator<=(const bool) const -> bool;
296 auto operator<=(const string&) const -> bool;
297 auto operator<=(const char * const) const -> bool;
299 auto operator>(const any&) const -> bool;
300 auto operator>(const long) const -> bool;
301 auto operator>(const double) const -> bool;
302 auto operator>(const char) const -> bool;
303 auto operator>(const bool) const -> bool;
304 auto operator>(const string&) const -> bool;
305 auto operator>(const char * const) const -> bool;
307 auto operator>=(const any&) const -> bool;
308 auto operator>=(const long) const -> bool;
309 auto operator>=(const double) const -> bool;
310 auto operator>=(const char) const -> bool;
311 auto operator>=(const bool) const -> bool;
312 auto operator>=(const string&) const -> bool;
313 auto operator>=(const char * const) const -> bool;
315 auto operator+(const any&) const -> any;
316 auto operator+(const long) const -> long;
317 auto operator+(const double) const -> double;
318 auto operator+(const char) const -> char;
319 auto operator+(const string&) const -> string;
320 auto operator+(const char * const) const -> string;
322 auto operator-(const any&) const -> any;
323 auto operator-(const long) const -> long;
324 auto operator-(const double) const -> double;
325 auto operator-(const char) const -> char;
327 auto operator*(const any&) const -> any;
328 auto operator*(const long) const -> long;
329 auto operator*(const double) const -> double;
331 auto operator/(const any&) const -> any;
332 auto operator/(const long) const -> long;
333 auto operator/(const double) const -> double;
335 auto operator%(const any&) const -> any;
336 auto operator%(const long) const -> long;
338 auto operator-() const -> any; // unary negate
340 friend auto operator==(const long, const any&) -> bool;
341 friend auto operator!=(const long, const any&) -> bool;
342 friend auto operator< (const long, const any&) -> bool;
343 friend auto operator<=(const long, const any&) -> bool;
344 friend auto operator> (const long, const any&) -> bool;
345 friend auto operator>=(const long, const any&) -> bool;
347 friend auto operator==(const double, const any&) -> bool;
348 friend auto operator!=(const double, const any&) -> bool;
349 friend auto operator< (const double, const any&) -> bool;
350 friend auto operator<=(const double, const any&) -> bool;
351 friend auto operator> (const double, const any&) -> bool;
352 friend auto operator>=(const double, const any&) -> bool;
354 friend auto operator==(const char, const any&) -> bool;
355 friend auto operator!=(const char, const any&) -> bool;
356 friend auto operator< (const char, const any&) -> bool;
357 friend auto operator<=(const char, const any&) -> bool;
358 friend auto operator> (const char, const any&) -> bool;
359 friend auto operator>=(const char, const any&) -> bool;
361 friend auto operator==(const bool, const any&) -> bool;
362 friend auto operator!=(const bool, const any&) -> bool;
363 friend auto operator< (const bool, const any&) -> bool;
364 friend auto operator<=(const bool, const any&) -> bool;
365 friend auto operator> (const bool, const any&) -> bool;
366 friend auto operator>=(const bool, const any&) -> bool;
368 friend auto operator==(const string&, const any&) -> bool;
369 friend auto operator!=(const string&, const any&) -> bool;
370 friend auto operator< (const string&, const any&) -> bool;
371 friend auto operator<=(const string&, const any&) -> bool;
372 friend auto operator> (const string&, const any&) -> bool;
373 friend auto operator>=(const string&, const any&) -> bool;
375 friend auto operator==(const char* const, const any&) -> bool;
376 friend auto operator!=(const char* const, const any&) -> bool;
377 friend auto operator< (const char* const, const any&) -> bool;
378 friend auto operator<=(const char* const, const any&) -> bool;
379 friend auto operator> (const char* const, const any&) -> bool;
380 friend auto operator>=(const char* const, const any&) -> bool;
382 friend auto operator+(const long, const any&) -> long;
383 friend auto operator-(const long, const any&) -> long;
384 friend auto operator*(const long, const any&) -> long;
385 friend auto operator/(const long, const any&) -> long;
386 friend auto operator%(const long, const any&) -> long;
388 friend auto operator+(const double, const any&) -> double;
389 friend auto operator-(const double, const any&) -> double;
390 friend auto operator*(const double, const any&) -> double;
391 friend auto operator/(const double, const any&) -> double;
393 friend auto operator+(const char, const any&) -> char;
394 friend auto operator-(const char, const any&) -> char;
396 friend auto operator+(const string&, const any&) -> string;
397 friend auto operator+(const char* const, const any&) -> string;
399};
401} // namespace PureScript
403#endif // PureScript_HH