git.iko.soy/rowtype-yoga/purescript-native
on adbb476 help sign in
339 lines 12 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>
34#include "map_key.hh"
36namespace PureScript {
38using string = std::string;
39using runtime_error = std::runtime_error;
40using nullptr_t = std::nullptr_t;
42const bool undefined = false;
44// A variant data class designed to provide some features of dynamic typing.
45//
46class any {
48 public:
49 enum class Type {
50 Unknown,
51 Integer,
52 Double,
53 Character,
54 Boolean,
55 String,
56 Map,
57 Vector,
58 Function,
59 EffFunction,
60 Thunk,
61 Pointer
62 };
64 mutable Type type = Type::Unknown;
66 struct as_thunk {
67 };
68 static constexpr as_thunk unthunk = as_thunk{};
70 using map = std::unordered_map<const map_key_t, const any, map_key_t::hasher, map_key_t::equal>;
71 using vector = std::vector<any>;
72 using fn = std::function<any(const any&)>;
73 using eff_fn = std::function<any()>;
74 using thunk = auto (*)(const as_thunk) -> const any&;
76 template <typename T>
77 using shared = std::shared_ptr<T>;
79 template <typename T, typename... Args>
80 inline static auto make_shared(Args&&... args) -> decltype(std::make_shared<T>(std::forward<Args>(args)...)) {
81 return std::make_shared<T>(std::forward<Args>(args)...);
82 }
84 private:
85 union {
86 mutable long i;
87 mutable double d;
88 mutable char c;
89 mutable bool b;
90 mutable shared<string> s;
91 mutable shared<map> m;
92 mutable shared<vector> v;
93 mutable shared<fn> f;
94 mutable shared<eff_fn> e;
95 mutable thunk t;
96 mutable shared<void> p;
97 };
99 public:
101 any(const long val) : type(Type::Integer), i(val) {}
102 any(const int val) : type(Type::Integer), i(val) {}
103 any(const unsigned int val) : type(Type::Integer), i(val) {}
104 any(const double val) : type(Type::Double), d(val) {}
105 any(const char val) : type(Type::Character), c(val) {}
107 template <typename T, typename = typename std::enable_if<std::is_same<bool,T>::value>::type>
108 any(const T val) : type(Type::Boolean), b(val) {}
110 any(const string& val) : type(Type::String), s(make_shared<string>(val)) {}
111 any(string&& val) noexcept : type(Type::String), s(make_shared<string>(std::move(val))) {}
112 any(const char val[]) : type(Type::String), s(make_shared<string>(val)) {}
114 any(const map& val) : type(Type::Map), m(make_shared<map>(val)) {}
115 any(map&& val) noexcept : type(Type::Map), m(make_shared<map>(std::move(val))) {}
117 any(const vector& val) : type(Type::Vector), v(make_shared<vector>(val)) {}
118 any(vector&& val) noexcept : type(Type::Vector), v(make_shared<vector>(std::move(val))) {}
120 template <typename T, typename = typename std::enable_if<!std::is_same<any,T>::value>::type>
121 any(const T& val, typename std::enable_if<std::is_assignable<fn,T>::value>::type* = 0)
122 : type(Type::Function), f(make_shared<fn>(val)) {}
124 template <typename T>
125 any(const T& val, typename std::enable_if<std::is_assignable<eff_fn,T>::value>::type* = 0)
126 : type(Type::EffFunction), e(make_shared<eff_fn>(val)) {}
128 template <typename T>
129 any(const T& val, typename std::enable_if<std::is_convertible<T,thunk>::value>::type* = 0)
130 : type(Type::Thunk), t(val) {}
132 template <typename T>
133 any(const T& val, typename std::enable_if<std::is_assignable<shared<void>,T>::value>::type* = 0)
134 : type(Type::Pointer), p(val) {}
136 template <typename T>
137 any(T&& val, typename std::enable_if<std::is_assignable<shared<void>,T>::value>::type* = 0) noexcept
138 : type(Type::Pointer), p(std::move(val)) {}
140 any(std::nullptr_t) : type(Type::Pointer), p(nullptr) {}
142 any(const any&);
143 any(any&&) noexcept;
145 auto operator=(const any&) -> any&;
146 auto operator=(any&) noexcept -> any&;
147 auto operator=(any&&) noexcept -> any&;
149 any() = delete;
150 ~any();
152 template <typename T>
153 auto cast() const -> typename std::enable_if<std::is_same<T, long>::value, T>::type;
155 template <typename T>
156 auto cast() const -> typename std::enable_if<std::is_same<T, double>::value, T>::type;
158 template <typename T>
159 auto cast() const -> typename std::enable_if<std::is_same<T, char>::value, T>::type;
161 template <typename T>
162 auto cast() const -> typename std::enable_if<std::is_same<T, bool>::value, T>::type;
164 template <typename T>
165 auto cast() const -> typename std::enable_if<std::is_same<T, string>::value, const T&>::type;
167 template <typename T>
168 auto cast() const -> typename std::enable_if<std::is_same<T, const char*>::value, const T>::type;
170 template <typename T>
171 auto cast() const -> typename std::enable_if<std::is_same<T, map>::value, const T&>::type;
173 template <typename T>
174 auto cast() const -> typename std::enable_if<std::is_same<T, vector>::value, const T&>::type;
176 template <typename T>
177 auto cast() const -> typename std::enable_if<std::is_assignable<shared<void>,T>::value, typename T::element_type*>::type {
178 return static_cast<typename T::element_type*>(extractPointer());
179 }
181 auto operator()(const any&) const -> any;
183 auto operator()(const as_thunk) const -> const any&;
185 auto operator()() const -> any;
187 operator long() const;
188 operator double() const;
189 operator bool() const;
191 operator const string&() const;
192 operator const map&() const;
193 operator const vector&() const;
195 auto operator[](const map_key_t&) const -> const any&;
196 auto operator[](const vector::size_type) const -> const any&;
197 auto operator[](const any&) const -> const any&;
199 auto extractPointer() const -> void*;
201 static auto extract_value(const any&) -> const any&;
203 auto operator==(const any&) const -> bool;
204 auto operator==(const long) const -> bool;
205 auto operator==(const double) const -> bool;
206 auto operator==(const char) const -> bool;
207 auto operator==(const bool) const -> bool;
208 auto operator==(const string&) const -> bool;
209 auto operator==(const char * const) const -> bool;
211 auto operator!=(const any&) const -> bool;
212 auto operator!=(const long) const -> bool;
213 auto operator!=(const double) const -> bool;
214 auto operator!=(const char) const -> bool;
215 auto operator!=(const bool) const -> bool;
216 auto operator!=(const string&) const -> bool;
217 auto operator!=(const char * const) const -> bool;
219 auto operator<(const any&) const -> bool;
220 auto operator<(const long) const -> bool;
221 auto operator<(const double) const -> bool;
222 auto operator<(const char) const -> bool;
223 auto operator<(const bool) const -> bool;
224 auto operator<(const string&) const -> bool;
225 auto operator<(const char * const) const -> bool;
227 auto operator<=(const any&) const -> bool;
228 auto operator<=(const long) const -> bool;
229 auto operator<=(const double) const -> bool;
230 auto operator<=(const char) const -> bool;
231 auto operator<=(const bool) const -> bool;
232 auto operator<=(const string&) const -> bool;
233 auto operator<=(const char * const) const -> bool;
235 auto operator>(const any&) const -> bool;
236 auto operator>(const long) const -> bool;
237 auto operator>(const double) const -> bool;
238 auto operator>(const char) const -> bool;
239 auto operator>(const bool) const -> bool;
240 auto operator>(const string&) const -> bool;
241 auto operator>(const char * const) const -> bool;
243 auto operator>=(const any&) const -> bool;
244 auto operator>=(const long) const -> bool;
245 auto operator>=(const double) const -> bool;
246 auto operator>=(const char) const -> bool;
247 auto operator>=(const bool) const -> bool;
248 auto operator>=(const string&) const -> bool;
249 auto operator>=(const char * const) const -> bool;
251 auto operator+(const any&) const -> any;
252 auto operator+(const long) const -> long;
253 auto operator+(const double) const -> double;
254 auto operator+(const char) const -> char;
255 auto operator+(const string&) const -> string;
256 auto operator+(const char * const) const -> string;
258 auto operator-(const any&) const -> any;
259 auto operator-(const long) const -> long;
260 auto operator-(const double) const -> double;
261 auto operator-(const char) const -> char;
263 auto operator*(const any&) const -> any;
264 auto operator*(const long) const -> long;
265 auto operator*(const double) const -> double;
267 auto operator/(const any&) const -> any;
268 auto operator/(const long) const -> long;
269 auto operator/(const double) const -> double;
271 auto operator%(const any&) const -> any;
272 auto operator%(const long) const -> long;
274 auto operator-() const -> any; // unary negate
276 friend auto operator==(const long, const any&) -> bool;
277 friend auto operator!=(const long, const any&) -> bool;
278 friend auto operator< (const long, const any&) -> bool;
279 friend auto operator<=(const long, const any&) -> bool;
280 friend auto operator> (const long, const any&) -> bool;
281 friend auto operator>=(const long, const any&) -> bool;
283 friend auto operator==(const double, const any&) -> bool;
284 friend auto operator!=(const double, const any&) -> bool;
285 friend auto operator< (const double, const any&) -> bool;
286 friend auto operator<=(const double, const any&) -> bool;
287 friend auto operator> (const double, const any&) -> bool;
288 friend auto operator>=(const double, const any&) -> bool;
290 friend auto operator==(const char, const any&) -> bool;
291 friend auto operator!=(const char, const any&) -> bool;
292 friend auto operator< (const char, const any&) -> bool;
293 friend auto operator<=(const char, const any&) -> bool;
294 friend auto operator> (const char, const any&) -> bool;
295 friend auto operator>=(const char, const any&) -> bool;
297 friend auto operator==(const bool, const any&) -> bool;
298 friend auto operator!=(const bool, const any&) -> bool;
299 friend auto operator< (const bool, const any&) -> bool;
300 friend auto operator<=(const bool, const any&) -> bool;
301 friend auto operator> (const bool, const any&) -> bool;
302 friend auto operator>=(const bool, const any&) -> bool;
304 friend auto operator==(const string&, const any&) -> bool;
305 friend auto operator!=(const string&, const any&) -> bool;
306 friend auto operator< (const string&, const any&) -> bool;
307 friend auto operator<=(const string&, const any&) -> bool;
308 friend auto operator> (const string&, const any&) -> bool;
309 friend auto operator>=(const string&, const any&) -> bool;
311 friend auto operator==(const char* const, const any&) -> bool;
312 friend auto operator!=(const char* const, const any&) -> bool;
313 friend auto operator< (const char* const, const any&) -> bool;
314 friend auto operator<=(const char* const, const any&) -> bool;
315 friend auto operator> (const char* const, const any&) -> bool;
316 friend auto operator>=(const char* const, const any&) -> bool;
318 friend auto operator+(const long, const any&) -> long;
319 friend auto operator-(const long, const any&) -> long;
320 friend auto operator*(const long, const any&) -> long;
321 friend auto operator/(const long, const any&) -> long;
322 friend auto operator%(const long, const any&) -> long;
324 friend auto operator+(const double, const any&) -> double;
325 friend auto operator-(const double, const any&) -> double;
326 friend auto operator*(const double, const any&) -> double;
327 friend auto operator/(const double, const any&) -> double;
329 friend auto operator+(const char, const any&) -> char;
330 friend auto operator-(const char, const any&) -> char;
332 friend auto operator+(const string&, const any&) -> string;
333 friend auto operator+(const char* const, const any&) -> string;
335};
337} // namespace PureScript
339#endif // PureScript_HH