diff options
| -rw-r--r-- | cpp.cpp | 21 | ||||
| -rw-r--r-- | cpp.h | 7 | ||||
| -rw-r--r-- | flowgraph/node.h | 2 | ||||
| -rw-r--r-- | tests/test-flowgraph.cpp | 1 | 
4 files changed, 26 insertions, 5 deletions
| @@ -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)); @@ -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);  } + | 
