From 387e2b87f11f0176267cc37cb4468b041636a672 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Wed, 12 Feb 2020 21:34:58 +0100 Subject: Debug --- grammer.cpp | 58 +++++++++++++++++++++++++++++++++------------------------- 1 file 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> todo{std::pair{static_cast(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{child_ids[i], indent + 1}); + std::cout << "--- Tree -------------------------------------" << std::endl; + std::deque> todo{std::pair{static_cast(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{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> Compiler::compile(std::vector T throw std::runtime_error("No tokens!"); while (!treeIsComplete()) { + DumpTree(); if (!FillTree()) { TrackBack(); } else if (!AddRootNode()) { -- cgit v1.2.3