-
Notifications
You must be signed in to change notification settings - Fork 40
/
purescript.h
188 lines (154 loc) · 6.34 KB
/
purescript.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
///////////////////////////////////////////////////////////////////////////////
//
// Module : purescript.h
// Copyright : (c) Andy Arvanitis 2018
// License : BSD
//
// Maintainer : Andy Arvanitis
// Stability : experimental
// Portability :
//
// Basic types and functions to support purescript-to-C++ rendering
//
///////////////////////////////////////////////////////////////////////////////
//
#ifndef purescript_H
#define purescript_H
#include <memory>
#include <functional>
#include <deque>
#include <string>
#include <limits>
#include "string_literal_dict.h"
namespace purescript {
using std::string;
class boxed : public std::shared_ptr<void> {
public:
using fn_t = std::function<boxed(const boxed&)>;
using eff_fn_t = std::function<boxed(void)>;
using dict_t = string_literal_dict_t<boxed>;
using array_t = std::deque<boxed>;
public:
using std::shared_ptr<void>::shared_ptr;
boxed() noexcept : std::shared_ptr<void>() {}
boxed(const std::nullptr_t) noexcept : std::shared_ptr<void>() {}
boxed(const int n) : std::shared_ptr<void>(std::make_shared<int>(n)) {}
boxed(const long n) : std::shared_ptr<void>(std::make_shared<int>(static_cast<int>(n))) {
#if !defined(NDEBUG) // if debug build
if (n < std::numeric_limits<int>::min() || n > std::numeric_limits<int>::max()) {
throw std::runtime_error("integer out of range");
}
#endif // !defined(NDEBUG)
}
boxed(const unsigned long n) : std::shared_ptr<void>(std::make_shared<int>(static_cast<int>(n))) {
#if !defined(NDEBUG) // if debug build
if (n > std::numeric_limits<int>::max()) {
throw std::runtime_error("integer out of range");
}
#endif // !defined(NDEBUG)
}
boxed(const double n) : std::shared_ptr<void>(std::make_shared<double>(n)) {}
boxed(const bool b) : std::shared_ptr<void>(std::make_shared<bool>(b)) {}
boxed(const char s[]) : std::shared_ptr<void>(std::make_shared<string>(s)) {}
boxed(string&& s) : std::shared_ptr<void>(std::make_shared<string>(std::move(s))) {}
boxed(const string& s) : std::shared_ptr<void>(std::make_shared<string>(s)) {}
boxed(array_t&& l) : std::shared_ptr<void>(std::make_shared<array_t>(std::move(l))) {}
boxed(const array_t& l) : std::shared_ptr<void>(std::make_shared<array_t>(l)) {}
boxed(dict_t&& m) : std::shared_ptr<void>(std::make_shared<dict_t>(std::move(m))) {}
boxed(const dict_t& m) : std::shared_ptr<void>(std::make_shared<dict_t>(m)) {}
template <typename T,
typename = typename std::enable_if<!std::is_same<boxed,T>::value>::type>
boxed(const T& f,
typename std::enable_if<std::is_assignable<std::function<boxed(boxed)>,T>::value>::type* = 0)
: std::shared_ptr<void>(std::make_shared<fn_t>(f)) {
}
template <typename T,
typename = typename std::enable_if<!std::is_same<boxed,T>::value>::type>
boxed(const T& f,
typename std::enable_if<std::is_assignable<std::function<boxed(void)>,T>::value>::type* = 0)
: std::shared_ptr<void>(std::make_shared<eff_fn_t>(f)) {
}
template <typename T,
typename = typename std::enable_if<!std::is_same<boxed,T>::value &&
std::is_convertible<T,fn_t>::value>::type>
auto operator=(const T& right) -> boxed& {
if (std::shared_ptr<void>::operator bool()) {
auto& f = *static_cast<fn_t*>(get());
f = right;
} else {
reset(new fn_t(right));
}
return *this;
}
auto operator()(const boxed& arg) const -> boxed {
auto& f = *static_cast<fn_t*>(get());
return f(arg);
}
auto operator()() const -> boxed {
auto& f = *static_cast<eff_fn_t*>(get());
return f();
}
auto operator[](const char key[]) const -> const boxed& {
return (*static_cast<const dict_t*>(get()))[key];
}
auto operator[](const char key[]) -> boxed& {
return (*static_cast<dict_t*>(get()))[key];
}
#if !defined(NDEBUG) // if debug build
auto operator[](const int index) const -> const boxed& {
return static_cast<const array_t*>(get())->at(index);
}
auto operator[](const int index) -> boxed& {
return static_cast<array_t*>(get())->at(index);
}
#else // not debug build
auto operator[](const int index) const -> const boxed& {
return (*static_cast<const array_t*>(get()))[index];
}
auto operator[](const int index) -> boxed& {
return (*static_cast<array_t*>(get()))[index];
}
#endif // !defined(NDEBUG)
}; // class boxed
template <typename T, typename... Args>
inline auto box(Args&&... args) -> boxed {
return std::make_shared<T>(std::forward<Args>(args)...);
}
template <typename T>
constexpr auto unbox(const boxed& b) -> const T& {
return *static_cast<const T*>(b.get());
}
template <typename T>
constexpr auto unbox(boxed& b) -> T& {
return *static_cast<T*>(b.get());
}
template <typename T>
constexpr auto unbox(const T value) -> T {
return value;
}
template <typename T,
typename = typename std::enable_if<std::is_same<T, int>::value>::type>
constexpr auto unbox(const std::size_t value) -> long long {
return value;
}
inline auto array_length(const boxed& a) -> boxed::array_t::size_type {
return unbox<boxed::array_t>(a).size();
}
using fn_t = boxed::fn_t;
using eff_fn_t = boxed::eff_fn_t;
using dict_t = boxed::dict_t;
using array_t = boxed::array_t;
constexpr auto undefined = nullptr;
} // namespace purescript
#define DEFINE_FOREIGN_DICTIONARY_AND_ACCESSOR() \
inline auto foreign() -> dict_t& {\
static dict_t $dict$;\
return $dict$;\
}
#define FOREIGN_BEGIN(NS) namespace NS {\
using namespace purescript;\
DEFINE_FOREIGN_DICTIONARY_AND_ACCESSOR()\
static const auto $foreign_exports_init$ = ([]() -> char {\
dict_t& exports = foreign();
#define FOREIGN_END return 0; }()); }
#endif // purescript_H