-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmp_lisp.hpp
313 lines (227 loc) · 8.43 KB
/
tmp_lisp.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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Restricted Scheme-like Language using Template Metaprogramming
//
// Copyright Thomas D Peters 2018-present
//
// Use, modification and distribution is subject to the
// Boost Software License, Version 1.0. (See accompanying
// file LICENSE or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#pragma once
/*****************
Syntax constructions
*****************/
template <bool> struct Bool {};
using True = Bool<true>;
using False = Bool<false>;
template <int> struct Int {};
template <class Car, class Cdr> struct Cons {};
struct EmptyList {};
template <class Operator, class... Operands> struct SExp {};
template <class Body, class... Params> struct Lambda {};
template <class Body, class Environment, class... Params> struct Closure {};
template <class Cond, class IfTrue, class IfFalse> struct If {};
enum class OpCode {
Add,
Sub,
Mul,
Eq,
Neq,
Leq,
Neg,
Or,
And,
Not,
Cons,
Car,
Cdr,
IsNull
};
template <OpCode> struct Op {};
template <int> struct Var {};
template <class Variable, class Value> struct Binding {};
template <class... Bindings> struct Env {};
using EmptyEnv = Env<>;
namespace detail {
template <class T0, class T1> struct Concat;
template <class... Bindings1, class... Bindings2>
struct Concat<Env<Bindings1...>, Env<Bindings2...>> {
using type = Env<Bindings1..., Bindings2...>;
};
template <class... Ts> struct List {};
template <class, class> struct MakeEnv;
template <> struct MakeEnv<List<>, List<>> { using type = EmptyEnv; };
template <class T> using Result_t = typename T::type;
template <class Variable, class... Variables, class Value, class... Values>
struct MakeEnv<List<Variable, Variables...>, List<Value, Values...>> {
static_assert(sizeof...(Variables) == sizeof...(Values));
using InitialEnv = Env<Binding<Variable, Value>>;
using FinalEnv = Result_t<MakeEnv<List<Variables...>, List<Values...>>>;
using type = Result_t<Concat<InitialEnv, FinalEnv>>;
};
} // namespace detail
template <class Variables, class Values>
using MakeEnv_t = detail::Result_t<detail::MakeEnv<Variables, Values>>;
template <class Env1, class Env2>
using ExtendEnv_t = detail::Result_t<detail::Concat<Env2, Env1>>;
template <int i> using Param = Var<i>;
template <class Variable, class Env> struct Lookup;
template <class Value, class Environment> struct PushEnv {
using type = Value;
};
template <class LambdaBody, class LambdaEnv, class... LambdaParams,
class Environment>
struct PushEnv<Closure<LambdaBody, LambdaEnv, LambdaParams...>, Environment> {
using type =
Closure<LambdaBody, ExtendEnv_t<LambdaEnv, Environment>, LambdaParams...>;
};
template <class Variable, class Value, class... Bindings>
struct Lookup<Variable, Env<Binding<Variable, Value>, Bindings...>> {
using type = Value;
};
template <class Variable, class Binding0, class... Bindings>
struct Lookup<Variable, Env<Binding0, Bindings...>> {
using type = detail::Result_t<Lookup<Variable, Env<Bindings...>>>;
};
template <class Variable, class Env>
using Lookup_t =
detail::Result_t<PushEnv<detail::Result_t<Lookup<Variable, Env>>, Env>>;
/********************
APPLY fwd definition
*********************/
template <class Operator, class... Operands> struct Apply_;
template <class Operator, class... Operands>
using Apply = detail::Result_t<Apply_<Operator, Operands...>>;
/*****************
EVAL
*****************/
template <class Exp, class Env> struct Eval_;
template <class Exp, class Env> using Eval = detail::Result_t<Eval_<Exp, Env>>;
template <int i, class _> struct Eval_<Int<i>, _> { using type = Int<i>; };
template <bool b, class _> struct Eval_<Bool<b>, _> { using type = Bool<b>; };
template <int i, class Env> struct Eval_<Var<i>, Env> {
using type = Eval<Lookup_t<Var<i>, Env>, Env>;
};
template <class Cond, class IfTrue, class IfFalse, class Env>
struct Eval_<If<Cond, IfTrue, IfFalse>, Env>;
template <class IfTrue, class _, class Env>
struct Eval_<If<True, IfTrue, _>, Env> {
using type = Eval<IfTrue, Env>;
};
template <class _, class IfFalse, class Env>
struct Eval_<If<False, _, IfFalse>, Env> {
using type = Eval<IfFalse, Env>;
};
namespace detail {
template <class Val> struct ConvertToBool { using type = True; };
template <> struct ConvertToBool<False> { using type = False; };
template <> struct ConvertToBool<Int<0>> { using type = False; };
template <class Val> using ConvertToBool_t = Result_t<ConvertToBool<Val>>;
} // namespace detail
template <class Cond, class IfTrue, class IfFalse, class Env>
struct Eval_<If<Cond, IfTrue, IfFalse>, Env> {
using type =
Eval<If<detail::ConvertToBool_t<Eval<Cond, Env>>, IfTrue, IfFalse>, Env>;
};
template <class Body, class... Params, class Env>
struct Eval_<Lambda<Body, Params...>, Env> {
using type = Closure<Body, Env, Params...>;
};
template <class Body, class LambdaEnv, class... Params, class Env>
struct Eval_<Closure<Body, LambdaEnv, Params...>, Env> {
using type = Closure<Body, ExtendEnv_t<LambdaEnv, Env>, Params...>;
};
template <class Car, class Cdr, class Env> struct Eval_<Cons<Car, Cdr>, Env> {
using type = Cons<Eval<Car, Env>, Eval<Cdr, Env>>;
};
template <class _> struct Eval_<EmptyList, _> { using type = EmptyList; };
template <OpCode opcode, class _> struct Eval_<Op<opcode>, _> {
using type = Op<opcode>;
};
template <class Operator, class... Operands, class Env>
struct Eval_<SExp<Operator, Operands...>, Env> {
using type = Apply<Eval<Operator, Env>, Eval<Operands, Env>...>;
};
/*****************
APPLY
*****************/
template <> struct Apply_<Op<OpCode::Add>> { using type = Int<0>; };
template <int... is> struct Apply_<Op<OpCode::Add>, Int<is>...> {
using type = Int<(... + is)>;
};
template <int i1, int... is>
struct Apply_<Op<OpCode::Sub>, Int<i1>, Int<is>...> {
using type = Int<i1 - (... + is)>;
};
template <int i> struct Apply_<Op<OpCode::Sub>, Int<i>> {
using type = Int<-i>;
};
template <int... is> struct Apply_<Op<OpCode::Mul>, Int<is>...> {
using type = Int<(... * is)>;
};
template <> struct Apply_<Op<OpCode::Mul>> { using type = Int<1>; };
template <int... is> struct Apply_<Op<OpCode::Eq>, Int<is>...> {
using type = Bool<(... == is)>;
};
template <int i1, int i2> struct Apply_<Op<OpCode::Neq>, Int<i1>, Int<i2>> {
using type = Bool<i1 != i2>;
};
template <int i1, int i2> struct Apply_<Op<OpCode::Leq>, Int<i1>, Int<i2>> {
using type = Bool<i1 <= i2>;
};
template <class... Exps> struct Apply_<Op<OpCode::Eq>, Exps...> {
using type = Bool<false>;
};
template <bool... bs> struct Apply_<Op<OpCode::Eq>, Bool<bs>...> {
using type = Bool<(... == bs)>;
};
template <class... Exps> struct Apply_<Op<OpCode::Neq>, Exps...> {
using type = Bool<true>;
};
template <bool b1, bool b2> struct Apply_<Op<OpCode::Neq>, Bool<b1>, Bool<b2>> {
using type = Bool<b1 != b2>;
};
template <bool... bs> struct Apply_<Op<OpCode::Or>, Bool<bs>...> {
using type = Bool<(... or bs)>;
};
template <bool... bs> struct Apply_<Op<OpCode::And>, Bool<bs>...> {
using type = Bool<(... and bs)>;
};
template <bool b> struct Apply_<Op<OpCode::Not>, Bool<b>> {
using type = Bool<not b>;
};
template <class Car, class Cdr> struct Apply_<Op<OpCode::Cons>, Car, Cdr> {
using type = Cons<Car, Cdr>;
};
template <class Car, class Cdr> struct Apply_<Op<OpCode::Car>, Cons<Car, Cdr>> {
using type = Car;
};
template <class Car, class Cdr> struct Apply_<Op<OpCode::Cdr>, Cons<Car, Cdr>> {
using type = Cdr;
};
template <class _> struct Apply_<Op<OpCode::IsNull>, _> {
using type = False;
};
template <> struct Apply_<Op<OpCode::IsNull>, EmptyList> { using type = True; };
template <class Body, class Env, class... Params, class... Args>
struct Apply_<Closure<Body, Env, Params...>, Args...> {
static_assert(sizeof...(Params) == sizeof...(Args));
using ExtendedEnv =
ExtendEnv_t<Env,
MakeEnv_t<detail::List<Params...>, detail::List<Args...>>>;
using type = Eval<Body, ExtendedEnv>;
};
/*****************
Compound forms
******************/
template <class Env, class Body> using Let = SExp<Closure<Body, Env>>;
template <class DefaultExp, class... Cases> struct Cond_;
template <class DefaultExp> struct Cond_<DefaultExp> {
using type = DefaultExp;
};
template <class DefaultExp, class Cond, class IfMatch, class... RemainingCases>
struct Cond_<DefaultExp, Cond, IfMatch, RemainingCases...> {
using type =
If<Cond, IfMatch, detail::Result_t<Cond_<DefaultExp, RemainingCases...>>>;
};
template <class DefaultExp, class... Cases>
using Cond = detail::Result_t<Cond_<DefaultExp, Cases...>>;