001: /*
002: * $Id: MultiParent.java,v 1.6 2002/09/16 08:05:05 jkl Exp $
003: *
004: * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
005: *
006: * Use is subject to license terms, as defined in
007: * Anvil Sofware License, Version 1.1. See LICENSE
008: * file, or http://njet.org/license-1.1.txt
009: */
010: package anvil.script.expression;
011:
012: import anvil.core.Any;
013: import anvil.script.Context;
014: import anvil.script.ParameterList;
015: import anvil.script.compiler.ByteCompiler;
016:
017: /**
018: * class MultiParent
019: *
020: * @author: Jani Lehtimäki
021: */
022: public abstract class MultiParent extends Parent {
023:
024: protected Node[] _child = null;
025: protected int _childs = 0;
026: protected boolean _hasSplices = false;
027:
028: public MultiParent(int childs) {
029: super ();
030: _childs = childs;
031: if (childs > 0) {
032: _child = new Node[childs];
033: }
034: }
035:
036: public MultiParent(Node[] child) {
037: super ();
038: _childs = child.length;
039: _child = child;
040: }
041:
042: public MultiParent(Parent parent) {
043: super ();
044: if (parent != null) {
045: int n = parent.childs();
046: _childs = n;
047: if (n > 0) {
048: _child = new Node[n];
049: for (int i = 0; i < n; i++) {
050: Node child = parent.getChild(i);
051: _child[i] = child;
052: if (child.typeOf() == EXPR_SPLICE) {
053: _hasSplices = true;
054: }
055: }
056: }
057: }
058: }
059:
060: public final int childs() {
061: return _childs;
062: }
063:
064: public final Node getChild(int i) {
065: return ((i >= 0) && (i < _childs)) ? _child[i] : null;
066: }
067:
068: public final void setChild(int i, Node child) {
069: if ((i >= 0) && (i < _childs)) {
070: _child[i] = child;
071: if (child.typeOf() == EXPR_SPLICE) {
072: _hasSplices = true;
073: }
074: }
075: }
076:
077: public Any[] evalChilds(int startIndex) {
078: int n = _childs;
079: if (startIndex >= n) {
080: return Any.ARRAY0;
081: }
082:
083: if (_hasSplices) {
084: ParameterList plist = new ParameterList(n - startIndex);
085: for (int i = startIndex; i < n; i++) {
086: Node child = _child[i];
087: if (child.typeOf() == EXPR_SPLICE) {
088: plist.splice(child.eval());
089: } else {
090: plist.add(child.eval());
091: }
092: }
093: return plist.toArray();
094:
095: } else {
096: Node[] child = _child;
097: Any[] list = new Any[n - startIndex];
098: for (int i = startIndex; i < n; i++) {
099: list[i - startIndex] = child[i].eval();
100: }
101: return list;
102:
103: }
104: }
105:
106: public Node[] getChilds(int startIndex) {
107: if (startIndex == 0) {
108: return _child;
109: }
110: int len = _childs - startIndex;
111: if (len <= 0) {
112: return null;
113: }
114: Node[] list = new Node[len];
115: System.arraycopy(_child, startIndex, list, 0, len);
116: return list;
117: }
118:
119: public boolean hasSplices() {
120: return _hasSplices;
121: }
122:
123: public void toString(StringBuffer buffer, int startIndex) {
124: int n = _childs;
125: for (int i = startIndex; i < n; i++) {
126: buffer.append(_child[i].toString());
127: if (i < n - 1) {
128: buffer.append(',');
129: buffer.append(' ');
130: }
131: }
132: }
133:
134: public void compile(ByteCompiler context, int operation) {
135: }
136:
137: }
|