Allow for TokenStream.COMMA node in the parsed tree to contain more then 2 children to avoid too-deep recursion during code generation for long array and object initialization lists.

This commit is contained in:
igor%mir2.org 2002-11-02 18:01:26 +00:00
Родитель 574e9443e3
Коммит 9b6ae7fdf7
3 изменённых файлов: 28 добавлений и 22 удалений

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

@ -584,14 +584,13 @@ public class IRFactory {
*/
public Object createArrayLiteral(Object obj) {
Node array;
Node result;
array = result = new Node(TokenStream.NEW,
Node.newString(TokenStream.NAME, "Array"));
Node temp = createNewTemp(result);
result = temp;
array = new Node(TokenStream.NEW,
Node.newString(TokenStream.NAME, "Array"));
Node temp = createNewTemp(array);
Node elem = null;
int i = 0;
Node comma = new Node(TokenStream.COMMA, temp);
for (Node cursor = ((Node) obj).getFirstChild(); cursor != null;) {
// Move cursor to cursor.next before elem.next can be
// altered in new Node constructor
@ -606,7 +605,7 @@ public class IRFactory {
Node addelem = new Node(TokenStream.SETELEM, createUseTemp(temp),
Node.newNumber(i), elem);
i++;
result = new Node(TokenStream.COMMA, result, addelem);
comma.addChildToBack(addelem);
}
/*
@ -630,15 +629,15 @@ public class IRFactory {
{
Node setlength = new Node(TokenStream.SETPROP,
createUseTemp(temp),
Node.newString(
"length"),
Node.newString("length"),
Node.newNumber(i));
result = new Node(TokenStream.COMMA, result, setlength);
comma.addChildToBack(setlength);
}
} else {
array.addChildToBack(Node.newNumber(i));
}
return new Node(TokenStream.COMMA, result, createUseTemp(temp));
comma.addChildToBack(createUseTemp(temp));
return comma;
}
/**
@ -651,8 +650,8 @@ public class IRFactory {
Node result = new Node(TokenStream.NEW, Node.newString(TokenStream.NAME,
"Object"));
Node temp = createNewTemp(result);
result = temp;
Node comma = new Node(TokenStream.COMMA, temp);
for (Node cursor = ((Node) obj).getFirstChild(); cursor != null;) {
Node n = cursor;
cursor = cursor.getNextSibling();
@ -663,9 +662,10 @@ public class IRFactory {
Node next = cursor;
cursor = cursor.getNextSibling();
Node addelem = new Node(op, createUseTemp(temp), n, next);
result = new Node(TokenStream.COMMA, result, addelem);
comma.addChildToBack(addelem);
}
return new Node(TokenStream.COMMA, result, createUseTemp(temp));
comma.addChildToBack(createUseTemp(temp));
return comma;
}
/**

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

@ -320,10 +320,11 @@ public class Interpreter {
case TokenStream.COMMA :
iCodeTop = generateICode(child, iCodeTop);
iCodeTop = addByte(TokenStream.POP, iCodeTop);
itsStackDepth--;
child = child.getNextSibling();
iCodeTop = generateICode(child, iCodeTop);
while (null != (child = child.getNextSibling())) {
iCodeTop = addByte(TokenStream.POP, iCodeTop);
itsStackDepth--;
iCodeTop = generateICode(child, iCodeTop);
}
break;
case TokenStream.SWITCH : {

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

@ -618,12 +618,17 @@ public class Codegen extends Interpreter {
visitSwitch(node, child);
break;
case TokenStream.COMMA:
generateCodeFromNode(child, node, -1, -1);
addByteCode(ByteCode.POP);
generateCodeFromNode(child.getNextSibling(),
node, trueLabel, falseLabel);
case TokenStream.COMMA: {
Node next = child.getNextSibling();
while (next != null) {
generateCodeFromNode(child, node, -1, -1);
addByteCode(ByteCode.POP);
child = next;
next = next.getNextSibling();
}
generateCodeFromNode(child, node, trueLabel, falseLabel);
break;
}
case TokenStream.NEWSCOPE:
addScriptRuntimeInvoke("newScope", "()",