-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.cpp
220 lines (196 loc) · 6.54 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
#include <string>
#include <stdexcept>
#include "token.h"
#include "parser.h"
using namespace std;
Tree Parser::parse() {
return Tree(parseStatementList());
}
void Parser::eatToken(Token::Type type) {
if (offset >= tokens.size()) {
string what = "Syntax error at offset ";
what.append(to_string(offset));
what.append(": unexpected end of file, expected token #");
what.append(to_string(type));
throw invalid_argument(what);
}
if (tokens[offset]->getType() != type) {
string what = "Syntax error at offset ";
what.append(to_string(offset));
what.append(": unexpected token, expected token #");
what.append(to_string(type));
throw invalid_argument(what);
} else offset++;
}
Operator *Parser::eatOperator(int precedence) {
if (offset >= tokens.size()) {
string what = "Syntax error at offset ";
what.append(to_string(offset));
what.append(": unexpected end of file, expected token #");
what.append(to_string(Token::Type::OPERATOR));
throw invalid_argument(what);
}
if (tokens[offset]->getType() != Token::Type::OPERATOR) {
string what = "Syntax error at offset ";
what.append(to_string(offset));
what.append(": unexpected token, expected token #");
what.append(to_string(Token::Type::OPERATOR));
throw invalid_argument(what);
}
auto op = dynamic_cast<Operator *>(tokens[offset]);
if (op->getPrecedence() != precedence) {
string what = "Syntax error at offset ";
what.append(to_string(offset));
what.append(": unexpected operator, expected operator with precedence ");
what.append(to_string(precedence));
throw invalid_argument(what);
}
offset++;
return op;
}
/*
* empty | (statement)*
*/
StatementListNode *Parser::parseStatementList() {
auto node = new StatementListNode;
while (offset < tokens.size() && tokens[offset] != ClosingCurlyBracket::getInstance()) {
node->statements.push_back(parseStatement());
}
return node;
}
/*
* LCURLY statementList RCURLY
*/
StatementListNode *Parser::parseStatementBlock() {
eatToken(Token::Type::OPENING_CURLY_BRACKET);
auto body = parseStatementList();
eatToken(Token::Type::CLOSING_CURLY_BRACKET);
return body;
}
/*
* (expression | assignment | branch | empty);
*/
TreeNode *Parser::parseStatement() {
if (offset + 1 < tokens.size() && tokens[offset]->getType() == Token::Type::IDENTIFIER
&& tokens[offset + 1]->getType() == Token::Type::ASSIGNMENT) {
auto assignment = parseAssignment();
eatToken(Token::Type::SEMICOLON);
return assignment;
} else if (offset < tokens.size() && tokens[offset]->getType() == Token::Type::IF_TOKEN) {
return parseBranch();
} else {
auto expr = parseExpression();
eatToken(Token::Type::SEMICOLON);
return expr;
}
}
/*
* term ((PLUS | MINUS) term)*
*/
ExpressionNode *Parser::parseExpression() {
ExpressionNode *node = parseTerm();
while (offset < tokens.size() && tokens[offset]->getType() == Token::Type::OPERATOR) {
auto op = dynamic_cast<Operator *>(tokens[offset]);
if (op->getPrecedence() != 1) break;
offset++;
auto term = parseTerm();
node = new OperatorNode(op, node, term);
}
return node;
}
/*
* factor ((MUL|DIV) factor)*
*/
ExpressionNode *Parser::parseTerm() {
ExpressionNode *node = parseFactor();
while (offset < tokens.size() && tokens[offset]->getType() == Token::Type::OPERATOR) {
auto op = dynamic_cast<Operator *>(tokens[offset]);
if (op->getPrecedence() != 2) break;
offset++;
auto term = parseFactor();
node = new OperatorNode(op, node, term);
}
return node;
}
/*
* value (^ value)*
*/
ExpressionNode *Parser::parseFactor() {
ExpressionNode *node = parseValue();
while (offset < tokens.size() && tokens[offset]->getType() == Token::Type::OPERATOR) {
auto op = dynamic_cast<Operator *>(tokens[offset]);
if (op->getPrecedence() != 3) break;
offset++;
auto term = parseValue();
node = new OperatorNode(op, node, term);
}
return node;
}
/*
* PLUS expression | MINUS expression | NUMBER | LPAREN expression RPAREN | variable
*/
ExpressionNode *Parser::parseValue() {
switch (tokens[offset]->getType()) {
case Token::Type::OPERATOR: { // unary operator
auto op = eatOperator(1);
auto value = parseExpression();
return new OperatorNode(op, new ConstantNode(new Number(0)), value);
}
case Token::Type::NUMBER: {
auto token = dynamic_cast<Number *>(tokens[offset]);
eatToken(Token::Type::NUMBER);
return new ConstantNode(token);
}
case Token::Type::OPENING_BRACKET: {
eatToken(Token::Type::OPENING_BRACKET);
auto expression = parseExpression();
eatToken(Token::Type::CLOSING_BRACKET);
return expression;
}
case Token::Type::IDENTIFIER: {
return parseVariable();
}
default: {
string what = "Syntax error at offset ";
what.append(to_string(offset));
what.append(": unexpected token, expected tokens #");
what.append(to_string(Token::Type::OPERATOR));
what.append(", ").append(to_string(Token::Type::OPENING_BRACKET));
what.append(" or ").append(to_string(Token::Type::IDENTIFIER));
throw invalid_argument(what);
}
}
}
/*
* ID
*/
VariableNode *Parser::parseVariable() {
string name = dynamic_cast<Identifier *>(tokens[offset])->getName();
offset++;
return new VariableNode(name);
}
/*
* variable ASSIGN expression
*/
AssignmentNode *Parser::parseAssignment() {
auto variable = parseVariable();
eatToken(Token::Type::ASSIGNMENT);
auto value = parseExpression();
return new AssignmentNode(variable, value);
}
/*
* IF LPAREN expression RPAREN statement_block (ELSE statement_block)
*/
BranchNode *Parser::parseBranch() {
eatToken(Token::Type::IF_TOKEN);
eatToken(Token::Type::OPENING_BRACKET);
auto condition = parseExpression();
eatToken(Token::Type::CLOSING_BRACKET);
auto ifTrue = parseStatementBlock();
StatementListNode *ifFalse = nullptr;
if (offset < tokens.size() && tokens[offset]->getType() == Token::Type::ELSE_TOKEN) {
eatToken(Token::Type::ELSE_TOKEN);
ifFalse = parseStatementBlock();
}
return new BranchNode(condition, ifTrue, ifFalse);
}