summaryrefslogtreecommitdiffhomepage
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
parent71c7fd62f8b5257b82cf32b0f747fcf313fcc617 (diff)
Add node map
-rw-r--r--cpp.cpp21
-rw-r--r--cpp.h7
-rw-r--r--flowgraph/node.h2
-rw-r--r--tests/test-flowgraph.cpp1
4 files changed, 26 insertions, 5 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));
diff --git a/cpp.h b/cpp.h
index ce4a516..2594a4c 100644
--- a/cpp.h
+++ b/cpp.h
@@ -53,7 +53,12 @@ private:
std::string typeOfChild(int32_t child_id) const;
bool childTypesOfNodeMatch(index_t, const std::vector<std::string>& pattern) const; ///< returns true iff specified type list matches; "" -> don't care
- std::deque<std::shared_ptr<std::any>> mValues;
+ std::deque<std::shared_ptr<std::any>> mValues; // values stack during phase 7.c
+
+ std::unordered_map<std::string, std::function<std::shared_ptr<std::any>(index_t)>> getNodeEvalMap();
+ std::unordered_map<std::string, std::function<std::shared_ptr<std::any>(index_t)>> node_eval_map;
+
+ CPPContext mContext;
void getValueOfToken(index_t index);
void getValueOfNode(index_t index);
void visitRecursive(index_t node_id);
diff --git a/flowgraph/node.h b/flowgraph/node.h
index 40846e2..aa9d333 100644
--- a/flowgraph/node.h
+++ b/flowgraph/node.h
@@ -17,7 +17,7 @@ namespace FlowGraph {
class Node
{
public:
- virtual ~Node() {}; // force class to be polymorphic
+ virtual ~Node() {}; // force class to be polymorphic (e.g. in a container)
};
// Memory on Heap: new and delete
diff --git a/tests/test-flowgraph.cpp b/tests/test-flowgraph.cpp
index 132af4b..f0cc204 100644
--- a/tests/test-flowgraph.cpp
+++ b/tests/test-flowgraph.cpp
@@ -48,3 +48,4 @@ TEST_F(FlowGraphTest, build_graph) {
graph.push_back(malloc1);
graph.push_back(free1);
}
+