-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.cpp
163 lines (137 loc) · 4.33 KB
/
runner.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
#include "runner.hpp"
Value& Environment::getValue(const Key& key) {
if (refs.count(key)) {
return refs.at(key);
}
if (env != nullptr) {
return env->getValue(key);
}
exit(-40);
}
void Environment::setValue(const Key& key, Value value) {
refs.insert_or_assign(key, value);
}
//void Environment::setValue(const Key& key, Value&& value) {
// refs.insert_or_assign(key, std::move(value));
//}
Value& Environment::operator[] (const Key& key) {
return this->getValue(key);
}
bool Environment::keyExists(const Key & key) const {
return refs.count(key) || (env != nullptr && env->keyExists(key));
}
Cell print_sym(const Cells& cells) {
for(const auto& cell : cells) {
std::cout << cell.val;
}
std::cout << '\n';
return nil;
}
Cell add_sym(const Cells& cells) {
long n = 0;
for (auto it = cells.cbegin(); it != cells.cend(); it++) {
if (it->type != Number) {
return nil;
}
n+= std::atol(it->val.c_str());
}
return Cell(Number, str(n));
}
Cell eq_sym(const Cells& cells) {
if (cells[0].val == cells[1].val) {
return true_sym;
} else {
return false_sym;
}
}
Cell lt_sym(const Cells& cells) {
if (std::atol(cells[0].val.c_str()) < std::atol(cells[1].val.c_str())) {
return true_sym;
} else {
return false_sym;
}
}
Cell sub_sym(const Cells& cells) {
return Cell(Number, str((std::atol(cells[0].val.c_str())
- std::atol(cells[1].val.c_str()))));
}
Value eval(Cell c, Environment* env) {
if (c.type == Symbol) {
return env->getValue(c.val);
}
if (c.type == Number || c.type == String) {
return c;
}
if (c.list.empty()) {
return nil;
}
if (c.list[0].type == Symbol) {
if (c.list[0].val == "quote") {
return c.list[1];
}
if (c.list[0].val == "if") {
return eval(eval(c.list[1], env).val == "false" ?
(c.list.size() < 4 ? nil : c.list[3]) : c.list[2], env);
}
if (c.list[0].val == "set!") {
env->getValue(c.list[1].val) = eval(c.list[2], env);
return env->getValue(c.list[1].val);
}
if (c.list[0].val == "def") {
env->setValue(c.list[1].val, eval(c.list[2], env));
return env->getValue(c.list[1].val);
}
if (c.list[0].val == "let") {
Environment* e = new Environment(env);
for (const auto& eli : c.list[1].list) {
if (eli.list[0].type != Symbol) {
// Freak
exit(45);
}
e->setValue(eli.list[0].val, eval(eli.list[1], e));
}
auto ret = eval(c.list[2], e);
delete e;
return ret;
}
if (c.list[0].val == "loop") {
// (loop (cond) (prog ...))
Cell last = c.list[1];
eval(last, env);
while (eval(c.list[1], env).val != "false") {
last = eval(c.list[2], env);
}
return last;
}
if (c.list[0].val == "fn") {
c.type = Fn;
c.env = env;
return c;
}
if (c.list[0].val == "prog") {
for(auto i = c.list.begin() + 1; i+1 != c.list.end(); i++) {
eval(*i, env);
}
return eval(*(c.list.end()-1), env);
}
}
Cell proc(eval(c.list[0], env));
Cells exps;
for (auto exp = c.list.begin() + 1; exp != c.list.end(); ++exp)
exps.push_back(eval(*exp, env));
if (proc.type == Fn) {
// Create an environment for the execution of this lambda function
// where the outer environment is the one that existed* at the time
// the lambda was defined and the new inner associations are the
// parameter names with the given arguments.
// *Although the environmet existed at the time the lambda was defined
// it wasn't necessarily complete - it may have subsequently had
// more symbols defined in that environment.
return eval(/*body*/proc.list[2],
new Environment(/*parms*/proc.list[1].list, /*args*/exps, proc.env));
}
else if (proc.type == Proc)
return proc.proc(exps);
exit(-50);
return nil;
}