From mboxrd@z Thu Jan  1 00:00:00 1970
Received: from pigeon.gentoo.org ([208.92.234.80] helo=lists.gentoo.org)
	by finch.gentoo.org with esmtp (Exim 4.60)
	(envelope-from <gentoo-commits+bounces-339794-garchives=archives.gentoo.org@lists.gentoo.org>)
	id 1QF6PI-0008R9-Ir
	for garchives@archives.gentoo.org; Wed, 27 Apr 2011 15:12:01 +0000
Received: from pigeon.gentoo.org (localhost [127.0.0.1])
	by pigeon.gentoo.org (Postfix) with SMTP id 8342A1C073;
	Wed, 27 Apr 2011 15:11:13 +0000 (UTC)
Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183])
	by pigeon.gentoo.org (Postfix) with ESMTP id 4F3541C03D
	for <gentoo-commits@lists.gentoo.org>; Wed, 27 Apr 2011 15:11:13 +0000 (UTC)
Received: from pelican.gentoo.org (unknown [66.219.59.40])
	(using TLSv1 with cipher ADH-CAMELLIA256-SHA (256/256 bits))
	(No client certificate requested)
	by smtp.gentoo.org (Postfix) with ESMTPS id 8E6C71B407C
	for <gentoo-commits@lists.gentoo.org>; Wed, 27 Apr 2011 15:11:11 +0000 (UTC)
Received: from localhost.localdomain (localhost [127.0.0.1])
	by pelican.gentoo.org (Postfix) with ESMTP id EC4F080507
	for <gentoo-commits@lists.gentoo.org>; Wed, 27 Apr 2011 15:11:10 +0000 (UTC)
From: "Petteri Räty" <betelgeuse@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Content-type: text/plain; charset=UTF-8
Reply-To: gentoo-dev@lists.gentoo.org, "Petteri Räty" <betelgeuse@gentoo.org>
Message-ID: <cab98c13280a953bc5cedb911dae493b74c8093c.betelgeuse@gentoo>
Subject: [gentoo-commits] proj/libbash:master commit in: bashast/
X-VCS-Repository: proj/libbash
X-VCS-Files: bashast/libbashWalker.g
X-VCS-Directories: bashast/
X-VCS-Committer: betelgeuse
X-VCS-Committer-Name: Petteri Räty
X-VCS-Revision: cab98c13280a953bc5cedb911dae493b74c8093c
Date: Wed, 27 Apr 2011 15:11:10 +0000 (UTC)
Precedence: bulk
List-Post: <mailto:gentoo-commits@lists.gentoo.org>
List-Help: <mailto:gentoo-commits+help@lists.gentoo.org>
List-Unsubscribe: <mailto:gentoo-commits+unsubscribe@lists.gentoo.org>
List-Subscribe: <mailto:gentoo-commits+subscribe@lists.gentoo.org>
List-Id: Gentoo Linux mail <gentoo-commits.gentoo.org>
X-BeenThere: gentoo-commits@lists.gentoo.org
Content-Transfer-Encoding: quoted-printable
X-Archives-Salt: 
X-Archives-Hash: 65e85f6597d682d987c669b6c1c4d066

commit:     cab98c13280a953bc5cedb911dae493b74c8093c
Author:     Mu Qiao <qiaomuf <AT> gentoo <DOT> org>
AuthorDate: Sun Apr 24 12:28:21 2011 +0000
Commit:     Petteri R=C3=A4ty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Mon Apr 25 10:07:50 2011 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=3Dproj/libbash.git;a=
=3Dcommit;h=3Dcab98c13

Walker: change the implementation of index searching

We used recursive counting algorithm to find the index of LT(2).
Now it's changed to recurrence algorithm that directly traverse the
input stream. This can benefit function definition and compound
command.

---
 bashast/libbashWalker.g |   40 ++++++++++++++++++++--------------------
 1 files changed, 20 insertions(+), 20 deletions(-)

diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g
index b6123d9..a3bf40d 100644
--- a/bashast/libbashWalker.g
+++ b/bashast/libbashWalker.g
@@ -64,24 +64,27 @@ options
 		index =3D value;
 	}
=20
-	// Recursively count number of nodes of curr
-	static int count_nodes(pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_=
TREE curr)
+	// seek to LT(2) and consume
+	static void seek_to_next_tree(plibbashWalker ctx)
 	{
-		int child_count =3D adaptor->getChildCount(adaptor, curr);
-		if(child_count =3D=3D 0)
-		{
-			// Leaf node
-			return 1;
-		}
-		else
+		// We start from LA(1)
+		int index =3D 1;
+		// Current depth of the tree we are traversing
+		int depth =3D 1;
+
+		for(index =3D 1; depth !=3D 0; ++index)
 		{
-			int result =3D 0;
-			// Count every child
-			for(int i =3D 0; i !=3D child_count; ++i)
-				result +=3D count_nodes(adaptor, (pANTLR3_BASE_TREE)(adaptor->getChi=
ld(adaptor, curr, i)));
-			// Add itself, DOWN and UP
-			return result + 3;
+			// Go one level done if we encounter DOWN
+			if(LA(index) =3D=3D DOWN)
+				++depth;
+			// Go one level up if we encounter UP. When depth=3D=3D0, we finishe =
one node
+			else if(LA(index) =3D=3D UP)
+				--depth;
 		}
+
+		// Seek to the correct offset and consume.
+		SEEK(INDEX() + index - 3);
+		CONSUME();
 	}
=20
 }
@@ -324,11 +327,8 @@ function_def returns[int placeholder]
 	:^(FUNCTION ^(STRING name) {
 		// Define the function with current index
 		walker->define_function($name.libbash_value, INDEX());
-		// Skip the AST for function body, minus one is needed to make the off=
set right.
-		// LT(1) is the function body. It should match the compound_command ru=
le.
-		SEEK(INDEX() + count_nodes(ADAPTOR, LT(1)) - 1);
-		// After seeking ahead, we need to call CONSUME to eat all the nodes w=
e've skipped.
-		CONSUME();
+		// Skip the AST for function body
+		seek_to_next_tree(ctx);
 	});
=20
 // Only used in arithmetic expansion