diff options
-rw-r--r-- | grammer.cpp | 58 |
1 files changed, 33 insertions, 25 deletions
diff --git a/grammer.cpp b/grammer.cpp index 65a4be3..896a928 100644 --- a/grammer.cpp +++ b/grammer.cpp @@ -71,35 +71,40 @@ namespace { void Compiler::DumpTree() { - std::cout << "=== Nodes ====================================" << std::endl; - std::cout << "root_node_id=" << root_node_id << std::endl; - for (const auto& node : nodes) { - std::cout << node.type << "(" << node.node_id << "): "; - for (const auto& child : node.child_ids) { - std::cout << " " << child; + std::cout << "= Dump =======================================" << std::endl; + std::cout << "nodes.size()=" << nodes.size() << std::endl; + if (nodes.size() > 0) { + std::cout << "--- Nodes ------------------------------------" << std::endl; + std::cout << "root_node_id=" << root_node_id << std::endl; + for (const auto& node : nodes) { + std::cout << node.type << "(" << node.node_id << "): "; + for (const auto& child : node.child_ids) { + std::cout << " " << child; + } + std::cout << std::endl; } - std::cout << std::endl; - } - std::cout << "=== Tree =====================================" << std::endl; - std::deque<std::pair<int32_t, size_t>> todo{std::pair<int32_t, size_t>{static_cast<int32_t>(root_node_id), 0}}; // id, indent - while (!todo.empty()) { - auto [current_index, indent] {todo.front()}; - todo.pop_front(); - std::cout << std::string(indent, ' '); - if (ChildIdIsToken(current_index)) { - index_t token_id {TokenIdFromChildId(current_index)}; - std::cout << "Token(" << token_id << "): " << tokens[token_id].type << "(" << tokens[token_id].value << ")"; - } else { - std::cout << "Node(" << current_index << "): " << nodes[current_index].type; - - auto child_ids{nodes[current_index].child_ids}; - for (int i = 0; i < child_ids.size(); i++) { - todo.insert(todo.begin() + i, std::pair<int32_t, size_t>{child_ids[i], indent + 1}); + std::cout << "--- Tree -------------------------------------" << std::endl; + std::deque<std::pair<int32_t, size_t>> todo{std::pair<int32_t, size_t>{static_cast<int32_t>(root_node_id), 0}}; // id, indent + while (!todo.empty()) { + auto [current_index, indent] {todo.front()}; + todo.pop_front(); + + std::cout << std::string(indent, ' '); + if (ChildIdIsToken(current_index)) { + index_t token_id {TokenIdFromChildId(current_index)}; + std::cout << "Token(" << token_id << "): " << tokens[token_id].type << "(" << tokens[token_id].value << ")"; + } else { + std::cout << "Node(" << current_index << "): " << nodes[current_index].type; + + auto child_ids{nodes[current_index].child_ids}; + for (int i = 0; i < child_ids.size(); i++) { + todo.insert(todo.begin() + i, std::pair<int32_t, size_t>{child_ids[i], indent + 1}); + } } - } - std::cout << std::endl; + std::cout << std::endl; + } } std::cout << "==============================================" << std::endl; } @@ -188,6 +193,7 @@ void Compiler::AddFirstNode() bool Compiler::AddRootNode() { + std::cout << "AddRootNode()" << std::endl; if (nodes.size() == 0) { AddFirstNode(); } else { @@ -224,6 +230,7 @@ bool Compiler::AddRootNode() return false; // now add! + std::cout << "AddRootNode(): Adding " << node_type << std::endl; nodes[old_root_node_id].parent_node_id = new_root_node_id; root_node_id = new_root_node_id; nodes.emplace_back(TreeNode{root_node_id, root_node_id, node_type, node_variant, alternatives, child_ids}); @@ -422,6 +429,7 @@ std::pair<index_t, std::vector<TreeNode>> Compiler::compile(std::vector<Token> T throw std::runtime_error("No tokens!"); while (!treeIsComplete()) { + DumpTree(); if (!FillTree()) { TrackBack(); } else if (!AddRootNode()) { |