commit 764d78a529e375df675e520cd628b3368e4949f2
parent 6c5d52ea0865cb623eead43535739b008b526928
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Sat, 5 Oct 2013 22:08:38 +0200
Add walk function
This function is created as a generic way of running over a tree
and run a function in each node of the tree. This function will
be used later for syntactial analisys.
Diffstat:
2 files changed, 17 insertions(+), 1 deletion(-)
diff --git a/syntax.h b/syntax.h
@@ -1,6 +1,10 @@
#ifndef SYNTAX_H
#define SYNTAX_H
+#if ! __bool_true_false_are_defined
+# include <stdbool.h>
+#endif
+
extern unsigned char curctx;
enum opcode {
@@ -32,7 +36,7 @@ extern struct node *node(unsigned char op, struct node *l, struct node *r);
extern struct node *nodesym(struct symbol *sym);
extern struct node *addstmt(struct compound *p, struct node *np);
extern struct node *addstmt(struct compound *p, struct node *np);
-
+extern bool walk(register struct node *np, bool (*fun)(struct node *));
extern void prtree(register struct node *np);
#endif
diff --git a/tree.c b/tree.c
@@ -64,6 +64,18 @@ addstmt(struct compound *p, struct node *np)
return p->tree;
}
+bool
+walk(register struct node *np, bool (*fun)(struct node *))
+{
+ struct node_op2 *p;
+
+ if (!np || np->op == OSYM)
+ return 1;
+
+ p = (struct node_op2 *) np;
+ return (*fun)(np) && walk(p->left, fun) && walk(p->right, fun);
+}
+
void
prtree(register struct node *np)
{