-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.cpp
More file actions
35 lines (28 loc) · 840 Bytes
/
Copy pathparser.cpp
File metadata and controls
35 lines (28 loc) · 840 Bytes
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
#include "parser.hpp"
#include <memory>
#include "double_node.hpp"
#include "iostream"
#include "operator_node.hpp"
#include "utils.hpp"
using namespace std;
vector<unique_ptr<INode>> Parser::parse(const vector<string> &tokens) const {
vector<unique_ptr<INode>> nodes{};
for (const auto &token : tokens) {
if (IsNumber(token)) {
auto value = ParseDouble(token);
nodes.push_back(make_unique<DoubleNode>(value));
continue;
}
if (IsOperator(token)) {
auto op = operations_.at(token).get();
nodes.push_back(make_unique<OperatorNode>(op));
continue;
}
std::cerr << "[ERROR] unexpected token " << token << std::endl;
exit(-1); // TODO: throw exception
}
return nodes;
}
bool Parser::IsOperator(const std::string &token) const {
return operations_.count(token) == 1;
}