There can be multiple ways to iterate a data structure that do not fit into the narrow descriptions of the basic For_Flags
enums such as reverse iteration or by pointer. For example, someone might want to make a Tree
struct with a breath-first search and a depth-first search iteration of a Tree
. This can be accomplished by writing a breath first search macro and a depth first search macro with the following parameter arguments:
macro :: (o: *Object, body: Code, flag: For_Flags) #expand;
Writing a macro with that function signature allows the macro to be used to label a for loop expansion. In the following example below, we create a bfs
and dfs
for expansion macro that allows the for loop to traverse either in breath first search or depth first search respectively.
tree: Tree;
for :bfs node: tree {
// breath first search the tree
print("%\n", node);
}
for :dfs node: tree {
// depth first search the tree
print("%\n", node);
}
Tree :: struct {
data: int;
left: *Tree;
right: *Tree;
}
bfs :: (t: *Tree, body: Code, flags: For_Flags) #expand {
// define breath first search here..
}
dfs :: (t: *Tree, body: Code, flags: For_Flags) #expand {
// define depth first search here..
}