1 |
commit: cab98c13280a953bc5cedb911dae493b74c8093c |
2 |
Author: Mu Qiao <qiaomuf <AT> gentoo <DOT> org> |
3 |
AuthorDate: Sun Apr 24 12:28:21 2011 +0000 |
4 |
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org> |
5 |
CommitDate: Mon Apr 25 10:07:50 2011 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=cab98c13 |
7 |
|
8 |
Walker: change the implementation of index searching |
9 |
|
10 |
We used recursive counting algorithm to find the index of LT(2). |
11 |
Now it's changed to recurrence algorithm that directly traverse the |
12 |
input stream. This can benefit function definition and compound |
13 |
command. |
14 |
|
15 |
--- |
16 |
bashast/libbashWalker.g | 40 ++++++++++++++++++++-------------------- |
17 |
1 files changed, 20 insertions(+), 20 deletions(-) |
18 |
|
19 |
diff --git a/bashast/libbashWalker.g b/bashast/libbashWalker.g |
20 |
index b6123d9..a3bf40d 100644 |
21 |
--- a/bashast/libbashWalker.g |
22 |
+++ b/bashast/libbashWalker.g |
23 |
@@ -64,24 +64,27 @@ options |
24 |
index = value; |
25 |
} |
26 |
|
27 |
- // Recursively count number of nodes of curr |
28 |
- static int count_nodes(pANTLR3_BASE_TREE_ADAPTOR adaptor, pANTLR3_BASE_TREE curr) |
29 |
+ // seek to LT(2) and consume |
30 |
+ static void seek_to_next_tree(plibbashWalker ctx) |
31 |
{ |
32 |
- int child_count = adaptor->getChildCount(adaptor, curr); |
33 |
- if(child_count == 0) |
34 |
- { |
35 |
- // Leaf node |
36 |
- return 1; |
37 |
- } |
38 |
- else |
39 |
+ // We start from LA(1) |
40 |
+ int index = 1; |
41 |
+ // Current depth of the tree we are traversing |
42 |
+ int depth = 1; |
43 |
+ |
44 |
+ for(index = 1; depth != 0; ++index) |
45 |
{ |
46 |
- int result = 0; |
47 |
- // Count every child |
48 |
- for(int i = 0; i != child_count; ++i) |
49 |
- result += count_nodes(adaptor, (pANTLR3_BASE_TREE)(adaptor->getChild(adaptor, curr, i))); |
50 |
- // Add itself, DOWN and UP |
51 |
- return result + 3; |
52 |
+ // Go one level done if we encounter DOWN |
53 |
+ if(LA(index) == DOWN) |
54 |
+ ++depth; |
55 |
+ // Go one level up if we encounter UP. When depth==0, we finishe one node |
56 |
+ else if(LA(index) == UP) |
57 |
+ --depth; |
58 |
} |
59 |
+ |
60 |
+ // Seek to the correct offset and consume. |
61 |
+ SEEK(INDEX() + index - 3); |
62 |
+ CONSUME(); |
63 |
} |
64 |
|
65 |
} |
66 |
@@ -324,11 +327,8 @@ function_def returns[int placeholder] |
67 |
:^(FUNCTION ^(STRING name) { |
68 |
// Define the function with current index |
69 |
walker->define_function($name.libbash_value, INDEX()); |
70 |
- // Skip the AST for function body, minus one is needed to make the offset right. |
71 |
- // LT(1) is the function body. It should match the compound_command rule. |
72 |
- SEEK(INDEX() + count_nodes(ADAPTOR, LT(1)) - 1); |
73 |
- // After seeking ahead, we need to call CONSUME to eat all the nodes we've skipped. |
74 |
- CONSUME(); |
75 |
+ // Skip the AST for function body |
76 |
+ seek_to_next_tree(ctx); |
77 |
}); |
78 |
|
79 |
// Only used in arithmetic expansion |