diff options
author | Roland Reichwein <mail@reichwein.it> | 2020-11-07 21:55:45 +0100 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2020-11-07 21:55:45 +0100 |
commit | 5c7df4f7b09d138df58f720260306afdf0f6713a (patch) | |
tree | 4e81ef887606486d4729204d629a5ad029b14256 | |
parent | e7d51fff32ea247fd35b56fc7cf5ce06df5dc6bf (diff) |
Make "add" result
-rw-r--r-- | cpp.cpp | 45 | ||||
-rw-r--r-- | cpp.h | 8 | ||||
-rw-r--r-- | flowgraph/node.h | 4 |
3 files changed, 29 insertions, 28 deletions
@@ -313,7 +313,7 @@ bool CPP::childTypesOfChildMatch(index_t index, index_t child_index, const std:: return childTypesOfNodeMatch(m_nodes[index].child_ids[child_index], pattern); } -std::shared_ptr<std::any> CPP::getValue(index_t node_id, index_t child_index) +std::any CPP::getValue(index_t node_id, index_t child_index) { size_t num_values_on_top {m_nodes[node_id].child_ids.size()}; @@ -352,71 +352,70 @@ std::string CPP::ruleString(index_t node_id) return result; } -std::unordered_map<std::string, std::function<std::shared_ptr<std::any>(index_t)>> CPP::getNodeEvalMap() +std::unordered_map<std::string, std::function<std::any(index_t)>> CPP::getNodeEvalMap() { return { - { "primary-expression", [&](index_t index) -> std::shared_ptr<std::any> + { "primary-expression", [&](index_t index) -> std::any { if (childTypesOfNodeMatch(index, {"literal"})) return getValue(index, 0); throw std::runtime_error("ICE: Unsupported childs: "s + ruleString(index)); // TODO } }, - { "postfix-expression", [&](index_t index) -> std::shared_ptr<std::any> + { "postfix-expression", [&](index_t index) -> std::any { - if (childTypesOfNodeMatch(index, {"primary-expression", ""}) && !getValue(index, 1)) + if (childTypesOfNodeMatch(index, {"primary-expression", ""}) && !getValue(index, 1).has_value()) return getValue(index, 0); throw std::runtime_error("ICE: Unsupported childs: "s + ruleString(index)); // TODO } }, - { "unary-expression", [&](index_t index) -> std::shared_ptr<std::any> + { "unary-expression", [&](index_t index) -> std::any { if (childTypesOfNodeMatch(index, {"postfix-expression"})) return getValue(index, 0); throw std::runtime_error("ICE: Unsupported childs: "s + ruleString(index)); // TODO } }, - { "cast-expression", [&](index_t index) -> std::shared_ptr<std::any> + { "cast-expression", [&](index_t index) -> std::any { if (childTypesOfNodeMatch(index, {"unary-expression"})) return getValue(index, 0); throw std::runtime_error("ICE: Unsupported childs: "s + ruleString(index)); // TODO } }, - { "pm-expression", [&](index_t index) -> std::shared_ptr<std::any> + { "pm-expression", [&](index_t index) -> std::any { - if (childTypesOfNodeMatch(index, {"cast-expression", ""}) && !getValue(index, 1)) + if (childTypesOfNodeMatch(index, {"cast-expression", ""}) && !getValue(index, 1).has_value()) return getValue(index, 0); throw std::runtime_error("ICE: Unsupported childs: "s + ruleString(index)); // TODO } }, - { "multiplicative-expression", [&](index_t index) -> std::shared_ptr<std::any> + { "multiplicative-expression", [&](index_t index) -> std::any { - if (childTypesOfNodeMatch(index, {"pm-expression", ""}) && !getValue(index, 1)) + if (childTypesOfNodeMatch(index, {"pm-expression", ""}) && !getValue(index, 1).has_value()) return getValue(index, 0); throw std::runtime_error("ICE: Unsupported childs: "s + ruleString(index)); // TODO } }, - { "additive-expression", [&](index_t index) -> std::shared_ptr<std::any> + { "additive-expression", [&](index_t index) -> std::any { if (childTypesOfNodeMatch(index, {"multiplicative-expression", "additive-expression-EXT"}) && childTypesOfChildMatch(index, 1, {"+", "", ""})) { - FlowGraph::LocalScope scope; + FlowGraph::LocalScope scope; // TODO: move to context! FlowGraph::Data destination{FlowGraph::MakeTemporaryInt(scope)}; - auto value0 {getValue(index, 0)}; - auto value1 {getValue(index, 1)}; + FlowGraph::Data value0 {std::any_cast<FlowGraph::Data>(getValue(index, 0))}; + FlowGraph::Data value1 {std::any_cast<FlowGraph::Data>(getValue(index, 1))}; - //return std::make_shared<FlowGraph::BinaryOperation>(FlowGraph::BinaryOperationType::Add, destination, value0, value1); - return getValue(index, 0); // TODO + return std::make_shared<FlowGraph::BinaryOperation>(FlowGraph::BinaryOperationType::Add, destination, value0, value1); } throw std::runtime_error("ICE: Unsupported childs: "s + ruleString(index)); // TODO } }, - { "additive-expression-EXT", [&](index_t index) -> std::shared_ptr<std::any> + { "additive-expression-EXT", [&](index_t index) -> std::any { - if (childTypesOfNodeMatch(index, {"+", "multiplicative-expression", ""}) && !getValue(index, 2)) { + if (childTypesOfNodeMatch(index, {"+", "multiplicative-expression", ""}) && !getValue(index, 2).has_value()) { return getValue(index, 1); } else if (childTypesOfNodeMatch(index, {})) { - return nullptr; + return {}; } throw std::runtime_error("ICE: Unsupported childs: "s + ruleString(index)); // TODO } @@ -434,7 +433,7 @@ void CPP::getValueOfNode(index_t index) throw std::runtime_error("ICE: Expected num_childs elements on Values stack at "s + locationOfNode(index)); auto function_it{node_eval_map.find(m_nodes[index].type)}; - std::shared_ptr<std::any> result {nullptr}; + std::any result; if (function_it != node_eval_map.end()) { result = function_it->second(index); } else { @@ -453,9 +452,9 @@ void CPP::getValueOfToken(index_t index) if (m_tokens[index].type == "literal") { // TODO: also support other types, different from Int FlowGraph::Data data{FlowGraph::MakeConstantInt(stoi(m_tokens[index].value))}; - mValues.push_back(std::make_shared<std::any>(data)); + mValues.push_back(data); } else { - mValues.push_back(std::make_shared<std::any>(nullptr)); + mValues.push_back(std::any{}); } } @@ -54,13 +54,13 @@ private: bool childTypesOfNodeMatch(index_t index, const std::vector<std::string>& pattern) const; ///< returns true iff specified type list matches; "" -> don't care bool childTypesOfChildMatch(index_t index, index_t child_index, const std::vector<std::string>& pattern) const; ///< returns true iff specified type list matches in specified child; "" -> don't care - std::deque<std::shared_ptr<std::any>> mValues; // values stack during phase 7.c - std::shared_ptr<std::any> getValue(index_t node_id, index_t child_id); + std::deque<std::any> mValues; // values stack during phase 7.c + std::any getValue(index_t node_id, index_t child_id); std::string getType(index_t node_id, index_t child_index); std::string ruleString(index_t node_id); - std::unordered_map<std::string, std::function<std::shared_ptr<std::any>(index_t)>> getNodeEvalMap(); - std::unordered_map<std::string, std::function<std::shared_ptr<std::any>(index_t)>> node_eval_map; + std::unordered_map<std::string, std::function<std::any(index_t)>> getNodeEvalMap(); + std::unordered_map<std::string, std::function<std::any(index_t)>> node_eval_map; CPPContext mContext; void getValueOfToken(index_t index); diff --git a/flowgraph/node.h b/flowgraph/node.h index c1b7380..89f6088 100644 --- a/flowgraph/node.h +++ b/flowgraph/node.h @@ -145,7 +145,9 @@ namespace FlowGraph { class BinaryOperation: public Node { public: - BinaryOperation(BinaryOperationType type, Data& destination, Data& source0, Data& source1): m_type(type), m_destination(destination), m_source0(source0), m_source1(source1) {} + BinaryOperation(BinaryOperationType type, Data& destination, Data& source0, Data& source1): + m_type(type), m_destination(destination), m_source0(source0), m_source1(source1) + {} private: BinaryOperationType m_type; Data m_destination; |