I changed PreorderNodeIterator so a pattern for its usage will be:

PreorderNodeIterator iter = new PreorderNodeIterator();
for (iter.start(tree); !iter.done(); iter.next()) {
    Node node = iter.getCurrent();
    ...
}

instead of

PreorderNodeIterator iter = tree.getPreorderIterator();
Node node;
while ((node = iter.nextNode()) != null) {
}

to allow for more flexible usage and added PreorderNodeIterator.nextSkipSubtree to skip iteration of the last visited node subtree which allows to have simple code  in Optimizer.buildStatementList when iterating over statements.
This commit is contained in:
igor%mir2.org 2003-02-15 12:47:45 +00:00
Родитель 7eb47250fe
Коммит 2e4cd83bf8
4 изменённых файлов: 34 добавлений и 199 удалений

Просмотреть файл

@ -169,10 +169,6 @@ public class Node implements Cloneable {
return n;
}
public PreorderNodeIterator getPreorderIterator() {
return new PreorderNodeIterator(this);
}
public void addChildToFront(Node child) {
child.next = first;
first = child;

Просмотреть файл

@ -72,9 +72,9 @@ public class NodeTransformer {
// to save against upchecks if no finally blocks are used.
boolean hasFinally = false;
PreorderNodeIterator iterator = tree.getPreorderIterator();
Node node;
while ((node = iterator.nextNode()) != null) {
PreorderNodeIterator iter = new PreorderNodeIterator();
for (iter.start(tree); !iter.done(); iter.next()) {
Node node = iter.getCurrent();
int type = node.getType();
typeswitch:
@ -151,7 +151,7 @@ public class NodeTransformer {
* right target.
*/
Node breakTarget = new Node(TokenStream.TARGET);
Node parent = iterator.getCurrentParent();
Node parent = iter.getCurrentParent();
Node next = node.getNext();
while (next != null &&
(next.getType() == TokenStream.LABEL ||
@ -176,7 +176,7 @@ public class NodeTransformer {
case TokenStream.SWITCH:
{
Node breakTarget = new Node(TokenStream.TARGET);
Node parent = iterator.getCurrentParent();
Node parent = iter.getCurrentParent();
parent.addChildAfter(breakTarget, node);
// make all children siblings except for selector
@ -276,10 +276,10 @@ public class NodeTransformer {
Node jsrnode = new Node(TokenStream.JSR);
Object jsrtarget = n.getProp(Node.FINALLY_PROP);
jsrnode.putProp(Node.TARGET_PROP, jsrtarget);
iterator.addBeforeCurrent(jsrnode);
iter.addBeforeCurrent(jsrnode);
} else if (elemtype == TokenStream.WITH) {
Node leave = new Node(TokenStream.LEAVEWITH);
iterator.addBeforeCurrent(leave);
iter.addBeforeCurrent(leave);
}
}
break;
@ -304,12 +304,12 @@ public class NodeTransformer {
int elemtype = n.getType();
if (elemtype == TokenStream.WITH) {
Node leave = new Node(TokenStream.LEAVEWITH);
iterator.addBeforeCurrent(leave);
iter.addBeforeCurrent(leave);
} else if (elemtype == TokenStream.TRY) {
Node jsrFinally = new Node(TokenStream.JSR);
Object jsrTarget = n.getProp(Node.FINALLY_PROP);
jsrFinally.putProp(Node.TARGET_PROP, jsrTarget);
iterator.addBeforeCurrent(jsrFinally);
iter.addBeforeCurrent(jsrFinally);
} else if (!labelled &&
(elemtype == TokenStream.LOOP ||
(elemtype == TokenStream.SWITCH &&
@ -394,7 +394,7 @@ public class NodeTransformer {
}
regexps.add(node);
Node n = new Node(TokenStream.REGEXP);
iterator.replaceCurrent(n);
iter.replaceCurrent(n);
n.putProp(Node.REGEXP_PROP, node);
break;
}
@ -417,7 +417,7 @@ public class NodeTransformer {
Node pop = new Node(TokenStream.POP, asn, node.getLineno());
result.addChildToBack(pop);
}
iterator.replaceCurrent(result);
iter.replaceCurrent(result);
break;
}
@ -444,7 +444,7 @@ public class NodeTransformer {
// Local variables are by definition permanent
Node n = new Node(TokenStream.PRIMARY,
TokenStream.FALSE);
iterator.replaceCurrent(n);
iter.replaceCurrent(n);
}
}
break;
@ -492,11 +492,11 @@ public class NodeTransformer {
protected void addVariables(Node tree, VariableTable vars) {
// OPT: a whole pass to collect variables seems expensive.
// Could special case to go into statements only.
boolean inFunction = tree.getType() == TokenStream.FUNCTION;
PreorderNodeIterator iterator = tree.getPreorderIterator();
boolean inFunction = (tree.getType() == TokenStream.FUNCTION);
ObjToIntMap fNames = null;
Node node;
while ((node = iterator.nextNode()) != null) {
PreorderNodeIterator iter = new PreorderNodeIterator();
for (iter.start(tree); !iter.done(); iter.next()) {
Node node = iter.getCurrent();
int nodeType = node.getType();
if (inFunction && nodeType == TokenStream.FUNCTION &&
node != tree &&

Просмотреть файл

@ -1,141 +0,0 @@
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code is Rhino code, released
* May 6, 1999.
*
* The Initial Developer of the Original Code is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1997-1999 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
* Norris Boyd
* Roger Lawrence
* Igor Bukanov
*
* Alternatively, the contents of this file may be used under the
* terms of the GNU Public License (the "GPL"), in which case the
* provisions of the GPL are applicable instead of those above.
* If you wish to allow use of your version of this file only
* under the terms of the GPL and not to allow others to use your
* version of this file under the NPL, indicate your decision by
* deleting the provisions above and replace them with the notice
* and other provisions required by the GPL. If you do not delete
* the provisions above, a recipient may use your version of this
* file under either the NPL or the GPL.
*/
package org.mozilla.javascript;
/**
* This class implements a preorder tree iterator for the Node class.
*
* @see Node
* @author Norris Boyd
*/
public final class PreorderNodeIterator {
public PreorderNodeIterator(Node n) {
start = n;
}
public Node currentNode() {
return current;
}
public Node getCurrentParent() {
// Should not be used when stackTop == 0,
// i.e. with start or its siblings
return stack[stackTop - 1];
}
public Node nextNode() {
if (current == null) {
current = start;
} else {
Node first = current.getFirstChild();
if (first != null) {
stackPush(current);
cachedPrev = null;
current = first;
} else {
for (;;) {
cachedPrev = current;
current = current.next;
if (current != null) { break; }
if (stackTop == 0) {
// Iteration end: clear cachedPrev that currently
// points to the last sibling of start
cachedPrev = null; break;
}
--stackTop;
current = stack[stackTop];
stack[stackTop] = null;
}
}
}
return current;
}
public void replaceCurrent(Node newNode) {
// Should not be used when stackTop == 0,
// i.e. with start or its siblings
Node parent = stack[stackTop - 1];
if (cachedPrev != null && cachedPrev.next == current) {
// Check cachedPrev.next == current is necessary due to possible
// tree mutations
parent.replaceChildAfter(cachedPrev, newNode);
}
else {
parent.replaceChild(current, newNode);
}
current = newNode;
}
public void addBeforeCurrent(Node newNode) {
// Should not be used when stackTop == 0,
// i.e. with start or its siblings
Node parent = stack[stackTop - 1];
if (cachedPrev != null && cachedPrev.next == current) {
parent.addChildAfter(newNode, cachedPrev);
}
else {
parent.addChildBefore(newNode, current);
}
cachedPrev = newNode;
}
private void stackPush(Node n) {
int N = stackTop;
if (N == 0) {
stack = new Node[16];
}
else if (N == stack.length) {
Node[] tmp = new Node[N * 2];
System.arraycopy(stack, 0, tmp, 0, N);
stack = tmp;
}
stack[N] = n;
stackTop = N + 1;
}
private Node start;
private Node[] stack;
private int stackTop;
private Node current;
//cache previous sibling of current not to search for it when
//replacing current
private Node cachedPrev;
}

Просмотреть файл

@ -137,11 +137,11 @@ public class Optimizer {
{
itsOptLevel = optLevel;
// run on one function at a time for now
PreorderNodeIterator iterator = tree.getPreorderIterator();
Node node;
while ((node = iterator.nextNode()) != null) {
// should be able to do this more cheaply ?
// - run through initial block children ?
PreorderNodeIterator iter = new PreorderNodeIterator();
for (iter.start(tree); !iter.done(); iter.next()) {
// should be able to do this more cheaply ?
// - run through initial block children ?
Node node = iter.getCurrent();
if (node.getType() == TokenStream.FUNCTION) {
OptFunctionNode theFunction = (OptFunctionNode)
node.getProp(Node.FUNCTION_PROP);
@ -1029,20 +1029,22 @@ public class Optimizer {
}
}
private Node[] buildStatementList(FunctionNode theFunction)
private static Node[] buildStatementList(FunctionNode theFunction)
{
ObjArray statements = new ObjArray();
ObjArray blockStack = new ObjArray();
Node node = theFunction;
for (;;) {
node = findNextStatementNode(node, blockStack);
if (node == null) { break; }
statements.add(node);
node = node.getNext();
if (node == null) {
if (blockStack.isEmpty()) { break; }
node = (Node)(blockStack.pop());
PreorderNodeIterator iter = new PreorderNodeIterator();
for (iter.start(theFunction); !iter.done(); ) {
Node node = iter.getCurrent();
int type = node.getType();
if (type == TokenStream.BLOCK
|| type == TokenStream.LOOP
|| type == TokenStream.FUNCTION)
{
iter.next();
} else {
statements.add(node);
iter.nextSkipSubtree();
}
}
@ -1051,27 +1053,5 @@ public class Optimizer {
return result;
}
private static Node findNextStatementNode(Node node, ObjArray blockStack)
{
for (;;) {
int type = node.getType();
boolean blockType = (type == TokenStream.BLOCK
|| type == TokenStream.LOOP
|| type == TokenStream.FUNCTION);
if (!blockType) { return node; }
Node first = node.getFirstChild();
Node next = node.getNext();
if (first == null) {
if (next == null) { return null; }
node = next;
} else {
if (next != null) {
blockStack.push(next);
}
node = first;
}
}
}
int itsOptLevel;
}