shithub: scc

Download patch

ref: 764d78a529e375df675e520cd628b3368e4949f2
parent: 6c5d52ea0865cb623eead43535739b008b526928
author: Roberto E. Vargas Caballero <[email protected]>
date: Sat Oct 5 18:08:38 EDT 2013

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.

--- 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 *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
--- a/tree.c
+++ b/tree.c
@@ -64,6 +64,18 @@
 	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)
 {