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
fromAST
- 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).
-
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?Yaroslav Kishchenko– Yaroslav Kishchenko08/08/2020 10:43:07Commented 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?eparham7861– eparham786108/10/2020 02:05:18Commented 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.Yaroslav Kishchenko– Yaroslav Kishchenko08/10/2020 13:03:41Commented 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).eparham7861– eparham786108/10/2020 15:29:33Commented Aug 10, 2020 at 15:29