git.iko.soy/rowtype-yoga/purescript-native
on f93d365 help sign in
324 lines 9.6 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 kDataCtor = 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 Vector,
77 Array,
78 Function,
79 Closure,
80 EffFunction,
81 Thunk,
82 Pointer
83 };
85 private:
86 mutable Type type = Type::Unknown;
87 using vector = std::vector<any>;
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 = vector;
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 };
106 template <typename T>
107 class _closure : public closure {
108 const T lambda;
109 public:
110 _closure(const T& l) noexcept : lambda(l) {}
111 auto operator()(const any& arg) const -> any {
112 return lambda(arg);
113 }
114 };
116 class eff_fn {
117 public:
118 virtual auto operator()() const -> any = 0;
119 };
121 template <typename T>
122 class _eff_fn : public eff_fn {
123 const T lambda;
124 public:
125 _eff_fn(const T& l) noexcept : lambda(l) {}
126 auto operator()() const -> any {
127 return lambda();
128 }
129 };
131 public:
132 template <typename T>
133 using shared = std::shared_ptr<T>;
135 template <typename T, typename... Args>
136 inline static auto make_shared(Args&&... args) -> decltype(std::make_shared<T>(std::forward<Args>(args)...)) {
137 return std::make_shared<T>(std::forward<Args>(args)...);
138 }
140 private:
141 union {
142 mutable long i;
143 mutable double d;
144 mutable char c;
145 mutable bool b;
146 mutable string r;
147 mutable shared<std::string> s;
148 mutable shared<map> m;
149 mutable shared<vector> v;
150 mutable shared<array> a;
151 mutable fn f;
152 mutable shared<closure> l;
153 mutable shared<eff_fn> e;
154 mutable thunk t;
155 mutable shared<void> p;
156 };
158 public:
160 any(const long val) : type(Type::Integer), i(val) {}
161 any(const int val) : type(Type::Integer), i(val) {}
162 any(const unsigned int val) : type(Type::Integer), i(val) {}
163 any(const double val) : type(Type::Double), d(val) {}
164 any(const char val) : type(Type::Character), c(val) {}
166 template <typename T, typename = typename std::enable_if<std::is_same<bool,T>::value>::type>
167 any(const T val) : type(Type::Boolean), b(val) {}
169 template <size_t N>
170 any(const char (&val)[N]) : type(Type::StringLiteral), r(val) {}
171 any(char * val) : type(Type::String), s(make_shared<std::string>(val)) {}
173 any(const std::string& val) : type(Type::String), s(make_shared<std::string>(val)) {}
174 any(std::string&& val) : type(Type::String), s(make_shared<std::string>(std::move(val))) {}
176 any(const shared<std::string>& val) : type(Type::String), s(val) {}
177 any(shared<std::string>&& val) noexcept : type(Type::String), s(std::move(val)) {}
179 any(const map& val) : type(Type::Map), m(make_shared<map>(val)) {}
180 any(map&& val) noexcept : type(Type::Map), m(make_shared<map>(std::move(val))) {}
182 any(const vector& val) : type(Type::Vector), v(make_shared<vector>(val)) {}
183 any(vector&& val) noexcept : type(Type::Vector), v(make_shared<vector>(std::move(val))) {}
185 any(const array& val) : type(Type::Array), a(make_shared<array>(val)) {}
186 any(array&& val) noexcept : type(Type::Array), a(make_shared<array>(std::move(val))) {}
188 template <typename T>
189 any(const T& val, typename std::enable_if<std::is_convertible<T,fn>::value>::type* = 0)
190 : type(Type::Function), f(val) {}
192 template <typename T, typename = typename std::enable_if<!std::is_same<any,T>::value &&
193 !std::is_convertible<T,fn>::value>::type>
194 any(const T& val, typename std::enable_if<std::is_assignable<std::function<any(const any&)>,T>::value>::type* = 0)
195 : type(Type::Closure), l(make_shared<_closure<T>>(val)) {}
197 template <typename T>
198 any(const T& val, typename std::enable_if<std::is_assignable<std::function<any()>,T>::value>::type* = 0)
199 : type(Type::EffFunction), e(make_shared<_eff_fn<T>>(val)) {}
201 template <typename T>
202 any(const T& val, typename std::enable_if<std::is_convertible<T,thunk>::value>::type* = 0)
203 : type(Type::Thunk), t(val) {}
205 template <typename T>
206 any(const T& val, typename std::enable_if<std::is_assignable<shared<void>,T>::value>::type* = 0)
207 : type(Type::Pointer), p(val) {}
209 template <typename T>
210 any(T&& val, typename std::enable_if<std::is_assignable<shared<void>,T>::value>::type* = 0) noexcept
211 : type(Type::Pointer), p(std::move(val)) {}
213 any(std::nullptr_t) : type(Type::Pointer), p(nullptr) {}
215 any(const any&);
216 any(any&&) noexcept;
218 auto operator=(const any&) -> any&;
219 auto operator=(any&) noexcept -> any&;
220 auto operator=(any&&) noexcept -> any&;
222 any() = delete;
223 ~any();
225 template <typename T>
226 auto cast() const -> typename std::enable_if<std::is_same<T, long>::value, T>::type;
228 template <typename T>
229 auto cast() const -> typename std::enable_if<std::is_same<T, double>::value, T>::type;
231 template <typename T>
232 auto cast() const -> typename std::enable_if<std::is_same<T, char>::value, T>::type;
234 template <typename T>
235 auto cast() const -> typename std::enable_if<std::is_same<T, bool>::value, T>::type;
237 template <typename T>
238 auto cast() const -> typename std::enable_if<std::is_same<T, string>::value, T>::type;
240 template <typename T>
241 auto cast() const -> typename std::enable_if<std::is_same<T, map>::value, const T&>::type;
243 template <typename T>
244 auto cast() const -> typename std::enable_if<std::is_same<T, vector>::value, const T&>::type;
246 template <typename T>
247 auto cast() const -> typename std::enable_if<std::is_same<T, array>::value, const T&>::type;
249 template <typename T>
250 auto cast() const -> typename std::enable_if<std::is_assignable<shared<void>,T>::value, typename T::element_type*>::type {
251 return static_cast<typename T::element_type*>(extractPointer());
252 }
254 auto operator()(const any&) const -> any;
256 auto operator()(const as_thunk) const -> const any&;
258 auto operator()() const -> any;
260 operator long() const;
261 operator double() const;
262 operator bool() const;
264 operator string() const;
265 operator const map&() const;
266 operator const vector&() const;
267 operator const array&() const;
269 auto operator[](const char[]) const -> const any&;
270 auto operator[](const size_t) const -> const any&;
271 auto operator[](const any&) const -> const any&;
273 auto contains(const char[]) const -> bool;
275 auto extractPointer() const -> void*;
277 static auto unthunkVariant(const any&) -> const any&;
279 DECLARE_COMPARISON_OPERATOR(==)
280 DECLARE_COMPARISON_OPERATOR(!=)
281 DECLARE_COMPARISON_OPERATOR(<)
282 DECLARE_COMPARISON_OPERATOR(<=)
283 DECLARE_COMPARISON_OPERATOR(>)
284 DECLARE_COMPARISON_OPERATOR(>=)
286 friend auto operator+(const any&, const any&) -> any;
288 DECLARE_OPERATOR(+, long, long)
289 DECLARE_OPERATOR(+, double, double)
290 DECLARE_OPERATOR(+, char, char)
291 DECLARE_OPERATOR(+, const char *, std::string)
293 friend auto operator-(const any&, const any&) -> any;
295 DECLARE_OPERATOR(-, long, long)
296 DECLARE_OPERATOR(-, double, double)
297 DECLARE_OPERATOR(-, char, char)
299 friend auto operator*(const any&, const any&) -> any;
301 DECLARE_OPERATOR(*, long, long)
302 DECLARE_OPERATOR(*, double, double)
303 DECLARE_OPERATOR(*, char, char)
305 friend auto operator/(const any&, const any&) -> any;
307 DECLARE_OPERATOR(/, long, long)
308 DECLARE_OPERATOR(/, double, double)
309 DECLARE_OPERATOR(/, char, char)
311 friend auto operator%(const any&, const any&) -> any;
313 DECLARE_OPERATOR(%, long, long)
314 DECLARE_OPERATOR(%, char, char)
316 friend auto operator-(const any&) -> any; // unary negate
317};
319} // namespace PureScript
321#undef DECLARE_OPERATOR
322#undef DECLARE_COMPARISON_OPERATOR
324#endif // PureScript_HH