From 5c7df4f7b09d138df58f720260306afdf0f6713a Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sat, 7 Nov 2020 21:55:45 +0100 Subject: Make "add" result --- cpp.cpp | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) (limited to 'cpp.cpp') diff --git a/cpp.cpp b/cpp.cpp index b884af4..52cd5f2 100644 --- a/cpp.cpp +++ b/cpp.cpp @@ -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 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(index_t)>> CPP::getNodeEvalMap() +std::unordered_map> CPP::getNodeEvalMap() { return { - { "primary-expression", [&](index_t index) -> std::shared_ptr + { "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 + { "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 + { "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 + { "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 + { "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 + { "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 + { "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(getValue(index, 0))}; + FlowGraph::Data value1 {std::any_cast(getValue(index, 1))}; - //return std::make_shared(FlowGraph::BinaryOperationType::Add, destination, value0, value1); - return getValue(index, 0); // TODO + return std::make_shared(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 + { "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 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(data)); + mValues.push_back(data); } else { - mValues.push_back(std::make_shared(nullptr)); + mValues.push_back(std::any{}); } } -- cgit v1.2.3