-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.hpp
72 lines (54 loc) · 1.71 KB
/
runner.hpp
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
#ifndef RUNNER_HPP
#define RUNNER_HPP
#include<map>
#include<string>
#include<sstream>
#include "parser.hpp"
using Value = Cell;
using Key = std::string;
using Refrences = std::map<Key, Value>;
struct Environment {
private:
Refrences refs;
Environment* env;
public:
explicit Environment(Environment* env): refs(), env(env) {}
template<typename IterableCont>
Environment(IterableCont keys, IterableCont vals, Environment* env): refs(), env(env) {
auto valIter = vals.cbegin();
auto keyIter = keys.cbegin();
while(valIter != vals.cend() && keyIter != keys.cend()) {
refs.insert_or_assign(keyIter->val, *valIter);
keyIter++;
valIter++;
}
}
void setValue(const Key&, Value);
//void setValue(const Key&, Value&&);
bool keyExists(const Key&) const;
Value& getValue(const Key&);
Value& operator[] (const Key&);
};
const Cell false_sym(Symbol, "false");
const Cell true_sym(Symbol, "true");
const Cell nil(Symbol, "nil");
inline std::string str(long n) { std::ostringstream os; os << n; return os.str(); }
Cell print_sym(const Cells& cells);
Cell add_sym(const Cells& cells);
Cell eq_sym(const Cells& cells);
Cell lt_sym(const Cells& cells);
Cell sub_sym(const Cells& cells);
inline void addGlobalVars(Environment& env) {
// Global consants
env.setValue("nil", nil);
env.setValue("false", false_sym);
env.setValue("true", true_sym);
// Pre defined functions
env.setValue("print!", Cell(print_sym));
env.setValue("+", Cell(add_sym));
env.setValue("-", Cell(sub_sym));
env.setValue("=", Cell(eq_sym));
env.setValue("<", Cell(lt_sym));
}
Value eval(Cell c, Environment* env);
#endif // RUNNER_HPP