-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcell.hpp
610 lines (544 loc) · 20.7 KB
/
cell.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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
#pragma once
#include <iomanip>
#include <memory>
#include <ostream>
#include <type_traits>
#include <string>
#include <sstream>
#include <tuple>
#include "acore.hpp"
#include "atoms.hpp"
namespace ALisp {
template<typename T>
std::tuple<bool, T> tryStringToNumber(const std::string &str) {
T value;
std::stringstream stream(str);
stream >> value;
return std::make_tuple(!stream.fail(), value);
}
enum CellType {
None,
Atom,
Integer,
Float,
String,
List,
Lambda,
Macro,
Proc,
ProcEnv,
EnvPointer
};
struct Cell; // forward dec
extern Cell Nil, True, False;
struct Cell {
public:
typedef ListType::const_iterator const_iterator;
// Cell interface
struct ICell {
ICell(CellType type) : _type(type) {}
virtual std::unique_ptr<ICell> clone() const = 0;
virtual ~ICell() {}
CellType type() const { return _type; }
void type(CellType t) { _type = t; }
// Type accessors
virtual AtomType atomId() const EXCEPT { throw Exception("Not an atom"); }
virtual IntegerType integerValue() const EXCEPT { throw Exception("Not an integer"); }
virtual FloatType floatValue() const EXCEPT { throw Exception("Not a float"); }
virtual StringType str() const = 0;
virtual StringType repr() const = 0;
virtual EnvironmentType env() EXCEPT { throw Exception("Not an environment"); }
virtual ProcType proc() const EXCEPT { throw Exception("Not a proc"); }
virtual ProcEnvType proc_env() const EXCEPT { throw Exception("Not a proc_env"); }
// Operators
virtual void multiply(const Cell &other) EXCEPT { throw Exception("Cannot multiply"); }
virtual void add(const Cell &other) EXCEPT { throw Exception("Cannot add"); }
virtual void subtract(const Cell &other) EXCEPT { throw Exception("Cannot subtract"); }
virtual void divide(const Cell &other) EXCEPT { throw Exception("Cannot divide"); }
virtual bool equals(const Cell &other) const { return false; }
virtual bool lessthan(const Cell &other) const { return false; }
virtual bool morethan(const Cell &other) const { return false; }
// List functions
virtual void push(const Cell &value) EXCEPT { throw Exception("Not a list"); }
virtual Cell head() const EXCEPT { throw Exception("Not a list"); }
virtual Cell tail() const EXCEPT { throw Exception("Not a list"); }
virtual bool empty() const EXCEPT { throw Exception("Not a list"); }
virtual size_t size() const EXCEPT { throw Exception("Not a list"); }
virtual bool size_atleast(size_t i) const EXCEPT { throw Exception("Not a list"); }
virtual Cell index(size_t i) const EXCEPT { throw Exception("Not a list"); }
virtual const_iterator cbegin() const EXCEPT { throw Exception("Not a list"); }
virtual const_iterator cend() const EXCEPT { throw Exception("Not a list"); }
// Lambda/macro functions
virtual Cell lambda_ptr() const EXCEPT { throw Exception("Not a lambda-like"); }
virtual void lambda_ptr(const Cell &ptr) EXCEPT { throw Exception("Not a lambda-like"); }
virtual Cell lambda_args() const EXCEPT { throw Exception("Not a lambda-like"); }
virtual void lambda_args(const Cell &ptr) EXCEPT { throw Exception("Not a lambda-like"); }
virtual Cell lambda_body() const EXCEPT { throw Exception("Not a lambda-like"); }
virtual void lambda_body(const Cell &ptr) EXCEPT { throw Exception("Not a lambda-like"); }
template<typename T>
StringType hex(T value) const {
std::stringbuf buf;
std::ostream os(&buf);
os << "0x" << std::setfill('0') << std::setw(sizeof(T) * 2)
<< std::hex << value;
return buf.str();
}
protected:
CellType _type;
};
public:
Cell(Cell&&)=default;
Cell& operator=(Cell&&)=default;
Cell(Cell const &o) : Cell(o.pImpl ? Cell(o.pImpl->clone()) : Cell()) {}
Cell()=default;
Cell& operator=(Cell const &o) {
Cell tmp(o);
swap(pImpl, tmp.pImpl);
return *this;
}
Cell(std::unique_ptr<ICell> x) : pImpl(std::move(x)) {}
CellType type() const {
if (!pImpl) return None;
return pImpl->type();
}
AtomType atomId() const EXCEPT {
if (!pImpl) return 0;
return pImpl->atomId();
}
IntegerType integerValue() const EXCEPT {
if (!pImpl) return 0;
return pImpl->integerValue();
}
FloatType floatValue() const EXCEPT {
if (!pImpl) return 0;
return pImpl->floatValue();
}
StringType str() const {
if (!pImpl) return "<no_ptr_ref>";
return pImpl->str();
}
StringType repr() const {
if (!pImpl) return "<no_ptr_ref>";
return pImpl->repr();
}
EnvironmentType env() const EXCEPT {
if (!pImpl) return EnvironmentType(nullptr);
return pImpl->env();
}
// Operator implementations
bool operator==(const Cell &other) const {
// Special case: no implementation, nothing is equal
if (!pImpl) return false;
// Special case: comparison to self
if (other.pImpl == pImpl) return true;
return pImpl->equals(other);
}
bool operator!= (const Cell &other) const {
return !(*this == other);
}
bool operator< (const Cell &other) const {
if (!pImpl) return false;
return pImpl->lessthan(other);
}
bool operator<= (const Cell &other) const {
if (!pImpl) return false;
return pImpl->lessthan(other) || pImpl->equals(other);
}
bool operator> (const Cell &other) const {
if (!pImpl) return false;
return pImpl->morethan(other);
}
bool operator>= (const Cell &other) const {
if (!pImpl) return false;
return pImpl->morethan(other) || pImpl->equals(other);
}
Cell& operator+= (const Cell &other) EXCEPT {
if (pImpl) {
pImpl->add(other);
}
return *this;
}
Cell& operator-= (const Cell &other) EXCEPT {
if (pImpl) {
pImpl->subtract(other);
}
return *this;
}
Cell& operator*= (const Cell &other) EXCEPT {
if (pImpl) {
pImpl->multiply(other);
}
return *this;
}
Cell& operator/= (const Cell &other) EXCEPT {
if (pImpl) {
pImpl->divide(other);
}
return *this;
}
// List functions
Cell head() const EXCEPT;
Cell tail() const EXCEPT;
bool empty() const EXCEPT;
size_t size() const EXCEPT;
bool size_atleast(size_t i) const EXCEPT;
void push(const Cell &value) EXCEPT { if (pImpl) pImpl->push(value); }
Cell index(size_t i) const EXCEPT;
const_iterator cbegin() const EXCEPT {
if (pImpl) return pImpl->cbegin();
throw Exception("Not a list");
}
const_iterator cend() const EXCEPT {
if (pImpl) return pImpl->cend();
throw Exception("Not a list");
}
// Lambda/Macro functions
Cell lambda_ptr() const EXCEPT;
void lambda_ptr(const Cell &) EXCEPT;
Cell lambda_args() const EXCEPT;
void lambda_args(const Cell &) EXCEPT;
Cell lambda_body() const EXCEPT;
void lambda_body(const Cell &) EXCEPT;
// Proc and ProcEnv
ProcType proc() const EXCEPT {
if (pImpl) return pImpl->proc();
throw Exception("Cell: Not a proc()");
}
ProcEnvType proc_env() const EXCEPT {
if (pImpl) return pImpl->proc_env();
throw Exception("Cell: Not a proc_env()");
}
protected:
std::unique_ptr<ICell> pImpl;
};
template<CellType CellTypeType = CellType::None>
struct NoneTypeCell : Cell {
struct INoneTypeCell : ICell {
INoneTypeCell() : ICell(CellTypeType) {}
std::unique_ptr<ICell> clone() const final override {
return std::make_unique<INoneTypeCell>();
}
StringType str() const final override { return "(none)"; }
StringType repr() const final override { return "(none)"; }
bool equals(const Cell &other) const final override {
return other.type() == CellType::None;
}
};
NoneTypeCell()
: Cell(std::make_unique<INoneTypeCell>()) {}
};
template<typename ValueType = AtomType, CellType CellTypeType = CellType::Atom>
struct AtomTypeCell : Cell {
struct IAtomCell : ICell {
IAtomCell(ValueType value) :
ICell(CellTypeType),
_value(value) {}
std::unique_ptr<ICell> clone() const final override {
return std::make_unique<IAtomCell>(_value);
}
AtomType atomId() const EXCEPT final override { return _value; }
StringType str() const final override { return Atoms::ToString(_value); }
StringType repr() const final override {
StringType r = "(atom \"";
r += str();
r += "\")";
return r;
}
bool equals(const Cell &other) const final override {
return other.type() == CellType::Atom && _value == other.atomId();
}
protected:
ValueType _value;
};
AtomTypeCell(ValueType value)
: Cell(std::make_unique<IAtomCell>(value)) {}
};
template<typename ValueType = StringType, CellType CellTypeType = CellType::String>
struct StringTypeCell : Cell {
struct IStringCell;
template<typename T>
StringTypeCell(T value)
: Cell(std::make_unique<IStringCell>(value)) {}
template<typename B, typename E>
StringTypeCell(B begin, E end)
: Cell(std::make_unique<IStringCell>(begin, end)) {}
struct IStringCell : ICell {
template<typename T>
IStringCell(T value) :
ICell(CellTypeType),
_value(value) {}
IStringCell(const char value) :
ICell(CellTypeType),
_value(StringType(1, value)) {}
template<typename B, typename E>
IStringCell(B begin, E end) :
ICell(CellTypeType),
_value(begin, end) {}
std::unique_ptr<ICell> clone() const final override {
return std::make_unique<IStringCell>(_value);
}
IntegerType integerValue() const EXCEPT final override {
auto isInteger = tryStringToNumber<IntegerType>(_value);
if (std::get<0>(isInteger))
return std::get<1>(isInteger);
else throw Exception("Failed to convert to integer: " + str());
}
FloatType floatValue() const EXCEPT final override {
auto isFloat = tryStringToNumber<FloatType>(_value);
if (std::get<0>(isFloat))
return std::get<1>(isFloat);
else throw Exception("Failed to convert to float: " + str());
}
StringType str() const final override { return _value; }
StringType repr() const final override { return "\"" + _value + "\""; }
void add(const Cell &other) EXCEPT final override { _value += other.str(); }
bool equals(const Cell &other) const final override {
return other.str() == _value;
}
// List functions
Cell head() const EXCEPT final override {
if (_value == "") return Nil;
return StringTypeCell<ValueType, CellTypeType>(_value[0]);
}
Cell tail() const EXCEPT final override {
if (_value == "") return Nil;
return StringTypeCell<ValueType, CellTypeType>(_value.substr(1));
}
Cell index(size_t i) const EXCEPT final override {
if (i >= _value.size())
return Nil;
return StringTypeCell<ValueType, CellTypeType>(_value[i]);
}
size_t size() const EXCEPT final override { return _value.length(); }
bool size_atleast(size_t i) const EXCEPT final override { return i < size(); }
bool empty() const EXCEPT final override { return _value == ""; }
protected:
ValueType _value;
};
};
template<typename ValueType, CellType CellTypeType>
struct MathematicalCell : Cell {
struct IMathematicalCell : ICell {
IMathematicalCell(ValueType value) :
ICell(CellTypeType),
_value(value) {}
std::unique_ptr<ICell> clone() const final override {
return std::make_unique<IMathematicalCell>(_value);
}
IntegerType integerValue() const EXCEPT final override {
return (IntegerType)_value;
}
FloatType floatValue() const EXCEPT final override {
return (FloatType)_value;
}
StringType str() const final override { return std::to_string(_value); }
StringType repr() const final override { return str(); }
bool equals(const Cell &other) const final override {
return (other.type() == CellType::Integer && (IntegerType)_value == other.integerValue()) ||
(other.type() == CellType::Float && (FloatType)_value == other.floatValue()) ||
str() == other.str();
}
bool lessthan(const Cell &other) const final override {
return (other.type() == CellType::Integer && (IntegerType)_value < other.integerValue()) ||
(other.type() == CellType::Float && (FloatType)_value < other.floatValue());
}
bool morethan(const Cell &other) const final override {
return (other.type() == CellType::Integer && (IntegerType)_value > other.integerValue()) ||
(other.type() == CellType::Float && (FloatType)_value > other.floatValue());
}
void add(const Cell &other) EXCEPT final override { _value += convertOther(other); }
void subtract(const Cell &other) EXCEPT final override { _value -= convertOther(other); }
void multiply(const Cell &other) EXCEPT final override { _value *= convertOther(other); }
void divide(const Cell &other) EXCEPT final override { _value /= convertOther(other); }
protected:
ValueType _value;
ValueType convertOther(const Cell &other) const EXCEPT {
switch (other.type()) {
case CellType::Integer: return (ValueType)other.integerValue();
case CellType::Float: return (ValueType)other.floatValue();
default:
throw Exception("Unable to convert other");
}
}
};
MathematicalCell(ValueType value)
: Cell(std::make_unique<IMathematicalCell>(value)) {}
};
template<typename ValueType = ListType, CellType CellTypeType = CellType::List>
struct ListTypeCell : Cell {
typedef typename ValueType::iterator iterator;
typedef typename ValueType::const_iterator const_iterator;
struct IListTypeCell;
ListTypeCell() : Cell(std::make_unique<IListTypeCell>()) {}
ListTypeCell(const ValueType &other) : Cell(std::make_unique<IListTypeCell>(other)) {}
template<typename InputIterator> ListTypeCell(InputIterator first, InputIterator last)
: Cell(std::make_unique<IListTypeCell>(first, last)) {}
struct IListTypeCell : ICell {
IListTypeCell() : ICell(CellTypeType), _value() {}
IListTypeCell(const ValueType &other) : ICell(CellTypeType), _value(other) {}
template<typename InputIterator> IListTypeCell(InputIterator first, InputIterator last) : ICell(CellTypeType), _value(first, last) {}
std::unique_ptr<ICell> clone() const final override {
return std::make_unique<IListTypeCell>(_value.cbegin(), _value.cend());
}
StringType str_or_repr(bool str) const {
std::ostringstream ss;
ss << stringOpen(str);
for (auto it = _value.cbegin(); it != _value.cend(); ++it) {
if (it != _value.cbegin())
ss << " ";
ss << (str ? it->str() : it->repr());
}
ss << stringClose();
return ss.str();
}
StringType str() const final override {
return str_or_repr(true);
}
StringType repr() const final override {
return str_or_repr(false);
}
bool equals(const Cell &other) const final override {
if (type() != other.type()) return false;
auto it1 = cbegin();
auto it2 = other.cbegin();
for (; it1 != cend() && it2 != cend(); ++it1, ++it2) {
if (*it1 != *it2) return false;
}
return it1 == cend() && it2 == other.cend();
}
void push(const Cell &value) EXCEPT final override { _value.push_back(value); }
Cell head() const EXCEPT final override {
if (_value.empty()) return Nil;
return *_value.cbegin();
}
Cell tail() const EXCEPT final override {
return ListTypeCell(_value.cbegin() + 1, _value.cend());
}
bool empty() const EXCEPT final override { return _value.empty(); }
size_t size() const EXCEPT final override { return _value.size(); }
bool size_atleast(size_t i) const EXCEPT final override {
return (cbegin() + i) != cend();
}
const_iterator cbegin() const EXCEPT final override { return _value.cbegin(); }
const_iterator cend() const EXCEPT final override { return _value.cend(); }
Cell index(size_t i) const EXCEPT final override {
if (i >= size())
return Nil;
auto it = cbegin() + i;
return *it;
}
// Mathematical operations
void add(const Cell &other) EXCEPT final override {
Cell to_add = other;
if (other.type() != CellType::List) {
to_add = ListTypeCell<ValueType, CellTypeType>();
to_add.push(other);
}
_value.insert(_value.end(), to_add.cbegin(), to_add.cend());
}
// Lambda/macro functions
Cell lambda_ptr() const EXCEPT final override { return _value[lm_ptr]; }
void lambda_ptr(const Cell &ptr) EXCEPT final override { lm(); _value[lm_ptr] = ptr; }
Cell lambda_args() const EXCEPT final override { return _value[lm_args]; }
void lambda_args(const Cell &args) EXCEPT final override { lm(); _value[lm_args] = args; }
Cell lambda_body() const EXCEPT final override { return _value[lm_body]; }
void lambda_body(const Cell &body) EXCEPT final override { lm(); _value[lm_body] = body; }
protected:
ValueType _value;
StringType stringOpen(bool str_or_repr) const {
switch (type()) {
case CellType::Lambda: return "(#ref:lambda ";
case CellType::Macro: return "(#ref:macro ";
//case CellType::Tuple: return "(tuple ";
default: return (str_or_repr ? "(" : "(list ");
}
}
StringType stringClose() const {
return ")";
}
static const size_t lm_ptr = 0, lm_args = 1, lm_body = 2, lm_required = 3;
// Helper function to reserve enough space for lambda values
void lm() {
if (size() < lm_required)
_value.resize(lm_required);
}
};
};
template<typename ProcTypeType = ProcType, CellType CellTypeType = CellType::Proc>
struct ProcTypeCell : Cell {
struct IProcTypeCell : ICell {
IProcTypeCell(ProcTypeType proc) : ICell(CellTypeType), _proc(proc) {}
std::unique_ptr<ICell> clone() const final override {
return std::make_unique<IProcTypeCell>(_proc);
}
StringType str() const final override {
StringType s = "(#ref:proc:unknown)";
return s;
}
StringType repr() const final override { return str(); }
bool equals(const Cell &other) const final override {
return other.type() == CellType::Proc && other.proc() == _proc;
}
ProcType proc() const EXCEPT final override { return _proc; }
ProcEnvType proc_env() const EXCEPT final override { throw Exception("Not a ProcEnv"); }
protected:
ProcTypeType _proc;
};
ProcTypeCell(ProcTypeType proc) : Cell(std::make_unique<IProcTypeCell>(proc)) {}
};
template<typename ProcTypeType = ProcEnvType, CellType CellTypeType = CellType::ProcEnv>
struct ProcEnvTypeCell : Cell {
struct IProcEnvTypeCell : ICell {
IProcEnvTypeCell(ProcTypeType proc) : ICell(CellTypeType), _proc(proc) {}
std::unique_ptr<ICell> clone() const final override {
return std::make_unique<IProcEnvTypeCell>(_proc);
}
StringType str() const final override {
StringType s = "(#ref:procenv:unknown)";
return s;
}
StringType repr() const final override { return str(); }
bool equals(const Cell &other) const final override {
return other.type() == CellType::ProcEnv && other.proc_env() == _proc;
}
ProcType proc() const EXCEPT final override { throw Exception("Not a Proc"); }
ProcEnvType proc_env() const EXCEPT final override { return _proc; }
protected:
ProcTypeType _proc;
};
ProcEnvTypeCell(ProcTypeType proc) : Cell(std::make_unique<IProcEnvTypeCell>(proc)) {}
};
template<CellType CellTypeType>
struct EnvironmentTypeCell : Cell {
struct IEnvironmentTypeCell : ICell {
IEnvironmentTypeCell(EnvironmentType value) : ICell(CellTypeType), _env(value) {}
std::unique_ptr<ICell> clone() const final override {
return std::make_unique<IEnvironmentTypeCell>(_env);
}
EnvironmentType env() EXCEPT final override { return _env; }
bool equals(const Cell &other) const final override {
return other.type() == CellType::EnvPointer && other.env() == _env;
}
StringType str() const final override {
StringType s = "(#ref:env:unknown)";
return s;
}
StringType repr() const final override { return str(); }
protected:
EnvironmentType _env;
};
EnvironmentTypeCell(EnvironmentType env) : Cell(std::make_unique<IEnvironmentTypeCell>(env)) {}
};
using NoneCell = NoneTypeCell<CellType::None>;
using AtomCell = AtomTypeCell<AtomType, CellType::Atom>;
using StringCell = StringTypeCell<StringType, CellType::String>;
using IntCell = MathematicalCell<IntegerType, CellType::Integer>;
using FloatCell = MathematicalCell<FloatType, CellType::Float>;
using ListCell = ListTypeCell<ListType, CellType::List>;
using LambdaCell = ListTypeCell<ListType, CellType::Lambda>;
using MacroCell = ListTypeCell<ListType, CellType::Macro>;
using ProcCell = ProcTypeCell<ProcType, CellType::Proc>;
using ProcEnvCell = ProcEnvTypeCell<ProcEnvType, CellType::ProcEnv>;
using EnvironmentCell = EnvironmentTypeCell<CellType::EnvPointer>;
std::ostream &operator<<(std::ostream &os, const Cell &cell);
}