-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.cpp
More file actions
221 lines (197 loc) · 7.63 KB
/
Copy pathparser.cpp
File metadata and controls
221 lines (197 loc) · 7.63 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
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
221
#include "parser.h"
#include <stdexcept>
#include <utility> // For std::move
#include <iostream>
// AST Node Constructors
NumberExpr::NumberExpr(double v): value(v) {}
std::unique_ptr<Expr> NumberExpr::clone() const {
return std::make_unique<NumberExpr>(value);
}
VariableExpr::VariableExpr(const std::string& n): name(n) {}
std::unique_ptr<Expr> VariableExpr::clone() const {
return std::make_unique<VariableExpr>(name);
}
BinaryExpr::BinaryExpr(char o, std::unique_ptr<Expr> l, std::unique_ptr<Expr> r): op(o), left(std::move(l)), right(std::move(r)) {}
std::unique_ptr<Expr> BinaryExpr::clone() const {
return std::make_unique<BinaryExpr>(op, left->clone(), right->clone());
}
// Parser Implementation
Parser::Parser(std::vector<Token> toks): tokens(std::move(toks)) {
for(auto x: tokens){
std::cout << "-> " <<x.text << std::endl;
}
}
Token Parser::peek() {
return (pos < tokens.size()) ? tokens[pos] : Token{TokenType::END, ""};
}
Token Parser::get() { return (pos < tokens.size()) ? tokens[pos++] : Token {TokenType::END, ""}; }
std::unique_ptr<Expr> Parser::parseExpression() {
auto expr = parseTerm();
while(peek().type == TokenType::PLUS || peek().type == TokenType::MINUS ) {
char op = get().text[0];
auto rhs = parseTerm();
expr = std::make_unique<BinaryExpr>(op, std::move(expr), std::move(rhs));
}
return expr;
}
std::unique_ptr<Expr> Parser::parseTerm() {
auto expr = parseFactor();
while(peek().type == TokenType::MUL || peek().type == TokenType::DIV){
char op = get().text[0];
auto rhs = parseFactor();
expr = std::make_unique<BinaryExpr>(op, std::move(expr), std::move(rhs));
}
return expr;
}
std::unique_ptr<Expr> Parser::parseFactor(){
return parsePower();
}
std::unique_ptr<Expr> Parser::parsePower() {
auto base = parsePrimary();
if(peek().type == TokenType::POW){
get(); // consume ^
auto exponent = parseFactor();
return std::make_unique<BinaryExpr>('^', std::move(base), std::move(exponent));
}
return base;
}
std::unique_ptr<Expr> Parser::parsePrimary() {
Token tok = get();
if(tok.type == TokenType::NUMBER){
return std::make_unique<NumberExpr>(std::stod(tok.text));
}
if (tok.type == TokenType::IDENT){
return std::make_unique<VariableExpr>(tok.text);
}
if (tok.type == TokenType::LPAREN) {
auto expr = parseExpression();
if(peek().type != TokenType::RPAREN) {
throw std::runtime_error("Expected )");
}
get();
return expr;
}
throw std::runtime_error("Unexpected token in primary: " + tok.text);
}
// Expression Printer
std::string printExpr(const Expr* e) {
if(auto num = dynamic_cast<const NumberExpr*>(e)) {
return std::to_string((int)num->value);
}
if(auto var = dynamic_cast<const VariableExpr*>(e)) {
return var->name;
}
if(auto bin = dynamic_cast<const BinaryExpr*>(e)) {
std::string left = printExpr(bin->left.get());
std::string right = printExpr(bin->right.get());
switch(bin->op) {
case '+': case '-':
return "(" + left + " " + bin->op + " " + right + ")";
case '*': case '/':
return "(" + left + " " + bin->op + " " + right + ")";
case '^':
return left + "^" + right;
}
}
return "?";
}
std::unique_ptr<Expr> simplifyBinary(char op, std::unique_ptr<Expr> left, std::unique_ptr<Expr> right){
auto leftNum = dynamic_cast<NumberExpr*>(left.get());
auto rightNum = dynamic_cast<NumberExpr*>(right.get());
switch(op){
case '+':
if(leftNum && leftNum->value == 0) return (right);
if(rightNum && rightNum->value == 0) return (left);
// if(leftNum && leftNum->value == 1) return std::move(right);
// if(rightNum && rightNum->value == 1) return std::move(left);
if(leftNum && rightNum){
return std::make_unique<NumberExpr>(leftNum->value+rightNum->value);
}
break;
case '-':
if(leftNum && leftNum->value == 0) return (right);
if(rightNum && rightNum->value == 0) return (left);
// if(leftNum && leftNum->value == 1) return std::move(right);
// if(rightNum && rightNum->value == 1) return std::move(left);
if(leftNum && rightNum){
return std::make_unique<NumberExpr>(leftNum->value-rightNum->value);
}
break;
case '*':
if(leftNum && leftNum->value == 0) return std::make_unique<NumberExpr>(0);
if(rightNum && rightNum->value == 0) return std::make_unique<NumberExpr>(0);
if(leftNum && leftNum->value == 1) return (right);
if(rightNum && rightNum->value == 1) return (left);
if(leftNum && rightNum){
return std::make_unique<NumberExpr>(leftNum->value*rightNum->value);
}
break;
case '/':
if(leftNum && leftNum->value == 0) return std::make_unique<NumberExpr>(0);
if(rightNum && rightNum->value == 0) return std::make_unique<NumberExpr>(0);
if(leftNum && leftNum->value == 1) return (right);
if(rightNum && rightNum->value == 1) return (left);
if(leftNum && rightNum){
return std::make_unique<NumberExpr>(leftNum->value/rightNum->value);
}
break;
case '^':
if(rightNum && rightNum->value == 0) return std::make_unique<NumberExpr>(1);
if(rightNum && rightNum->value == 1) return (left);
break;
}
return std::make_unique<BinaryExpr>(op, std::move(left), std::move(right));
}
// Add this function after printExpr
std::unique_ptr<Expr> simplify(const Expr* e){
if(auto num = dynamic_cast<const NumberExpr*>(e)){
return std::make_unique<NumberExpr>(num->value);
}
if(auto var = dynamic_cast<const VariableExpr*>(e)){
return std::make_unique<VariableExpr>(var->name);
}
if(auto bin = dynamic_cast<const BinaryExpr*>(e)){
// switch(bin->op):
auto left = simplify(bin->left.get());
auto right = simplify(bin->right.get());
return simplifyBinary(bin->op, std::move(left), std::move(right));
}
return e->clone();
}
std::string printSimplified(const Expr* e) {
if (auto num = dynamic_cast<const NumberExpr*>(e)) {
if (num->value == (int)num->value) {
return std::to_string((int)num->value);
}
return std::to_string(num->value);
}
if (auto var = dynamic_cast<const VariableExpr*>(e)) {
return var->name;
}
if (auto bin = dynamic_cast<const BinaryExpr*>(e)) {
std::string left = printSimplified(bin->left.get());
std::string right = printSimplified(bin->right.get());
switch(bin->op) {
case '+':
return left + " + " + right;
case '-':
return left + " - " + right;
case '*':
// Special formatting for multiplication
if (auto leftNum = dynamic_cast<const NumberExpr*>(bin->left.get())) {
if (leftNum->value == 1) return right;
return std::to_string((int)leftNum->value) + "*" + right;
}
return left + "*" + right;
case '/':
return left + "/" + right;
case '^':
return left + "^" + right;
}
}
return "?";
}
std::string simplifyAndPrint(const Expr* e) {
auto simplified = simplify(e);
return printSimplified(simplified.get());
}