diff options
author | Roland Reichwein <mail@reichwein.it> | 2020-11-12 19:02:46 +0100 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2020-11-12 19:02:46 +0100 |
commit | bbf81cd21bbe36fe7e613e5411b9d366d8411921 (patch) | |
tree | bf9e3c8501d27dddb506ad7e5e36616cd4de6162 | |
parent | 9f28c5b7fb5d97d5b5def918978d4232e5dde31e (diff) |
Adjust rules to new bnf
-rw-r--r-- | cpp.cpp | 86 | ||||
-rw-r--r-- | tests/test-cpp.cpp | 2 |
2 files changed, 32 insertions, 56 deletions
@@ -368,7 +368,7 @@ std::unordered_map<std::string, std::function<std::any(index_t)>> 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<std::string, std::function<std::any(index_t)>> 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<FlowGraph::Data>(getValue(index, 0))}; - FlowGraph::Data value1 {std::any_cast<FlowGraph::Data>(getValue(index, 1))}; - - std::shared_ptr<FlowGraph::Node> node {std::make_shared<FlowGraph::BinaryOperation>(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<FlowGraph::Data>(getValue(index, 0))}; + FlowGraph::Data value1 {std::any_cast<FlowGraph::Data>(getValue(index, 2))}; + + std::shared_ptr<FlowGraph::Node> node {std::make_shared<FlowGraph::BinaryOperation>(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<FlowGraph::Data>(getValue(index, 0))}; - FlowGraph::Data value1 {std::any_cast<FlowGraph::Data>(getValue(index, 1))}; - - std::shared_ptr<FlowGraph::Node> node {std::make_shared<FlowGraph::BinaryOperation>(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<FlowGraph::Data>(getValue(index, 0))}; + FlowGraph::Data value1 {std::any_cast<FlowGraph::Data>(getValue(index, 2))}; + + std::shared_ptr<FlowGraph::Node> node {std::make_shared<FlowGraph::BinaryOperation>(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<std::string, std::function<std::any(index_t)>> 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<FlowGraph::Node> node {std::any_cast<std::shared_ptr<FlowGraph::Node>>(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) { |