-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnvironment.cpp
More file actions
272 lines (229 loc) · 7.47 KB
/
Environment.cpp
File metadata and controls
272 lines (229 loc) · 7.47 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
#include "Environment.h"
#include "Memory.h"
#include <limits.h>
using namespace std;
Environment globalEnvironment;
extern Memory memory;
void popFromStack(int numArgs) {
for (int i = 0; i < numArgs; i++) {
memory.stack.pop();
}
}
//
// (+ <number>...)
// special: with 0 args returns 0 (neutral element of addition)
//
Object* builtinPlus(int numArgs, Environment& environment) {
long int sum = 0;
for (int i = 0; i < numArgs; i++) {
Object* nextArg = memory.stack.pop();
long int number = nextArg->getInt(environment);
if (((sum > 0) && (number > INT_MAX - sum)) || ((sum < 0) && (number < INT_MIN - sum))) {
popFromStack(numArgs - i);
throw runtime_error("plus under/overflow");
}
sum += number;
}
return ObjectInt::allocate(sum);
}
//
// (- <number>...)
// (- 5) returns -5
// (- 3 2) returns 1
// (- 3 2 2) returns -1
//
Object* builtinMinus(int numArgs, Environment& environment) {
if (numArgs == 0) {
throw runtime_error("no args for minus operator");
} else if (numArgs == 1) {
Object* nextArg = memory.stack.pop();
int intValue = nextArg->getInt(environment);
return ObjectInt::allocate(-intValue);
} else {
long int result = 0;
for (int i = 0; i < numArgs; i++) {
Object* nextArg = memory.stack.pop();
if (i == numArgs - 1) // first number, needs to be added
result += nextArg->getInt(environment);
else { // the rest subtracted
int number = nextArg->getInt(environment);
if (((result < 0) && (number > INT_MAX + result)) || ((result > 0) && (number < INT_MIN + result))) {
popFromStack(numArgs - i);
throw runtime_error("minus under/overflow");
}
result -= number;
}
}
return ObjectInt::allocate(result);
}
}
//
// (* <number>...)
// special: with 0 args returns 1 (neutral element of multiplication)
//
Object* builtinTimes(int numArgs, Environment& environment) {
long int result = 1;
for (int i = 0; i < numArgs; i++) {
Object* nextArg = memory.stack.pop();
int number = nextArg->getInt(environment);
if ((result > INT_MAX / number) || ((result < INT_MIN / number))) {
popFromStack(numArgs - i);
throw runtime_error("multiplication under/overflow");
}
else if(((result == -1) && (number == INT_MIN)) || ((number == -1) && (result == INT_MIN))) {
popFromStack(numArgs - i);
throw runtime_error("multiplication possible under/overflow");
}
result *= number;
}
return ObjectInt::allocate(result);
}
//
// (= <number1> <number2>) -> boolean
//
Object* builtinEquals(int numArgs, Environment& environment) {
if (numArgs != 2) {
popFromStack(numArgs);
throw runtime_error("= expects exactly 2 arguments");
}
Object* first = memory.stack.pop();
Object* second = memory.stack.pop();
if (first->getInt(environment) == second->getInt(environment))
return ObjectTrue::allocate();
else
return ObjectFalse::allocate();
}
//
// (< <number1> <number2>) -> boolean
//
Object* builtinLessThan(int numArgs, Environment& environment) {
if (numArgs != 2) {
popFromStack(numArgs);
throw runtime_error("< expects exactly 2 arguments");
}
// arguments are reversed on stack
Object* second = memory.stack.pop();
Object* first = memory.stack.pop();
if (first->getInt(environment) < second->getInt(environment))
return ObjectTrue::allocate();
else
return ObjectFalse::allocate();
}
//
// (> <number1> <number2>) -> boolean
//
Object* builtinGreaterThan(int numArgs, Environment& environment) {
if (numArgs != 2) {
cout << "> expects exactly 2 arguments";
popFromStack(numArgs);
return ObjectVoid::allocate();
}
// arguments are reversed on stack
Object* second = memory.stack.pop();
Object* first = memory.stack.pop();
if (first->getInt(environment) > second->getInt(environment))
return ObjectTrue::allocate();
else
return ObjectFalse::allocate();
}
//
// (if <cond> <trueExpr> <falseExpr>
//
Object* builtinIf(int numArgs, Environment& environment) {
if (numArgs != 3) {
popFromStack(numArgs);
throw runtime_error("if expects exactly 3 arguments");
}
// arguments are reversed on stack
Object* ifFalseExpr = memory.stack.pop();
Object* ifTrueExpr = memory.stack.pop();
Object* condition = memory.stack.pop();
Object* evaluatedCondition = condition->eval(environment);
if (evaluatedCondition->getBool())
return ifTrueExpr->eval(environment);
else
return ifFalseExpr->eval(environment);
}
//
// (lambda <argList> <bodyexpr>...)
//
Object* builtinLambda(int numArgs, Environment& environment) {
if (numArgs != 2) {
popFromStack(numArgs);
throw runtime_error("lambda expects 2 arguments");
}
return ObjectUserDefinedFunction::fromStack();
}
//
// (define var expr)
//
Object* builtinDefine(int numArgs, Environment& environment) {
if (numArgs != 2) {
popFromStack(numArgs);
throw runtime_error("define expects exactly 2 arguments");
}
// arguments are reversed on stack
Object* expr = memory.stack.pop();
Object* symbol = memory.stack.pop();
Object* exprValue = expr->eval(environment);
environment.addObject(symbol->getString(), exprValue);
return ObjectVoid::allocate();
}
//
// (car <aCons>) -> CAR(aCons)
//
Object* builtinCar(int numArgs, Environment& environment) {
if( numArgs != 1 ) {
popFromStack(numArgs);
throw runtime_error("car expects exactly 1 argument ");
}
Object* theCell = memory.stack.pop();
return theCell->getCar();
}
//
// (cdr <aCons>) -> CDR(aCons)
//
Object* builtinCdr(int numArgs, Environment& environment) {
if( numArgs != 1 ) {
popFromStack(numArgs);
throw runtime_error("cdr expects exactly 1 argument ");
}
Object* theCell = memory.stack.pop();
return theCell->getCdr();
}
Environment::Environment() {
parent = nullptr;
addObject("+", ObjectBuiltInFunction::allocate(builtinPlus));
addObject("-", ObjectBuiltInFunction::allocate(builtinMinus));
addObject("*", ObjectBuiltInFunction::allocate(builtinTimes));
addObject("=", ObjectBuiltInFunction::allocate(builtinEquals));
addObject("<", ObjectBuiltInFunction::allocate(builtinLessThan));
addObject(">", ObjectBuiltInFunction::allocate(builtinGreaterThan));
addObject("car", ObjectBuiltInFunction::allocate(builtinCar));
addObject("cdr", ObjectBuiltInFunction::allocate(builtinCdr));
addObject("if", ObjectBuiltInSyntax::allocate(builtinIf));
addObject("define", ObjectBuiltInSyntax::allocate(builtinDefine));
addObject("lambda", ObjectBuiltInSyntax::allocate(builtinLambda));
}
Environment::Environment(Environment& environment) {
parent = &environment;
}
void Environment::addObject(std::string key, Object* object) {
objectMap[key] = object;
}
Object* Environment::getObject(std::string key) {
Object* object = objectMap[key];
if (object != nullptr)
return object;
else if (parent != nullptr)
return parent->getObject(key);
else
return nullptr;
}
void Environment::mark() {
for (auto & pair : objectMap)
if (pair.second)
pair.second->mark();
if (parent)
parent->mark();
}