-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatum.h
65 lines (45 loc) · 1.23 KB
/
datum.h
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
#ifndef _stran_datum_h_
#define _stran_datum_h_
#include "tok.h"
#include <cassert>
#include <unordered_map>
namespace stran {
struct datum;
struct env {
env(const sp<env> &p) : parent(p) {}
const sp<datum> &find(const std::string &name) const;
void define(const std::string &name, const sp<datum> &val) {
table[name] = val;
}
private:
std::unordered_map<std::string, sp<datum>> table;
const sp<env> parent;
};
struct datum : tok, std::enable_shared_from_this<datum> {
friend sp<datum> eval(const sp<datum> &p_d, const sp<env> &curr_env) {
assert(p_d && "attempted to evaluate nil");
return p_d->internal_eval(curr_env);
}
private:
// self-evaluating by default
virtual sp<datum> internal_eval(const sp<env> &) {
return shared_from_this();
}
};
struct iden : datum {
iden(const std::string &n) : name(n) {}
const std::string name;
private:
operator std::string() const override { return name; }
sp<datum> internal_eval(const sp<env> &curr_env) override {
return curr_env->find(name);
}
};
struct inexact : datum {
inexact(const double v) : val(v) {}
private:
operator std::string() const override { return std::to_string(val); }
const double val;
};
} // namespace stran
#endif