-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspecialforms.cpp
348 lines (303 loc) · 9.34 KB
/
specialforms.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
/**
* File: specialforms.cpp
* ----------------------
*/
#include "specialforms.h"
#include "expression.h"
#include "lst.h"
#include "symbol.h"
#include "boolean.h"
#include "set.h"
#include "lambda.h"
#include "rational.h"
#include "str.h"
#include "scanner.h"
#include <iostream>
using namespace std;
Expression *DefineSpecialForm::call(Vector<Expression *>& arguments,
EvalState& state) {
if (arguments.size() != 2) {
Error("define expects exactly two arguments");
}
SymbolExpression *symbol = dynamic_cast<SymbolExpression *>(arguments[0]);
if (symbol == NULL) {
Error("The first argument to define by be a symbol");
}
Expression *value = arguments[1];
state.setExpression(symbol->toString(),
value->eval(state));
return symbol;
}
Expression *QuitSpecialForm::call(Vector<Expression *>& arguments,
EvalState& state) {
cout << endl;
cout << "[Press <return> to close]";
string dummy;
getline(cin, dummy);
exit(0);
return this; // hush the compiler
}
Expression *CarSpecialForm::call(Vector<Expression *>& arguments,
EvalState& state) {
if (arguments.size() != 1) {
Error("car expects exactly one argument");
}
evaluateAllArguments(arguments, state);
ListExpression *listArgument =
dynamic_cast<ListExpression *>(arguments[0]);
if (listArgument == NULL) {
Error("car expects its one argument to be a list");
}
return listArgument->car();
}
Expression *CdrSpecialForm::call(Vector<Expression *>& arguments,
EvalState& state) {
if (arguments.size() != 1) {
Error("cdr expects exactly one argument.");
}
evaluateAllArguments(arguments, state);
ListExpression *listArgument =
dynamic_cast<ListExpression *>(arguments[0]);
if (listArgument == NULL) {
Error("cdr expects its one argument to be a list.");
}
return listArgument->cdr();
}
Expression *ConsSpecialForm::call(Vector<Expression *>& arguments,
EvalState& state) {
if (arguments.size() != 2) {
Error("cons expects precisely two arguments.");
}
evaluateAllArguments(arguments, state);
Expression *newCar = arguments[0];
ListExpression *newCdr = dynamic_cast<ListExpression *>(arguments[1]);
if (newCdr == NULL) {
Error("Second argument passed to cons must be a list.");
}
return new ListExpression(newCar, newCdr);
}
Expression *ListSpecialForm::call(Vector<Expression *>& arguments,
EvalState& state) {
evaluateAllArguments(arguments, state);
return new ListExpression(arguments);
}
Expression *IfSpecialForm::call(Vector<Expression *>& arguments,
EvalState& state) {
if (arguments.size() != 3) {
Error("if expressions expect precisely three arguments.");
}
if (arguments[0]->eval(state)->isTrue())
return arguments[1]->eval(state);
else
return arguments[2]->eval(state);
}
Expression *NullSpecialForm::call(Vector<Expression *>& arguments,
EvalState& state)
{
if (arguments.size() != 1) {
Error("null expressions take just one argument.");
}
evaluateAllArguments(arguments, state);
ListExpression *list =
dynamic_cast<ListExpression *>(arguments[0]);
if (list == NULL) {
Error("null expects a list and only a list");
}
return new BooleanExpression(list->isEmpty());
}
Expression *AddSpecialForm::call(Vector<Expression *>& arguments,
EvalState& state)
{
evaluateAllArguments(arguments, state);
RationalExpression *sum = new RationalExpression(0);
for (int i = 0; i < arguments.size(); i++) {
RationalExpression *addend =
dynamic_cast<RationalExpression *>(arguments[i]);
if (addend == NULL) {
Error("add expects all arguments to be rational numbers.");
}
sum = sum->add(addend);
}
return sum;
}
Expression *SubtractSpecialForm::call(Vector<Expression *>& arguments,
EvalState& state)
{
if (arguments.size() != 2) {
Error("subtract takes precisely two arguments.");
}
evaluateAllArguments(arguments, state);
RationalExpression *minuend =
dynamic_cast<RationalExpression *>(arguments[0]);
RationalExpression *subtrahend =
dynamic_cast<RationalExpression *>(arguments[1]);
if (minuend == NULL || subtrahend == NULL) {
Error("subtract only takes rational numbers as arguments.");
}
return minuend->subtract(subtrahend);
}
Expression *MultiplySpecialForm::call(Vector<Expression *>& arguments,
EvalState& state)
{
evaluateAllArguments(arguments, state);
RationalExpression *product = new RationalExpression(1);
for (int i = 0; i < arguments.size(); i++) {
RationalExpression *factor =
dynamic_cast<RationalExpression *>(arguments[i]);
if (factor == NULL) {
Error("multiply expects all arguments to be rational numbers.");
}
product = product->multiply(factor);
}
return product;
}
Expression *EqualSpecialForm::call(Vector<Expression *>& arguments,
EvalState& state)
{
if (arguments.size() != 2) {
Error("equal takes precisely two rational arguments.");
}
evaluateAllArguments(arguments, state);
RationalExpression *first =
dynamic_cast<RationalExpression *>(arguments[0]);
RationalExpression *second =
dynamic_cast<RationalExpression *>(arguments[1]);
if (first == NULL || second == NULL) {
Error("equal only takes rational numbers as arguments.");
}
return new BooleanExpression(first->equals(second));
}
Expression *LessSpecialForm::call(Vector<Expression *>& arguments,
EvalState& state)
{
if (arguments.size() != 2) {
Error("less takes precisely two rational arguments.");
}
evaluateAllArguments(arguments, state);
RationalExpression *first =
dynamic_cast<RationalExpression *>(arguments[0]);
RationalExpression *second =
dynamic_cast<RationalExpression *>(arguments[1]);
if (first == NULL || second == NULL) {
Error("less only takes rational numbers as arguments.");
}
return new BooleanExpression(first->lessThan(second));
}
Expression *LoadSpecialForm::call(Vector<Expression *>& arguments,
EvalState &state) {
if (arguments.size() != 1) {
Error("load requires precisely one argument.");
}
evaluateAllArguments(arguments, state);
StringExpression *fileExp = dynamic_cast<StringExpression *>(arguments[0]);
if (fileExp == NULL) {
Error("load's argument must be a string.");
}
string filename = fileExp->getStr();
ifstream fs;
fs.open(filename.c_str());
if (fs.fail()) {
Error("file does not exist.");
}
// remove all newlines - the scanner treats them as tokens
string input = "";
while (fs.peek() != EOF) {
string line;
getline (fs, line);
if (line.size() > 0 && line[0] == ';') {
continue;
}
input += " " + line;
}
Scanner scanner;
scanner.setSpaceOption(Scanner::IgnoreSpaces);
scanner.setStringOption(Scanner::ScanQuotesAsStrings);
scanner.setInput(input);
Expression *res = NULL;
Expression *exp = NULL;
do {
exp = Expression::readExpression(scanner);
if (exp != NULL) {
res = exp->eval(state);
}
} while (exp != NULL);
return res;
}
Expression *LambdaSpecialForm::call(Vector<Expression *>& arguments,
EvalState& state) {
if (arguments.size() < 2) {
Error("Lambda expects at least 2 arguments");
}
LambdaExpression * newLambda = new LambdaExpression(arguments[0], arguments[1]);
if (arguments.size() == 2) {
return newLambda;
}
else {
arguments.removeAt(0);
arguments.removeAt(0);
// cout << arguments.size() << endl;
return newLambda->call(arguments, state);
}
}
Expression *AndSpecialForm::call(Vector<Expression *>& arguments,
EvalState& state) {
//Error("AndSpecialForm::call not supported yet");
if (arguments.size() == 0) {
return new BooleanExpression(true);
}
Expression * evaled;
for (int i = 0; i <= arguments.size() - 1; ++i) {
evaled = arguments[i]->eval(state);
if (!evaled->isTrue()) {
return new BooleanExpression(false);
}
}
return evaled;
}
Expression *OrSpecialForm::call(Vector<Expression *>& arguments,
EvalState& state) {
//Error("OrSpecialForm::call not supported yet");
if (arguments.size() == 0) {
return new BooleanExpression(false);
}
Expression * eval;
for (int i = 0; i <= arguments.size() - 1; ++i) {
eval = arguments[i]->eval(state);
if (eval->isTrue()) {
return eval;
}
}
return new BooleanExpression(false);
}
Expression *NotSpecialForm::call(Vector<Expression *>& arguments,
EvalState& state) {
//Error("NotSpecialForm::call not supported yet");
if (arguments.size() != 1) {
Error("not takes exactly one argument!");
}
Expression * eval = arguments[0]->eval(state);
if (eval->isTrue()) {
return new BooleanExpression(false);
}
return new BooleanExpression(true);
}
Expression *ApplySpecialForm::call(Vector<Expression *>& arguments,
EvalState& state) {
if (arguments.size() != 2) {
Error("apply should take exactly 2 arguments.");
}
Callable * called = dynamic_cast<Callable *>(arguments[0]->eval(state));
if (called == NULL) {
Error("The first argument of apply should be a callable");
}
ListExpression * list = dynamic_cast<ListExpression *>(arguments[1]->eval(state));
if (list == NULL) {
Error("The 2nd argument of apply procedure should be evaluated to a list");
}
Vector<Expression *> args;
while (!list->isEmpty()) {
args.add(list->car());
list = list->cdr();
}
return called->call(args, state);
}