-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.cpp
192 lines (161 loc) · 6.68 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
#include "lexer.cpp"
#include "expression.h"
#include <cstdarg>
class Parser {
public:
Parser(const std::vector<Token>& tok) : tokens(tok) {
current = 0;
}
Token peek() {
return tokens.at(current);
}
bool is_at_end() {
return current == tokens.size();
}
bool check(TokenType type) {
if (is_at_end()) return false;
// std::cout << peek().get_type() << " " << type << std::endl;
// std::cout << "checking ";
return peek().get_type() == type;
}
Token previous() {
return tokens.at(current - 1);
}
Token advance() {
if (!is_at_end()) current++;
return previous();
}
Expression * parse() {
return expression();
}
Expression * expression() {
// std::cout << "expression - " << current << std::endl;
if (match(1, LET)) {
// std::cout << "dec" << std::endl;
Token var = advance();
advance();
Expression * right = comparison();
return new AssignmentExpression(var.get_string(), right);
} else if (match(1, IF)) {
Expression * if_exp = if_expression();
return if_exp;
}
return comparison();
}
Expression * comparison() {
// std::cout << "comparison - " << current << std::endl;
Expression * left = term();
while (match(4, GEQ, LEQ, GT, LT)) {
Token op = previous();
Expression * right = term();
switch (op.get_type()) {
case GT: left = new BinaryExpression(BinaryOperator::GtOp, left, right); break;
case GEQ: left = new BinaryExpression(BinaryOperator::GteOp, left, right); break;
case LT: left = new BinaryExpression(BinaryOperator::LtOp, left, right); break;
case LEQ: left = new BinaryExpression(BinaryOperator::GteOp, left, right); break;
case NEQ: left = new BinaryExpression(BinaryOperator::NotEqualsOp, left, right); break;
case EQUALITY: left = new BinaryExpression(BinaryOperator::EqualityOp, left, right); break;
};
}
return left;
};
Expression * term() {
// std::cout << "term - " << current << std::endl;
Expression * left = factor();
while (match(2, PLUS, MINUS)) {
Token op = previous();
Expression * right = factor();
switch (op.get_type()) {
case PLUS: left = new BinaryExpression(BinaryOperator::IntPlusOp, left, right); break;
case MINUS: left = new BinaryExpression(BinaryOperator::IntMinusOp, left, right); break;
};
}
return left;
}
Expression * factor() {
// std::cout << "factor - " << current << std::endl;
Expression * left = unary();
while (match(3, TIMES, DIVIDES, MOD)) {
Token op = previous();
Expression * right = unary();
switch (op.get_type()) {
case TIMES: left = new BinaryExpression(BinaryOperator::IntTimesOp, left, right); break;
case DIVIDES: left = new BinaryExpression(BinaryOperator::IntDivOp, left, right); break;
case MOD: left = new BinaryExpression(BinaryOperator::ModOp, left, right); break;
};
}
return left;
}
Expression * unary() {
// std::cout << "unary - " << current << std::endl;
if (match(2, PRINT, MINUS)) {
Token op = previous();
Expression * right = unary();
switch (op.get_type()) {
case PRINT: return new MonadicExpression(MonadicOperator::PrintOp, right);
case MINUS: return new MonadicExpression(MonadicOperator::IntNegOp, right);
};
}
return atomic();
}
Expression * atomic() {
// std::cout << "atomic - " << current << std::endl;
if (match(1, FALSE)) return new ConstExp(false);
// std::cout << "atomic - " << current << std::endl;
if (match(1, TRUE)) return new ConstExp(true);
// std::cout << "atomic - " << current << std::endl;
if (match(1, INTEGER)) {
return new ConstExp(stoi(previous().get_string()));
}
// std::cout << "atomic - " << current << std::endl;
if (match(1, STRING)) return new ConstExp(previous().get_string());
// std::cout << "atomic - " << current << std::endl;
if (match(1, IDENTIFIER)) return new VarExp(previous().get_string());
// std::cout << "atomic - " << current << std::endl;
if (match(1, LEFT_PAREN)) {
Expression * inner_exp = expression();
if (match(1, RIGHT_PAREN)) {
return inner_exp;
}
}
}
Expression * if_expression() {
// std::cout << "if-statment - " << current << std::endl;
if (!match(1, LEFT_PAREN)) {}
Expression * conditional = expression();
if (!match(1, RIGHT_PAREN)) {}
if (!match(1, LBRACE)) {}
while (!match(1, RBRACE))
Expression * if_exp = expression();
// std::cout << current << std::endl;
advance(); // right brace
advance(); // else
advance();
advance(); // left brace
// std::cout << current << std::endl;
Expression * else_exp = expression();
advance(); // right brace
advance();
return new IfExpression(conditional, if_exp, else_exp);
}
private:
size_t current;
const std::vector<Token> tokens;
bool match(int count, ...) {
// std::cout << "match - " << current << std::endl;
va_list args;
va_start(args, count);
for (int i = 0; i < count; i++) {
TokenType type = va_arg(args, TokenType);
// std::cout << type;
if (check(type)) {
// std::cout << "found match ";
va_end(args);
advance();
return true;
}
}
va_end(args);
return false;
}
};