summaryrefslogtreecommitdiffhomepage
path: root/cpp.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'cpp.cpp')
-rw-r--r--cpp.cpp85
1 files changed, 70 insertions, 15 deletions
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<TreeNode>& Tree)
+std::string CPP::valueOfNode(index_t node_index) const
{
std::string result;
std::optional<size_t> pos0;
@@ -84,7 +84,7 @@ std::string CPP::valueOfNode(index_t node_index, const std::vector<TreeNode>& 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<TreeNode>& 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<std::string> pp_types {
"identifier",
@@ -236,28 +243,76 @@ std::vector<Gram::TreeNode> CPP::analysis(const std::vector<Token>& 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<std::string>& pattern) const
+{
+ const std::vector<int32_t>& 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