0

I have two classes AST and ASTNode for working with some tree-like data. Both these classes have networkx graph as a field and a node id (integer). For AST this id refers to the root of a tree. These two classes looks pretty similar, but I have splited functionality in a following way:

  • I use AST for global functionality like get nodes of some type from a whole tree
  • I use ASTNode for local functionality like get direct child of a node and other node properties

I need to be able to shift from AST to ASTNode functionality and backward when needed. Here comes circular dependency. I want to be able:

  • to query some ASTNode from AST
  • to build a subtree from ASTNode with this node as a root

So this makes ASTNode to know how to create AST. How can I deal with it? Should I use absolute module import? Or it can be done otherwise?

Currently I moved get_subtree(node) function to AST, but this make usage really uncomfortable (you have to pass node and a tree together all the time).

asked Aug 8, 2020 at 9:59
4
  • So how does the dependencies would look like? ASTBuilder knows how to create AST and ASTNode. And AST and ASTNode use it? Seems like I didn't get the solution. Coud you clarify? Commented Aug 8, 2020 at 10:43
  • For AST, is the ID that refers the root of tree considered the overall tree's root node, or the current root node of some subtree? Commented Aug 10, 2020 at 2:05
  • @eparham7861 If AST is original tree build from source code, than it refers to its root. If it is subtree, than to the root of subtree. Commented Aug 10, 2020 at 13:03
  • Would it be possible for you to push all of the functionality to a separate class, like ASTLogic or ASTInteractions? That way, instead of the nodes "knowing" how to create the tree, this separate class would be able to hold global functionality (AST specific), "local" functionality (AST->ASTNode specific), and generation functionality (sub-AST specific). Commented Aug 10, 2020 at 15:29

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.