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 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'cpp.cpp') 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)); -- cgit v1.2.3