-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.h
146 lines (117 loc) · 3.39 KB
/
Parser.h
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
//
// Created by User on 7/10/2024.
//
#ifndef PARSER_H
#define PARSER_H
#include <memory>
#include <vector>
#include "AbstractSyntaxTree.h"
#include "Lexer.h"
class Parser {
public:
explicit Parser(Lexer lexer) : lexer(std::move(lexer)) {
advance();
}
ExpressionPtr parse();
private:
Lexer lexer;
Token currentToken;
void advance() {
currentToken = lexer.getNextToken();
}
ExpressionPtr expression();
ExpressionPtr term();
ExpressionPtr factor();
ExpressionPtr functionDef();
};
inline ExpressionPtr Parser::parse() {
auto root = std::make_shared<StatementListExpression>();
while (currentToken.type != END) {
if (currentToken.type == DEF) {
root->addStatement(functionDef());
} else {
root->addStatement(expression());
}
if (currentToken.type == SEMICOLON) {
advance();
}
}
return root;
}
inline ExpressionPtr Parser::expression() {
ExpressionPtr node = term();
while (currentToken.type == PLUS || currentToken.type == MINUS) {
char op = currentToken.value[0];
advance();
node = std::make_shared<BinaryExpression>(op, node, term());
}
return node;
}
inline ExpressionPtr Parser::term() {
ExpressionPtr node = factor();
while (currentToken.type == STAR || currentToken.type == SLASH) {
char op = currentToken.value[0];
advance();
node = std::make_shared<BinaryExpression>(op, node, factor());
}
return node;
}
inline ExpressionPtr Parser::factor() {
if (currentToken.type == NUMBER) {
double value = std::stod(currentToken.value);
advance();
return std::make_shared<Number>(value);
}
if (currentToken.type == IDENTIFIER) {
std::string name = currentToken.value;
advance();
if (currentToken.type == LPAREN) {
advance();
std::vector<ExpressionPtr> args;
if (currentToken.type != RPAREN) {
do {
args.push_back(expression());
if (currentToken.type == COMMA) {
advance();
}
} while (currentToken.type != RPAREN);
}
advance();
return std::make_shared<MethodCallExpression>(name, args);
}
if (currentToken.type == ASSIGN) {
advance();
return std::make_shared<AssignmentExpression>(name, expression());
}
return std::make_shared<VariableExpression>(name);
}
if (currentToken.type == LPAREN) {
advance();
ExpressionPtr node = expression();
advance();
return node;
}
throw std::runtime_error("Unexpected token");
}
inline ExpressionPtr Parser::functionDef() {
advance(); // 'def'
std::string name = currentToken.value;
advance(); // name
std::vector<std::string> params;
advance(); // lparen
while (currentToken.type != RPAREN) {
params.push_back(currentToken.value);
advance();
if (currentToken.type == COMMA) {
advance();
}
}
advance(); // rparen
advance(); // '{'
ExpressionPtr body = expression();
advance(); // '}'
auto funcDef = std::make_shared<FunctionDefExpression>(name, params, body);
userFunctions[name] = funcDef;
return funcDef;
}
#endif //PARSER_H