This repository was archived by the owner on Sep 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforms.cpp
More file actions
160 lines (157 loc) · 5.41 KB
/
Copy pathforms.cpp
File metadata and controls
160 lines (157 loc) · 5.41 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
#include "forms.h"
#include "error.h"
#include "builtins.h"
const std::unordered_map<std::string, SpecialFormType> SPECIAL_FORMS{
{"define", defineForm},
{"quote", quoteForm},
{"if", ifForm},
{"and", andForm},
{"or", orForm},
{"lambda", lambdaForm},
{"cond", condForm},
{"begin", beginForm},
{"let", letForm},
{"quasiquote", quasiquoteForm}
};
ValuePtr defineForm(const std::deque<ValuePtr>& args, EvalEnv& env) {
checkParam(args, 2);
if (auto name = args[0]->asSymbol()) {
env.defineBinding(*name, env.eval(args[1]));
return std::make_shared<NilValue>();
}
else {
auto func = static_cast<PairValue&>(*args[0]);
auto fname = func.getCar()->asSymbol();
auto params = func.getCdr()->toDeque();
auto body = args;
body.pop_front();
env.defineBinding(*fname, std::make_shared<LambdaValue>(params, body, env.shared_from_this()));
return std::make_shared<NilValue>();
}
throw LispError("Unimplemented");
}
ValuePtr quoteForm(const std::deque<ValuePtr>& args, EvalEnv& env) {
checkParam(args, 1, 1);
return args[0];
}
ValuePtr ifForm(const std::deque<ValuePtr>& args, EvalEnv& env) {
checkParam(args, 3, 3);
auto condition = env.eval(args[0]);
if (typeid(*condition) == typeid(BooleanValue) &&
static_cast<BooleanValue&>(*condition).getValue() == false) {
return env.eval(args[2]);
}
else {
return env.eval(args[1]);
}
}
ValuePtr andForm(const std::deque<ValuePtr>& args, EvalEnv& env) {
for (auto& arg : args) {
auto condition = env.eval(arg);
if (typeid(*condition) == typeid(BooleanValue) &&
static_cast<BooleanValue&>(*condition).getValue() == false) {
return condition;
}
}
if (args.size() == 0)
return std::make_shared<BooleanValue>(true);
else
return env.eval(args[args.size() - 1]);
}
ValuePtr orForm(const std::deque<ValuePtr>& args, EvalEnv& env) {
for (auto& arg : args) {
auto condition = env.eval(arg);
if (typeid(*condition) == typeid(BooleanValue) &&
static_cast<BooleanValue&>(*condition).getValue() == false) {
continue;
}
return condition;
}
return std::make_shared<BooleanValue>(false);
}
ValuePtr lambdaForm(const std::deque<ValuePtr>& args, EvalEnv& env) {
checkParam(args, 2);
auto params = static_cast<PairValue&>(*args[0]).toDeque();
auto body = args;
body.pop_front();
return std::make_shared<LambdaValue>(params, body, env.shared_from_this());
}
ValuePtr condForm(const std::deque<ValuePtr>& args, EvalEnv& env) {
checkParam(args, 1);
ValuePtr ret;
for (auto clause = args.begin(); clause != args.end(); ++clause) {
if (auto symbol = static_cast<PairValue&>(**clause).getCar()->asSymbol()) {
if (*symbol == "else")
if (clause != args.end() - 1)
throw LispError("Else must be in the Last Clause.");
else {
auto exprs =
static_cast<PairValue&>(**clause).getCdr()->toDeque();
if (exprs.empty())
throw LispError("Else doesn't include expressions.");
else
for (auto& expr : exprs) ret = env.eval(expr);
break;
}
} else {
auto condition = env.eval(static_cast<PairValue&>(**clause).getCar());
if (typeid(*condition) == typeid(BooleanValue) &&
(!static_cast<BooleanValue&>(*condition).getValue()))
continue;
auto exprs = static_cast<PairValue&>(**clause).getCdr()->toDeque();
if (exprs.empty())
ret = condition;
else
for (auto& expr : exprs)
ret = env.eval(expr);
break;
}
}
return ret;
}
ValuePtr beginForm(const std::deque<ValuePtr>& args, EvalEnv& env) {
checkParam(args, 1);
ValuePtr ret;
for (auto& clause : args) {
ret = env.eval(clause);
}
return ret;
}
ValuePtr letForm(const std::deque<ValuePtr>& args, EvalEnv& env) {
checkParam(args, 2);
auto bindings = args[0]->toDeque();
std::deque<ValuePtr> params;
std::deque<ValuePtr> values;
for (auto& binding : bindings) {
auto binding_deque = binding->toDeque();
params.push_back(binding_deque[0]);
values.push_back(env.eval(binding_deque[1]));
}
auto body = args;
body.pop_front();
auto _lambda =
std::make_shared<LambdaValue>(params, body, env.shared_from_this());
return _lambda->apply(values);
}
ValuePtr quasiquoteForm(const std::deque<ValuePtr>& args, EvalEnv& env) {
checkParam(args, 1, 1);
auto _args = args[0];
if (!_args->isList())
return _args;
else {//_args is a list
auto _args_pair = static_cast<PairValue&>(*_args);
if (auto symbol = _args_pair.getCar()->asSymbol()) {
if (*symbol == "unquote")
return env.eval(_args_pair.toDeque()[1]);
}
auto _args_deque = _args->toDeque();
std::deque<ValuePtr> res;
for (auto& _arg : _args_deque) {
res.push_back(quasiquoteForm({_arg}, env));
}
if (res.empty())
return std::make_shared<NilValue>();
else
return std::make_shared<PairValue>(res);
}
}