-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuiltin.hpp
More file actions
291 lines (236 loc) · 7 KB
/
builtin.hpp
File metadata and controls
291 lines (236 loc) · 7 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#ifndef SS_BUILTIN_HPP
#define SS_BUILTIN_HPP
#include <cstddef>
#include <string.h>
#include <iostream>
#include <stdexcept>
#ifndef NULL
#define NULL 0
#endif
typedef int __ss_int;
typedef double __ss_float;
typedef bool __ss_bool;
static const bool True = true;
static const bool False = false;
#define ASSERT(x, msg) __ESBMC_assert(x, "Assertion Failed")
#define __NOT(x) (!(x))
#define __AND(a, b, t) ((!___bool(__ ## t = a))?(__ ## t):(b))
#define FAST_FOR(var, start, stop, step, zero, dir) \
for(__ss_int var = (start); \
dir ? (var < (stop)) : (var > (stop)); \
var = var + (step))
#define FOR_IN(var, container, start, stop, step) \
for(__ss_int __##var##_iter = start; \
__##var##_iter < stop; \
__##var##_iter += step) \
if(auto var = container->__getitem__(__##var##_iter))
#define END_FOR
namespace shedskin {
class str;
class class_;
// Forward declare char_cache array - definition will be in cpp
extern str* __char_cache[256];
class pyobj {
public:
class_ *__class__;
pyobj() : __class__(NULL) {}
virtual ~pyobj() {}
virtual bool equals(const pyobj* other) const {
return this == other;
}
};
class str : public pyobj {
public:
const char* data;
str() : data(NULL) {}
str(const char* s) : data(s) {}
const char* c_str() const { return data; }
virtual bool equals(const pyobj* other) const override {
const str* other_str = dynamic_cast<const str*>(other);
if (!other_str) return false;
if (!data || !other_str->data) return false;
return strcmp(data, other_str->data) == 0;
}
str* strip() const;
str* upper() const;
str* lower() const;
str* replace(const str* old_str, const str* new_str) const;
str* format() const;
str* __add__(const str* other) const;
char __getfast__(__ss_int i) const {
return data[i];
}
__ss_int __len__() const {
return data ? strlen(data) : 0;
}
str* __getitem__(__ss_int i) const;
};
__ss_int len(str* s) {
return s->__len__();
}
class class_ {
public:
str *__name__;
class_ *__bases__;
class_() : __name__(new str()), __bases__(NULL) {}
class_(const char* name) : __name__(new str(name)), __bases__(NULL) {}
~class_() { delete __name__; }
};
__ss_bool isinstance(pyobj* obj, class_* cls) {
if (!obj || !cls) return false;
class_* curr = obj->__class__;
while (curr) {
if (curr == cls) return true;
curr = curr->__bases__;
}
return false;
}
bool isinstance_bool(bool val) { return true; }
bool isinstance_int(__ss_int val) { return true; }
bool isinstance_float(__ss_float val) { return true; }
bool isinstance_str(str *s) { return true; }
inline void print() {
// std::cout << std::endl;
}
template<typename T>
void print(const T& value) {
// std::cout << value << std::endl;
}
template<typename T1, typename T2>
void print(const T1& value1, const T2& value2) {
// std::cout << value1 << value2 << std::endl;
}
template<typename... Args>
void print(const Args&... args) {
//(std::cout << ... << args) << std::endl;
}
// Max function implementations
template<typename T>
inline T ___max(T a, T b) {
return (a > b) ? a : b;
}
template<typename T>
inline T ___max(T a, T b, T c) {
return ___max(___max(a, b), c);
}
template<typename T>
inline T ___max(T a, T b, T c, T d) {
return ___max(___max(a, b), ___max(c, d));
}
template<typename T, typename... Args>
inline T ___max(T first, Args... args) {
T result = first;
((result = (args > result) ? args : result), ...);
return result;
}
// Min function implementations
template<typename T>
inline T ___min(T a, T b) {
return (a < b) ? a : b;
}
template<typename T>
inline T ___min(T a, T b, T c) {
return ___min(___min(a, b), c);
}
template<typename T>
inline T ___min(T a, T b, T c, T d) {
return ___min(___min(a, b), ___min(c, d));
}
template<typename T, typename... Args>
inline T ___min(T first, Args... args) {
T result = first;
((result = (args < result) ? args : result), ...);
return result;
}
inline __ss_int power(__ss_int base, __ss_int exp) {
__ss_int result = 1;
while (exp > 0) {
if (exp & 1)
result *= base;
base *= base;
exp >>= 1;
}
return result;
}
inline __ss_int __power(__ss_int base, __ss_int exp) {
return power(base, exp);
}
inline bool ___bool(bool b) {
return b;
}
inline bool __eq(pyobj* a, pyobj* b) {
if (a == b) return true;
if (!a || !b) return false;
return a->equals(b);
}
inline bool __eq(char a, const str* b) {
if (!b || !b->data) return false;
return a == b->data[0];
}
inline bool __eq(const str* a, char b) {
return __eq(b, a);
}
bool __eq(char a, char b) {
return a == b;
}
inline bool __ne(pyobj* a, pyobj* b) {
return !__eq(a, b);
}
void __init() {}
int __int(int value) {
return value;
}
int __floordiv(int a, int b) {
if (b == 0) {
throw std::runtime_error("Division by zero");
}
return a / b;
}
inline __ss_int __mods(__ss_int a, __ss_int b) {
if (b == 0) {
throw std::runtime_error("Modulo by zero is undefined");
}
return a % b;
}
// Defining __divs to manage whole divisions
inline __ss_int __divs(__ss_int a, __ss_int b) {
if (b == 0) {
std::cerr << "Error: Division by zero detected" << std::endl;
throw std::runtime_error("Division by zero detected");
}
return a / b; // Renvoie le résultat de la division entière
}
void __start(void (*initfunc)()) {
initfunc();
}
inline __ss_int __range(__ss_int stop) {
return stop;
}
inline __ss_int __range(__ss_int start, __ss_int stop) {
return stop;
}
inline __ss_int __range(__ss_int start, __ss_int stop, __ss_int step) {
return stop;
}
inline str* input(str* prompt) {
if (prompt && prompt->data) {
std::cout << prompt->data;
}
std::string line;
std::getline(std::cin, line);
return new str(line.c_str());
}
inline str* input() {
return input(new str(""));
}
} // namespace shedskin
namespace ss = shedskin;
#include "list.hpp"
#include "dict.hpp"
#include "set.hpp"
#include "string.hpp"
#include "tuple.hpp"
#include "bytes.hpp"
#include "math.hpp"
#include "random.hpp"
#endif