diff options
Diffstat (limited to 'cpp.cpp')
-rw-r--r-- | cpp.cpp | 79 |
1 files changed, 44 insertions, 35 deletions
@@ -400,18 +400,29 @@ std::unordered_map<std::string, std::function<std::any(index_t)>> CPP::getNodeEv { "multiplicative-expression", [&](index_t index) -> std::any { if (childTypesOfNodeMatch(index, {"multiplicative-expression", "*", "pm-expression"})) { - if (getValue(index, 0).type() != typeid(FlowGraph::Data)) + if (getValue(index, 0).type() != typeid(FlowGraph::Graph)) throw std::runtime_error("ICE: multiplicative-expression: Bad data type for argument 1: "s + demangle(getValue(index, 0).type())); - if (getValue(index, 2).type() != typeid(FlowGraph::Data)) + if (getValue(index, 2).type() != typeid(FlowGraph::Graph)) throw std::runtime_error("ICE: multiplicative-expression: Bad data type for argument 3: "s + demangle(getValue(index, 2).type())); - 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))}; + FlowGraph::Graph value0{std::any_cast<FlowGraph::Graph>(getValue(index, 0))}; + std::shared_ptr<FlowGraph::Node> lastOp0{value0.lastOp()}; - std::shared_ptr<FlowGraph::Node> node {std::make_shared<FlowGraph::BinaryOperation>(FlowGraph::BinaryOperationType::Multiply, destination, value0, value1)}; - return node; + FlowGraph::Graph value1{std::any_cast<FlowGraph::Graph>(getValue(index, 2))}; + std::shared_ptr<FlowGraph::Node> lastOp1{value1.lastOp()}; + + FlowGraph::Graph result{value0}; + result.append(value1); + + FlowGraph::Data destination{FlowGraph::MakeTemporaryInt(result.scope())}; + + std::shared_ptr<FlowGraph::Node> node {std::make_shared<FlowGraph::BinaryOperation>(FlowGraph::BinaryOperationType::Multiply, + destination, + lastOp0->destination(), lastOp1->destination())}; + + result.append(node); + + return result; } if (childTypesOfNodeMatch(index, {"pm-expression"})) return getValue(index, 0); @@ -421,18 +432,29 @@ std::unordered_map<std::string, std::function<std::any(index_t)>> CPP::getNodeEv { "additive-expression", [&](index_t index) -> std::any { if (childTypesOfNodeMatch(index, {"additive-expression", "+", "multiplicative-expression"})) { - if (getValue(index, 0).type() != typeid(FlowGraph::Data)) + if (getValue(index, 0).type() != typeid(FlowGraph::Graph)) throw std::runtime_error("ICE: additive-expression: Bad data type for argument 1: "s + demangle(getValue(index, 0).type())); - if (getValue(index, 2).type() != typeid(FlowGraph::Data)) + if (getValue(index, 2).type() != typeid(FlowGraph::Graph)) throw std::runtime_error("ICE: additive-expression: Bad data type for argument 3: "s + demangle(getValue(index, 2).type())); - 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))}; + FlowGraph::Graph value0{std::any_cast<FlowGraph::Graph>(getValue(index, 0))}; + std::shared_ptr<FlowGraph::Node> lastOp0{value0.lastOp()}; + + FlowGraph::Graph value1{std::any_cast<FlowGraph::Graph>(getValue(index, 2))}; + std::shared_ptr<FlowGraph::Node> lastOp1{value1.lastOp()}; + + FlowGraph::Graph result{value0}; + result.append(value1); - std::shared_ptr<FlowGraph::Node> node {std::make_shared<FlowGraph::BinaryOperation>(FlowGraph::BinaryOperationType::Add, destination, value0, value1)}; - return node; + FlowGraph::Data destination{FlowGraph::MakeTemporaryInt(result.scope())}; + + std::shared_ptr<FlowGraph::Node> node {std::make_shared<FlowGraph::BinaryOperation>(FlowGraph::BinaryOperationType::Add, + destination, + lastOp0->destination(), lastOp1->destination())}; + + result.append(node); + + return result; } if (childTypesOfNodeMatch(index, {"multiplicative-expression"})) return getValue(index, 0); @@ -519,27 +541,13 @@ std::unordered_map<std::string, std::function<std::any(index_t)>> CPP::getNodeEv { "expression", [&](index_t index) -> std::any { if (childTypesOfNodeMatch(index, {"assignment-expression"})) { - std::shared_ptr<FlowGraph::CreateScopeOp> scope_node {std::make_shared<FlowGraph::CreateScopeOp>()}; - mCPPContext.graph.push_back(scope_node); - - FlowGraph::LocalScope& scope{scope_node->scope()}; - - if (getValue(index, 0).type() == typeid(FlowGraph::Data)) { // got Data -> make trivial Node out of it and return it - - FlowGraph::Data destination{FlowGraph::MakeTemporaryInt(scope)}; - FlowGraph::Data source {std::any_cast<FlowGraph::Data>(getValue(index, 0))}; - - std::shared_ptr<FlowGraph::Node> node {std::make_shared<FlowGraph::UnaryOperation>(FlowGraph::UnaryOperationType::Store, destination, source)}; - mCPPContext.graph.push_back(node); - return node; - } else if (getValue(index, 0).type() == typeid(std::shared_ptr<FlowGraph::Node>)) { - 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); + if (getValue(index, 0).type() == typeid(FlowGraph::Graph)) { + FlowGraph::Graph graph {std::any_cast<FlowGraph::Graph>(getValue(index, 0))}; + mCPPContext.graph = graph; + return graph; } else { throw std::runtime_error("ICE: expression: Unsupported argument type: "s + demangle(getValue(index, 0).type())); } - mCPPContext.graph.push_back(std::make_shared<FlowGraph::DestroyScopeOp>(scope)); } throw std::runtime_error("ICE: Unsupported childs: "s + ruleString(index)); // TODO } @@ -576,7 +584,8 @@ 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(data); + FlowGraph::Graph graph{{std::make_shared<FlowGraph::DataNode>(data)}}; + mValues.push_back(graph); } else { mValues.push_back(std::any{}); } |