-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.cpp
223 lines (197 loc) · 4.22 KB
/
parser.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
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
#include "parser.h"
Number::Number(double value) : val(value) {}
double Number::value() const
{
return val;
}
Symbol::Symbol(const std::string& symbol) : sym(symbol) {}
std::string Symbol::value() const
{
return sym;
}
ConsPair::ConsPair(const ElemPtr& car, const ElemPtr& cdr)
: car_(car), cdr_(cdr) {}
ConsPair::ConsPair() {}
ElemPtr ConsPair::car()
{
return car_;
}
ElemPtr ConsPair::cdr()
{
return cdr_;
}
void ConsPair::set_car(const ElemPtr& car)
{
car_ = car;
}
void ConsPair::set_cdr(const ElemPtr& cdr)
{
cdr_ = cdr;
}
bool is_nil(const PairPtr& p)
{
return ((!(p->car())) && (!(p->cdr())));
}
NumberPtr make_number(double value)
{
return NumberPtr(new Number(value));
}
SymbolPtr make_symbol(const std::string& symbol)
{
return SymbolPtr(new Symbol(symbol));
}
PairPtr to_cons(const ElemPtr& e)
{
return boost::dynamic_pointer_cast<ConsPair>(e);
}
SymbolPtr to_symbol(const ElemPtr& e)
{
return boost::dynamic_pointer_cast<Symbol>(e);
}
NumberPtr to_number(const ElemPtr& e)
{
return boost::dynamic_pointer_cast<Number>(e);
}
Parser::Parser(const std::string& input)
{
parse_expr(input);
}
void Parser::add_elem(const ElemPtr& elem)
{
if (current->car())
{
current->set_cdr(elem);
current = to_cons(current->cdr());
}
else
{
current->set_car(elem);
PairPtr P(new ConsPair());
current->set_cdr(P);
current = to_cons(current->cdr());
}
}
// The heart of our parser, which as mentioned is basically a pushdown
// automaton. The strategy is: examine the input character by
// character. When we reach an open-paren, we push an empty conspair
// onto the stack and start adding to that. When we reach a
// close-paren, we know that our conspair is finished, so we pop the
// stack and continue where we left off previously.
void Parser::parse_expr(const std::string& input)
{
size_t currindex = 0;
while((currindex < (input.length())) &&
currindex != std::string::npos)
{
switch(input[currindex])
{
case ' ':
currindex++;
break;
case '(':
{
// Beginning of conspair
PairPtr P(new ConsPair());
S.push(P);
if (S.size() > 1)
add_elem(P);
current = S.top();
currindex++;
break;
}
case ')':
{
if (S.size() > 1)
{
// We are done processing this list, so let's pop
// the stack and continue where we left off on the
// previous one.
S.pop();
current = to_cons(S.top()->cdr());
while(!(is_nil(to_cons(current))))
current = to_cons(current->cdr());
}
else
{
// No more to process. Return what we have.
expr = S.top();
S.pop();
}
currindex++;
break;
}
default:
{
size_t nextindex = next_index(input, currindex);
std::string::const_iterator end;
if (nextindex == std::string::npos)
end = input.end();
else
end = input.begin() + nextindex;
std::string token(input.begin() + currindex, end);
ElemPtr e;
if (is_number(token))
{
double d = convert<double>(token);
e = make_number(d);
}
else
e = make_symbol(token);
if (S.empty())
expr = e;
else
add_elem(e);
currindex = nextindex;
}
break;
}
}
if (!(S.empty()))
throw Exception("Parse error: Malformed input");
}
ElemPtr Parser::parsed_expr()
{
return expr;
}
// Would be nice to make this a virtual member function, but it's
// awkward with the recursion for list printing.
std::ostream& print(std::ostream& os, const ElemPtr& e, bool first=true)
{
SymbolPtr sym = to_symbol(e);
NumberPtr num = to_number(e);
if (sym)
{
os << sym->value();
}
else if (num)
{
os << num->value();
}
else
{
PairPtr p = to_cons(e);
if (p)
{
if (first)
os << "(";
if (is_nil(p))
os << ")";
else
{
if (!(to_symbol(p->car())))
os << "(";
print(os, p->car(), false);
if (!(is_nil(to_cons(p->cdr()))))
os << " ";
print(os, p->cdr(), false);
}
}
}
return os;
}
// Overload function to enable us to print any expression, be it a
// list, single number, or symbol.
std::ostream& operator<<(std::ostream& os, const ElemPtr& e)
{
return print(os, e);
}