-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLispEnv.cpp
203 lines (180 loc) · 5.65 KB
/
LispEnv.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
// TO DO : Need c-set-style to ensure consistent formatting
#include "LispObj.h"
#include "LispEnv.h"
#include "LispNil.h"
#include "LispQuote.h"
#include "LispCons.h"
#include "LispSymbol.h"
#include "LispPrimitive.h"
#include "LispPrinter.h"
#include "LispEval.h"
#include "LispLambda.h"
using namespace std;
namespace Lisp {
// there is one global ennvironment at the root of everything
LispEnvRef LispEnv::globalEnv = LispEnvRef(new LispEnv);
// constructor - new environment is constructed referring to
// it's parent, normally, but this is for the global environment
LispEnv::LispEnv() : parent_(LispEnvRef()) {
fEnv_["QUOTE"] = make_literal(PrimType(LispQuote::quote_fn));
fEnv_["SET!"] = make_literal(PrimType(LispEnv::set_fn));
fEnv_["DEFINE"] = make_literal(PrimType(LispEnv::define_fn));
fEnv_["FN"] = make_literal(PrimType(LispEnv::defun_fn));
fEnv_["CAR"] = make_literal(PrimType(LispEnv::car_fn));
fEnv_["CDR"] = make_literal(PrimType(LispEnv::cdr_fn));
fEnv_["PRINT"] = make_literal(PrimType(LispEnv::print_fn));
fEnv_["LAMBDA"] = make_literal(PrimType(LispEnv::lambda_fn));
fEnv_["IF"] = make_literal(PrimType(LispEnv::if_fn));
fEnv_["PROGN"] = make_literal(PrimType(LispEnv::prog_fn));
}
// constructor - new environment created with parent
LispEnv::LispEnv(LispEnvRef parentEnv) : parent_(parentEnv) {
}
// resolve a reference to a symbol in an environment
// corresponds to lookup-variable-value in sicp
LispObjRef LispEnv::ref(std::string var) {
EnvironmentT::iterator it(env_.find(var));
if(it != env_.end()) {
return it->second;
};
if(parent_ != NULL) {
return parent_->ref(var);
}
return nil;
}
// bind a symnbol to a variable in this environment
// corresponts to set-variable-value! in sicp
LispObjRef LispEnv::set(std::string var, LispObjRef ref) {
EnvironmentT::iterator it(env_.find(var));
if(it != env_.end()) {
it->second = ref;
return ref;
} else {
env_[var] = ref;
}
return nil;
}
// bind a symnbol to a variable in this environment
LispObjRef LispEnv::define(std::string var, LispObjRef ref) {
EnvironmentT::iterator it(env_.find(var));
if(it != env_.end()) {
it->second = ref;
} else {
env_[var] = ref;
}
return ref;
}
// resolve a reference to a function in an environment
LispObjRef LispEnv::fref(std::string var) {
EnvironmentT::iterator it(fEnv_.find(var));
if(it != fEnv_.end()) {
return it->second;
};
if(parent_ != NULL) {
return parent_->fref(var);
}
return nil;
}
// set the value associated with a symbol in an environment
LispObjRef LispEnv::fset(std::string var, LispObjRef ref) {
fEnv_[var] = ref;
return ref;
}
// Function used for the (set ) special form - bind car to cons
LispObjRef LispEnv::set_fn(LispObjRef cons, LispEnvRef env) {
// car of cons should be a symbol
if(is_symbol(car(cons)))
return env->set(get_ctype<SymbolType>(car(cons)).name, eval(cadr(cons), env));
return nil;
}
LispObjRef LispEnv::define_fn(LispObjRef cons, LispEnvRef env) {
if(is_symbol(car(cons)))
return env->define(get_ctype<SymbolType>(car(cons)).name, eval(cadr(cons), env));
return nil;
}
LispObjRef LispEnv::defun_fn(LispObjRef cons, LispEnvRef env) {
if(is_symbol(car(cons)))
env->fset(get_ctype<SymbolType>(car(cons)).name, eval(cadr(cons), env));
return car(cons);
}
LispObjRef LispEnv::car_fn(LispObjRef cons, LispEnvRef env) {
return car(eval(cadr(cons), env));
}
LispObjRef LispEnv::cdr_fn(LispObjRef cons, LispEnvRef env) {
return cdr(eval(cadr(cons), env));
}
LispObjRef LispEnv::print_fn(LispObjRef cons, LispEnvRef env) {
Printer::printer->print(eval(cadr(cons), env));
return nil;
}
LispObjRef LispEnv::lambda_fn(LispObjRef cons, LispEnvRef env) {
(void) env;
// really just returns an unevaluated cons
LispObjRef args = car(cons);
LispObjRef body = car(cdr(cons));
return make_lambda(args, body);
}
LispObjRef LispEnv::apply_fn(LispObjRef cons, LispEnvRef env) {
LispObjRef result = nil;
// should be a cons cell [apply|-]-[fn|-]-[arg0|-]-[arg1|-]-...
LispObjRef obj = eval(cadr(cons), env);
// [fn|-]-[arg0|-]-[arg1|-]-...||
// [.|-]-[arg0|-]-[arg1]-[
// [lambda|-]-
if (is_cons(obj))
{
// must be function invocation -- function symbol
LispObjRef fnsym(car(obj));
if (is_symbol(fnsym) || is_primitive(fnsym)) {
// it's a function
LispObjRef fn = is_symbol(fnsym) ? env->fref(get_ctype<SymbolType>(fnsym).name) : fnsym;
if (is_primitive(fn)) {
CPrim cfn = (CPrim)(boost::get<PrimType>(*fn));
// call it on the cdr
result = cfn(cdr(obj), env);
}
}
if (is_cons(fnsym)) {
LispObjRef lambda(eval(car(fnsym), env));
if (is_cons(lambda)) {
// this should be a lambda form - args are car, cdr is body
LispObjRef params = car(lambda);
LispObjRef args = cddr(obj);
LispEnvRef lambda_env(new LispEnv(env));
LispObjRef param;
LispObjRef arg;
do {
param = car(params);
arg = car(args);
lambda_env->set(get_ctype<SymbolType>(param).name, eval(arg,env));
param = cadr(params);
arg = is_nil(arg) ? nil : cadr(args);
} while (!is_nil(param));
result = eval(cdr(lambda), lambda_env);
}
}
}
return result;
}
LispObjRef LispEnv::if_fn(LispObjRef cons, LispEnvRef env) {
LispObjRef condition = car(cons);
LispObjRef result = eval(condition, env);
LispObjRef trueclause = cadr(cons);
LispObjRef falseclause = car(cdr(cdr(cons)));
if(!is_nil(result)) {
return eval(trueclause, env);
} else {
return eval(falseclause, env);
}
}
LispObjRef LispEnv::prog_fn(LispObjRef cons, LispEnvRef env) {
LispObjRef result = nil;
LispObjRef fnx = cons;
while(!is_nil(fnx)) {
LispObjRef arg = car(fnx);
result = eval(arg, env);
fnx = cdr(fnx);
}
return result;
}
} // namespace Lisp