-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCell.hpp
executable file
·545 lines (443 loc) · 14.3 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
/**
* \file Cell.hpp
*
* Encapsulates the abstract interface for a concrete class-based
* implementation of cells. Has CellABC (typedef alias Cell) as Abstract Base
* Class. Polymorphism is used in this case to get rid of numerous if-then
* statements, having cleaner implementations.
*/
#ifndef CELL_HPP
#define CELL_HPP
#include <cstddef>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <stdexcept>
#include <functional>
////////////////////////////////////////////////////////////////////////////////
///
/// ##Outline:##
/// 1. The Abstract Base Class: CellABC
/// 2. Cells containing Data: IntCell, DoubleCell, SymbolCell, ConsCell
/// 3. Cells which are able to call functions: FunctionCell,
/// ArithmeticCell
///
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// 1. The Abstract Base Class
////////////////////////////////////////////////////////////////////////////////
/**
* \class CellABC
* \brief Abstract Base Class (ABC) for Cells. Polymorphism is used in this
* case to get rid of numerous if-then statements, having cleaner
* implementations.<br />
* <b>Important:</b> A typedef alias Cell is used in order to comply to
* the given interface.
*/
class CellABC {
public:
/**
* \brief Make sure derived classes such as SymbolCell cleans up properly
*/
virtual ~CellABC();
/**
* \brief Checks if it is an integer. Remarks: returns 0 (false) by default
* should be overritten by IntCell
*/
virtual bool is_int() const;
/**
* \brief Checks if it is a double. Remarks: returns 0 (false) by default
* should be overritten by DoubleCell
*/
virtual bool is_double() const;
/**
* \brief Checks if it is a Symbol. Remarks: returns 0 (false) by default
* should be overritten by SymbolCell
*/
virtual bool is_symbol() const;
/**
* \brief Checks if it is a ConsPair. Remarks: returns 0 (false) by default
* should be overritten by ConsCell
*/
virtual bool is_cons() const;
/**
* \brief Checks if it is a ProcedureCell. Remarks: returns 0 (false) by default
* should be overritten by ProcedureCell.
*/
virtual bool is_lambda() const;
/**
* \brief Accessor (error if this is not an int cell). Remarks: IntConsCell has
* to override this method
*/
virtual int get_int() const throw (std::runtime_error);
/**
* \brief Accessor (error if this is not a double cell). Remarks: DoubleCell has
* to override this method
*/
virtual double get_double() const throw (std::runtime_error);
/**
* \brief Accessor (error if this is not an int or double cell). Remarks: IntCell
* and DoubleCell have to override this method
*/
virtual double get_numeral() const throw (std::runtime_error);
/**
* \brief Accessor (error if this is not a symbol cell). Remarks: SymbolConsCell has
* to override this method
*/
virtual std::string get_symbol() const throw (std::runtime_error);
/**
* \brief Accessor (error if this is not a cons cell). Remarks: ConsCell has
* to override this method
*/
virtual CellABC* get_car() const throw (std::runtime_error);
/**
* \brief Accessor (error if this is not a cons cell). Remarks: ConsCell has
* to override this method
*/
virtual CellABC* get_cdr() const throw (std::runtime_error);
/**
* \brief Accessor (error if this is not a procedure cell). Remarks:
* ProcedureCell has to override this method
*/
virtual CellABC* get_formals() const throw (std::runtime_error);
/**
* \brief Accessor (error if this is not a procedure cell). Remarks:
* ProcedureCell has to override this method
*/
virtual CellABC* get_body() const throw (std::runtime_error);
/**
* \brief Accessor (error if this is not a SymbolCell with definition). Remarks:
* SymbolCell has override this method
*/
virtual CellABC* get_definition() const throw (std::runtime_error);
/**
* \brief Generalisation for all functions
*/
virtual CellABC* apply(CellABC* const args) const throw (std::runtime_error);
/**
* \brief Requires the child class to specify how to print out its content.
* \param os The output stream to print to.
*/
virtual void print(std::ostream& os = std::cout) const = 0;
};
/// Using a typedef to comform to the type-namings of the given interface in
/// cons.hpp
typedef CellABC Cell;
/**
* \class SentinelCell
* \brief Is used for nil. Prevents SigFaults when accessing memberfunction on
* nil.
*/
class SentinelCell : public Cell {
virtual void print(std::ostream& os = std::cout) const;
};
// Reminder: cons.hpp expects nil to be defined somewhere (for this
// implementation, Cell.cpp is the logical place to define it).
// Here we promise this again, just to be safe.
extern Cell* const nil;
////////////////////////////////////////////////////////////////////////////////
/// 2. Cells containing Data
////////////////////////////////////////////////////////////////////////////////
/**
* \class IntCell
* \brief Implements CellABC for Cells containing an integer
*/
class IntCell : public Cell {
public:
/**
* \brief Constructor to make int cell.
*/
IntCell(int const i);
/**
* \brief Implements type check of the Cell ABC
* \return true if Cell is a IntCell
*/
virtual bool is_int() const;
/**
* \brief Implements Accessor of the Cell ABC
*/
virtual int get_int() const throw (std::runtime_error);
/**
* \brief Implements Accessor of the Cell ABC
*/
virtual double get_numeral() const throw (std::runtime_error);
/**
* \brief Specifies how the content of this type of Cell should be
* printed
*/
virtual void print(std::ostream& os = std::cout) const;
private:
int content_m;
};
/**
* \class DoubleCell
* \brief Implements CellABC for Cells containing a double
*/
class DoubleCell : public Cell {
public:
/**
* \brief Constructor to make double cell.
*/
DoubleCell(double const d);
/**
* \brief Implements type check of the Cell ABC
* \return true if Cell is a DoubleCell
*/
virtual bool is_double() const;
/**
* \brief Implements Accessor of the Cell ABC
*/
virtual double get_double() const throw (std::runtime_error);
/**
* \brief Implements Accessor of the Cell ABC
*/
virtual double get_numeral() const throw (std::runtime_error);
/**
* \brief Specifies how the content of this type of Cell should be
* printed
*/
virtual void print(std::ostream& os = std::cout) const;
private:
double content_m;
};
/**
* \class SymbolCell
* \brief Implements CellABC for Cells containing a sybmol. _Note:_ Is also
* BaseClass for FunctionCell and ArithmeticCell
*/
class SymbolCell : public Cell {
public:
/**
* \brief Constructor to make symbol cell.
*/
SymbolCell(const char* const s);
/**
* \brief Makes sure that char* content is freed on destruction (even when
* Base class is deleted)
*/
virtual ~SymbolCell();
/**
* \brief Implements type check of the Cell ABC, in this case used for runtime
* defined variables (definitions)
* \return true if Cell is a SymbolIntCell
*/
virtual bool is_int() const;
/**
* \brief Implements type check of the Cell ABC, in this case used for runtime
* defined variables (definitions)
* \return true if Cell is a SymbolIntCell
*/
virtual bool is_double() const;
/**
* \brief Implements type check of the Cell ABC
* \return true if Cell is a SymbolIntCell
*/
virtual bool is_symbol() const;
/**
* \brief Implements Accessor of the Cell ABC, in this case used for runtime
* defined variables (definitions)
*/
virtual int get_int() const throw (std::runtime_error);
/**
* \brief Implements Accessor of the Cell ABC, in this case used for runtime
* defined variables (definitions)
*/
virtual double get_double() const throw (std::runtime_error);
/**
* \brief Implements Accessor of the Cell ABC, in this case used for runtime
* defined variables (definitions)
*/
virtual double get_numeral() const throw (std::runtime_error);
/**
* \brief Implements Accessor of the Cell ABC
*/
virtual std::string get_symbol() const throw (std::runtime_error);
/**
* \brief Implements Accessor of the Cell ABC
*/
virtual Cell* get_definition() const throw (std::runtime_error);
/**
* \brief Specifies how the content of this type of Cell should be
* printed
*/
virtual void print(std::ostream& os = std::cout) const;
/**
* \brief static func to add new definitions. _Note:_ static function, in order
* to append to static defs_m map. (independent, but shared within the
* class
*/
static void add_definition(std::string key, Cell* val) throw (std::runtime_error);
private:
char* content_m;
/**
* \brief checks internally (private), if symbol is already defined
*/
static bool is_defined(std::string key);
};
/**
* \class ConsCell
* \brief Implements CellABC for Cells containing a ConsPair. Uses Lisp ConsPair
* List approach in order to have a powerful general purpose data type.
*/
class ConsCell : public Cell {
public:
/**
* \brief Constructor to make cons cell.
*/
ConsCell(Cell* const my_car, Cell* const my_cdr);
/**
* \brief Make sure it cleans up properly
*/
virtual ~ConsCell();
/**
* \brief Knowing the size can be convenient in many cases. E.g. to
* get to know how many arguments there are
* \return int size or in other words the number of hops until nil /
* terminating cell reached
*/
static int get_list_size(Cell* head);
/**
* \brief Implements type check of the Cell ABC
* \return true if Cell is a ConsCell
*/
virtual bool is_cons() const;
/**
* \brief Implements Accessor of the Cell ABC
*/
virtual Cell* get_car() const throw (std::runtime_error);
/**
* \brief Implements Accessor of the Cell ABC
*/
virtual Cell* get_cdr() const throw (std::runtime_error);
/**
* \brief Specifies how the content of this type of Cell should be
* printed
*/
virtual void print(std::ostream& os = std::cout) const;
private:
Cell* car;
Cell* cdr;
/**
* \brief Prints out s-expression, resolves recursively. Is static since no
* instance is needed.
*/
static std::string get_sexpr(Cell* const c);
};
////////////////////////////////////////////////////////////////////////////////
/// 3. Cells which are able to call functions
////////////////////////////////////////////////////////////////////////////////
/**
* \class ArithmeticCell
* \brief Derived from SymbolCell. Is used for functions which are chainable
* or in other words, do not have fixed number of arguments.
*/
class ArithmeticCell : public SymbolCell {
public:
/**
* \brief Constructor for an ArithmeticCell, which is capable to compute
* simple calculation. _Note:_ You can check available operation
* with ArithmeticCell::is_arithmetic()
*/
ArithmeticCell(const char* const s);
/**
* \brief Checks if operator is currently available. Static since no instance
* is required.
*/
static bool is_arithmetic(std::string s);
/**
* \brief Method to execution a single calculation
*/
Cell* calculate(Cell* c1, Cell* c2) const throw (std::runtime_error);
/**
* \brief Used for generalization, and getting rid of various if-else
* statements. Overrides CellABC's method.
*/
virtual Cell* apply(Cell* const args) const throw (std::runtime_error);
private:
/**
* \brief Used e.g. for zero argument calls. Most likely, can be useful
* for other tasks
* \throw runtime_error when called by - or / operator
*/
Cell* get_identity() const throw (std::runtime_error);
/**
* \brief Used for single argument calls.
*/
Cell* calculate(Cell* c) const;
};
/**
* \class FunctionCell
* \brief Derived from SymbolCell. Is used for functions which have fixed
* number of arguments.
*/
class FunctionCell : public SymbolCell {
public:
/**
* \brief Constructor for an FunctionCell, which is capable to handle
* functions with fixed paramterss (as opposed to the
* ArithmeticCell). _Note:_ You can check available operation
* with FunctionCell::is_arithmetic()
*/
FunctionCell(const char* const s);
/**
* \brief Checks if function is currentlyavailable. Static since no
* instance is required.
*/
static bool is_function(std::string fname);
/**
* \brief Used for generalization, and getting rid of various if-else
* statements. Overrides CellABC's method.
*/
virtual Cell* apply(Cell* const args) const throw (std::runtime_error);
};
/**
* \class ProcedureCell
*
* \brief Implements CellABC for Cells containing a procedure. Uses
* Lisp ConsPair List approach in order to have a powerful
* general purpose data type.
*/
class ProcedureCell : public Cell {
public:
/**
* \brief Constructor to make cons cell.
*/
ProcedureCell(Cell* const my_car, Cell* const my_cdr);
/**
* \brief Make sure it cleans up properly
*/
virtual ~ProcedureCell();
/**
* \brief Implements type check of the Cell ABC
* \return true if Cell is a ProcedureCell
*/
virtual bool is_lambda() const;
/// \todo do we need that?
/**
* \brief Implements Accessor of the Cell ABC
*/
virtual Cell* get_formals() const throw (std::runtime_error);
/**
* \brief Implements Accessor of the Cell ABC
*/
virtual Cell* get_body() const throw (std::runtime_error);
/**
* \brief Used for generalization, and getting rid of various if-else
* statements. Overrides CellABC's method.
*/
virtual Cell* apply(Cell* const args) const throw (std::runtime_error);
/**
* \brief Specifies how the content of this type of Cell should be
* printed
*/
virtual void print(std::ostream& os = std::cout) const;
private:
Cell* param;
Cell* body;
/** \brief -1 Indicates variable number of arguments */
int num_param;
};
#endif // CELL_HPP