git.iko.soy/rowtype-yoga/purescript-native
on ce67f6c help sign in
279 lines 8.5 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 <utility>
33#include <stdexcept>
35#define DECLARE_OPERATOR(op, ty, rty) \
36 friend auto operator op (const any&, ty) -> rty; \
37 friend auto operator op (ty, const any&) -> rty;
39#define DECLARE_COMPARISON_OPERATOR(op) \
40 friend auto operator op (const any&, const any&) -> bool; \
41 DECLARE_OPERATOR(op, long, bool) \
42 DECLARE_OPERATOR(op, double, bool) \
43 DECLARE_OPERATOR(op, char, bool) \
44 DECLARE_OPERATOR(op, const char *, bool)
47namespace PureScript {
49using string = const char *;
50using nullptr_t = std::nullptr_t;
52// Workaround for missing C++11 version in gcc
53class runtime_error : public std::runtime_error {
54public:
55 runtime_error(const char message[]) : std::runtime_error(std::string(message)) {}
56};
58const bool undefined = false;
60// A variant data class designed to provide some features of dynamic typing.
61//
62class any {
64 public:
65 enum class Type : int8_t {
66 Unknown,
67 Integer,
68 Double,
69 Character,
70 Boolean,
71 StringLiteral,
72 String,
73 Map,
74 Vector,
75 Function,
76 Closure,
77 EffFunction,
78 Thunk,
79 Pointer
80 };
82 mutable Type type = Type::Unknown;
84 struct as_thunk {
85 };
86 static constexpr as_thunk unthunk = as_thunk{};
88 using map = std::vector<std::pair<const char * const, const any>>;
89 using vector = std::vector<any>;
90 using fn = auto (*)(const any&) -> any;
91 using closure = std::function<any(const any&)>;
92 using eff_fn = std::function<any()>;
93 using thunk = auto (*)(const as_thunk) -> const any&;
95 template <typename T>
96 using shared = std::shared_ptr<T>;
98 template <typename T, typename... Args>
99 inline static auto make_shared(Args&&... args) -> decltype(std::make_shared<T>(std::forward<Args>(args)...)) {
100 return std::make_shared<T>(std::forward<Args>(args)...);
101 }
103 private:
104 union {
105 mutable long i;
106 mutable double d;
107 mutable char c;
108 mutable bool b;
109 mutable string r;
110 mutable shared<std::string> s;
111 mutable shared<map> m;
112 mutable shared<vector> v;
113 mutable fn f;
114 mutable shared<closure> l;
115 mutable shared<eff_fn> e;
116 mutable thunk t;
117 mutable shared<void> p;
118 };
120 public:
122 any(const long val) : type(Type::Integer), i(val) {}
123 any(const int val) : type(Type::Integer), i(val) {}
124 any(const unsigned int val) : type(Type::Integer), i(val) {}
125 any(const double val) : type(Type::Double), d(val) {}
126 any(const char val) : type(Type::Character), c(val) {}
128 template <typename T, typename = typename std::enable_if<std::is_same<bool,T>::value>::type>
129 any(const T val) : type(Type::Boolean), b(val) {}
131 template <size_t N>
132 any(const char (&val)[N]) : type(Type::StringLiteral), r(val) {}
133 any(char * val) : type(Type::String), s(make_shared<std::string>(val)) {}
135 any(const std::string& val) : type(Type::String), s(make_shared<std::string>(val)) {}
136 any(std::string&& val) : type(Type::String), s(make_shared<std::string>(std::move(val))) {}
138 any(const shared<std::string>& val) : type(Type::String), s(val) {}
139 any(shared<std::string>&& val) noexcept : type(Type::String), s(std::move(val)) {}
141 any(const map& val) : type(Type::Map), m(make_shared<map>(val)) {}
142 any(map&& val) noexcept : type(Type::Map), m(make_shared<map>(std::move(val))) {}
144 any(const vector& val) : type(Type::Vector), v(make_shared<vector>(val)) {}
145 any(vector&& val) noexcept : type(Type::Vector), v(make_shared<vector>(std::move(val))) {}
147 template <typename T>
148 any(const T& val, typename std::enable_if<std::is_convertible<T,fn>::value>::type* = 0)
149 : type(Type::Function), f(val) {}
151 template <typename T, typename = typename std::enable_if<!std::is_same<any,T>::value &&
152 !std::is_convertible<T,fn>::value>::type>
153 any(const T& val, typename std::enable_if<std::is_assignable<closure,T>::value>::type* = 0)
154 : type(Type::Closure), l(make_shared<closure>(val)) {}
156 template <typename T>
157 any(const T& val, typename std::enable_if<std::is_assignable<eff_fn,T>::value>::type* = 0)
158 : type(Type::EffFunction), e(make_shared<eff_fn>(val)) {}
160 template <typename T>
161 any(const T& val, typename std::enable_if<std::is_convertible<T,thunk>::value>::type* = 0)
162 : type(Type::Thunk), t(val) {}
164 template <typename T>
165 any(const T& val, typename std::enable_if<std::is_assignable<shared<void>,T>::value>::type* = 0)
166 : type(Type::Pointer), p(val) {}
168 template <typename T>
169 any(T&& val, typename std::enable_if<std::is_assignable<shared<void>,T>::value>::type* = 0) noexcept
170 : type(Type::Pointer), p(std::move(val)) {}
172 any(std::nullptr_t) : type(Type::Pointer), p(nullptr) {}
174 any(const any&);
175 any(any&&) noexcept;
177 auto operator=(const any&) -> any&;
178 auto operator=(any&) noexcept -> any&;
179 auto operator=(any&&) noexcept -> any&;
181 any() = delete;
182 ~any();
184 template <typename T>
185 auto cast() const -> typename std::enable_if<std::is_same<T, long>::value, T>::type;
187 template <typename T>
188 auto cast() const -> typename std::enable_if<std::is_same<T, double>::value, T>::type;
190 template <typename T>
191 auto cast() const -> typename std::enable_if<std::is_same<T, char>::value, T>::type;
193 template <typename T>
194 auto cast() const -> typename std::enable_if<std::is_same<T, bool>::value, T>::type;
196 template <typename T>
197 auto cast() const -> typename std::enable_if<std::is_same<T, string>::value, T>::type;
199 template <typename T>
200 auto cast() const -> typename std::enable_if<std::is_same<T, map>::value, const T&>::type;
202 template <typename T>
203 auto cast() const -> typename std::enable_if<std::is_same<T, vector>::value, const T&>::type;
205 template <typename T>
206 auto cast() const -> typename std::enable_if<std::is_assignable<shared<void>,T>::value, typename T::element_type*>::type {
207 return static_cast<typename T::element_type*>(extractPointer());
208 }
210 auto operator()(const any&) const -> any;
212 auto operator()(const as_thunk) const -> const any&;
214 auto operator()() const -> any;
216 operator long() const;
217 operator double() const;
218 operator bool() const;
220 operator string() const;
221 operator const map&() const;
222 operator const vector&() const;
224 auto operator[](const char[]) const -> const any&;
225 auto operator[](const vector::size_type) const -> const any&;
226 auto operator[](const any&) const -> const any&;
228 auto contains(const char[]) const -> bool;
230 auto extractPointer() const -> void*;
232 static auto extractValue(const any&) -> const any&;
234 DECLARE_COMPARISON_OPERATOR(==)
235 DECLARE_COMPARISON_OPERATOR(!=)
236 DECLARE_COMPARISON_OPERATOR(<)
237 DECLARE_COMPARISON_OPERATOR(<=)
238 DECLARE_COMPARISON_OPERATOR(>)
239 DECLARE_COMPARISON_OPERATOR(>=)
241 friend auto operator+(const any&, const any&) -> any;
243 DECLARE_OPERATOR(+, long, long)
244 DECLARE_OPERATOR(+, double, double)
245 DECLARE_OPERATOR(+, char, char)
246 DECLARE_OPERATOR(+, const char *, std::string)
248 friend auto operator-(const any&, const any&) -> any;
250 DECLARE_OPERATOR(-, long, long)
251 DECLARE_OPERATOR(-, double, double)
252 DECLARE_OPERATOR(-, char, char)
254 friend auto operator*(const any&, const any&) -> any;
256 DECLARE_OPERATOR(*, long, long)
257 DECLARE_OPERATOR(*, double, double)
258 DECLARE_OPERATOR(*, char, char)
260 friend auto operator/(const any&, const any&) -> any;
262 DECLARE_OPERATOR(/, long, long)
263 DECLARE_OPERATOR(/, double, double)
264 DECLARE_OPERATOR(/, char, char)
266 friend auto operator%(const any&, const any&) -> any;
268 DECLARE_OPERATOR(%, long, long)
269 DECLARE_OPERATOR(%, char, char)
271 friend auto operator-(const any&) -> any; // unary negate
272};
274} // namespace PureScript
276#undef DECLARE_OPERATOR
277#undef DECLARE_COMPARISON_OPERATOR
279#endif // PureScript_HH