summaryrefslogtreecommitdiffhomepage
path: root/cpp.cpp
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2020-11-07 15:05:45 +0100
committerRoland Reichwein <mail@reichwein.it>2020-11-07 15:05:45 +0100
commitf0f7a8f7fd237d1f8e2bab2bfb2cb4442e1a692f (patch)
treef3c08014262ad519b183aa9218d48bf65e6bb9f4 /cpp.cpp
parent71c7fd62f8b5257b82cf32b0f747fcf313fcc617 (diff)
Add node map
Diffstat (limited to 'cpp.cpp')
-rw-r--r--cpp.cpp21
1 files changed, 18 insertions, 3 deletions
diff --git a/cpp.cpp b/cpp.cpp
index b9f405f..4ab3b94 100644
--- a/cpp.cpp
+++ b/cpp.cpp
@@ -21,7 +21,11 @@ using namespace Gram;
namespace fs = std::filesystem;
-CPP::CPP(){}
+CPP::CPP()
+{
+ node_eval_map = getNodeEvalMap();
+}
+
CPP::~CPP(){}
@@ -295,7 +299,13 @@ bool CPP::childTypesOfNodeMatch(index_t node_id, const std::vector<std::string>&
return true; // match
}
-//std::unordered_map<std::string, std::function<>>
+
+std::unordered_map<std::string, std::function<std::shared_ptr<std::any>(index_t)>> CPP::getNodeEvalMap()
+{
+ return {
+ { "primary-expression ", [&](index_t index) -> std::shared_ptr<std::any> { return nullptr; }},
+ };
+}
// precondition: stack contains child values c1, ..., cn on top -> to be popped
// postcondition: stack contains value on top -> to be pushed
@@ -304,9 +314,13 @@ void CPP::getValueOfNode(index_t index)
size_t num_childs {m_nodes[index].child_ids.size()};
if (mValues.size() < num_childs)
- throw std::runtime_error("Expected num_childs elements on Values stack at "s + locationOfNode(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};
+ if (function_it != node_eval_map.end()) {
+ result = function_it->second(index);
+ }
mValues.resize(mValues.size() - num_childs);
@@ -316,6 +330,7 @@ void CPP::getValueOfNode(index_t index)
// pushes result onto stack
void CPP::getValueOfToken(index_t index)
{
+ // TODO: also support the other tokens ...
if (m_tokens[index].type == "literal") {
FlowGraph::Data data{FlowGraph::MakeConstantInt(stoi(m_tokens[index].value))};
mValues.push_back(std::make_shared<std::any>(data));