-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
152 lines (119 loc) · 4.28 KB
/
parser.cpp
File metadata and controls
152 lines (119 loc) · 4.28 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
#include "parser.hpp"
#include "exception.cpp"
bool Parser::current_type_is(TokenType type) {
return tokens[currentIndex].get_type() == type;
}
bool Parser::current_type_in(std::vector<TokenType> tokenTypes) {
return std::count(tokenTypes.begin(), tokenTypes.end(), tokens[currentIndex].get_type()) == 1;
}
TokenValue Parser::get_current_value() {
return tokens[currentIndex].get_value();
}
Token& Parser::current() {
return tokens[currentIndex];
}
void Parser::advance() {
if (current().get_type() != TokenType::_EOF) {
++currentIndex;
}
}
void Parser::eat(TokenType tokenType) {
if (current().get_type() == tokenType) {
advance();
} else {
throw SyntaxError("Invalid Syntax");
}
}
std::unique_ptr<ASTNode> Parser::parse_function() {
if (current_type_is(TokenType::IDENTIFIER)) {
std::string functionIdentifier = std::get<std::string>(get_current_value());
eat(TokenType::IDENTIFIER);
eat(TokenType::LPAREN);
std::string parameter = std::get<std::string>(get_current_value());
eat(TokenType::IDENTIFIER);
eat(TokenType::RPAREN);
eat(TokenType::EQUAL);
std::unique_ptr<ASTNode> functionBody = parse_expression();
return std::make_unique<FunctionNode>(functionIdentifier, parameter, std::move(functionBody));
}
}
std::unique_ptr<ASTNode> Parser::parse_expression() {
// Parse the first term of the expression
std::unique_ptr<ASTNode> firstTerm = parse_term();
// Iteratively parse the chained terms to ensure left associativity
return parse_rest_expression(std::move(firstTerm));
}
std::unique_ptr<ASTNode> Parser::parse_rest_expression(std::unique_ptr<ASTNode> firstTerm) {
while (current_type_is(TokenType::PLUS) || current_type_is(TokenType::MINUS)) {
char op;
if (current_type_is(TokenType::PLUS)) {
op = '+';
eat(TokenType::PLUS);
} else if (current_type_is(TokenType::MINUS)) {
op = '-';
eat(TokenType::MINUS);
}
std::unique_ptr<ASTNode> chainedTerm = parse_term();
firstTerm = std::make_unique<BinaryOpNode>(op, std::move(firstTerm), std::move(chainedTerm));
}
return firstTerm;
}
std::unique_ptr<ASTNode> Parser::parse_term() {
std::unique_ptr<ASTNode> firstFactor = parse_unary();
return parse_rest_term(std::move(firstFactor));
}
std::unique_ptr<ASTNode> Parser::parse_rest_term(std::unique_ptr<ASTNode> firstFactor) {
while (current_type_is(TokenType::MUL) || current_type_is(TokenType::DIV)) {
char op;
if (current_type_is(TokenType::MUL)) {
op = '*';
eat(TokenType::MUL);
} else if (current_type_is(TokenType::DIV)) {
op = '/';
eat(TokenType::DIV);
}
std::unique_ptr<ASTNode> chainedFactor = parse_unary();
firstFactor = std::make_unique<BinaryOpNode>(op, std::move(firstFactor), std::move(chainedFactor));
}
return firstFactor;
}
std::unique_ptr<ASTNode> Parser::parse_unary() {
if (current_type_is(TokenType::MINUS)) {
eat(TokenType::MINUS);
char op = '-';
return std::make_unique<UnaryOpNode>(op, parse_power());
}
return parse_power();
}
std::unique_ptr<ASTNode> Parser::parse_power() {
std::unique_ptr<ASTNode> factor = parse_factor();
if (current_type_is(TokenType::POW)) {
eat(TokenType::POW);
return std::make_unique<BinaryOpNode>('^', std::move(factor), parse_power());
}
return factor;
}
std::unique_ptr<ASTNode> Parser::parse_factor() {
if (current_type_is(TokenType::LPAREN)) {
eat(TokenType::LPAREN);
std::unique_ptr<ASTNode> exprNode = parse_expression();
eat(TokenType::RPAREN);
return exprNode;
} else if (current_type_is(TokenType::NUMBER)) {
auto numNode = std::make_unique<NumberNode>(std::get<double>(get_current_value()));
eat(TokenType::NUMBER);
return numNode;
} else if (current_type_is(TokenType::IDENTIFIER)) {
auto identifierNode = std::make_unique<IdentifierNode>(std::get<std::string>(get_current_value()));
eat(TokenType::IDENTIFIER);
return identifierNode;
} else if (current_type_in(mathFunctions)) {
// cleaner way to do this
std::string functionName = std::get<std::string>(get_current_value());
eat(tokens[currentIndex].get_type());
eat(TokenType::LPAREN);
std::unique_ptr<ASTNode> functionArg = parse_expression();
auto inlineFunctionNode = std::make_unique<InlineFunctionNode>(functionName, std::move(functionArg));
return inlineFunctionNode;
}
}