Repository navigation
Expand file tree
/
Copy pathparser.cpp
More file actions
169 lines (134 loc) · 3.39 KB
/
Copy pathparser.cpp
File metadata and controls
169 lines (134 loc) · 3.39 KB
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
#include "parser.h"
#include "lox.h"
std::unique_ptr<expr> parser::parse() {
try {
return expression();
} catch (const parse_error&) {
return nullptr;
}
}
std::unique_ptr<expr> parser::expression()
{
return equality();
}
std::unique_ptr<expr> parser::equality()
{
auto expr = comparison();
while (match({BANG_EQUAL, EQUAL_EQUAL})) {
token op = previous();
auto right = comparison();
expr = std::make_unique<binary>(std::move(expr), op, std::move(right));
}
return expr;
}
std::unique_ptr<expr> parser::comparison()
{
auto expr = term();
while (match({GREATER, GREATER_EQUAL, LESS, LESS_EQUAL})) {
token op = previous();
auto right = term();
expr = std::make_unique<binary>(std::move(expr), op, std::move(right));
}
return expr;
}
std::unique_ptr<expr> parser::term()
{
auto expr = factor();
while (match({MINUS, PLUS})) {
token op = previous();
auto right = factor();
expr = std::make_unique<binary>(std::move(expr), op, std::move(right));
}
return expr;
}
std::unique_ptr<expr> parser::factor()
{
auto expr = unary();
while (match({SLASH, STAR})) {
token op = previous();
auto right = unary();
expr = std::make_unique<binary>(std::move(expr), op, std::move(right));
}
return expr;
}
std::unique_ptr<expr> parser::unary()
{
while (match({BANG, MINUS})) {
token op = previous();
auto right = unary();
return std::make_unique<::unary>(op, std::move(right));
}
return primary();
}
std::unique_ptr<expr> parser::primary() {
if (match({FALSE})) return std::make_unique<literal>("false");
if (match({TRUE})) return std::make_unique<literal>("true");
if (match({NIL})) return std::make_unique<literal>("null");
if (match({NUMBER, STRING})) {
return std::make_unique<literal>(previous().literal);
}
if (match({LEFT_PAREN})) {
std::unique_ptr<expr> expr = expression();
consume(RIGHT_PAREN, "Expect ')' after expression.");
return std::make_unique<grouping>(std::move(expr));
}
throw error(peek(), "Expect expression.");
}
bool parser::match(std::initializer_list<token_type> types)
{
for (token_type type : types) {
if (check(type)) {
advance();
return true;
}
}
return false;
}
token parser::consume(token_type type, std::string message) {
if (check(type)) return advance();
throw error(peek(), message);
}
bool parser::check(token_type type)
{
if (is_at_end()) return false;
return peek().type == type;
}
token parser::advance()
{
if (!is_at_end()) current++;
return previous();
}
bool parser::is_at_end() const
{
return peek().type == END_OF_FILE;
}
token parser::peek() const
{
return tokens[current];
}
token parser::previous() const
{
return tokens[current - 1];
}
parser::parse_error parser::error(const token& token, const std::string& message) {
::error(token, message);
return {};
}
void parser::synchronize() {
advance();
while (!is_at_end()) {
if (previous().type == SEMICOLON) return;
switch (peek().type) {
case CLASS:
case FUN:
case VAR:
case FOR:
case IF:
case WHILE:
case PRINT:
case RETURN:
return;
}
advance();
}
}