-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatoms.cpp
More file actions
367 lines (303 loc) · 6.7 KB
/
atoms.cpp
File metadata and controls
367 lines (303 loc) · 6.7 KB
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
#include "atoms.h"
#include <algorithm>
#include <stdexcept>
#include <sstream>
std::list<const Atom*> Atom::pool_;
class Env::Matcher
{
public:
Matcher(const std::string& key) : key_(key) {}
bool operator () (const std::pair<std::string, const Atom*>& i)
{
return i.first == key_;
}
private:
const std::string& key_;
};
void Env::push(const std::string& key, const Atom* value)
{
dictionary_.push_front(std::make_pair(key, value));
}
void Env::pop()
{
dictionary_.pop_front();
}
const Atom* Env::find(const std::string& key) const
{
Dictionary::const_iterator i = std::find_if(dictionary_.begin(), dictionary_.end(), Matcher(key));
if(i == dictionary_.end())
{
throw std::runtime_error(key + " is not defined");
}
return i->second;
}
void Atom::releaseAll()
{
struct _ { static void delete_(const Atom* atom) { delete atom; } };
std::for_each(pool_.begin(), pool_.end(), _::delete_);
}
void Atom::assert_(bool cond, const std::string& message)
{
if( ! cond)
{
throw std::runtime_error(message);
}
}
Atom::Atom()
{
}
Atom::~Atom()
{
}
const Atom* Atom::evalList(const Node* node, Env& env) const
{
std::stringstream ss;
ss << *node;
throw std::runtime_error("cannot eval " + ss.str());
}
std::ostream& operator << (std::ostream& out, const Atom& atom)
{
atom.write(out);
return out;
}
Integer::Integer(int i) : i_(i)
{
pool_.push_back(this);
}
void Integer::write(std::ostream& out) const
{
out << i_;
}
const Atom* Integer::eval(Env& env) const
{
return this;
}
int Integer::value() const
{
return i_;
}
Real::Real(double r) : r_(r)
{
pool_.push_back(this);
}
void Real::write(std::ostream& out) const
{
out << r_;
}
const Real* Real::eval(Env& env) const
{
return this;
}
double Real::value() const
{
return r_;
}
Bool::Bool(bool b) : b_(b)
{
pool_.push_back(this);
}
void Bool::write(std::ostream& out) const
{
out << std::boolalpha << b_;
}
const Bool* Bool::eval(Env& env) const
{
return this;
}
bool Bool::value() const
{
return b_;
}
Symbol::Symbol(const std::string& s) : s_(s)
{
pool_.push_back(this);
}
void Symbol::write(std::ostream& out) const
{
out << s_;
}
const Atom* Symbol::eval(Env& env) const
{
return env.find(s_);
}
const Atom* Symbol::evalList(const Node* node, Env& env) const
{
return node->evalSymbol(this, env);
}
const std::string& Symbol::value() const
{
return s_;
}
Function::Function(const Node* args) : args_(args)
{
}
void Function::write(std::ostream& out) const
{
out << "primitive function";
}
const Atom* Function::evalList(const Node* node, Env& env) const
{
return node->evalFunction(this, env);
}
const Node* Function::args() const
{
return args_;
}
Lambda::Lambda(const Node* args, const Node* exp) : Function(args), exp_(exp)
{
pool_.push_back(this);
}
void Lambda::write(std::ostream& out) const
{
out << "lambda " << *args() << " " << *exp_;
}
const Atom* Lambda::eval(Env& env) const
{
return exp_->eval(env);
}
Node::Iterator::Iterator(const Node* node) : node_(node)
{
}
bool Node::Iterator::good() const
{
return (node_ != 0) && (node_ != Node::getNull());
}
const Node* Node::Iterator::operator * () const
{
Atom::assert_(node_, "Node is null");
return node_;
}
const Node* Node::Iterator::operator -> () const
{
Atom::assert_(node_, "Node is null");
return node_;
}
Node::Iterator& Node::Iterator::operator ++ ()
{
node_ = node_->cdr_;
return *this;
}
Node::Iterator Node::Iterator::operator ++ (int)
{
Iterator result(*this);
node_ = node_->cdr_;
return result;
}
const Node* Node::getNull()
{
static const Node* null = new Node;
return null;
}
Node::Node() : car_(0), cdr_(0)
{
pool_.push_back(this);
}
Node::Node(const Atom* car, const Node* cdr) : car_(car), cdr_(cdr)
{
assert_(car, "atom is null");
pool_.push_back(this);
}
void Node::write(std::ostream& out) const
{
out << "( ";
for(const Node* i = this; i->car_ != 0; i = i->cdr_)
{
out << *i->car_ << " ";
}
out << ")";
}
const Atom* Node::car() const
{
assert_(car_, "car is null");
return car_;
}
const Node* Node::cdr() const
{
return cdr_;
}
const Atom* Node::eval(Env& env) const
{
if(this == getNull())
{
return this;
}
Node::Iterator i(this);
const Node* head = *i++;
return head->car()->evalList(*i, env);
}
const Atom* Node::evalSymbol(const Symbol* symbol, Env& env) const
{
Node::Iterator i(this);
if (symbol->value() == "quote") return evalQuote(i, env);
else if(symbol->value() == "if") return evalIf(i, env);
else if(symbol->value() == "set!") return evalSet(i, env);
else if(symbol->value() == "define") return evalDefine(i, env);
else if(symbol->value() == "lambda") return evalLambda(i, env);
else if(symbol->value() == "begin") return evalBegin(i, env);
else return symbol->eval(env)->evalList(this, env);
}
const Atom* Node::evalQuote(Node::Iterator i, Env&) const
{
return i->car();
}
const Atom* Node::evalIf(Node::Iterator i, Env& env) const
{
const Atom* test = (i++)->car();
const Atom* conseq = (i++)->car();
const Atom* alt = (i++)->car();
const Bool* cond = test->eval(env)->as<Bool>();
return (cond->value() ? conseq : alt)->eval(env);
}
const Atom* Node::evalSet(Node::Iterator i, Env& env) const
{
const Symbol* s = (i++)->car()->as<Symbol>();
const Atom* value = (i++)->car()->eval(env);
env.find(s->value());
env.push(s->value(), value);
return value;
}
const Atom* Node::evalDefine(Node::Iterator i, Env& env) const
{
const Symbol* s = (i++)->car()->as<Symbol>();
const Atom* value = (i++)->car()->eval(env);
env.push(s->value(), value);
return value;
}
const Atom* Node::evalLambda(Node::Iterator i, Env& env) const
{
const Node* args = (i++)->car()->as<Node>();
const Node* exp = (i++)->car()->as<Node>();
return new Lambda(args, exp);
}
const Atom* Node::evalBegin(Node::Iterator i, Env& env) const
{
const Atom* result = 0;
for(; i.good(); ++i)
{
result = i->car()->eval(env);
}
return result;
}
const Atom* Node::evalFunction(const Function* fun, Env& env) const
{
int i = 0;
const Node* args = fun->args();
for(Node::Iterator a(args), v(this); a.good(); ++a, ++v)
{
++i;
const std::string& s = a->car()->as<Symbol>()->value();
if(s == " ")
{
env.push(s, *v);
break;
}
env.push(s, v->car()->eval(env));
}
const Atom* result = fun->eval(env);
while(i > 0)
{
env.pop();
--i;
}
return result;
}