From fe063834e53e856823b9a42ad3a5e04153446849 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Mon, 9 Nov 2020 16:41:07 +0100 Subject: Support multiply (WIP) --- cpp.cpp | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) (limited to 'cpp.cpp') diff --git a/cpp.cpp b/cpp.cpp index e4d935a..ce4a7c5 100644 --- a/cpp.cpp +++ b/cpp.cpp @@ -361,6 +361,8 @@ std::unordered_map> CPP::getNodeEv { if (childTypesOfNodeMatch(index, {"literal"})) return getValue(index, 0); + if (childTypesOfNodeMatch(index, {"(", "expression", ")"})) + return getValue(index, 1); throw std::runtime_error("ICE: Unsupported childs: "s + ruleString(index)); // TODO } }, @@ -394,21 +396,46 @@ std::unordered_map> CPP::getNodeEv }, { "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, {"pm-expression", ""}) && !getValue(index, 1).has_value()) 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"}) && 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; + 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 } -- cgit v1.2.3