-
Notifications
You must be signed in to change notification settings - Fork 15
/
basic.hpp
448 lines (373 loc) · 11.1 KB
/
basic.hpp
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//
// Originally written by Björn Fahller ([email protected])
// Modified by Jeff Bush
//
// No rights claimed. The sources are released to the public domain
//
// For license information, please refer to <http://unlicense.org>
//
#include <setjmp.h>
#include <stdlib.h>
#include <iostream>
#include <ostream>
#include <forward_list>
#include <vector>
#include <algorithm>
#include <numeric>
namespace basic {
struct stack_frame { jmp_buf buf; };
std::forward_list<stack_frame> stack;
#define GOSUB \
if (!(setjmp((basic::stack.emplace_front(),basic::stack.front().buf)) \
&& (basic::stack.pop_front(),true))) \
goto
#define RETURN if (basic::stack.empty()) return 0; longjmp(basic::stack.front().buf, 1)
template <typename T, typename U>
struct separator
{
static char const *str() { return ""; }
};
template <typename T>
struct separator<T,T>
{
static char const *str() { return ","; }
};
inline void type_mismatch() {
std::cout << "Type mismatch error\n";
exit(1);
}
inline void array_out_of_bounds() {
std::cout << "Array out of bounds error\n";
exit(1);
}
inline void dimension_error() {
std::cout << "Array Dimension error\n";
exit(1);
}
inline void illegal_quantity_error() {
std::cout << "Illegal quantity error\n";
exit(1);
}
class variant
{
public:
variant()
: isnum(true),
numval(0)
{}
variant(double _numval)
: isnum(true),
numval(_numval)
{}
variant(int _numval)
: isnum(true),
numval(_numval)
{}
variant(const std::string _strval)
: isnum(false),
strval(_strval)
{}
variant(const char *_strval)
: isnum(false),
strval(_strval)
{}
double numeric() const {
if (!isnum)
type_mismatch();
return numval;
}
std::string string() const {
if (isnum)
type_mismatch();
return strval;
}
variant toString() const {
if (isnum)
return variant(std::to_string(numval));
else
return *this;
}
variant toNum() const {
if (!isnum)
return variant(stod(strval));
else
return *this;
}
variant &operator=(const variant ©from) {
isnum = copyfrom.isnum;
numval = copyfrom.numval;
strval = copyfrom.strval;
return *this;
}
// Note that strings in BASIC are 1 based, so subtract 1 from the left
// index
variant midStr(const variant &left, const variant &right) const {
if (isnum || !left.isnum || !right.isnum)
type_mismatch();
return variant(strval.substr(int(left.numval) - 1, (int(right.numval) - int(left.numval) + 1)));
}
variant leftStr(const variant &count) const {
if (isnum)
type_mismatch();
return variant(strval.substr(0, int(count.numval)));
}
variant rightStr(const variant &count) const {
if (isnum)
type_mismatch();
return variant(strval.substr(strval.length() - int(count.numval) - 1, int(count.numval)));
}
variant strlen() const {
if (isnum)
type_mismatch();
return variant((int)strval.length());
}
variant operator+(const variant op) const {
if (isnum != op.isnum)
type_mismatch();
if (isnum)
return variant(numval + op.numval);
else
return variant(strval + op.strval);
}
variant operator-(const variant op) const {
if (!isnum || !op.isnum)
type_mismatch();
return variant(numval - op.numval);
}
variant operator*(const variant op) const {
if (!isnum || !op.isnum)
type_mismatch();
return variant(numval * op.numval);
}
variant operator/(const variant op) const {
if (!isnum || !op.isnum)
type_mismatch();
return variant(numval / op.numval);
}
bool operator>(const variant op) const {
if (isnum != op.isnum)
type_mismatch();
if (isnum)
return numval > op.numval;
else
return strval > op.strval;
}
bool operator>=(const variant op) const {
if (isnum != op.isnum)
type_mismatch();
if (isnum)
return numval >= op.numval;
else
return strval >= op.strval;
}
bool operator<(const variant op) const {
if (isnum != op.isnum)
type_mismatch();
if (isnum)
return numval < op.numval;
else
return strval < op.strval;
}
bool operator<=(const variant op) const {
if (isnum != op.isnum)
type_mismatch();
if (isnum)
return numval <= op.numval;
else
return strval <= op.strval;
}
bool operator!=(const variant op) const {
if (isnum != op.isnum)
type_mismatch();
if (isnum)
return numval != op.numval;
else
return strval != op.strval;
}
bool operator==(const variant op) const {
if (isnum != op.isnum)
type_mismatch();
if (isnum)
return numval == op.numval;
else
return strval == op.strval;
}
bool isnum;
double numval;
std::string strval;
};
template <typename T>
typename std::enable_if<std::is_constructible<variant, T>::value && !std::is_same<typename std::decay<T>::type, variant>::value, variant>::type
operator+(T&& t, variant const& p)
{
return variant(std::forward<T>(t)) + p;
}
template <typename T>
typename std::enable_if<std::is_constructible<variant, T>::value && !std::is_same<typename std::decay<T>::type, variant>::value, variant>::type
operator-(T&& t, variant const& p)
{
return variant(std::forward<T>(t)) - p;
}
template <typename T>
typename std::enable_if<std::is_constructible<variant, T>::value && !std::is_same<typename std::decay<T>::type, variant>::value, variant>::type
operator*(T&& t, variant const& p)
{
return variant(std::forward<T>(t)) * p;
}
template <typename T>
typename std::enable_if<std::is_constructible<variant, T>::value && !std::is_same<typename std::decay<T>::type, variant>::value, variant>::type
operator/(T&& t, variant const& p)
{
return variant(std::forward<T>(t)) / p;
}
template <typename T>
typename std::enable_if<std::is_constructible<variant, T>::value && !std::is_same<typename std::decay<T>::type, variant>::value, bool>::type
operator==(T&& t, variant const& p)
{
return variant(std::forward<T>(t)) == p;
}
template <typename T>
struct is_dimmable
{
using DT = typename std::decay<T>::type;
static constexpr bool value = std::is_integral<DT>::value || std::is_same<DT, variant>::value;
};
template <template <typename> class pred, typename ... T>
struct all_are
{
static constexpr bool value = true;
};
template <template <typename> class pred, typename H, typename ... T>
struct all_are<pred, H, T...>
{
static constexpr bool value = pred<H>::value && all_are<pred, T...>::value;
};
template <typename T>
T numeric_value(T t) { return t; }
inline
long numeric_value(const variant& v) { return long(v.numeric()); }
class array
{
public:
template <typename ... T, typename = typename std::enable_if<all_are<is_dimmable, T...>::value>::type>
array(T const & ... t)
: dimensions{1LU+numeric_value(t)..., 1LU},
elements(std::accumulate(std::begin(dimensions),
std::end(dimensions),
1U,
[](std::size_t i, std::size_t j){ return i*j;}))
{}
template <typename ... T>
typename std::enable_if<all_are<is_dimmable, T...>::value, variant&>::type
operator()(T const& ... t) {
if (sizeof...(t) != dimensions.size() - 1)
dimension_error();
long indexes[] { long(numeric_value(t))... };
auto i = sizeof...(t);
size_t mul = 1U;
size_t idx = 0U;
while (i--)
{
if (indexes[i] < 0 || indexes[i] >= dimensions[i])
array_out_of_bounds();
idx += indexes[i]*mul;
mul = dimensions[i];
}
return elements[idx];
}
private:
std::vector<std::size_t> dimensions;
std::vector<variant> elements;
};
class printer
{
public:
printer &operator,(const char *s) {
std::cout << s;
return *this;
}
printer &operator,(const variant &v) {
if (v.isnum)
std::cout << v.numval;
else
std::cout << v.strval;
return *this;
}
~printer() { std::cout << std::endl; }
};
class input
{
public:
input& operator,(char const *s) {
std::cout << s << std::flush;
return *this;
}
input& operator,(variant& v) {
if (v.isnum)
std::cin >> v.numval;
else
std::cin >> v.strval;
return *this;
}
};
template <typename T>
variant to_char_str(T const& t)
{
auto n = numeric_value(t);
char a[] = { char(n), '\0' };
return variant{a};
}
template <std::size_t N>
inline variant to_asc_val(char const (&a)[N])
{
if (N < 2) illegal_quantity_error();
return variant(double(a[0]));
}
inline variant to_asc_val(variant const& v)
{
auto s = v.string();
if (s.empty()) illegal_quantity_error();
return variant(double(s[0]));
}
#define INPUT basic::input(),
#define PRINT basic::printer(),
#define IF if (
#define THEN )
#define LET basic::variant
#define GOTO goto
#define FOR { basic::variant& for_loop_variable =
#define TO ; \
{ \
jmp_buf for_loop_top; \
bool for_loop_exit = false; \
while (!for_loop_exit) \
{ \
if (setjmp(for_loop_top) == 0) \
{ \
basic::variant for_loop_step=1; \
basic::variant const for_loop_endval=
#define STEP ; \
for_loop_step=
#define NEXT for_loop_variable=for_loop_variable+for_loop_step; \
for_loop_exit=( ( for_loop_step > 0 \
&& for_loop_variable > for_loop_endval) \
||( for_loop_step < 0 \
&& for_loop_variable < for_loop_endval)); \
longjmp(for_loop_top, 1); \
} \
} \
} \
}
#define END exit(0)
#define VAL(x) x.toNum()
#define STR(x) x.toString()
#define MID$(str, left, right) str.midStr(left, right)
#define LEFT$(str, count) str.leftStr(count)
#define RIGHT$(str, count) str.rightStr(count)
#define LEN(str) str.strlen()
#define DIM basic::array
#define RND(x) ((rand() & 0xffff)/65535.0)
#define INT(x) static_cast<int>(basic::numeric_value(x))
#define CHR$(num) basic::to_char_str(num)
#define ASC(str) basic::to_asc_val(str)
} // namespace basic