-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatoms.h
170 lines (136 loc) · 3.39 KB
/
atoms.h
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
#ifndef ATOMS_H
#define ATOMS_H
#include <iosfwd>
#include <list>
#include <string>
class Atom;
class Env
{
public:
typedef std::list<std::pair<std::string, const Atom*> > Dictionary;
void push(const std::string& key, const Atom* value);
void pop();
const Atom* find(const std::string& key) const;
private:
class Matcher;
Dictionary dictionary_;
};
class Node;
class Atom
{
public:
Atom();
virtual ~Atom();
virtual void write(std::ostream& out) const = 0;
virtual const Atom* eval(Env& env) const = 0;
virtual const Atom* evalList(const Node* node, Env& env) const;
static void releaseAll();
protected:
static void assert_(bool cond, const std::string& message);
template<class T> const T* as() const;
static std::list<const Atom*> pool_;
};
std::ostream& operator << (std::ostream& out, const Atom& atom);
template<class T>
const T* Atom::as() const
{
const T* result = dynamic_cast<const T*>(this);
assert_(result, "cannot cast");
return result;
}
class Integer : public Atom
{
public:
Integer(int i);
void write(std::ostream& out) const;
const Atom* eval(Env& env) const;
int value() const;
private:
const int i_;
};
class Real : public Atom
{
public:
Real(double r);
void write(std::ostream& out) const;
const Real* eval(Env& env) const;
double value() const;
private:
const double r_;
};
class Bool : public Atom
{
public:
Bool(bool b);
void write(std::ostream& out) const;
const Bool* eval(Env& env) const;
bool value() const;
private:
const bool b_;
};
class Symbol : public Atom
{
public:
Symbol(const std::string& s);
void write(std::ostream& out) const;
const Atom* eval(Env& env) const;
const Atom* evalList(const Node* node, Env& env) const;
const std::string& value() const;
private:
const std::string s_;
};
class Function : public Atom
{
public:
Function(const Node* args);
void write(std::ostream& out) const;
const Atom* evalList(const Node* node, Env& env) const;
const Node* args() const;
private:
const Node* args_;
};
class Lambda : public Function
{
public:
Lambda(const Node* args, const Node* exp);
void write(std::ostream& out) const;
const Atom* eval(Env& env) const;
private:
const Node* exp_;
};
class Node : public Atom
{
public:
class Iterator
{
friend class Node;
public:
Iterator(const Node* node);
bool good() const;
const Node* operator * () const;
const Node* operator -> () const;
Iterator& operator ++ ();
Iterator operator ++ (int);
private:
const Node* node_;
};
static const Node* getNull();
Node(const Atom* atom, const Node* next);
void write(std::ostream& out) const;
const Atom* car() const;
const Node* cdr() const;
const Atom* eval(Env& env) const;
const Atom* evalSymbol(const Symbol* symbol, Env& env) const;
const Atom* evalFunction(const Function* function, Env& env) const;
private:
Node();
const Atom* evalQuote(Iterator i, Env& env) const;
const Atom* evalIf(Iterator i, Env& env) const;
const Atom* evalSet(Iterator i, Env& env) const;
const Atom* evalDefine(Iterator i, Env& env) const;
const Atom* evalLambda(Iterator i, Env& env) const;
const Atom* evalBegin(Iterator i, Env& env) const;
const Atom* car_;
const Node* cdr_;
};
#endif//ATOMS_H