-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cell.cpp
executable file
·507 lines (387 loc) · 11.5 KB
/
Cell.cpp
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
/**
* \file Cell.cpp
*
* Implementation of the Cell.hpp interface
*/
#include "Cell.hpp"
#include "eval.hpp"
#include "FunctionManager.hpp"
#include "DefinitionManager.hpp"
#include <cstring>
#include <sstream>
#include <iostream>
#include <iomanip>
Cell* const nil = new SentinelCell();
using namespace std;
//////////////////////////////////////////
// CellABC
CellABC::~CellABC() {}
bool CellABC::is_int() const {
return 0;
}
bool CellABC::is_double() const {
return 0;
}
bool CellABC::is_symbol() const {
return 0;
}
bool CellABC::is_cons() const {
return 0;
}
bool CellABC::is_lambda() const {
return 0;
}
int CellABC::get_int() const throw (runtime_error) {
throw runtime_error("Cell does not contain an integer");
}
double CellABC::get_double() const throw (runtime_error) {
throw runtime_error("Cell does not contain a double");
}
double CellABC::get_numeral() const throw (runtime_error) {
throw runtime_error("Cell does neither contain an integer nor a double");
}
string CellABC::get_symbol() const throw (runtime_error) {
throw runtime_error("Cell does not contain an symbol");
}
CellABC* CellABC::get_car() const throw (runtime_error) {
throw runtime_error("Cell is not a ConsPair");
}
CellABC* CellABC::get_cdr() const throw (runtime_error) {
throw runtime_error("Cell is not a ConsPair");
}
CellABC* CellABC::get_formals() const throw (runtime_error) {
throw runtime_error("Cell is not a ProcedurePair");
}
CellABC* CellABC::get_body() const throw (runtime_error) {
throw runtime_error("Cell is not a ProcedurePair");
}
CellABC* CellABC::get_definition() const throw (runtime_error) {
throw runtime_error("Cell is not a defined SymbolCell");
}
CellABC* CellABC::apply(CellABC* const args) const throw (runtime_error) {
throw runtime_error("Cell is not a FunctionCell");
}
void SentinelCell::print(std::ostream& os) const {
os << "()";
}
//////////////////////////////////////////
// IntCell
IntCell::IntCell(int const i) : content_m(i) {}
bool IntCell::is_int() const {
return 1;
}
int IntCell::get_int() const throw (runtime_error) {
return content_m;
}
double IntCell::get_numeral() const throw (runtime_error) {
return get_int();
}
void IntCell::print(std::ostream& os) const {
os << content_m;
}
//////////////////////////////////////////
// DoubleCell
DoubleCell::DoubleCell(double const d) : content_m(d) {}
bool DoubleCell::is_double() const {
return 1;
}
double DoubleCell::get_double() const throw (runtime_error) {
return content_m;
}
double DoubleCell::get_numeral() const throw (runtime_error) {
return get_double();
}
void DoubleCell::print(std::ostream& os) const {
os << std::setprecision(6) << std::fixed;
os << content_m;
}
//////////////////////////////////////////
// SynmbolCell
SymbolCell::SymbolCell(const char* const s) {
//Make a deepcopy of the parameter value to ensure that the user's variable
//will be untouched
content_m = new char[strlen(s)+1];
strcpy(content_m, s);
}
SymbolCell::~SymbolCell() {
if(content_m != NULL) {
delete content_m;
content_m = NULL;
}
}
bool SymbolCell::is_int() const {
return get_definition()->is_int();
}
bool SymbolCell::is_double() const {
return get_definition()->is_double();
}
bool SymbolCell::is_symbol() const {
return 1;
}
Cell* SymbolCell::get_definition() const throw (runtime_error) {
return DefinitionManager::Instance()->get_definition(content_m);
}
int SymbolCell::get_int() const throw (runtime_error) {
return get_definition()->get_int();
}
double SymbolCell::get_double() const throw (runtime_error) {
return get_definition()->get_double();
}
double SymbolCell::get_numeral() const throw (runtime_error) {
return get_definition()->get_numeral();
}
std::string SymbolCell::get_symbol() const throw (runtime_error) {
return content_m;
}
void SymbolCell::print(std::ostream& os) const {
os << content_m;
}
bool SymbolCell::is_defined(string key) {
return DefinitionManager::Instance()->is_definition(key);
}
void SymbolCell::add_definition(string key, Cell* val) throw (runtime_error) {
DefinitionManager::Instance()->add_definition(key, val);
}
//////////////////////////////////////////
// ConsCell
ConsCell::ConsCell(Cell* const my_car, Cell* const my_cdr) : car(my_car), cdr(my_cdr) {}
ConsCell::~ConsCell() {
if(car != nil) {
delete car;
car = NULL;
}
if(cdr != nil) {
delete cdr;
cdr = NULL;
}
}
int ConsCell::get_list_size(Cell* head) {
if (head == nil) {
return 0;
}
int counter = 0;
while(head != nil) {
head = head->get_cdr();
++counter;
}
return counter;
}
bool ConsCell::is_cons() const {
return 1;
}
Cell* ConsCell::get_car() const throw (runtime_error) {
return car;
}
Cell* ConsCell::get_cdr() const throw (runtime_error) {
return cdr;
}
void ConsCell::print(ostream& os) const {
string cdr_sexpr = get_sexpr(get_cdr());
string car_sexpr = get_sexpr(get_car());
/// In short it makes sure that not every bracket of cons will be shown
/// (2 (3 (4 ()))) ==> (2 3 4)
if (cdr_sexpr == "()") {
os << "(" + car_sexpr + ")";
return;
} else if (cdr_sexpr[0] == '(') {
os << "(" + car_sexpr
+ " "
+ cdr_sexpr.substr(1, cdr_sexpr.length() - 1);
return;
} else {
throw runtime_error("Error: List is not correct");
}
}
string ConsCell::get_sexpr(Cell* c) {
ostringstream outs;
/// recursive approach to print out ConsPair Lists
c->print(outs);
return outs.str();
}
//////////////////////////////////////////
// ArithmeticCell
ArithmeticCell::ArithmeticCell(const char* const s) : SymbolCell(s) {};
bool ArithmeticCell::is_arithmetic(string s) {
if (s == "+" || s == "-" || s == "/" || s == "*") {
return 1;
}
return 0;
}
Cell* ArithmeticCell::get_identity() const throw (runtime_error) {
string op = get_symbol();
if (op == "+") {
return (Cell*) new IntCell(0);
}
else if (op == "*") {
return (Cell*) new IntCell(1);
}
else {
throw runtime_error("- and / cannot have zero arguments!");
}
}
Cell* ArithmeticCell::apply(Cell* const args) const throw (runtime_error) {
if (nullp(args)) { /// no arguments
return get_identity();
}
else if (nullp(cdr(args))) { /// only one argument
Cell* argument = eval(car(args));
return calculate(argument);
}
else {
Cell* pos = args;
Cell* result = eval(car(pos));
pos = cdr(pos);
while (!nullp(pos)) {
Cell* c1 = eval(car(pos));
// Arithmetic Cell itself deals with the calculations
result = this->calculate(result, c1);
pos = cdr(pos);
}
return result;
}
}
Cell* ArithmeticCell::calculate(Cell* c) const {
string op = get_symbol();
double num = c->get_numeral();
if (op == "-") {
num = -1*num;
}
else if (op == "/") {
if (num == 0) {
throw runtime_error("Can not devide by zero");
}
num = 1/num;
}
/// Nothing to do for + and * operation
if (c->is_double()) {
return (Cell*) new DoubleCell(num);
}
return (Cell*) new IntCell((int) num);
}
Cell* ArithmeticCell::calculate(Cell* c1, Cell* c2) const throw (std::runtime_error) {
string op = get_symbol();
double result = 0;
double num1 = c1->get_numeral();
double num2 = c2->get_numeral();
if (op == "+") {
result = num1 + num2;
}
else if (op == "-") {
result = num1 - num2;
}
else if (op == "*") {
result = num1 * num2;
}
else if (op == "/") {
if (num2 == 0) {
throw runtime_error("Can not devide by zero");
}
result = num1 / num2;
}
if (c1->is_double() || c2->is_double()) {
return (Cell*) new DoubleCell(result);
}
return (Cell*) new IntCell((int) result);
}
//////////////////////////////////////////
// FunctionCell
FunctionCell::FunctionCell(const char* const s) : SymbolCell(s) {};
bool FunctionCell::is_function(string fname) {
return FunctionManager::Instance()->is_function(fname);
}
Cell* FunctionCell::apply(Cell* const args) const throw (runtime_error) {
string fname = get_symbol(); // just for readability
if(args == nil && fname != "<") {
string msg = fname // provides function name
+ " cannot be called without any argument"; // for 'backtracking' bugs
throw runtime_error(msg.c_str());
}
/// this pointer is given to the program in order to give the
/// function more information. E.g. for generalised error_handlers it can
/// dump a simple backtrace
return FunctionManager::Instance()->call_function(this, args);
}
//////////////////////////////////////////
// ProcedureCell
ProcedureCell::ProcedureCell(Cell* const my_param, Cell* const my_body) : param(my_param), body(my_body) {
if (!listp(my_param)) {
/// Indicates variable number of arguments
num_param = -1;
}
else {
num_param = ConsCell::get_list_size(my_param);
}
}
ProcedureCell::~ProcedureCell() {
if(param != nil) {
delete param;
param = NULL;
}
if(body != nil) {
delete body;
body = NULL;
}
}
bool ProcedureCell::is_lambda() const {
return 1;
}
Cell* ProcedureCell::get_formals() const throw (runtime_error) {
return param;
}
Cell* ProcedureCell::get_body() const throw (runtime_error) {
return body;
}
Cell* ProcedureCell::apply(Cell* const args) const throw (std::runtime_error) {
/// In other words: if num_param -1 then there is a variabe number
/// of arguments
if (num_param != -1 && ConsCell::get_list_size(args) != num_param) {
stringstream ss;
ss << "Mismatch of number of arguments in:" << endl;
ss << "\t" << *this->get_body() << endl;
throw runtime_error(ss.str());
}
Cell* pos_args = args;
Cell* pos_param = this->get_formals();
Cell* pos_body = this->get_body();
DefinitionManager::Instance()->add_stackframe();
try {
/// define local variables in local stack frame
if (num_param == -1) {
string key = pos_param->get_symbol();
Cell* c = pos_args;
DefinitionManager::Instance()->add_definition(key, c);
}
else {
while (!nullp(pos_args)) {
string key = (car(pos_param))->get_symbol();
Cell* c = eval(car(pos_args));
DefinitionManager::Instance()->add_definition(key, c);
pos_args = cdr(pos_args);
pos_param = cdr(pos_param);
}
}
/// evaluates bodies, remembers last result
Cell* res = nil;
while (!nullp(pos_body)) {
res = eval(car(pos_body));
/// in case of variable number of args, eval has to be called,
/// since we are forced to skipped the step above in defining the
/// variable
if (!nullp(res) && num_param == -1) {
res = eval(res);
}
pos_body = cdr(pos_body);
}
DefinitionManager::Instance()->pop_stackframe();
/// Note: only returns last remembered result according to specs
return res;
}
catch (runtime_error) {
/// makes sure stackframe gets pop in case of an error
DefinitionManager::Instance()->pop_stackframe();
throw;
}
}
void ProcedureCell::print(ostream& os) const {
os << "#<function>";
}