-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.cpp
178 lines (154 loc) · 3.55 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
#include <sstream>
#include "object.hpp"
#include "parser.hpp"
Parser::Parser(const std::string &input)
: input(input)
, ix(0) {
symQuote = Object::internSymbol("quote");
symQuasiQuote = Object::internSymbol("quasiquote");
symUnquote = Object::internSymbol("unquote");
symUnquoteSplicing = Object::internSymbol("unquote-splicing");
}
Object *Parser::parseProg(bool *outOk) {
bool ok;
Handle head = Object::newNil(),
tail = Object::newNil();
while (true) {
Handle x = parse(&ok);
if (!ok) {
break;
}
if (head->isNil()) {
head = tail = Object::newPair(x, Object::newNil());
}
else {
// XXX: need to separate lhs with rhs to avoid memory corruption
// since lhs (raw()->cdr()) will be evaluated before rhs (t).
Handle t = Object::newPair(x, Object::newNil());
tail->raw()->cdr() = t.getPtr();
tail = tail->raw()->cdr();
}
}
ok = *outOk = !hasNext();
return ok ? head.getPtr() : NULL;
}
Object *Parser::parse(bool *ok) {
assert(hasNext());
char c;
while (true) {
switch ((c = getNextSkipWS(ok))) {
case '(': case '[':
return parseList(c);
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
return parseFixnum(c);
case '\'': case '`': case ',':
return parseQuote(c);
default:
if (!*ok) {
return NULL;
}
return parseAtom(c);
}
}
}
Object *Parser::parseList(char open) {
char c;
char close = open == '(' ? ')' : ']';
bool ok;
Handle head = Object::newNil(),
tail = Object::newNil();
while (hasNext()) {
switch ((c = getNextSkipWS())) {
case ']': case ')':
assert(close == c);
return head.getPtr();
default:
putBack();
{
Handle curr = parse(&ok);
assert(ok);
if (head->isNil()) {
head = tail = Object::newPair(curr.getPtr(), Object::newNil());
}
else {
Handle t = Object::newPair(curr.getPtr(), Object::newNil());
tail->raw()->cdr() = t.getPtr();
tail = tail->raw()->cdr();
}
}
}
}
assert(0);
}
Object *Parser::parseFixnum(char open) {
intptr_t val;
std::stringstream xs;
xs << open;
while (hasNext()) {
char c = getNext();
switch (c) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
xs << c;
continue;
default:
putBack();
xs >> val;
goto done;
}
}
done:
return Object::newFixnum(val);
}
Object *Parser::parseAtom(char open) {
char c;
std::stringstream xs;
switch (open) {
case '#':
c = getNext();
assert(!isspace(c));
if (c == 't') {
return Object::newTrue();
}
else if (c == 'f') {
return Object::newFalse();
}
putBack();
}
xs << open;
while (hasNext()) {
c = getNext();
if (isspace(c) || c == '[' || c == '(' ||
c == ']' || c == ')') {
putBack();
break;
}
xs << c;
}
return Object::internSymbol(xs.str().c_str());
}
Object *Parser::parseQuote(char fst) {
Handle tag;
if (fst == '\'') {
tag = symQuote;
}
else if (fst == ',') {
if (getNext() == '@') {
tag = symUnquoteSplicing;
}
else {
putBack();
tag = symUnquote;
}
}
else {
assert(fst == '`');
tag = symQuasiQuote;
}
bool ok;
Handle body = parse(&ok);
assert(ok);
Handle wrapped = Object::newPair(body, Object::newNil());
return Object::newPair(tag, wrapped);
}