From 62aafc5c9273cb0b7a91bf2e4dee1ac2d3658bb3 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sun, 1 Nov 2020 22:26:56 +0100 Subject: translate() translation-unit and declaration (WIP) --- cpp.cpp | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 70 insertions(+), 15 deletions(-) (limited to 'cpp.cpp') diff --git a/cpp.cpp b/cpp.cpp index 21f4df1..2135e67 100644 --- a/cpp.cpp +++ b/cpp.cpp @@ -64,7 +64,7 @@ void CPP::concatenate_strings() // TODO } -std::string CPP::valueOfNode(index_t node_index, const std::vector& Tree) +std::string CPP::valueOfNode(index_t node_index) const { std::string result; std::optional pos0; @@ -84,7 +84,7 @@ std::string CPP::valueOfNode(index_t node_index, const std::vector& Tr last_index = TokenIdFromChildId(current_index); } else { - const TreeNode &node{Tree[current_index]}; + const TreeNode &node{m_nodes[current_index]}; // iterate backwards in childs, to get depth-first search in tree, from the beginning std::for_each(node.child_ids.rbegin(), node.child_ids.rend(), [&](int32_t child){ @@ -99,6 +99,13 @@ std::string CPP::valueOfNode(index_t node_index, const std::vector& Tr return m_code.substr(*pos0, m_tokens[last_index].location.pos - *pos0) + m_tokens[last_index].value; }; +std::string CPP::typeOfNode(index_t node_id) const +{ + if (node_id >= m_nodes.size()) + throw std::runtime_error("CPP::typeOfNode(): node_id="s + std::to_string(node_id) + ", m_nodes.size()="s + std::to_string(m_nodes.size())); + return m_nodes[node_id].type; +} + namespace { std::unordered_set pp_types { "identifier", @@ -236,28 +243,76 @@ std::vector CPP::analysis(const std::vector& tokens) return compiler.compile(tokens); } -#if 0 -void CPP::traverse(index_t node_id) +std::string CPP::locationOfNode(index_t node_index) const { - fs::path current_path{parent_path / m_nodes[node_id].type}; + if (node_index >= m_nodes.size()) + throw std::runtime_error("ICE: locationOfNode(): Bad node_index "s + std::to_string(node_index) + " vs. "s + std::to_string(m_nodes.size())); - // execute callbacks - auto it{map.find(current_path.generic_string())}; - if (it != map.end()) { - it->second(current_path, node_id); - } - - // recurse tree - for (const auto& child_id: m_nodes[node_id].child_ids) { + for (const auto& child_id: m_nodes[node_index].child_ids) { if (ChildIdIsNode(child_id)) { - traverse(child_id, map, current_path); + std::string location{locationOfNode(child_id)}; + if (location.size() > 0) + return location; + } else { // ChildIdIsToken + return m_tokens[TokenIdFromChildId(child_id)].location.toString(); } } + + return ""; // not found +} + +void CPP::compileError(index_t node_id, const std::string& msg) const +{ + std::string location{locationOfNode(node_id)}; + if (location.size() == 0) + location = "Node "s + std::to_string(node_id) + "doesn't have any token"; + throw std::runtime_error("Compile error: "s + location + ": "s + msg); +} + +std::string CPP::typeOfChild(int32_t child_id) const +{ + if (ChildIdIsToken(child_id)) { + index_t token_id {TokenIdFromChildId(child_id)}; + return m_tokens[token_id].type; + } else { // ChildIdIsNode + return m_nodes[child_id].type; + } +} + +bool CPP::childTypesOfNodeMatch(index_t node_id, const std::vector& pattern) const +{ + const std::vector& child_ids{m_nodes[node_id].child_ids}; + + if (child_ids.size() != pattern.size()) + return false; // mismatch + + for (size_t i = 0; i < child_ids.size(); i++) { + if (pattern[i] != "" && typeOfChild(child_ids[i]) != pattern[i]) + return false; // mismatch + } + + return true; // match +} + +void CPP::trDeclaration(index_t node_id) +{ + std::cout << "DEBUG" << std::endl; } -#endif void CPP::trTranslationUnit(index_t node_id) { + if (childTypesOfNodeMatch(node_id, {"declaration-seq"})) { + // resolve sequence + do { + node_id = m_nodes[node_id].child_ids[0]; + if (childTypesOfNodeMatch(node_id, {"declaration"})) { + trDeclaration(m_nodes[node_id].child_ids[0]); + return; + } + } while (childTypesOfNodeMatch(node_id, {"declaration-seq", "declaration"})); + compileError(node_id, "ICE: bad declaration-seq"); + } else + compileError(node_id, "declaration-seq expected"); } // Phase 7.c: Translate -- cgit v1.2.3