-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepr.cpp
185 lines (136 loc) · 3.91 KB
/
repr.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
#include "repr.hpp"
#include "ast.hpp"
#include "tool.hpp"
namespace ast {
////////////////////////////////////////////////////////////////////////////////
namespace {
struct repr_visitor {
template<class T>
sexpr operator()(const T& self) const {
return repr(self);
}
};
}
template<class T>
static sexpr repr(const T& ) {
static_assert(sizeof(T) == 0, "unimplemented repr");
// throw std::logic_error("unimplemented repr: " + tool::type_name(typeid(T)));
}
template<class T>
static sexpr repr(const lit<T>& self) {
return self.value;
}
static sexpr repr(const lit<unit>& ) {
return sexpr::list();
}
static sexpr repr(const var& self) {
return self.name;
}
static sexpr repr(const abs::typed& self) {
return repr(self.type) >>= self.id.name >>= sexpr::list();
}
sexpr repr(const abs::arg& self) {
return self.visit(repr_visitor());
}
static sexpr repr(const abs& self) {
return kw::abs
>>= map(self.args, [](abs::arg arg) -> sexpr {
return repr(arg);
})
>>= repr(*self.body)
>>= sexpr::list();
}
static sexpr repr(const app& self) {
return
repr(*self.func)
>>= map(self.args, [](const expr& e) {
return repr(e);
});
}
static sexpr repr(const let& self) {
return kw::let
>>= map(self.defs, [](const bind& self) -> sexpr {
return self.id.name >>= repr(self.value) >>= sexpr::list();
})
>>= repr(*self.body)
>>= sexpr::list();
}
static sexpr repr(const io& self) {
return self.visit(repr_visitor());
}
static sexpr repr(const seq& self) {
return kw::seq
>>= concat(map(self.items, repr_visitor()),
repr(*self.last) >>= sexpr::list());
}
static sexpr repr(const run& self) {
return kw::run
>>= repr(*self.value)
>>= sexpr::list();
}
static sexpr repr(const def& self) {
return kw::def
>>= self.id.name
>>= repr(*self.value)
>>= sexpr::list();
}
static sexpr repr(const sel& self) {
return symbol(std::string(1, selection_prefix) + self.id.name.get());
}
static sexpr repr(const inj& self) {
return symbol(std::string(1, injection_prefix) + self.id.name.get());
}
static sexpr repr(const record& self) {
return kw::record
>>= map(self.attrs, [](record::attr attr) -> sexpr {
return attr.id.name >>= repr(attr.value) >>= sexpr::list();
});
}
static sexpr repr(const bind& self) {
return kw::bind
>>= self.id.name
>>= repr(self.value)
>>= sexpr::list();
}
static sexpr repr(const cond& self) {
return kw::cond
>>= repr(*self.test)
>>= repr(*self.conseq)
>>= repr(*self.alt)
>>= sexpr::list();
}
static sexpr repr(const make& self) {
return kw::make
>>= repr(*self.type)
>>= map(self.attrs, [](record::attr attr) -> sexpr {
return attr.id.name >>= repr(attr.value) >>= sexpr::list();
});
}
static sexpr repr(const module& self) {
return (self.type == module::product ? kw::product : kw::coproduct)
>>= sexpr(self.id.name >>= map(self.args, repr_visitor()))
>>= map(self.attrs, [](record::attr attr) -> sexpr {
return attr.id.name >>= repr(attr.value) >>= sexpr::list();
});
}
static sexpr repr(const use& self) {
return kw::use
>>= repr(*self.env)
// >>= repr(*self.body)
>>= sexpr::list();
}
static sexpr repr(const import& self) {
return kw::import
>>= self.package
>>= sexpr::list();
}
static sexpr repr(const match& self) {
return kw::match
>>= map(self.cases, [](match::handler self) -> sexpr {
return self.id.name >>= repr(self.arg) >>= repr(self.value) >>= sexpr::list();
});
}
sexpr repr(const expr& self) {
return self.visit(repr_visitor());
}
}