summaryrefslogtreecommitdiffhomepage
path: root/cpp.cpp
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2020-11-07 21:55:45 +0100
committerRoland Reichwein <mail@reichwein.it>2020-11-07 21:55:45 +0100
commit5c7df4f7b09d138df58f720260306afdf0f6713a (patch)
tree4e81ef887606486d4729204d629a5ad029b14256 /cpp.cpp
parente7d51fff32ea247fd35b56fc7cf5ce06df5dc6bf (diff)
Make "add" result
Diffstat (limited to 'cpp.cpp')
-rw-r--r--cpp.cpp45
1 files changed, 22 insertions, 23 deletions
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<std::any> 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<std::string, std::function<std::shared_ptr<std::any>(index_t)>> CPP::getNodeEvalMap()
+std::unordered_map<std::string, std::function<std::any(index_t)>> CPP::getNodeEvalMap()
{
return {
- { "primary-expression", [&](index_t index) -> std::shared_ptr<std::any>
+ { "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<std::any>
+ { "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<std::any>
+ { "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<std::any>
+ { "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<std::any>
+ { "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<std::any>
+ { "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<std::any>
+ { "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<FlowGraph::Data>(getValue(index, 0))};
+ FlowGraph::Data value1 {std::any_cast<FlowGraph::Data>(getValue(index, 1))};
- //return std::make_shared<FlowGraph::BinaryOperation>(FlowGraph::BinaryOperationType::Add, destination, value0, value1);
- return getValue(index, 0); // TODO
+ return std::make_shared<FlowGraph::BinaryOperation>(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<std::any>
+ { "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<std::any> 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<std::any>(data));
+ mValues.push_back(data);
} else {
- mValues.push_back(std::make_shared<std::any>(nullptr));
+ mValues.push_back(std::any{});
}
}