Skip to content

Node object

Мишуткин Александр edited this page Jan 26, 2021 · 2 revisions

Class uast::Node

Use cases

Nodes are used to give informations about AST nodes. Each node contains it's type (std::string Type()) and std::map of edges <std::string, std::shared_ptr<Node>>

Creating tree:

std::shared_ptr<uast::Node> node_1 = std::make_shared<uast::Node>("1");
    std::shared_ptr<uast::Node> node_2 = std::make_shared<uast::Node>("2");
    std::shared_ptr<uast::Node> node_p =
            std::make_shared<uast::Node>("+", std::vector<std::string>{"lhs", "rhs"},
                                         std::vector<std::shared_ptr<uast::Node>>{node_1, node_2});
    std::shared_ptr<uast::Node> node_m;

Adding nodes:

std::shared_ptr<uast::Node> add1 = std::make_shared<uast::Node>("add1");
std::shared_ptr<uast::Node> add2 = std::make_shared<uast::Node>("add2");
node_1->AddChild("kek", add1);
node_1->AddChild("kek2", *add2);

Iterating over subtree (DFS):

/*
*  1
* | \
* 2  4
*    |
*    3
*    |
*    6
*/


bool one, two, three, four, five;
one = two = three = four = five = false;

for (auto pair : node_1->DFS()) {
    if (pair.first == "1") {
        one = true;
    }
    if (pair.first == "2") {
        two = true;
    }
    if (pair.first == "3") {
        three = true;
    }
    if (pair.first == "4") {
        four = true;            
    }
    if (pair.first == "5") {
        five = true;
    }
}
// false, true, true, true, true

Clone this wiki locally