Sprint 2 Step 2: child links, Function and Variable concepts

Adds generic child link support to ASTNode (addChild/setChild for multi/single-
valued roles, getChildren/getChild/allChildren for traversal). Function and
Variable as first child concepts of Module. Test builds Module->Function tree,
walks parent/child pointers in both directions, verifies single-valued replace.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Bill
2026-02-06 18:54:18 -07:00
parent 586914df4e
commit 0f90bec21f
5 changed files with 152 additions and 0 deletions

View File

@@ -1,5 +1,7 @@
#pragma once
#include <string>
#include <vector>
#include <map>
class ASTNode {
public:
@@ -8,4 +10,50 @@ public:
ASTNode* parent = nullptr;
virtual ~ASTNode() = default;
// Multi-valued child: append to role
void addChild(const std::string& role, ASTNode* child) {
child->parent = this;
children_[role].push_back(child);
}
// Single-valued child: set (replaces if exists)
void setChild(const std::string& role, ASTNode* child) {
auto& vec = children_[role];
if (!vec.empty()) {
vec[0]->parent = nullptr;
vec[0] = child;
} else {
vec.push_back(child);
}
child->parent = this;
}
// Get single-valued child (nullptr if not set)
ASTNode* getChild(const std::string& role) const {
auto it = children_.find(role);
if (it != children_.end() && !it->second.empty()) {
return it->second[0];
}
return nullptr;
}
// Get multi-valued children (empty vector if role not present)
const std::vector<ASTNode*>& getChildren(const std::string& role) const {
static const std::vector<ASTNode*> empty;
auto it = children_.find(role);
return it != children_.end() ? it->second : empty;
}
// Get all children across all roles
std::vector<ASTNode*> allChildren() const {
std::vector<ASTNode*> result;
for (const auto& [role, kids] : children_) {
result.insert(result.end(), kids.begin(), kids.end());
}
return result;
}
private:
std::map<std::string, std::vector<ASTNode*>> children_;
};