-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionParser.cpp
More file actions
52 lines (41 loc) · 1.78 KB
/
Copy pathFunctionParser.cpp
File metadata and controls
52 lines (41 loc) · 1.78 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
#include "FunctionParser.hpp"
#include "tokenize/LexicalAnalyzer.hpp"
#include "tokenize/Token.hpp"
#include "ast/Expression.hpp"
#include <iostream>
std::unique_ptr<std::vector<std::shared_ptr<Token>>> FunctionParser::tokenize(const std::string &input) const {
LexicalAnalyzer lexicalAnalyzer;
return lexicalAnalyzer.parseToTokens(cleanFromSpaces(input));
}
std::shared_ptr<Expression>
FunctionParser::tokensToExpression(const std::unique_ptr<std::vector<std::shared_ptr<Token>>> &tokens) const {
auto astBuilder = ASTBuilder::getSelf();
return astBuilder->tokensToExpression(*tokens);
}
std::shared_ptr<Expression> FunctionParser::debugParse(const std::string &input) const {
std::unique_ptr<std::vector<std::shared_ptr<Token>>> tokens = tokenize(input);
debugOutput(tokens);
std::shared_ptr<Expression> expression = tokensToExpression(tokens);
std::cout << "Expression: " << expression->toString() << std::endl;
return expression;
}
long double FunctionParser::debugResolve(const std::string &input, std::map<std::string, long double> vars) const {
return debugParse(input)->resolve(vars);
}
void FunctionParser::debugOutput(const std::unique_ptr<std::vector<std::shared_ptr<Token>>> &tokens) const {
std::cout << "Tokens: [";
for (auto it = tokens->begin(); it != tokens->end(); ++it) {
if (it != tokens->begin()) {
std::cout << ',';
}
std::cout << (*it)->toString();
}
std::cout << "]" << std::endl;
}
std::string FunctionParser::cleanFromSpaces(const std::string &input) const {
std::string cleanString(input);
cleanString.erase(
std::remove_if(cleanString.begin(), cleanString.end(), [](unsigned char x) { return std::isspace(x); }),
cleanString.end());
return cleanString;
}