/
expr.h
276 lines (145 loc) · 3.93 KB
/
expr.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
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
#ifndef __expr_h_
#define __expr_h_
#include <memory>
#include <string>
#include <map>
#include <iostream>
#include <poly.h>
#define EXPR_NODE 0
#define EXPR_SYMBOL 1
#define EXPR_VAL 2
//#define DC(x, y) (std::cout << "Destructor called:" << x << y << std::endl)
#define DC(x, y) if (_alloc_cnt == 0) { std::cout << "Destructor called: alloc_cnt = " << _alloc_cnt << std::endl; }
class Expr;
class ExprVal;
class ExprSymbol;
class ExprNode;
class Ex {
public:
Ex();
Ex(const Ex & a);
Ex & operator=(const Ex & a);
~Ex();
Ex(Expr* a);
Ex(ExprVal* a);
Ex(ExprSymbol * a);
Ex(ExprNode * a);
Ex(double val);
Ex(std::string str);
Ex(Ex a, Ex b);
Ex set_ident(std::string idstr);
Ex set_double(double val);
Ex pre_init(Ex a);
Ex add(Ex a);
Ex mul(Ex a);
Ex pow(int d);
Ex neg();
std::string to_str() const;
std::string get_name () const;
double get_val() const;
Ex rplaca(Ex e);
Ex rplacd(Ex e);
Ex clone();
Expr* operator->() {return _impl;}
Expr* _impl;
};
class Expr {
public:
Expr(int typea): type(typea), ref_cnt(0) { ++_alloc_cnt;
//std::cout << "Expr(int): type = " << type << " ref_cnt = " << ref_cnt << std::endl;
};
virtual ~Expr() {--_alloc_cnt; DC("alloc cnt ", _alloc_cnt);};
virtual std::string to_str() const = 0;
virtual Expr* clone() = 0;
virtual Ex get_car() = 0;
virtual Ex get_cdr() = 0;
virtual std::string get_name() const = 0;
virtual double get_val() const = 0;
virtual Ex rplaca(Ex e) = 0;
virtual Ex rplacd(Ex e) = 0;
int ref_cnt;
int type;
static int _alloc_cnt;
};
class ExprVal: public Expr {
public:
ExprVal(double vala): Expr(EXPR_VAL), val(vala) {
//std::cout << "ExprVal(double): val = " << val << std::endl;
};
virtual ~ExprVal() { DC("val = ", val); };
virtual std::string to_str() const;
virtual ExprVal* clone();
virtual Ex get_car();
virtual Ex get_cdr();
virtual std::string get_name() const;
virtual double get_val() const;
virtual Ex rplaca(Ex e);
virtual Ex rplacd(Ex e);
double val;
};
class ExprSymbol: public Expr {
public:
ExprSymbol(const std::string stra): Expr(EXPR_SYMBOL), str(stra) {
//std::cout << "ExprSymbol(string): str = " << str << std::endl;
};
virtual ~ExprSymbol() { DC("str = ", str);};
virtual std::string to_str() const;
virtual ExprSymbol* clone();
virtual Ex get_car();
virtual Ex get_cdr();
virtual std::string get_name() const;
virtual double get_val() const;
virtual Ex rplaca(Ex e);
virtual Ex rplacd(Ex e);
std::string str;
};
class ExprNode: public Expr {
public:
ExprNode(Ex a, Ex b): Expr(EXPR_NODE), car(a), cdr(b) {
//std::cout << "ExprNode(Ex, Ex): ( " << car.to_str() << ". " << cdr.to_str() << " )" << std::endl;
};
virtual ~ExprNode() { DC("Node", ""); };
virtual std::string to_str() const;
virtual ExprNode* clone();
virtual Ex get_car();
virtual Ex get_cdr();
virtual std::string get_name() const;
virtual double get_val() const;
virtual Ex rplaca(Ex e);
virtual Ex rplacd(Ex e);
Ex car, cdr;
};
class FactExpr {
public:
static Ex create(const std::string & str);
static Ex create(double vala);
static Ex create(Ex a, Ex b);
static Ex create(Ex a);
};
class ExEnviron {
public:
Ex lookup(std::string str) const;
Ex enter(std::string str, Ex val);
std::map<std::string, Ex> _env_map;
};
Ex evalsimp(Ex e, const ExEnviron & env, bool deep);
std::ostream & operator<<(std::ostream & os, const Ex & a);
Ex car(Ex e);
Ex cdr(Ex e);
bool is_atom(Ex e);
bool is_val(Ex e);
bool is_symbol(Ex e);
bool is_nil(Ex e);
std::string to_str_infix(Ex e);
Ex operator-(Ex a);
Ex operator+(Ex a, Ex b);
Ex operator-(Ex a, Ex b);
Ex operator*(Ex a, Ex b);
Ex operator/(Ex a, Ex b);
Ex pow(Ex a, Ex b);
int read_expr(std::string expr_str, Ex & perg);
int read_module(std::string expr_str, Ex & perg);
Ex eval_statement_list(Ex & statlis, Poly<double> & retpoly);
void test_expr_1(std::string expr_str);
extern Ex nilEx;
#endif /* __expr_h_ */