From bbf81cd21bbe36fe7e613e5411b9d366d8411921 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Thu, 12 Nov 2020 19:02:46 +0100 Subject: Adjust rules to new bnf --- cpp.cpp | 86 ++++++++++++++++++++---------------------------------- tests/test-cpp.cpp | 2 +- 2 files changed, 32 insertions(+), 56 deletions(-) diff --git a/cpp.cpp b/cpp.cpp index ce4a7c5..0b078ee 100644 --- a/cpp.cpp +++ b/cpp.cpp @@ -368,7 +368,7 @@ std::unordered_map> CPP::getNodeEv }, { "postfix-expression", [&](index_t index) -> std::any { - if (childTypesOfNodeMatch(index, {"primary-expression", ""}) && !getValue(index, 1).has_value()) + if (childTypesOfNodeMatch(index, {"primary-expression"})) return getValue(index, 0); throw std::runtime_error("ICE: Unsupported childs: "s + ruleString(index)); // TODO } @@ -389,126 +389,102 @@ std::unordered_map> CPP::getNodeEv }, { "pm-expression", [&](index_t index) -> std::any { - if (childTypesOfNodeMatch(index, {"cast-expression", ""}) && !getValue(index, 1).has_value()) + if (childTypesOfNodeMatch(index, {"cast-expression"})) return getValue(index, 0); throw std::runtime_error("ICE: Unsupported childs: "s + ruleString(index)); // TODO } }, { "multiplicative-expression", [&](index_t index) -> std::any { - if (childTypesOfNodeMatch(index, {"pm-expression", "multiplicative-expression-EXT"})) { - if (childTypesOfChildMatch(index, 1, {"*", "", ""})) { - FlowGraph::LocalScope scope; // TODO: move to context! - FlowGraph::Data destination{FlowGraph::MakeTemporaryInt(scope)}; - FlowGraph::Data value0 {std::any_cast(getValue(index, 0))}; - FlowGraph::Data value1 {std::any_cast(getValue(index, 1))}; - - std::shared_ptr node {std::make_shared(FlowGraph::BinaryOperationType::Multiply, destination, value0, value1)}; - return node; - } + if (childTypesOfNodeMatch(index, {"multiplicative-expression", "*", "pm-expression"})) { + FlowGraph::LocalScope scope; // TODO: move to context! + FlowGraph::Data destination{FlowGraph::MakeTemporaryInt(scope)}; + FlowGraph::Data value0 {std::any_cast(getValue(index, 0))}; + FlowGraph::Data value1 {std::any_cast(getValue(index, 2))}; + + std::shared_ptr node {std::make_shared(FlowGraph::BinaryOperationType::Multiply, destination, value0, value1)}; + return node; } - if (childTypesOfNodeMatch(index, {"pm-expression", ""}) && !getValue(index, 1).has_value()) + if (childTypesOfNodeMatch(index, {"pm-expression"})) return getValue(index, 0); throw std::runtime_error("ICE: Unsupported childs: "s + ruleString(index)); // TODO } }, - { "multiplicative-expression-EXT", [&](index_t index) -> std::any - { - if (childTypesOfNodeMatch(index, {"*", "pm-expression", ""}) && !getValue(index, 2).has_value()) { - return getValue(index, 1); - } else if (childTypesOfNodeMatch(index, {})) { - return {}; - } - throw std::runtime_error("ICE: Unsupported childs: "s + ruleString(index)); // TODO - } - }, { "additive-expression", [&](index_t index) -> std::any { - if (childTypesOfNodeMatch(index, {"multiplicative-expression", "additive-expression-EXT"})) { - if (childTypesOfChildMatch(index, 1, {"+", "", ""})) { - FlowGraph::LocalScope scope; // TODO: move to context! - FlowGraph::Data destination{FlowGraph::MakeTemporaryInt(scope)}; - FlowGraph::Data value0 {std::any_cast(getValue(index, 0))}; - FlowGraph::Data value1 {std::any_cast(getValue(index, 1))}; - - std::shared_ptr node {std::make_shared(FlowGraph::BinaryOperationType::Add, destination, value0, value1)}; - return node; - } else if (!getValue(index, 1).has_value()) { - return getValue(index, 0); - } - } - throw std::runtime_error("ICE: Unsupported childs: "s + ruleString(index)); // TODO - } - }, - { "additive-expression-EXT", [&](index_t index) -> std::any - { - if (childTypesOfNodeMatch(index, {"+", "multiplicative-expression", ""}) && !getValue(index, 2).has_value()) { - return getValue(index, 1); - } else if (childTypesOfNodeMatch(index, {})) { - return {}; + if (childTypesOfNodeMatch(index, {"additive-expression", "+", "multiplicative-expression"})) { + FlowGraph::LocalScope scope; // TODO: move to context! + FlowGraph::Data destination{FlowGraph::MakeTemporaryInt(scope)}; + FlowGraph::Data value0 {std::any_cast(getValue(index, 0))}; + FlowGraph::Data value1 {std::any_cast(getValue(index, 2))}; + + std::shared_ptr node {std::make_shared(FlowGraph::BinaryOperationType::Add, destination, value0, value1)}; + return node; } + if (childTypesOfNodeMatch(index, {"multiplicative-expression"})) + return getValue(index, 0); throw std::runtime_error("ICE: Unsupported childs: "s + ruleString(index)); // TODO } }, { "shift-expression", [&](index_t index) -> std::any { - if (childTypesOfNodeMatch(index, {"additive-expression", ""}) && !getValue(index, 1).has_value()) + if (childTypesOfNodeMatch(index, {"additive-expression"})) return getValue(index, 0); throw std::runtime_error("ICE: Unsupported childs: "s + ruleString(index)); // TODO } }, { "compare-expression", [&](index_t index) -> std::any { - if (childTypesOfNodeMatch(index, {"shift-expression", ""}) && !getValue(index, 1).has_value()) + if (childTypesOfNodeMatch(index, {"shift-expression"})) return getValue(index, 0); throw std::runtime_error("ICE: Unsupported childs: "s + ruleString(index)); // TODO } }, { "relational-expression", [&](index_t index) -> std::any { - if (childTypesOfNodeMatch(index, {"compare-expression", ""}) && !getValue(index, 1).has_value()) + if (childTypesOfNodeMatch(index, {"compare-expression"})) return getValue(index, 0); throw std::runtime_error("ICE: Unsupported childs: "s + ruleString(index)); // TODO } }, { "equality-expression", [&](index_t index) -> std::any { - if (childTypesOfNodeMatch(index, {"relational-expression", ""}) && !getValue(index, 1).has_value()) + if (childTypesOfNodeMatch(index, {"relational-expression"})) return getValue(index, 0); throw std::runtime_error("ICE: Unsupported childs: "s + ruleString(index)); // TODO } }, { "and-expression", [&](index_t index) -> std::any { - if (childTypesOfNodeMatch(index, {"equality-expression", ""}) && !getValue(index, 1).has_value()) + if (childTypesOfNodeMatch(index, {"equality-expression"})) return getValue(index, 0); throw std::runtime_error("ICE: Unsupported childs: "s + ruleString(index)); // TODO } }, { "exclusive-or-expression", [&](index_t index) -> std::any { - if (childTypesOfNodeMatch(index, {"and-expression", ""}) && !getValue(index, 1).has_value()) + if (childTypesOfNodeMatch(index, {"and-expression"})) return getValue(index, 0); throw std::runtime_error("ICE: Unsupported childs: "s + ruleString(index)); // TODO } }, { "inclusive-or-expression", [&](index_t index) -> std::any { - if (childTypesOfNodeMatch(index, {"exclusive-or-expression", ""}) && !getValue(index, 1).has_value()) + if (childTypesOfNodeMatch(index, {"exclusive-or-expression"})) return getValue(index, 0); throw std::runtime_error("ICE: Unsupported childs: "s + ruleString(index)); // TODO } }, { "logical-and-expression", [&](index_t index) -> std::any { - if (childTypesOfNodeMatch(index, {"inclusive-or-expression", ""}) && !getValue(index, 1).has_value()) + if (childTypesOfNodeMatch(index, {"inclusive-or-expression"})) return getValue(index, 0); throw std::runtime_error("ICE: Unsupported childs: "s + ruleString(index)); // TODO } }, { "logical-or-expression", [&](index_t index) -> std::any { - if (childTypesOfNodeMatch(index, {"logical-and-expression", ""}) && !getValue(index, 1).has_value()) + if (childTypesOfNodeMatch(index, {"logical-and-expression"})) return getValue(index, 0); throw std::runtime_error("ICE: Unsupported childs: "s + ruleString(index)); // TODO } @@ -529,7 +505,7 @@ std::unordered_map> CPP::getNodeEv }, { "expression", [&](index_t index) -> std::any { - if (childTypesOfNodeMatch(index, {"assignment-expression", ""}) && !getValue(index, 1).has_value()) { + if (childTypesOfNodeMatch(index, {"assignment-expression"})) { std::shared_ptr node {std::any_cast>(getValue(index, 0))}; mCPPContext.graph.push_back(node); return getValue(index, 0); diff --git a/tests/test-cpp.cpp b/tests/test-cpp.cpp index 5d997f1..39e1513 100644 --- a/tests/test-cpp.cpp +++ b/tests/test-cpp.cpp @@ -42,7 +42,7 @@ TEST_F(CppTest, preprocessing_tokenize) { auto nodes = cpp.analysis(tokens); - ASSERT_EQ(nodes.size(), 58/*44*/); + ASSERT_EQ(nodes.size(), 43); } TEST_F(CppTest, preprocessing_tokenize_empty) { -- cgit v1.2.3