Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get children by tag #48

Merged
merged 5 commits into from
Apr 18, 2016
Merged

Get children by tag #48

merged 5 commits into from
Apr 18, 2016

Conversation

petermlm
Copy link
Contributor

Regarding issue #45, I've implemented four functions. They are:

int mpc_ast_get_index(mpc_ast_t *ast, const char *tag);
int mpc_ast_get_index_lb(mpc_ast_t *ast, const char *tag, int lb);
mpc_ast_t *mpc_ast_get_child(mpc_ast_t *ast, const char *tag);
mpc_ast_t *mpc_ast_get_child_lb(mpc_ast_t *ast, const char *tag, int lb);

The first function will return the index of of the child given the tag of the child. The second is similar but returns a pointer to the child and not the index. The tag is compared using the function strcmp.

The third and fourth functions are similar, but they have a third parameter called lower bound lb. This parameters gives a minimum index from which the child node will be looked for in the array of children of a given node.

I've added an example to display the usage of this function.

@petermlm
Copy link
Contributor Author

To have a better tree traversal we could implement an iterator like mechanism, similar to how strtok_r uses a save pointer. The functions could be something like:

mpc_ast_trav_t *mpc_ast_trav_start(mpc_ast_ast          *ast,
                                   mpc_ast_trav_order_t  order);
mpc_ast_t *mpc_ast_trav_next(mpc_ast_trav_t *trav);

The start function would return a mpc_ast_trav_t structure which holds information on which is the current node of the tree, which is the order, and so on. The mpc_ast_trav_next function would return the next node of the tree.

The iterator would follow a pre order or post order given the order parameter of the start function.

@orangeduck orangeduck merged commit 317c284 into orangeduck:master Apr 18, 2016
@orangeduck
Copy link
Owner

Looks great, thanks! Regarding the iteratiors - is there much advantage over just storing the child index as an integer?

@petermlm
Copy link
Contributor Author

You mean storing just the child's index over having a structure for traversal? Yes.

I haven't given much thought to it yet, but I think a traversal would have to keep more information then just the child's index. For example, in a given node, we would need to store a pointer to a parent so we can go back. The order of traversal, that is, if the traversal is in preorder or postorder, would also be stored in the structure so each call to next can know which the next node to return.

Tonight I'll think about this further. I'll make a proper proposal in the issues section.

@orangeduck
Copy link
Owner

Ah so you mean a full traversal over the tree - yeah that makes sense then.

@petermlm
Copy link
Contributor Author

Yes that was it

@petermlm petermlm mentioned this pull request Apr 18, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants