/*
* $Id: MultiParent.java,v 1.6 2002/09/16 08:05:05 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.script.expression;
import anvil.core.Any;
import anvil.script.Context;
import anvil.script.ParameterList;
import anvil.script.compiler.ByteCompiler;
/**
* class MultiParent
*
* @author: Jani Lehtim�ki
*/
public abstract class MultiParent extends Parent
{
protected Node[] _child = null;
protected int _childs = 0;
protected boolean _hasSplices = false;
public MultiParent(int childs)
{
super();
_childs = childs;
if (childs > 0) {
_child = new Node[childs];
}
}
public MultiParent(Node[] child)
{
super();
_childs = child.length;
_child = child;
}
public MultiParent(Parent parent)
{
super();
if (parent != null) {
int n = parent.childs();
_childs = n;
if (n > 0) {
_child = new Node[n];
for(int i=0; i<n; i++) {
Node child = parent.getChild(i);
_child[i] = child;
if (child.typeOf() == EXPR_SPLICE) {
_hasSplices = true;
}
}
}
}
}
public final int childs()
{
return _childs;
}
public final Node getChild(int i)
{
return ((i >= 0) && (i < _childs)) ? _child[i] : null;
}
public final void setChild(int i, Node child)
{
if ((i >= 0) && (i < _childs)) {
_child[i] = child;
if (child.typeOf() == EXPR_SPLICE) {
_hasSplices = true;
}
}
}
public Any[] evalChilds(int startIndex)
{
int n = _childs;
if (startIndex >= n) {
return Any.ARRAY0;
}
if (_hasSplices) {
ParameterList plist = new ParameterList(n - startIndex);
for(int i=startIndex; i<n; i++) {
Node child = _child[i];
if (child.typeOf() == EXPR_SPLICE) {
plist.splice(child.eval());
} else {
plist.add(child.eval());
}
}
return plist.toArray();
} else {
Node[] child = _child;
Any[] list = new Any[n - startIndex];
for(int i = startIndex; i<n; i++) {
list[i - startIndex] = child[i].eval();
}
return list;
}
}
public Node[] getChilds(int startIndex)
{
if (startIndex == 0) {
return _child;
}
int len = _childs - startIndex;
if (len <= 0) {
return null;
}
Node[] list = new Node[len];
System.arraycopy(_child, startIndex, list, 0, len);
return list;
}
public boolean hasSplices()
{
return _hasSplices;
}
public void toString(StringBuffer buffer, int startIndex) {
int n = _childs;
for(int i=startIndex; i < n; i++) {
buffer.append(_child[i].toString());
if (i < n-1) {
buffer.append(',');
buffer.append(' ');
}
}
}
public void compile(ByteCompiler context, int operation)
{
}
}