-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParser.cpp
156 lines (148 loc) · 3.69 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
#include <string>
#include <vector>
#include "Expr.h"
#include "Parser.h"
namespace lx {
//! run
std::vector<std::string>
Tokenizer::run (const std::string& str)
{
std::vector<std::string> tokens;
int size = str.size();
int i = 0;
while (i < size) {
while (str[i] == ' ') {
i += 1;
}
if (str[i] == '(' || str[i] == ')') {
tokens.push_back(std::string(1, str[i]));
i += 1;
} else {
int m = i;
while (i < size &&
str[i] != ' ' &&
str[i] != '(' &&
str[i] != ')') {
i += 1;
}
if (m < i) {
tokens.push_back(str.substr(m, i - m));
}
}
}
return tokens;
}
Expr*
Parser::run (std::vector<std::string>::const_iterator& iter)
{
const std::string token(*iter);
iter = std::next(iter, 1);
if (token == "(") {
List* expr = nullptr;
if (*iter == "+" ||
*iter == "-" ||
*iter == "*" ||
*iter == "/")
{
expr = new ArithmeticExpr(*iter);
iter = std::next(iter, 1);
}
else if (*iter == "=" ||
*iter == "<" ||
*iter == "<=" ||
*iter == ">" ||
*iter == ">=")
{
expr = new RelationExpr(*iter);
iter = std::next(iter, 1);
}
else if (*iter == "define")
{
expr = new DefineExpr();
iter = std::next(iter, 1);
}
else if (*iter == "let")
{
expr = new LetExpr();
iter = std::next(iter, 1);
}
else if (*iter == "lambda")
{
expr = new LambdaExpr();
iter = std::next(iter, 1);
}
else if (*iter == "if")
{
expr = new IfExpr();
iter = std::next(iter, 1);
}
else if (*iter == "cond")
{
expr = new CondExpr();
iter = std::next(iter, 1);
}
else if (*iter == "cons")
{
expr = new ConsExpr();
iter = std::next(iter, 1);
}
else if (*iter == "car" ||
*iter == "cdr")
{
expr = new ListOpExpr(*iter);
iter = std::next(iter, 1);
}
else if (*iter == "begin")
{
expr = new BeginExpr();
iter = std::next(iter, 1);
}
else
{
expr = new List();
}
while (*iter != ")") {
expr->_exprs.push_back(std::move(run(iter)));
}
iter = std::next(iter, 1);
return expr;
} else {
if (is_integer(token)) {
return new Integer(std::stoi(token));
} else if (is_float(token)) {
return new Float(std::stof(token));
} else {
if (token == "else")
{
return new ElseExpr();
}
else if (token == "#T" ||
token == "#t" ||
token == "true")
{
return new Boolean(true);
}
else if (token == "#F" ||
token == "#f" ||
token == "false")
{
return new Boolean(false);
}
else if (token == "nil")
{
return List::Nil();
}
else
{
return new Symbol(token);
}
}
}
}
Expr*
Parser::run (const std::vector<std::string>& tokens) // FIXME
{
auto&& iter = tokens.begin();
return run(iter);
}
}