-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCell.cpp
231 lines (203 loc) · 3.84 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
#include "Cell.hpp"
// Reminder: cons.hpp expects nil to be defined somewhere. For this
// implementation, this is the logical place to define it.
Cell* const nil = 0;
//all Cell have two common methods:
//one is to check the type of the Cell, which is is_type()
//another is to print the content of the Cell, which is print()
//
//and also, based on the type different constructors
//with different types of value(s) to be stored will be passed in.
//accessor(s) to member(s) in the Cell will also be defined
//a destructor may needed to release the dynamic resourse(s) used by the Cell
///////////////////////////////////
//implementation of IntCell
std::string IntCell::get_type() const
{
return "int";
}
Cell* IntCell::copy() const
{
return new IntCell(this->int_m);
}
void IntCell::print(std::ostream& os) const
{
os<<get_int();
}
IntCell::IntCell(const int i)
{
int_m = i;
}
int IntCell::get_int() const
{
return int_m;
}
bool IntCell::is_zero() const
{
return int_m==0? true : false;
}
///////////////////////////////////
//implementation of DoubleCell
std::string DoubleCell::get_type() const
{
return "double";
}
Cell* DoubleCell::copy() const
{
return new DoubleCell(this->double_m);
}
void DoubleCell::print(std::ostream& os) const
{
//os.precision(10);
os.setf(ios::showpoint);
os<<get_double();
}
DoubleCell::DoubleCell(const double d)
{
double_m = d;
}
double DoubleCell::get_double() const
{
return double_m;
}
bool DoubleCell::is_zero() const
{
return double_m==0? true : false;
}
///////////////////////////////////
//implementation of SymbolCell
std::string SymbolCell::get_type() const
{
return "symbol";
}
Cell* SymbolCell::copy() const
{
return new SymbolCell(this->symbol_m);
}
void SymbolCell::print(std::ostream& os) const
{
os<<get_symbol();
}
SymbolCell::SymbolCell(string s)
{
symbol_m =s;
}
string SymbolCell::get_symbol() const
{
return symbol_m;
}
bool SymbolCell::is_zero() const
{
return symbol_m=="0" ? true : false;
}
///////////////////////////////////
//implementation of ConsCell
std::string ConsCell::get_type() const
{
return "cons";
}
Cell* ConsCell::copy() const
{
Cell* car_temp;
if(this->car_m==nil) {
car_temp=nil;
} else {
car_temp=this->car_m->copy();
}
Cell* cdr_temp;
if(this->cdr_m==nil) {
cdr_temp=nil;
} else {
cdr_temp=this->cdr_m->copy();
}
return new ConsCell(car_temp, cdr_temp);
}
void ConsCell::print(std::ostream& os) const
{
os<<"(";
if(get_car()==nil) {
os<<"() ";
} else {
get_car()->print();
}
Cell* temp = get_cdr();
while(temp != nil)
{
os<<" ";
if(temp->get_car()==nil) {
os<<"() ";
} else {
temp->get_car()->print();
}
temp=temp->get_cdr();
}
os<<")";
}
ConsCell::ConsCell(Cell* const my_car, Cell* const my_cdr)
{
car_m = my_car;
cdr_m = my_cdr;
}
Cell* ConsCell::get_car() const
{
return car_m;
}
Cell* ConsCell::get_cdr() const
{
return cdr_m;
}
ConsCell::~ConsCell()
{
delete car_m;
delete cdr_m;
}
///////////////////////////////////
//implementation of ProcedureCell
std::string ProcedureCell::get_type() const
{
return "procedure";
}
Cell* ProcedureCell::copy() const
{
Cell* formals_temp;
if(this->formals_m==nil) {
formals_temp=nil;
} else {
formals_temp=this->formals_m->copy();
}
Cell* body_temp;
if(this->body_m==nil) {
body_temp=nil;
} else {
body_temp=this->body_m->copy();
}
return new ProcedureCell(formals_temp, body_temp);
}
/*
Cell* ProcedureCell::apply(Cell* const args) const
{
return new ConsCell(ConsCell(this->formals_m, args));
}
*/
void ProcedureCell::print(std::ostream& os) const
{
os<<"#<function>";
}
ProcedureCell::ProcedureCell(Cell* const my_formals, Cell* const my_body)
{
formals_m = my_formals;
body_m = my_body;
}
Cell* ProcedureCell::get_formals() const
{
return formals_m;
}
Cell* ProcedureCell::get_body() const
{
return body_m;
}
ProcedureCell::~ProcedureCell()
{
delete formals_m;
delete body_m;
}