-
Notifications
You must be signed in to change notification settings - Fork 10
/
syntax.hpp
225 lines (184 loc) · 3.92 KB
/
syntax.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
#pragma once
#include <string>
#include <iostream>
#include <boost/variant.hpp>
namespace syntax
{
class integer_literal
{
public:
inline integer_literal(const int v)
: m_value(v)
{}
int value(void) const
{
return m_value;
}
private:
int m_value;
};
inline std::ostream &operator<<(std::ostream &os, const integer_literal &il)
{
return os << il.value();
} // end operator<<()
class identifier
{
public:
inline identifier(const std::string &name)
: m_name(name)
{}
inline const std::string &name(void) const
{
return m_name;
}
private:
std::string m_name;
};
inline std::ostream &operator<<(std::ostream &os, const identifier &i)
{
return os << i.name();
}
class apply;
class lambda;
class let;
class letrec;
typedef boost::variant<
integer_literal,
identifier,
boost::recursive_wrapper<apply>,
boost::recursive_wrapper<lambda>,
boost::recursive_wrapper<let>,
boost::recursive_wrapper<letrec>
> node;
class apply
{
public:
inline apply(node &&fn,
node &&arg)
: m_fn(std::move(fn)),
m_arg(std::move(arg))
{}
inline apply(const node &fn,
const node &arg)
: m_fn(fn),
m_arg(arg)
{}
inline const node &function(void) const
{
return m_fn;
}
inline const node &argument(void) const
{
return m_arg;
}
private:
node m_fn, m_arg;
};
inline std::ostream &operator<<(std::ostream &os, const apply &a)
{
return os << "(" << a.function() << " " << a.argument() << ")";
}
class lambda
{
public:
inline lambda(const std::string ¶m,
node &&body)
: m_param(param),
m_body(std::move(body))
{}
inline lambda(const std::string ¶m,
const node &body)
: m_param(param),
m_body(body)
{}
inline const std::string ¶meter(void) const
{
return m_param;
}
inline const node &body(void) const
{
return m_body;
}
private:
std::string m_param;
node m_body;
};
inline std::ostream &operator<<(std::ostream &os, const lambda &l)
{
return os << "(fn " << l.parameter() << " => " << l.body() << ")";
}
class let
{
public:
inline let(const std::string &name,
node &&def,
node &&body)
: m_name(name),
m_definition(std::move(def)),
m_body(std::move(body))
{}
inline let(const std::string &name,
const node &def,
const node &body)
: m_name(name),
m_definition(def),
m_body(body)
{}
inline const std::string &name() const
{
return m_name;
}
inline const node &definition() const
{
return m_definition;
}
inline const node &body() const
{
return m_body;
}
private:
std::string m_name;
node m_definition, m_body;
};
inline std::ostream &operator<<(std::ostream &os, const let &l)
{
return os << "(let " << l.name() << " = " << l.definition() << " in " << l.body() << ")";
}
class letrec
{
public:
inline letrec(const std::string &name,
node &&def,
node &&body)
: m_name(name),
m_definition(std::move(def)),
m_body(std::move(body))
{}
inline letrec(const std::string &name,
const node &def,
const node &body)
: m_name(name),
m_definition(def),
m_body(body)
{}
inline const std::string &name() const
{
return m_name;
}
inline const node &definition() const
{
return m_definition;
}
inline const node &body() const
{
return m_body;
}
private:
std::string m_name;
node m_definition, m_body;
};
inline std::ostream &operator<<(std::ostream &os, const letrec &l)
{
return os << "(letrec " << l.name() << " = " << l.definition() << " in " << l.body() << ")";
}
} // end syntax