From f0f7a8f7fd237d1f8e2bab2bfb2cb4442e1a692f Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sat, 7 Nov 2020 15:05:45 +0100 Subject: Add node map --- cpp.cpp | 21 ++++++++++++++++++--- cpp.h | 7 ++++++- flowgraph/node.h | 2 +- tests/test-flowgraph.cpp | 1 + 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& return true; // match } -//std::unordered_map> + +std::unordered_map(index_t)>> CPP::getNodeEvalMap() +{ + return { + { "primary-expression ", [&](index_t index) -> std::shared_ptr { 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 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(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& pattern) const; ///< returns true iff specified type list matches; "" -> don't care - std::deque> mValues; + std::deque> mValues; // values stack during phase 7.c + + std::unordered_map(index_t)>> getNodeEvalMap(); + std::unordered_map(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); } + -- cgit v1.2.3