blob: 6f47890832660e9429ffa3a981e3a162ed576c2c (
plain)
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
|
#include <iostream>
#include "parser.hpp"
Parser::Parser(const std::vector<Token>& tokens) : tokens_(tokens), position_(0) {}
std::unique_ptr<Program> Parser::parse() {
auto program = std::make_unique<Program>();
while (peek().type != TokenType::END_OF_FILE) {
try {
auto statement = parseStatement();
program->declarations.push_back(std::move(statement));
if (peek().type == TokenType::SEMICOLON) {
advance();
}
}
catch (const std::exception& e) {
std::cerr << " Parser error: " << e.what() << std::endl;
break;
}
}
return program;
}
std::unique_ptr<ASTNode> Parser::parseStatement() {
switch(peek().type) {
case TokenType::INT:
case TokenType::STRING:
{
return parseDeclaration();
break;
}
default:
{
error("Unexpected statement");
return nullptr;
}
}
}
std::unique_ptr<ASTNode> Parser::parseExpression() {
return parseAddSub();
}
std::unique_ptr<ASTNode> Parser::parseDeclaration() {
std::string type;
switch(peek().type) {
case TokenType::INT:
{
type = "int";
break;
}
case TokenType::STRING:
{
type = "string";
break;
}
default:
{
error("Unexpected declaration: Invalid type");
return nullptr;
}
}
advance();
std::string name = peek().value;
advance();
if (peek().type == TokenType::ASSIGN) {
advance();
auto value = parseExpression();
return std::make_unique<Declaration>(type, name, std::move(value));
}
else if (peek().type == TokenType::SEMICOLON) {
return std::make_unique<Declaration>(type, name);
}
else {
error("Invalid declaration");
return nullptr;
}
}
std::unique_ptr<ASTNode> Parser::parseAssignment() {
std::string name = peek().value;
advance();
advance(); // consume =
auto value = parseExpression();
return std::make_unique<Assignment>(name, std::move(value));
}
std::unique_ptr<ASTNode> Parser::parseAddSub() {
auto left = parseMulDiv();
while (peek().type == TokenType::PLUS || peek().type == TokenType::MINUS) {
std::string op = peek().value;
advance();
auto right = parseMulDiv();
left = std::make_unique<BinaryOp>(std::move(left), op, std::move(right));
}
return left;
}
std::unique_ptr<ASTNode> Parser::parseMulDiv() {
auto left = parsePrimary();
while (peek().type == TokenType::MULTIPLY || peek().type == TokenType::DIVIDE) {
std::string op = peek().value;
advance();
auto right = parsePrimary();
left = std::make_unique<BinaryOp>(std::move(left), op, std::move(right));
}
return left;
}
std::unique_ptr<ASTNode> Parser::parsePrimary() {
switch(peek().type) {
case TokenType::NUMBER:
{
double value = std::stod(peek().value);
advance();
return std::make_unique<NumberLiteral>(value);
}
case TokenType::IDENTIFIER:
{
std::string name = peek().value;
advance();
return std::make_unique<Identifier>(name);
}
case TokenType::STRING:
{
std::string value = peek().value;
advance();
return std::make_unique<StringLiteral>(value);
}
default:
{
error("Unknown keyword");
return nullptr;
}
}
}
Token Parser::advance() {
return tokens_[position_++];
}
Token Parser::peek() {
return tokens_[position_];
}
bool Parser::isAtEnd() {
return position_ >= tokens_.size();
}
void Parser::error(std::string s) {
}
|