-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
228 lines (204 loc) · 8.1 KB
/
parser.cpp
File metadata and controls
228 lines (204 loc) · 8.1 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
222
223
224
225
226
227
228
#include "parser.h"
#include <cctype>
#include <algorithm>
#include <stack>
#include <stdexcept>
#include <map>
std::vector<Token> tokenize(const std::string& expr) {
std::vector<Token> tokens;
size_t i = 0;
while (i < expr.length()) {
char c = expr[i];
if (isspace(c)) { ++i; continue; }
if (isdigit(c) || c == '.') {
std::string num;
while (i < expr.length() && (isdigit(expr[i]) || expr[i] == '.' ||
expr[i] == 'e' || expr[i] == 'E' ||
(i > 0 && (expr[i-1] == 'e' || expr[i-1] == 'E') &&
(expr[i] == '+' || expr[i] == '-')))) {
num += expr[i++];
}
tokens.emplace_back(TokenType::Number, num);
}
else if (isalpha(c)) {
std::string name;
while (i < expr.length() && isalpha(expr[i])) name += expr[i++];
if (name == "sin" || name == "cos" || name == "log" || name == "ln" ||
name == "exp" || name == "sqrt" || name == "tan" || name == "abs")
tokens.emplace_back(TokenType::Function, name);
else
tokens.emplace_back(TokenType::Variable, name);
}
else if (std::string("+-*/^").find(c) != std::string::npos) {
if ((c == '-' || c == '+') && (i == 0 ||
(i > 0 && (expr[i-1] == '(' ||
std::string("+-*/^").find(expr[i-1]) != std::string::npos)))) {
if (c == '-') {
tokens.emplace_back(TokenType::Number, "0");
tokens.emplace_back(TokenType::Operator, std::string(1, c));
}
} else {
tokens.emplace_back(TokenType::Operator, std::string(1, c));
}
++i;
}
else if (c == '(') {
tokens.emplace_back(TokenType::LeftParen, "(");
++i;
}
else if (c == ')') {
tokens.emplace_back(TokenType::RightParen, ")");
++i;
}
else {
throw std::runtime_error(std::string("Invalid character: ") + c);
}
}
return tokens;
}
int precedence(const std::string& op) {
if (op == "+" || op == "-") return 1;
if (op == "*" || op == "/") return 2;
if (op == "^") return 3;
return 0;
}
bool is_right_associative(const std::string& op) {
return op == "^";
}
std::vector<Token> to_postfix(const std::vector<Token>& tokens) {
std::vector<Token> output;
std::stack<Token> stack;
for (const auto& token : tokens) {
if (token.type == TokenType::Number || token.type == TokenType::Variable) {
output.push_back(token);
}
else if (token.type == TokenType::Function) {
stack.push(token);
}
else if (token.type == TokenType::Operator) {
while (!stack.empty() && stack.top().type == TokenType::Operator &&
((precedence(stack.top().value) > precedence(token.value)) ||
(precedence(stack.top().value) == precedence(token.value) &&
!is_right_associative(token.value)))) {
output.push_back(stack.top());
stack.pop();
}
stack.push(token);
}
else if (token.type == TokenType::LeftParen) {
stack.push(token);
}
else if (token.type == TokenType::RightParen) {
while (!stack.empty() && stack.top().type != TokenType::LeftParen) {
output.push_back(stack.top());
stack.pop();
}
if (stack.empty()) throw std::runtime_error("Mismatched parentheses");
stack.pop();
if (!stack.empty() && stack.top().type == TokenType::Function) {
output.push_back(stack.top());
stack.pop();
}
}
}
while (!stack.empty()) {
if (stack.top().type == TokenType::LeftParen)
throw std::runtime_error("Mismatched parentheses");
output.push_back(stack.top());
stack.pop();
}
return output;
}
std::vector<std::string> split_expression(const std::string& expr) {
std::vector<std::string> terms;
std::string current;
int paren = 0;
for (size_t i = 0; i < expr.length(); ++i) {
char c = expr[i];
if (c == '(') paren++;
else if (c == ')') paren--;
if ((c == '+' || c == '-') && paren == 0 && i > 0) {
char prev = expr[i-1];
bool is_binary = !(prev == '+' || prev == '-' || prev == '*' ||
prev == '/' || prev == '^' || prev == '(' ||
prev == 'e' || prev == 'E');
if (is_binary) {
if (!current.empty()) {
terms.push_back(current);
current.clear();
}
if (c == '-') current += c;
continue;
}
}
current += c;
}
if (!current.empty()) terms.push_back(current);
return terms;
}
CompiledExpr compile_expression(const std::vector<Token>& postfix, int dims,
const std::vector<std::string>* var_names) {
CompiledExpr compiled;
compiled.expr_length = postfix.size();
compiled.dimensions = dims;
// Build variable name to index mapping
std::map<std::string, int> var_map;
if (var_names != nullptr) {
for (size_t i = 0; i < var_names->size() && i < (size_t)dims; ++i) {
var_map[(*var_names)[i]] = static_cast<int>(i);
}
}
for (const auto& token : postfix) {
compiled.types.push_back(token.type);
if (token.type == TokenType::Number) {
compiled.constants.push_back(std::stof(token.value));
compiled.var_indices.push_back(-1);
compiled.op_codes.push_back(-1);
}
else if (token.type == TokenType::Variable) {
compiled.constants.push_back(0.0f);
int idx = -1;
// First try using the variable map
if (var_map.count(token.value)) {
idx = var_map[token.value];
}
// Fallback to old single-character mapping
else if (token.value.length() == 1) {
char c = token.value[0];
if (c >= 'a' && c <= 'z') idx = c - 'a';
else if (c >= 'A' && c <= 'Z') idx = c - 'A';
}
compiled.var_indices.push_back(idx);
compiled.op_codes.push_back(-1);
}
else if (token.type == TokenType::Operator) {
compiled.constants.push_back(0.0f);
compiled.var_indices.push_back(-1);
if (token.value == "+") compiled.op_codes.push_back(0);
else if (token.value == "-") compiled.op_codes.push_back(1);
else if (token.value == "*") compiled.op_codes.push_back(2);
else if (token.value == "/") compiled.op_codes.push_back(3);
else if (token.value == "^") compiled.op_codes.push_back(4);
else compiled.op_codes.push_back(-1);
}
else if (token.type == TokenType::Function) {
compiled.constants.push_back(0.0f);
compiled.var_indices.push_back(-1);
if (token.value == "sin") compiled.op_codes.push_back(10);
else if (token.value == "cos") compiled.op_codes.push_back(11);
else if (token.value == "log") compiled.op_codes.push_back(12);
else if (token.value == "ln") compiled.op_codes.push_back(13);
else if (token.value == "exp") compiled.op_codes.push_back(14);
else if (token.value == "sqrt") compiled.op_codes.push_back(15);
else if (token.value == "tan") compiled.op_codes.push_back(16);
else if (token.value == "abs") compiled.op_codes.push_back(17);
else compiled.op_codes.push_back(-1);
}
else {
compiled.constants.push_back(0.0f);
compiled.var_indices.push_back(-1);
compiled.op_codes.push_back(-1);
}
}
return compiled;
}